4 * rc5.c - decode rc5 signals
6 * This file is released under the GNU General Public License. Refer
7 * to the COPYING file distributed with this package.
9 * (c) 2007 Carsten Presser cpresser AT fsing.uni-sb.de
20 /** V A R I A B L E S **********************************************/
21 unsigned char rc5_status;
22 unsigned int rc5_receive_buffer;
24 unsigned char gg_ir_address;
26 /** C O D E ********************************************************/
29 OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_256);
30 // SOLLTE SEIN: ein Timer Takt = 51,2 E-6 sec. (mit prescaler 256)
31 // ist aber 25,6usec [woher kommt das 1/2??]
33 /* kurze pulsdauer: 444us bis 1333us mitte: 889us
34 entspricht in timer0: 17,3 52,0 34,7
36 lange pulsdauer: 1334us bis 2222us 1778us
37 in timer0: 52,1 87 69,5*/
39 INTCONbits.TMR0IF = 0; // clear flag
40 INTCON2bits.TMR0IP = 1; // make TMR0 a high-priority-interrupt
42 rc5_receive_buffer = 0x0000;
45 rc5_status = RC5_UNDEF;
51 unsigned char pulse = 0xFF;
53 /* G E T P U L S E W I D T H & T Y P E */
54 tmr0 = (unsigned char)(ReadTimer0());
55 // decide if this is long or short...
56 if ((tmr0 > 20) && (tmr0 < 49)) {pulse = RC5_SPACE_SHORT;}
57 if ((tmr0 > 55) && (tmr0 < 82)) {pulse = RC5_SPACE_LONG;}
59 // if RB0 = 1 -> this is a space, not a pulse -> add one
62 /* S T A T U S M A C H I N E */
66 if (pulse == RC5_PULSE_SHORT)
68 rc5_status = RC5_START1;
70 else if (pulse == RC5_PULSE_LONG)
72 rc5_status = RC5_MID0;
73 rc5_receive_buffer = rc5_receive_buffer << 1; // emit zero
80 if (pulse == RC5_SPACE_SHORT)
82 rc5_status = RC5_START0;
84 else if (pulse == RC5_SPACE_LONG)
86 rc5_status = RC5_MID1;
87 rc5_receive_buffer = rc5_receive_buffer << 1;
88 rc5_receive_buffer |= 0x01; // emit one
95 if (pulse == RC5_SPACE_SHORT)
97 rc5_status = RC5_MID1;
98 rc5_receive_buffer = rc5_receive_buffer << 1;
99 rc5_receive_buffer |= 0x01; // emit one
106 if (pulse == RC5_PULSE_SHORT)
108 rc5_status = RC5_MID0;
109 rc5_receive_buffer = rc5_receive_buffer << 1; // emit zero
122 // i recieved 13 + startbit bits...
123 if (rc5_receive_buffer & 0x6000)
125 rc5_status = RC5_DONE;
126 gg_ir = (unsigned char)(rc5_receive_buffer & 0x3F); // lower 6 bits -> command
127 gg_ir_address = (unsigned char)((rc5_receive_buffer & 0x032) >> 6); // upper 6 bits -> address + toogle
129 gg_ioflags |= FLAG_IR;
132 // reset timer and wait for next pulse....
139 // enter start-state and emit one
140 rc5_status = RC5_MID1;
141 rc5_receive_buffer = 0x0001;