Saturday, January 24, 2015

RC5 Protocol Remote Control Decoder


RC-5 remote control protocol decoder with PIC16F84A
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).
RC5 remote control decoder circuit:
The circuit of our system is shown below:

rc5 ir remote control protocol decoder pic microcontroller mikroc code

 My RC5 remote control is shown in the following picture:

RC5 IR remote control

RC5 remote control decoder mikroC code:
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
//Written by: BENCHEROUDA Okba
//http://www.elecnote.blogspot.com
//electronnote@gmail.com
//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
 }
}

RC5 remote control decoder video:
The following video shows this mini project in reality: