This project shows how to decode IR remote controls which uses Philips RC5 protocol with microchip PIC16F84A microcontroller.
You can find details about RC5 on Wikipedia
Related topics:
Philips RC5 & LG TV Remote Control Decoder
IR Remote Control Based On PIC Microcontroller
The RC5Β protocols can be divided into 4 parts:
1-start bit,
2-Toggle bit,
3-Address (5 bits),
4-Command (6 bits).
The code of this project is written using mikroc pro for pic compiler. The crystal used is 12MHz.
//PIC16F84A RC5 Protocol Remote control decoder MikroC code
//http://www.elecnote.blogspot.com
//[email protected]
//UsedΒ crystal:Β 12MHz
//UseΒ atΒ yourΒ ownΒ risk
Β //Β LCDΒ moduleΒ connections
sbitΒ LCD_RSΒ atΒ RB6_bit;
sbitΒ LCD_ENΒ atΒ RB5_bit;
sbitΒ LCD_D4Β atΒ RB4_bit;
sbitΒ LCD_D5Β atΒ RB3_bit;
sbitΒ LCD_D6Β atΒ RB2_bit;
sbitΒ LCD_D7Β atΒ RB1_bit;
sbitΒ LCD_RS_DirectionΒ atΒ TRISB6_bit;
sbitΒ LCD_EN_DirectionΒ atΒ TRISB5_bit;
sbitΒ LCD_D4_DirectionΒ atΒ TRISB4_bit;
sbitΒ LCD_D5_DirectionΒ atΒ TRISB3_bit;
sbitΒ LCD_D6_DirectionΒ atΒ TRISB2_bit;
sbitΒ LCD_D7_DirectionΒ atΒ TRISB1_bit;
//Β EndΒ LCDΒ moduleΒ connections
unsignedΒ shortΒ ir_read,Β j,Β toggle,Β address=0,Β command=0;
unsignedΒ charΒ Β *text,Β mytext[3];
Β voidΒ Interrupt(){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //ExternalΒ interruptΒ occured
Β //CheckΒ ifΒ theΒ receivedΒ signalΒ isΒ RC5Β protocol
Β Β delay_us(370);
Β Β if(PORTB.F0==0){
Β Β delay_us(889);
Β Β if(PORTB.F0==1){
Β Β delay_us(889);
Β Β if(PORTB.F0==0){
Β Β delay_us(1778);Β ir_readΒ =Β 1;
Β Β INTCONΒ Β =Β 0;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //DisabeΒ theΒ externalΒ interrupt
Β }}}
Β INTF_bitΒ =Β 0;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //ClearΒ InterruptΒ flag
}
Β Β Β //DisplayΒ theΒ resultsΒ onΒ 1602Β LCD
Β Β voidΒ display_resultsΒ (){
Β Β Β Lcd_Cmd(_LCD_CLEAR);Β Β Β Β Β Β Β Β Β Β Β Β //ClearΒ LCD
Β Β Β textΒ =Β "Tgl:"Β ;
Β Β Β Lcd_Out(1,Β 2,Β text);
Β Β Β textΒ =Β "Ads:"Β ;
Β Β Β Lcd_Out(1,Β 10,Β text);
Β Β Β textΒ =Β "Cmd:"Β ;
Β Β Β Lcd_Out(2,Β 6,Β text);
Β Β Β ByteToStr(toggle,Β mytext);Β Β Β Lcd_Out(1,Β 6,Β Ltrim(mytext));
Β Β Β ByteToStr(address,Β mytext);Β Β Lcd_Out(1,Β 14,Β Ltrim(mytext));
Β Β Β ByteToStr(command,Β mytext);Β Β Lcd_Out(2,Β 10,Β Ltrim(mytext));Β }
voidΒ main()Β {
Β OPTION_REGΒ =Β 0;
Β TRISB.F0Β =Β 1;
Β Lcd_Init();
Β Lcd_Cmd(_LCD_CURSOR_OFF);Β Β Β Β Β Β Β Β //Β cursorΒ off
Β Lcd_Cmd(_LCD_CLEAR);Β Β Β Β Β Β Β Β Β Β Β Β Β //Β clearΒ LCD
Β textΒ =Β "RC5Β Decoder"Β ;
Β Lcd_Out(1,Β 3,Β text);
Β delay_ms(1000);
Β INTCONΒ Β Β Β Β Β Β Β Β =Β 0x90;Β Β Β //ExternalΒ InterruptΒ enabled
while(1){
while(!ir_read);Β Β Β Β Β Β Β Β Β //WaitΒ untilΒ IRΒ RC5Β protoclΒ received
ir_readΒ =Β 0;
for(jΒ =Β 0;Β jΒ <Β 12;Β j++){
if(jΒ ==Β 0){
ifΒ (PORTB.F0Β ==Β 0)Β toggleΒ =Β 1;
elseΒ toggleΒ =Β 0;}
elseΒ {
if(jΒ <Β 6){Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //ReadΒ addressΒ bits
ifΒ (PORTB.F0Β ==Β 0)Β address|=Β (1<<(Β 5Β -Β j));Β //SetΒ bitΒ (5-j)
ifΒ (PORTB.F0Β ==Β 1)Β address&=~(1<<(5Β -Β j));}Β //ClearΒ bitΒ (5-j)
elseΒ {Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //ReadΒ commandΒ bits
ifΒ (PORTB.F0Β ==Β 0)Β command|=Β (1<<(Β 11Β -Β j));//SetΒ bitΒ (11-j)
elseΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β command&=~(1<<(11Β -Β j));Β //ClearΒ bitΒ (11-j)
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }}
Β Β delay_us(1778);}
display_results();Β delay_ms(200);
INTCONΒ Β =Β 0x90;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β //ExternalΒ InterruptΒ enabled }}
For more detail: RC5 Protocol Remote Control Decoder