#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_B0)
#bit T0IF = 0xb.2
int duty = 12;
int tempo = 25;
int contRtCC = 0;
int rebote = 0;
//int m_nuevo = 0;
int1 RTCC = 0;//variable tipo bit
#bit rebD = rebote.0
#bit rebI = rebote.1
void detecta_reb (void){
if(!sensor_ir){
if (RTCC == 0){//Esta emitiendo el derecho?
rebD = 1;
}
if (RTCC == 1){//Esta emitiendo el izquierdo?Podria ponerse como else
rebI = 1;
}
if (contRTCC >= 20){
contRtCC = 0;
switch (rebote) {//tomar decisiones, si problemas rebote&&3
case 0 : //ningun obstaculo
//no hace nada
//nunca entra
break;
case 1 : //obstaculo a la derecha
output_high(pin_b2);
break;
case 2 : //obstaculo a la izquierda
output_high(pin_b1);
break;
case 3 : //obstaculo al frente
output_high(pin_b3);
break;
}
rebote = 0;
}
}
}
#int_RTCC
RTCC_isr(){
++contRTCC;
if(RTCC==1){//Emito con el izquierdo
RTCC = 0;
setup_ccp1(CCP_PWM); // Activo CCP1 como PWM
setup_ccp2(CCP_OFF);// Desactivo CCP2
}
else{
RTCC = 1;//Emito con el derecho
setup_ccp1(CCP_OFF);// Desactivo CCP1
setup_ccp2(CCP_PWM); // Activo CCP2 como PWM
}
}
void main() {
output_b(0x00);//se podra quitar
setup_ccp1(CCP_PWM); // Configura CCP1 como PWM
setup_ccp2(CCP_PWM); // Configura CCP2 como PWM
setup_timer_2(T2_DIV_BY_1,tempo,1); // f=38KHz (t = 26us)
set_pwm1_duty(duty); // Determina tiempo en alto
set_pwm2_duty(duty); // Determina tiempo en alto
setup_counters(RTCC_INTERNAL,RTCC_DIV_8);
enable_interrupts(INT_RTCC);
enable_interrupts(global);
do{
if(!sensor_ir){
detecta_reb();
}
delay_ms(1000);
output_low(pin_b1);//borra para poner a 0 sino detecta
output_low(pin_b2);//borra para poner a 0 sino detecta
output_low(pin_b3);//borra para poner a 0 sino detecta
}while (TRUE);
}