NEC remote control decoder with PIC16F887 and mikroC compiler

In this blog I made NEC remote control decoder using PIC16F887 microcontroller and CCS C compiler, and this topic shows the same project but using mikroC compiler.
Previous project and some information about the NEC protocol:
NEC Protocol decoder with PIC16F887 microcontroller – CCS C

Components Required:

  • PIC16F887 microcontroller
  • IR Receiver
  • 1602 LCD screen
  • 10K ohm variable resistor
  • 5V Power source
  • Protoboard
  • Jumper wires

The Circuit:
NEC decoder circuit diagram is shown below.

NEC remote control decoder circuit

In this example the PIC16F887 runs with its internal oscillator and MCLR pin function is disabled (configured in software).
As shown in the circuit diagram, the output of the IR receiver is connected to pin RB0, the LCD is connected to RD port.

NEC remote control decoder mikroC code:
The mikroC code below was tested with mikroC pro for PIC version 7.1.0.
The PIC16F887 is configured to work with its internal oscillator @ 8MHz.

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 the external interrupt pin RB0 (PORTB change interrupt), every change in the pin status generates an interrupt and Timer1 starts calculating, Timer1 value will be used in the next interrupt, this means Timer1 calculates 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 very long 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.

This project should gives the result shown in the following video:


  1. hy
    please can you tell me that if i wouldn’t use Internal oscillator then i what to do?? mean i wan to use this program in PIC16F72. please can you help me??

  2. Hi
    The start 9ms pulse and 526.5us pulse composed of many small number of carrier pulses, so wouldn’t it trigger interrupt service routine.?

    The diagram of the nec signal can be found in the link:

    It seems that the code considers the received signal as a continuous pulses instead of modulated carrier wave. Please help me understand why wouldn’t the timer1 be reset when a second high(or low-according to the type of edge detection) of the same pulse comes(according to your code)?

Leave a Reply

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