#include <16f628a.h>
#fuses NOWDT, INTRC_IO, PUT, NOPROTECT, MCLR
#use delay (clock=4000000) //4 MHZ
#byte CMCON=0x07;
#define IR_PIN PIN_B0
unsigned char bitcount;
int32 ir_tempbyte_hi;
int16 ir_tempbyte_lo;
int16 headp,heads,rise,fall;
void ir_read(void);
void ir_get_rise(void);
void ir_get_fall(void);
void ir_build_bytes(void);
#int_ext
void ext_int ()
{
ir_read();
}
void ir_read(void)
{
ir_tempbyte_hi=0;
ir_tempbyte_lo=0;
while(IR_PIN); // //Wait for minimal pre which stays low
ir_get_rise(); // Get headp & heads to see what protocol it is
headp=rise;
ir_get_fall();
heads=fall;
if ((headp>8800)&&(headp<9090)&&(heads>4200)&&(heads<4570)) // We got NEC Start Sequence
{
output_high(PIN_A2);
ir_build_bytes(); // Now trying to read data bits
}
else
{
output_high(PIN_A2);
}
}
void ir_get_rise(void)
{
output_high(PIN_A7);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1); // Get high
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(0);
while(!IR_PIN); // Wait for signal to go high
rise = get_timer1();
delay_ms(1000);
output_low(PIN_A7);
}
void ir_get_fall(void)
{
output_high(PIN_A6);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1); // Get low
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(0);
while(IR_PIN); // Wait for signal to go low
fall = get_timer1();
delay_ms(1000);
output_low(PIN_A6);
}
void ir_build_bytes(void)
{
for (bitcount=0;bitcount!=32;bitcount++)
{
ir_get_rise();
ir_get_fall();
if ((rise>450)&&(rise<650)&&(fall>1400)&&(fall<1655))
{
ir_tempbyte_hi|=1; // bit=1
}
else if ((rise>450)&&(rise<650)&&(fall>390)&&(fall<600)){;} // bit=0
else return; // Byte Check Fault
}
if (bitcount<31){ir_tempbyte_hi<<=1;} // Don't shift last bit
if (bitcount==32){output_high(PIN_A2);} // reception is valid
}
void main ()
{
setup_CCP1(CCP_OFF); // CCP1 unit outbound
ext_int_edge(H_TO_L); // INT_EXT is High to Low
setup_timer_0(t1_disabled);
setup_timer_1(t2_disabled);
enable_interrupts(INT_EXT); // INT_EXT interrupt inbound
enable_interrupts(GLOBAL); // All interrupts inbound
while(1); //Stand by
}