#include <18f2550.h>
#FUSES NOWDT,PUT,MCLR,NOBROWNOUT,NOLVP,NOPROTECT,NODEBUG,NOFCMEN,NOIESO,XT
#include <MATH.h>
#define COMENZAR 0
#define CONTANDO 1
#define TERMINO 2
#define VECES 1
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//------------ Pines del LCD ---------------------//
#define LCD_E PIN_C0
#define LCD_CK PIN_C4
#define LCD_DAT PIN_C5
//--------------------------------------------------//
#include "lcd3.c"
#use standard_io (b)
char cEstado;
int32 int32Cont;
float fRpm, vel;
long int dato;
//INTERRUPCION POR RB3 CCP1//
#INT_CCP1
void CCP1__isr(void) {
if (cEstado == CONTANDO) { // Segundo Pulso
int32Cont += CCP_1; // Toma el valor de captura
cEstado = TERMINO; // Evito que vuelva a procesarse durante esta interrupcion
}
if (cEstado == COMENZAR) { // Primer Pulso
set_timer1(0); // Restaura el timer1 en este pulso
cEstado = CONTANDO;
int32Cont = 0;
}
}
//velocidad//
float velocidad(void){
char cCnt;
int32 int32Suma;
long tiempo;
cEstado = TERMINO;
while (TRUE) {
int32Suma = 0;
int32Cont = 0;
for (cCnt = 0; cCnt < VECES; cCnt++) { // Acumula VECES lecturas
cEstado = COMENZAR; // Permite comenzar a la interrupcion
while (cEstado != TERMINO) { // Espera hasta que se activa la interrupcion
tiempo = get_timer1();
if (tiempo > 60000) { // Controla si el contador excedio el tiempo. Valor Original = 60000
set_timer1(0); // Restaura el timer1 para seguir sumando el tiempo de este pulso
int32Cont += tiempo; // Acumula para velocidades de viento bajas
} // Fin if
} // Fin if
int32Suma += int32Cont; // Acumula los tiempos
} // fin for
int32Suma /= VECES; // Saca el promedio de las VECES que se tomo la medicion
fRpm = 1 / ( float ) int32Suma / 20 * 2.5; // Periodo en uS
fRpm *= 1000000; // Periodo en segundos
fRpm *= 60; // Periodo en minutos
dato = fRpm;
vel= (((dato)/60)*(2*pi*15.90))/10;
return dato;
}
}
void main()
{
float pwm;
int s;
lcd_init();
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 255, 1);
set_pwm2_duty(0);
delay_ms (50);
setup_timer_1 (T1_INTERNAL | T1_DIV_BY_8 );
setup_ccp1 (CCP_CAPTURE_FE);
delay_ms (50);
enable_interrupts (INT_CCP1);
enable_interrupts (GLOBAL);
for(;;) {
if(kbhit()){
pwm=(pwm/255)*100;
lcd_gotoxy(1,1);
printf(lcd_putc
,"Velocidad %f",pwm
); set_pwm2_duty(s);
// }
// else {
velocidad();
dato = (long int)fRpm;
delay_ms(2);
lcd_gotoxy(1,2); //retardo para apreciarse todos los caracteres
printf(lcd_putc
,"RPM: %lu RPM ",dato
); //muestra por pantalla el mensaje }
}
}