/* -*- mode: c -*- */ //////////////////////////////////////////////////////////////////////////////////////////////////// // IR Remote Controller Raw Codes Printer // // 2007 Javier Valcarce García, // $Id$ //////////////////////////////////////////////////////////////////////////////////////////////////// // Definition of interrupt names and ISR interrupt service routine #include #include #define PIN_IRRX 2 // This is the INT0 Pin of the ATMega8 #define TIME_OVER 32000 // TIME_OVER % 16 == 0, after TIME_OVER us since last edge, we // consider the transmision has finished #define TABLESIZE 200 #define STATE_CAPTURE 0 #define STATE_PRINT 1 volatile unsigned int usec; volatile unsigned int pulse_width; volatile byte pulse; volatile byte state; volatile unsigned int table[TABLESIZE - 1]; volatile boolean overflow; // Aruino runs at 16 Mhz, so we have 1000 Overflows per second... // 1 / ((16000000 / 1) / 256) = 1 / 62500 = 16us //////////////////////////////////////////////////////////////////////////////////////////////////// ISR(TIMER2_OVF_vect) { usec += 16; // Maximun amount of time to wait after a received frame if (usec == TIME_OVER) { if ((state == STATE_CAPTURE) && (pulse > 0)) { state = STATE_PRINT; } usec = 0; } }; //////////////////////////////////////////////////////////////////////////////////////////////////// // ISR INT0 ISR(INT0_vect) { if (state != STATE_CAPTURE) { return; } if (pulse == TABLESIZE) { overflow = true; state = STATE_PRINT; return; } pulse_width = usec; usec = 0; if (pulse > 0) { table[pulse-1] = pulse_width; } pulse++; } //////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { usec = 0; pulse = 0; overflow = false; state = STATE_CAPTURE; // Timer Setup // Normal mode TCCR2 &= ~(1 << WGM21); TCCR2 &= ~(1 << WGM20); // No Timer Prescaler TCCR2 |= (1 << CS20); TCCR2 &= ~(1 << CS22); TCCR2 &= ~(1 << CS21); // Use internal clock ASSR &= ~(1 << AS2); // TMR2 Overflow Interrupt motor_enable TIMSK |= (1 << TOIE2); TIMSK &= ~(1 << OCIE2); // INT0 Global motor_enable INT0 interrupt GICR |= (1 << INT0); MCUCR |= (1 << ISC00); // positive edge MCUCR |= (1 << ISC01); // negative edge sei(); Serial.begin(9600); // prints title with ending line break Serial.println("IR Remote Raw Codes Printer"); Serial.println("Capturing..."); pinMode(PIN_IRRX, INPUT); delay(100); } //////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { byte i; if (state == STATE_PRINT) { for (i = 0; i < pulse; i++) { Serial.println(table[i], DEC); delay(10); } Serial.print("Detected "); Serial.print(pulse, DEC); Serial.println(" pulse/s (including initial AGC pulse)"); if (overflow) { Serial.println("Overflow!, increment TABLESIZE"); overflow = false; } state = STATE_CAPTURE; pulse = 0; Serial.println("\nCapturing..."); delay(200); } } ////////////////////////////////////////////////////////////////////////////////////////////////////