This shows you the differences between two versions of the page.
| irdecode [2009/03/04 05:38] – created jonmash | irdecode [2009/04/08 22:24] (current) – jonmash | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| <code cpp> | <code cpp> | ||
| / | / | ||
| - | * Code to access the raw data coming from teh I2C thermo | + | * Code to access the raw data coming from the IR receiver |
| - | * | + | * and display |
| + | * the code that sends commands to the tv. | ||
| * By: Jonathan Mash | * By: Jonathan Mash | ||
| * Date: Feb. 2009 | * Date: Feb. 2009 | ||
| - | * Based on work by: Jon McPhalen | + | * Based on work by: Walter Anderson |
| *********************************************************/ | *********************************************************/ | ||
| - | int irPin = 2; | + | #include < |
| - | bool incoming_ir = false; | + | #include < |
| - | void setup() | + | #define TIMER_RESET |
| - | { | + | #define SAMPLE_SIZE |
| - | pinMode(irPin, | + | #define DEGBUG |
| - | | + | |
| - | | + | |
| - | attachInterrupt(0, IRISR, FALLING); | + | |
| - | } | + | |
| - | void IRISR() | + | int IRpin = 2; |
| - | { | + | unsigned int TimerValue[SAMPLE_SIZE]; |
| - | | + | char direction[SAMPLE_SIZE]; |
| + | byte change_count; | ||
| + | long time; | ||
| + | int value; | ||
| + | void setup() { | ||
| + | | ||
| + | Serial.println(" | ||
| + | TCCR1A | ||
| + | // ICNC1=0 => Capture Noise Canceler disabled -- ICES1=0 => Input Capture Edge Select (not used) -- CTC1=0 => Clear Timer/ | ||
| + | // CS12=0 CS11=1 CS10=1 => Set prescaler to clock/64 | ||
| + | TCCR1B = 0x03; // 16MHz clock with prescaler means TCNT1 increments every 4uS | ||
| + | // ICIE1=0 => Timer/ | ||
| + | // TOIE1=0 => Timer 1 Overflow Interrupt Enable | ||
| + | TIMSK1 = 0x00; | ||
| + | pinMode(IRpin, | ||
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | | + | |
| - | + | | |
| - | if(incoming_ir | + | |
| - | { | + | TIMER_RESET; |
| - | | + | TimerValue[change_count] = TCNT1; |
| + | direction[change_count++] = ' | ||
| + | while (change_count < SAMPLE_SIZE) { | ||
| + | | ||
| + | while(digitalRead(IRpin) == LOW) {} | ||
| + | TimerValue[change_count] = TCNT1; | ||
| + | direction[change_count++] = ' | ||
| + | } else { | ||
| + | while(digitalRead(IRpin) == HIGH) {} | ||
| + | TimerValue[change_count] = TCNT1; | ||
| + | direction[change_count++] = ' | ||
| + | } | ||
| + | | ||
| + | // | ||
| + | change_count = 0; | ||
| + | time = (long) TimerValue[change_count] * 4; | ||
| + | change_count++; | ||
| + | while (change_count < SAMPLE_SIZE) | ||
| + | | ||
| | | ||
| - | if(key == 0x1D01) | + | if(direction[change_count-1] |
| - | Serial.println(" | + | { |
| - | else if(key == 0x701) | + | Serial.print(" |
| - | Serial.println(" | + | Serial.print((int)change_count); |
| - | else if(key == 0x901) | + | Serial.print(" |
| - | Serial.println(" | + | Serial.print(" |
| - | else | + | Serial.print(" |
| - | Serial.println(" | + | Serial.println(time - (long)TimerValue[change_count-1] * 4); |
| + | Serial.print("\toscillationWrite(led_pin, | ||
| + | | ||
| + | Serial.println(" | ||
| + | } | ||
| + | else if(direction[change_count-1] == ' | ||
| + | { | ||
| + | Serial.print(" | ||
| + | Serial.print((int)change_count); | ||
| + | Serial.print(" | ||
| + | Serial.print(" | ||
| + | Serial.print(" | ||
| + | Serial.println(time - (long)TimerValue[change_count-1] * 4); | ||
| + | Serial.print("\tdelayMicroseconds("); | ||
| + | | ||
| + | Serial.println(" | ||
| + | } | ||
| + | change_count++; | ||
| } | } | ||
| - | + | value=0; | |
| - | Serial.println(analogRead(0)); | + | delay(100); |
| - | + | ||
| - | delay(50); | + | |
| } | } | ||
| - | int getSircs() { | ||
| - | int duration; | ||
| - | int irCode; | ||
| - | int mask; | ||
| - | | ||
| - | incoming_ir = false; | ||
| - | // wait for start bit | ||
| - | do { | ||
| - | duration = pulseIn(irPin, | ||
| - | } while (duration < 2000); | ||
| - | |||
| - | // get 12-bit SIRCS code | ||
| - | irCode = 0; // clear ir code | ||
| - | mask = 1; // set mask to bit 0 | ||
| - | for (int idx = 0; idx < 16; idx++) { // get all 12 bits | ||
| - | duration = pulseIn(irPin, | ||
| - | if (duration >= 600) // 1 bit? | ||
| - | irCode |= mask; | ||
| - | mask <<= 1; // shift mask to next bit | ||
| - | } | ||
| - | return irCode; | ||
| - | } | ||
| </ | </ | ||