001#include "reg51.h"
002#include "irq52.h"
003#include <stdio.h>
004
005near unsigned int RC5_Word;
006 unsigned char bit RC5_LastBit;
007near unsigned char RC5_Timer;
008near unsigned char RC5_BitCount;
009 unsigned char bit RC5_Valid;
010 unsigned char bit RC5_MediumBit;
011near unsigned char RC5_Command;
012near unsigned char RC5_System;
013
014IRQ_VECTOR(int1,IRQ_INT1)
015
016void int1 (void) interrupt //Routine for decoding RC5. Works but could use some refining.
017{
018 //Puls from 1 to 0 detected, depending on the timing from the last puls a bit is decoded.
019 #asm
020 PUSH PSW
021 PUSH ACC
022 MOV A,_RC5_Timer
023 MOV _RC5_Timer,#255 //Reset timer to calculate next RC5 bit timing
024 CPL A //Timing is negative so make it positive "makes it easier to understand"
025 CLR C //Carry flag need to be 0 before subtracting with borrow
026 SUBB A,#44 //Timer <44 short lenght pulse detected. No bit change
027 JC RC5_ShortPulse //The Timer is smaller than 44. Carry was set.
028 SUBB A,#18 //Timer <62 medium length pulse detected. 001 or 100 sequence
029 JC RC5_MediumPulse
030 SUBB A,#18 //Timer <80 long length pulse detected. Bit change.
031 JC RC5_LongPulse
032 MOV A,_RC5_BitCount //End of 13 bits. Check if last bit was detected
033 CJNE A,#12,RC5_13Bits //Last bit detected?. If not add a zero bit to the sequence
034 CLR _RC5_LastBit
035 ACALL RC5_ShiftBit //Add a zero bit
036 MOV A,_RC5_BitCount //End of 13 bits. Check if last bit was detected
037RC5_13Bits: CJNE A,#13,RC5_Error //We started halfway the RC5 code. Try again.
038 MOV A,_RC5_Word //Calculate Command en System codes.
039 ANL A,#0x3F
040 MOV _RC5_Command,A
041 MOV A,_RC5_Word
042 RL A
043 RL A
044 ANL A,#0x03
045 MOV _RC5_System,A
046 MOV A,_RC5_Word+1
047 ANL A,#0x1f