RC-5 remote control protocol decoder using PIC16F877A and CCS C

PIC16F877A RC-5 decoder

The RC-5 protocol was developed by Philips in the late 1980s as a semi-proprietary consumer IR (infrared) remote control communication protocol for consumer electronics.
The RC5 has 14 bits per 1 code transmission, the 14 bits can be divided into 4 parts:

The first 2 bits are start bits and they are always logic 1.
The third bit called toggle bit, it can be logic 1 or logic 0.
The next 5 bits are address bits, each device type has its address number for example TV address number is 0, CD player address = 20 …………
And the last 6 bits are command bits, each button has its command number.
For the same device for example TV all the remote control buttons has the same address but each button has its command.
The toggle bit changes whenever a button is pressed.
The RC5 protocol uses Manchester coding, logic 1 and logic 0 are coded as shown in the following drawing where toggle bit is 1, address = 0 and command is 2: 

RC5 IR remote control protocol coding 

This link from Wikipedia has more details about Philips RC5 protocol.

RC-5 IR remote control protocol decoder using PIC16F877A microcontroller:
This topic shows how to decode and get RC-5 data of a TV remote control uses RC5 protocol with PIC16F877A microcontroller and CCS PIC C compiler.

The project circuit schematic is shown below.
PIC16F877A RC-5 remote control decoder circuit
The IR receiver is used to receive the IR signals transmitted from the remote control and convert this signals to a digital data (logic 0, logic 1). The microcontroller PIC16F877A reads digital data from the IR receiver, this data is decoded and the results displayed on 1602 LCD display. The LED which is connected to RB1 pin blinks when RC5 protocol is received.
The following drawing shows how the IR receiver receive and transmit data to the microcontroller:

IR receiver circuit 

RC-5 IR remote control protocol decoder using PIC16F877A C code:
The C code below was tested with CCS C compiler version 5.051.
I made the code simple as I can, no Timer module and no interrupt are used.
The microcontroller waits until the IR receiver receives IR data which means it waits until RB0 pin goes from high to low.
The 2 start bits are used to check if the received IR signal uses RC5 protocol. Each bit is 1778us long. The fist start bit is 1 which means it is 0 for 889us and 1 for 889us, the first 889us is not detected because RB0 not triggered yet and the second 889us is detected using the following code:
while((input(PIN_B0) == 0) && (count < 20)){
count++;
delay_us(50);}
if( (count > 20) || (count < 14))

    goto ret;
The signal is checked every 50us and a variable (count) is used to count number of 50s, for example if count = 5 so the total time before RB0 goes high is 250us.
If count >= 20 it means time out and the received signal is high for more than 889us.
If count < 14 it means signal high time is lower than 889us.
The second start bit is checked using the following code:
// Check first 889us
while((input(PIN_B0)) && (count < 20)){
count++;
delay_us(50);}
if( (count > 20) || (count < 14))            

      goto ret;
// Check 2nd 889us
      count = 0;
while((input(PIN_B0) == 0) && (count < 40)){
count++;
delay_us(50);}
if( (count > 40) || (count < 14))      // Signal doesn’t use RC5 protocol
goto ret;
if(count > 30)
delay_us(400);
else
delay_us(1200);

Second bit checking is the same as the first bit checking except that both 889us spaces are checked.
After the 2nd start bit there is toggle bit which is 1 or 0, and if toggle bit is 0 time required for RB0 pin is 1778us which means to start reading bits we have to add a delay about 400us, and if the toggle bit is 1 we have to wait 1200us.
The following drawing shows the 2 start bit with toggle bit when it’s 0 and when it’s 1:

RC-5 IR remote control code

And the following drawing shows when the microcontroller reads the RC5 bits:

RC5 IR remote control decoding

The complete PIC16F877A RC-5 decoder C code is below.

RC5 IR remote control protocol decoder using PIC16F877A video:
The following video shows the project using hardware circuit with some details.


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top