NEC Protocol decoder with PIC16F887 microcontroller – CCS C

This article shows the decoding of an IR remote control uses NEC protocol. The microcontroller used in this project is PIC16F887 and the compiler is CCS PIC C. Decoding results are displayed on 16×2 LCD screen.

The complete extended NEC protocol message is started by 9ms burst followed by 4.5ms space which is then followed by the Address and Command. The address is 16-bit length and the command is transmitted twice (8 bits + 8 bits) where in the second time all bits are inverted and can be used for verification of the received message. The following drawing shows an extended NEC message example.

Extended NEC protocol message code

The NEC protocol uses pulse distance encoding of the bits. Each pulse is a 562.5µs long with carrier frequency of 38KHz. Logic bits are transmitted as follows:
Logic 0: 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1125µs (562.5 x 2).
Logic 1: a 562.5µs pulse burst followed by a 1687.5µs (562.5 x 3) space, with a total transmit time of 2250µs (562.5 x 4).

NEC protocol modulation

Hardware Required:

  • PIC16F887 microcontroller
  • IR Receiver
  • 16×2 LCD screen
  • 10K ohm variable resistor
  • 47µF capacitor
  • 0.1µF ceramic capacitor (optional)
  • 10K ohm resistor
  • 5V Power source
  • Protoboard
  • Jumper wires

PIC16F887 NEC decoder remote control hardware circuit

NEC Protocol decoder with PIC16F887 microcontroller circuit:
Extended NEC protocol decoder using PIC16F887 circuit schematic is shown below where a 1602 LCD is used to display the NEC protocol parameters (address and command). 

PIC16F887 extended NEC decoder remote control circuit

PIC16F887 uses its internal oscillator which is configured in the C code.
As shown in the circuit diagram, the output of the IR receiver is connected to external interrupt pin which is RB0. A 10K ohm resistor is connected between the IR receiver output and +5V in order to minimize noise which comes from it.

NEC Protocol decoder with PIC16F887 microcontroller C code:
The IR receiver output is logic high at idle state and when it receives a burst it changes the output to logic low.

The message of the NEC protocol is 32-bit long, address (16 bits), command (8 bits), and inverted command (8 bits). Before the previous 32 bits there is 9ms burst and 4.5ms space.
A logic 1 is represented by 562.5µs burst and 562.5µs space (total of 1125µs) and a logic 0 is represented by 562.5µs burst and 1687.5µs space (total of 2250µs).
Keep in mind that the IR receiver output is always inverted.

The interval [ 9500µs, 8500µs ] is used for the 9ms pulse and for the 4.5ms space the interval [ 5000µs, 4000µs ] is used.
The 562.5µs pulse is checked with the interval [ 700µs, 400µs ] .
For the 562.5µs or 1687.5µs space I used the interval [ 1800µs, 400µs ], and to know if its a short or long space I used a length of 1000µs.

The output of the IR receiver is connected to the external interrupt pin (RB0) and every change in the pin status generates an interrupt and Timer1 starts, Timer1 value will be used in the next interrupt, this means Timer1 measures the time between two interrupts which is pulse time or space time. Also, Timer1 interrupt is used to reset the decoding process in case of very long pulse or space.
Timer1 time step is 1µs (Timer1 increments every 1µs). If you use mcu frequency other than 8MHz, make sure to keep Timer1 time step to 1µs, otherwise time intervals have to be changed.
The decoding results are displayed on 1602 LCD screen connected to PORTD.

NEC Protocol decoder with PIC16F887 microcontroller video:


One comment

  1. Hello,
    I am trying to make something similar with this project. But I have troubles to get “nec_ok” part true. If you can help me, I will be very grateful

Leave a Reply

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