/* Name: main.c * Author: Nathan Williams * 2011 */ #include #include #include #include #include #include #include #define CLOCK_DIV8 TCCR0B = _BV(CS01); #define CLOCK_DIV64 TCCR0B = _BV(CS01) | _BV(CS00); #define CLOCK_DIV256 TCCR0B = _BV(CS02); #define TIMER TCNT0 #define TIMER_CLEAR TIMER = 0; #define IR_HIGH PINB & _BV(PB1) #define IR_LOW (!(PINB & _BV(PB1))) #define LED_ON PORTB |= _BV(PB0); #define LED_OFF PORTB &= ~(_BV(PB0)); #define TOGGLE_LED PORTB ^= _BV(PB0); char pattern[9] = { 0xC0, 0x00, 0x55, 0x55, 0x04, 0x10, 0x11, 0x45, 0x5D}; void listenForIR() { char state = 0; char current_byte = 0; uint8_t bit_pos = 0; char bytes[9]; uint8_t byte_pos = 0; while(byte_pos < 9) { CLOCK_DIV256; TIMER_CLEAR; while(IR_HIGH) //wait for a low signal to come in { if(TIMER > 219) //219 is ~56ms, larger then the biggest in my signals return; //abort out } //We didn't time out! CLOCK_DIV8; TIMER_CLEAR; state = 0; while(IR_LOW) { if(TIMER >= 125) {state = 1;break;} //1ms } current_byte <<= 1; current_byte |= state; while(IR_LOW); //in the case of a low longer then the above timeout TIMER_CLEAR; state = 0; while(IR_HIGH) { if(TIMER >= 125) {state=1;break;} } current_byte <<= 1; current_byte |= state; bit_pos += 2; if(bit_pos >= 8) //should never be greater then, but it is a good idea to be safe { bytes[byte_pos] = current_byte; byte_pos++; bit_pos = 0; current_byte = 0; } } int i = 0; for(i=0;i<9;++i) { if(bytes[i] != pattern[i]) return; //we hit a byte that didn't match } TOGGLE_LED; //all matched, toggle the LED! } int main(void) { DDRB |= _BV(PB0); //set the LED pin to low impedance so it can source current for(;;) { listenForIR(); } return 0; /* never reached */ }