TODOPIC
Microcontroladores PIC => Lenguaje C para microcontroladores PIC => Mensaje iniciado por: mansoconductor en 15 de Febrero de 2005, 14:18:00
-
diseñe un programa que me hace señales cuadradas en el puerto b de una frecuencia determinada por una formula, en la formula se ingresa los pulsos que capturo con el timer 1, el timer 0 se usa como reloj que cuenta hasta 10 ms , si hay errores de sintaxis, diganmelos , pues es la primera vez que hago un proyecto con pic.
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
/*Start Declaracion de Funciones*/
void interrupcion_t0(void);
void setup(void);
void seleccion(float ts);
#define pe 3756
// OUTPUts
#define IGBT1 PIN_B1
#define IGBT2 PIN_B2
#define IGBT3 PIN_B3
#define IGBT4 PIN_B4
#define IGBT5 PIN_B5
#define IGBT6 PIN_B6
char contador_t0 = 0;
char contador=0;
unsigned long valor_encoder=0;
float a=0;
float b=0;
float ts_interrup;
float ts;
#INT_timer0
void interrupcion_T0 (void) //timer0 , 8 bit, se ocupara como reloj para 10ms
{ // timer 1 no se desborda nunca, como maximo
// hasta 2200 pulsos en 10ms
valor_encoder=get_timer1(); //toma valor del contador
disable_interrupts(int_timer0);
contador_T0 ++;
if (contador_t0==25)
{
enable_interrupts(int_timer0); //se deberia iniciar T0 de nuevo
set_timer0(6); // (4/20e6)*8(256-6)=0.4 ms
disable_interrupts(int_timer1); //desabilitar el contador
a=0.04*Pe*0.01;
b=Pe*0.01-0.04*valor_encoder;
if(b!=0)
ts_interrup=a/b;
set_timer1(0); //valores iniciales del contador de pulsos
disable_interrupts(global);
}
}
main()
{
void setup(void)
{
/*Register set*/
set_tris_A(0xFF); //(FF)entradas todas menes
set_tris_B(0x01); //(0000 0001) todas salidas menos B0
set_tris_C(0xFF); //FF, entrdas, T1*/
set_tris_D(0x00); //salidas
set_tris_E(0x00);
}
setup_timer_0(rtcc_INTERNAL|rtcc_DIV_8); // setup interrupts
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); // setup interrupts
disable_interrupts(INT_TIMER0);
disable_interrupts(GLOBAL);
ts=0;
while(1)
{
if (ts > 0)
{
ts = a/b;
if (contador == 6)
contador = 0;
contador++;
seleccion(ts);
ts=ts_interrup;
}
if (ts< 0)
{
ts =-ts;
if (contador == 1)
contador = 7;
contador--;
seleccion(ts);
ts=ts_interrup;
enable_interrupts(int_timer0);
enable_interrupts(global);
set_timer0(6);
set_timer1(0);
}
while(1)
{
if (contador_t0=25)
{
contador_t0=0;
}
}
}
} //fin main
void seleccion(float ts)
{
switch (contador) {
case 1:
output_high(IGBT1); // encender IGBT 1,3,5
output_low(IGBT2);
output_high(IGBT3);
output_low(IGBT4);
output_high(IGBT5);
output_low(IGBT6);
delay_ms(ts);
output_low(IGBT1); // apagar IGBT 1,3,5
output_low(IGBT3);
output_low(IGBT5);
break;
case 2:
output_high(IGBT1); // encender IGBT 1,5,6
output_low(IGBT2);
output_low(IGBT3);
output_low(IGBT4);
output_high(IGBT5);
output_high(IGBT6);
delay_ms(ts);
output_low(IGBT1); // apagar IGBT 1,5,6
output_low(IGBT5);
output_low(IGBT6);
break;
case 3:
output_high(IGBT1); // encender IGBT 1,2,6
output_high(IGBT2);
output_low(IGBT3);
output_low(IGBT4);
output_low(IGBT5);
output_high(IGBT6);
delay_ms(ts);
output_low(IGBT1); // apagar IGBT 1,2,6
output_low(IGBT2);
output_low(IGBT6);
break;
case 4:
output_low(IGBT1); // encender IGBT 2,4,6
output_high(IGBT2);
output_low(IGBT3);
output_high(IGBT4);
output_low(IGBT5);
output_high(IGBT6);
delay_ms(ts);
output_low(IGBT2); // apagar IGBT 2,4,6
output_low(IGBT4);
output_low(IGBT6);
break;
case 5:
output_low(IGBT1); // encender IGBT 2,3,4
output_high(IGBT2);
output_high(IGBT3);
output_high(IGBT4);
output_low(IGBT5);
output_low(IGBT6);
delay_ms(ts);
output_low(IGBT2); // apagar IGBT 2,3,4
output_low(IGBT3);
output_low(IGBT4);
break;
case 6:
output_low(IGBT1); // encender IGBT 3,4,5
output_low(IGBT2);
output_high(IGBT3);
output_high(IGBT4);
output_high(IGBT5);
output_low(IGBT6);
delay_ms(ts);
output_low(IGBT3); // apagar IGBT 3,4,5
output_low(IGBT4);
output_low(IGBT5);
}
}