slime@2: /* slime@2: * Project Frontplatte slime@2: * slime@2: * rc5.c - decode rc5 signals slime@2: * slime@2: * This file is released under the GNU General Public License. Refer slime@2: * to the COPYING file distributed with this package. slime@2: * slime@2: * (c) 2007 Carsten Presser cpresser AT fsing.uni-sb.de slime@2: */ slime@2: slime@2: #include slime@2: #include "rc5.h" slime@2: #include "io_cfg.h" slime@2: #include "delay.h" slime@2: #include "user.h" slime@2: slime@2: #include slime@2: slime@2: /** V A R I A B L E S **********************************************/ slime@2: unsigned char rc5_status; slime@2: unsigned int rc5_receive_buffer; slime@2: unsigned char gg_ir; slime@2: unsigned char gg_ir_address; slime@2: slime@2: /** C O D E ********************************************************/ slime@2: void rc5_init(void) slime@2: { slime@2: OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_256); slime@2: // SOLLTE SEIN: ein Timer Takt = 51,2 E-6 sec. (mit prescaler 256) slime@2: // ist aber 25,6usec [woher kommt das 1/2??] slime@2: slime@2: /* kurze pulsdauer: 444us bis 1333us mitte: 889us slime@2: entspricht in timer0: 17,3 52,0 34,7 slime@2: slime@2: lange pulsdauer: 1334us bis 2222us 1778us slime@2: in timer0: 52,1 87 69,5*/ slime@2: slime@2: INTCONbits.TMR0IF = 0; // clear flag slime@2: INTCON2bits.TMR0IP = 1; // make TMR0 a high-priority-interrupt slime@2: slime@2: rc5_receive_buffer = 0x0000; slime@2: gg_ir_address = 0xFF; slime@2: gg_ir = 0xFF; slime@2: rc5_status = RC5_UNDEF; slime@2: } slime@2: slime@2: void rc5_decode(void) slime@2: { slime@2: unsigned char tmr0; slime@2: unsigned char pulse = 0xFF; slime@2: slime@2: /* G E T P U L S E W I D T H & T Y P E */ slime@2: tmr0 = (unsigned char)(ReadTimer0()); slime@2: // decide if this is long or short... slime@2: if ((tmr0 > 20) && (tmr0 < 49)) {pulse = RC5_SPACE_SHORT;} slime@2: if ((tmr0 > 55) && (tmr0 < 82)) {pulse = RC5_SPACE_LONG;} slime@2: slime@2: // if RB0 = 1 -> this is a space, not a pulse -> add one slime@2: pulse += PIN_IR; slime@2: slime@2: /* S T A T U S M A C H I N E */ slime@2: switch (rc5_status) slime@2: { slime@2: case RC5_MID1: slime@2: if (pulse == RC5_PULSE_SHORT) slime@2: { slime@2: rc5_status = RC5_START1; slime@2: } slime@2: else if (pulse == RC5_PULSE_LONG) slime@2: { slime@2: rc5_status = RC5_MID0; slime@2: rc5_receive_buffer = rc5_receive_buffer << 1; // emit zero slime@2: } slime@2: else slime@2: rc5_scrap(); slime@2: break; slime@2: slime@2: case RC5_MID0: slime@2: if (pulse == RC5_SPACE_SHORT) slime@2: { slime@2: rc5_status = RC5_START0; slime@2: } slime@2: else if (pulse == RC5_SPACE_LONG) slime@2: { slime@2: rc5_status = RC5_MID1; slime@2: rc5_receive_buffer = rc5_receive_buffer << 1; slime@2: rc5_receive_buffer |= 0x01; // emit one slime@2: } slime@2: else slime@2: rc5_scrap(); slime@2: break; slime@2: slime@2: case RC5_START1: slime@2: if (pulse == RC5_SPACE_SHORT) slime@2: { slime@2: rc5_status = RC5_MID1; slime@2: rc5_receive_buffer = rc5_receive_buffer << 1; slime@2: rc5_receive_buffer |= 0x01; // emit one slime@2: } slime@2: else slime@2: rc5_scrap(); slime@2: break; slime@2: slime@2: case RC5_START0: slime@2: if (pulse == RC5_PULSE_SHORT) slime@2: { slime@2: rc5_status = RC5_MID0; slime@2: rc5_receive_buffer = rc5_receive_buffer << 1; // emit zero slime@2: } slime@2: else slime@2: rc5_scrap(); slime@2: break; slime@2: slime@2: case RC5_DONE: slime@2: case RC5_UNDEF: slime@2: default: slime@2: rc5_scrap(); slime@2: break; slime@2: } slime@2: slime@2: // i recieved 13 + startbit bits... slime@2: if (rc5_receive_buffer & 0x6000) slime@2: { slime@2: rc5_status = RC5_DONE; slime@2: gg_ir = (unsigned char)(rc5_receive_buffer & 0x3F); // lower 6 bits -> command slime@2: gg_ir_address = (unsigned char)((rc5_receive_buffer & 0x032) >> 6); // upper 6 bits -> address + toogle slime@2: slime@2: gg_ioflags |= FLAG_IR; slime@2: } slime@2: slime@2: // reset timer and wait for next pulse.... slime@2: WriteTimer0(0x00); slime@2: } slime@2: slime@2: slime@2: void rc5_scrap(void) slime@2: { slime@2: // enter start-state and emit one slime@2: rc5_status = RC5_MID1; slime@2: rc5_receive_buffer = 0x0001; slime@2: }