Saturday, December 19, 2015

RC5 IR remote control protocol decoder with PIC12F1822


RC-5 Protocol decoder with PIC microcontroller and mikroC compiler
The most IR protocol used by hobbyists is the RC-5 protocol from Philips, because it is very used in the world today, and it is easy to understand, encode and decode.
To fully understand this protocol go to this link from Wikipedia.
The complete IR message can be divided into 4 parts:
Start bit (actually one bit and half) which is a property of the RC5 protocol, this bit used to detect if the received signal is RC5, this bit has a length of 2667µs.
Toggle bit: This bit is inverted every time a key is released and pressed again. This way the receiver can distinguish between a key that remains down, or is pressed repeatedly. This bit has a length of 1778µs.
Address: The next 5 bits represent the IR device address, which is sent with MSB first. This 5-bit number has a length of 8890µs.
Command: every key has its command number, this is a 6-bit number with a length of 10668.This topic shows how to decode a remote control uses RC-5 IR protocol using PIC12F1822 microcontroller. The results are displayed on 1602 LCD display.
The LCD is connected to a shift register to get a 2-wire serial LCD, the shift register can be 74HC595, 74HC164, CD4094 or any other compatible type.
To see how to interface LCD display with PIC12F1822 go to the following topic:

Interface PIC12F1822 with LCD display
    
RC-5 remote control decoder circuit schematic diagram:
The internal oscillator of the microcontroller PIC12F1822 is used at 32MHz and MCLR pin function is disabled.
rc-5 remote control protocol decoder pic microcontroller mikroc code

I took the following image from my home made hardware circuit:

RC-5 remote control decoder with PIC microcontroller and mikroc

RC-5 IR protocol decoder mikroC code:

 // RC5 IR remote control decoder with PIC12F1822
 // Serial LCD library must be installed
 // Written by: BENCHEROUDA Okba
 // electronnote@gmail.com
 // http://elecnote.blogspot.com/
 // Internal oscillator used @ 32MHz
 // Use at your own risk

 // LCD module connections
 sbit Serial_LCD_DATA at RA4_bit;
 sbit Serial_LCD_CLK at RA5_bit;
 sbit Serial_LCD_DATA_Direction at TRISA4_bit;
 sbit Serial_LCD_CLK_Direction at TRISA5_bit;
 // End LCD module connections
 bit RC5, toggle;
 unsigned short i, address = 0, command = 0;
 void main() {
   OSCCON = 0xF0; // Internal oscillator 8MHz and PLL enabled (gives 32MHz)
   ANSELA = 0;    // All I/O pins are configured as digital
   ADCON0 = 0;    // Disable ADC module
   PORTA = 0;     // Initial value of port A bits
   TRISA = 8;     // Configure RA3 as input
   Serial_Lcd_Init();                 // LCD display initialization
   Serial_Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
   Serial_Lcd_Cmd(_LCD_CLEAR);             // clear LCD
   Serial_Lcd_Out(1, 4, "RC5 Remote");
   Serial_Lcd_Out(2, 1, "control decoder");
   delay_ms(1000);
   while(1){
     while(PORTA.F3);
       // Check if the received signal is RC5 protocol
       delay_us(445);
       if(!PORTA.F3){
        delay_us(889);
        if(PORTA.F3){
         delay_us(889);
         if(!PORTA.F3){
          delay_us(1778);
          RC5 = 1;
          }
         }
        }
        if (RC5){
         RC5 = 0;
         for(i = 0; i < 12; i++){
          if(!i){
           if (PORTA.F3) toggle = 0;
           else          toggle = 1;
           }
          else {
           if(i < 6){                   //Read address bits
            if (PORTA.F3)
             address &= ~(1 << (5 - i));  //Clear bit (5-i)
            else
             address |= (1 << (5 - i));   //Set bit (5-i)
            }
           else {                       //Read command bits
            if (PORTA.F3)
             command &= ~(1 << (11 - i)); //Clear bit (11-i)
            else
             command |= (1 << (11 - i));  //Set bit (11-i)
            }
           }
          delay_us(1778);
          }
         Serial_Lcd_Cmd(_LCD_CLEAR);
         Serial_Lcd_Out(1, 2, "Toggle bit:");
         Serial_Lcd_Out(2, 1, "Ads:");
         Serial_Lcd_Out(2, 10, "Cmd:");
         Serial_Lcd_Chr(1, 14, toggle + 48);
         Serial_Lcd_Chr(2, 6, ((address / 10) % 10) + 48);
         Serial_Lcd_Chr(2, 7, (address % 10) + 48);
         Serial_Lcd_Chr(2, 15, ((command / 10) % 10) + 48);
         Serial_Lcd_Chr(2, 16, (command % 10) + 48);
         }
     }
   }

RC-5 IR protocol decoder video:
The following video shows how the circuit works with more details about this project.