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).
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.
NEC Protocol IR remote control decoder with PIC16F84A microcontroller:
It is easy to decode IR remote control uses NEC protocol using microcontrollers.
Here Microchip PIC16F84A microcontroller is used to decode IR remote controls which uses NEC and extended NEC protocol. Decoder circuit schematic is shown below. The following drawing shows an extended NEC message example.
NEC Protocol IR remote control decoder with PIC16F84A CCS C code:
There are different ways to decode the NEC protocol for example using CCP module and Timer, using Timer module or using port change interrupt. In this project I didn’t use any interrupt or Timer, I used delay command to make the code as simple as possible with time-out property and the code checks the IR signal with resolution of 50µs.
Programming hints:
From the decoder circuit schematic above the output of the IR receiver is connected to RB0 and when an IR signal is received RB0 pin goes from logic high (+5V) to logic low (0V).
The NEC message has 32 bits which are divided into address (16 bits), command (8 bits) and inverted command (8 bits).
The microcontroller waits until the IR receiver receives an IR signal which makes RB0 goes from logic high to logic low and the command used is:
while(input(PIN_B0));
After that a function named nec_remote_read() is called, this function checks if the received signal has NEC protocol form all the time.
The last function reads the 9ms burst using the following lines and if the pulse is more than 10ms (50µs x 200) or less than 8ms (50µs x 160) the function returns with false result which means the received signal doesn’t have NEC protocol form.
while((input(PIN_B0) == 0) && (count < 200)){
count++;
delay_us(50);}
if( (count > 199) || (count < 160))
return false;
The 4.5ms space is checked as the 9ms burst with small differences:
while((input(PIN_B0)) && (count < 100)){
count++;
delay_us(50);}
if( (count > 99) || (count < 60))
return false;
After that the microcontroller starts reading the 32 bits and keeps checking of NEC protocol form.
The following code is the complete CCS PIC C code written with compiler version 5.051.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | // NEC IR remote control decoder using PIC16F84A CCS C code //LCD module connections #define LCD_RS_PIN PIN_B1 #define LCD_RW_PIN PIN_B2 #define LCD_ENABLE_PIN PIN_B3 #define LCD_DATA4 PIN_B4 #define LCD_DATA5 PIN_B5 #define LCD_DATA6 PIN_B6 #define LCD_DATA7 PIN_B7 //End LCD module connections #include <16F84A.h> #fuses HS,NOWDT,PUT,NOPROTECT #use delay(crystal=8000000) #include <lcd.c> #use fast_io(B) unsigned int32 ir_code; unsigned int16 address; unsigned int8 command, inv_command; short nec_remote_read(){ unsigned int8 count = 0, i; // Check 9ms pulse (remote control sends logic high) while((input(PIN_B0) == 0) && (count < 200)){ count++; delay_us(50); } if( (count > 199) || (count < 160)) // NEC protocol? return FALSE; count = 0; // Check 4.5ms space (remote control sends logic low) while((input(PIN_B0)) && (count < 100)){ count++; delay_us(50); } if( (count > 99) || (count < 60)) // NEC protocol? return FALSE; for(i = 0; i < 32; i++){ count = 0; while((input(PIN_B0) == 0) && (count < 14)){ count++; delay_us(50); } if( (count > 13) || (count < 8)) // NEC protocol? return FALSE; count = 0; while((input(PIN_B0)) && (count < 40)){ count++; delay_us(50); } if( (count > 39) || (count < 8)) // NEC protocol? return FALSE; if( count > 20) // If space width > 1ms bit_set(ir_code, (31 - i)); // Write 1 to bit (31 - i) else // If space width < 1ms bit_clear(ir_code, (31 - i)); // Write 0 to bit (31 - i) } return TRUE; } void main(){ output_b(0); // PORTB initial state set_tris_b(1); // Configure RB0 pin as input lcd_init(); // Initialize LCD module lcd_putc('\f'); // LCD clear lcd_gotoxy(3, 1); // Go to column 3 row 1 lcd_putc("NEC protocol"); lcd_gotoxy(5, 2); // Go to column 5 row 2 lcd_putc("decoder"); delay_ms(2000); lcd_putc('\f'); lcd_gotoxy(1, 1); // Go to column 1 row 1 printf(lcd_putc, "Address:0x0000"); lcd_gotoxy(1, 2); // Go to column 1 row 2 printf(lcd_putc, "Com:0x00 In:0x00"); while(TRUE){ while(input(PIN_B0)); // Wait until RB0 pin falls if(nec_remote_read()){ address = ir_code >> 16; command = ir_code >> 8; inv_command = ir_code; lcd_gotoxy(11, 1); printf(lcd_putc,"%4LX",address); lcd_gotoxy(7, 2); printf(lcd_putc,"%2X",command); lcd_gotoxy(15, 2); printf(lcd_putc,"%2X",inv_command); delay_ms(200);} } } |
NEC Protocol IR remote control decoder with PIC16F84A video:
The following video shows how this decoder works in a hardware circuit.
Reference:
http://www.sbprojects.com/
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.