#include <16f876a.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT
#use delay(clock=4000000)
#use standard_io(b)
#use standard_io(c)
#define sensor_ir input(PIN_B3)
#bit T0IF = 0xb.2
int duty = 12;
int tempo = 25;
int cuenta_reb = 0;
void detecta_reb (void){
output_high(pin_b0);
set_timer0(0x00);//pongo a 0 timer
setup_counters(RTCC_INTERNAL,RTCC_DIV_16);
//mientras temporizo cuento rebotes
while(!T0IF){
if(!sensor_ir){
output_high(pin_b1);
++cuenta_reb;
while(!sensor_ir){}
}
}
T0IF=0;//pongo a 0 la bandera
if (cuenta_reb!=0){//Por ahora 0, mirar si cambiar
output_high(pin_b2);
}
}
void main() {
output_b(0x00);
setup_ccp1(CCP_PWM); // Configura CCP1 como PWM
setup_timer_2(T2_DIV_BY_1,tempo,1); // f=38KHz (t = 26us)
set_pwm1_duty(duty); // Determina tiempo en alto
do{
if(!sensor_ir){
detecta_reb();
}
delay_ms(100);//para poder ver que se enciende
output_low(pin_b2);//borra para poner a 0 sino detecta
cuenta_reb = 0;// pongo a cero contador
}while (TRUE);
}