NEC Remote control decoder with PIC microcontroller | MPLAB Projects

This topic shows how to decode IR (Infra-Red) remote controls which use NEC protocol (NEC and extended NEC) using PIC16F887 microcontroller. Results are displayed on 16×2 LCD screen.
The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

About NEC protocol:
The complete extended NEC protocol message is started by 9ms burst followed by 4.5ms space which is then followed by 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

The difference between the NEC protocol and the extended NEC protocol is that in the first one the address (16-bit long) is transmitted twice (8-bit + 8-bit) where in the second time all bits are inverted (the same thing with command and inverted command).
In the extended NEC protocol the second 8 bits are independent of the first ones.

PIC16F887 NEC decoder remote control hardware circuit

Related Project:
To see how to interface PIC microcontroller with LCD module using MPLAB XC8 compiler, read the following post:
Interfacing LCD with PIC microcontroller | MPLAB Projects

Hardware Required:

  • PIC16F887 microcontroller   —->  datasheet
  • IR Receiver
  • 16×2 LCD screen
  • 10k ohm variable resistor or potentiometer
  • 330 ohm resistor
  • 5V Power source
  • Protoboard
  • Jumper wires

NEC Remote control decoder with PIC microcontroller circuit:
The following image shows project circuit diagram.

Remote control decoder using PIC16F887 MCU - MPLAB XC8 remote control

(All grounded terminals are connected together)

Generally the IR receiver has 3 pins: GND, VCC and data. The data pin is connected to pin RB0 of the PIC16F887 microcontroller.

The 16×2 LCD screen is connected to the PIC16F887 microcontroller as follows:
RS —> RD0 pin
E  —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground)
VEE to the variable resistor (or potentiometer) output pin
VDD to +5V and A to +5V through 330 ohm resistor

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

NEC Remote control decoder with PIC microcontroller C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05.

To be able to compile the C code, a small LCD library for MPLAB XC8 compiler is required which can be downloaded from the following link:
MPLAB XC8 LCD Library

after the download, add the library file (LCD_Lib.c) to project folder.

Programming hints:
The message of the NEC protocol is 32-bit long, address (16 bits), command (8 bits), and inverted command (8 bits). Before the 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 external interrupt pin (RB0) (interrupt on change) and every change in the pin status generates an interrupt and Timer1 starts ticking, 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.

The microcontroller used in this example is PIC16F887, configuration words are:

Where:

  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Full MPLAB XC8 code:

The following video shows how this project should work:

Reference:
http://www.sbprojects.com/

2 thoughts on “NEC Remote control decoder with PIC microcontroller | MPLAB Projects”

  1. Hello Sir,
    Thanks for sharing NEC Protocol IR remote control decoder with PIC16F887 MCU C Code for MPLAB XC8 compiler.
    project is working fine,i need a help, suppose i am using 10 led bar and i want to on/off all led sequentially with single long/continuous press by any remote key. now it works only one time because when i long press a key it generates repeat code followed by a command, could you please help me to rectify this problem.
    Thanks,

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