Implementation of IR NEC protocol By Rubashka Vasiliy , Ukraine
NEC Protocol
To my knowledge the protocol I describe here was developed by NEC. I've
seen very similar protocol descriptions on the internet, and there the
protocol is called Japanese Format. I do admit that I don't know
exactly who developed it. What I do know is that it is used in my late VCR
produced by Sanyo and was marketed under the name of Fisher. NEC
manufactured the remote control IC. This description was taken from
the VCR's service manual. Those were the days, when service manuals were
fulled with useful information!
- 8 bit address and 8 bit command length
- Address and command are transmitted twice for reliability
- Pulse distance modulation
- Carrier frequency of 38kHz
- Bit time of 1.12ms or 2.25ms
The NEC protocol uses pulse distance
encoding of the bits. Each pulse is a 560µs long 38kHz carrier burst
(about 21 cycles). A logical "1" takes 2.25ms to transmit, while a logical
"0" is only 1.12ms. The recommended carrier duty-cycle is 1/4 or 1/3.
The picture above shows a typical pulse train of the NEC protocol. With
this protocol the LSB is transmitted first. In this case Address $59 and
Command $16 is transmitted. A message is started by a 9ms AGC burst, which
was used to set the gain of the earlier IR receivers. This AGC burst is
then followed by a 4.5ms space, which is then followed by the Address and
Command. Address and Command are transmitted twice. The second time all
bits are inverted and can be used for verification of the received
message. The total transmission time is constant because every bit is
repeated with its inverted length. If you're not interested in this
reliability you can ignore the inverted values, or you can expand the
Address and Command to 16 bits each!
A command is transmitted only once, even when the key on the remote
control remains pressed. Every 110ms a repeat code is transmitted for as
long as the key remains down. This repeat code is simply a 9ms AGC pulse
followed by a 2.25ms space and a 560µs burst.
The table below lists the messages sent by the remote control of my
late Fisher 530 VCR (it served us well during its 20 years long life).
NEC Message |
Key Function |
$68-$00 |
Play |
$68-$01 |
Rec |
$68-$02 |
Audio Dub |
$68-$03 |
Frame Adv |
$68-$04 |
Slow |
$68-$05 |
Quick |
$68-$06 |
Cue |
$68-$07 |
Review |
$68-$08 |
FF |
$68-$09 |
Rew |
$68-$0A |
Stop |
$68-$0B |
Pause/Still |
$68-$0C |
Up key |
$68-$1E |
Down key | Source code:
'****************************************************************************
'* Filename : IR NEC by http://www.sbprojects.com/knowledge/ir/ir.htm *
'* Revision : 1.0 *
'* Controller : ATMEGA8 *
'* Compiler : BASCOM-AVR 1.11.8.3 DEMO *
'* Author : Rubashka Vasiliy , Ukraine , 2007 *
'* WWW : http://ledeffects.net *
'* Mail : info@ledeffects.net *
'****************************************************************************
'* *
'* IR 36kHz ATMEGA 8 *
'* .-O-. .--_/--. *
'* |___| /-[10k]-|Res | *
'* ||| | | | *
'* /--/|| --- | | *
'* | |*--------+---|Pd2 Pc2|----[510]-----|>|---- LED *
'* | || | | | | *
'* | |-[47k]--* | | | *
'* | *--[100]--*---|Vcc GND|--------- | *
'* *---+---------+---|GND | | | *
'* | | /---+---|Pb6 Vcc|------- | | *
'* | | | /-+---|Pb7 Pb5|----- | | | *
'* | | | | | | Pb4|--- | | | | *
'* | | | | | | | | | | | | *
'* | | | | | | Pb2|- | | | | | *
'* | | | | | /-|Pb0 | | | | | | | *
'* | | | | | | '-------' | | | | | | *
'* | | | | | --------- | | | | | | *
'* | | | | -----------+-+-+-+-*-+-----*-----+-----*------- *
'* | +|10,0 | ----------- | | | | | | |0,1 | +|100,0 | *
'* | --- ----------- | | | | | | | --- | --- | *
'* | --- | | | | | | | | --- | --- | *
'* | | | | | | | | | | | | | | *
'* ---*-----------------+-+-+-+-+-+-+-*-*-*-*-----*-----*----- | *
'* | | | | | | | | | | | | | *
'* .-------------------. --- V V *
'* |D D R E D D V V R V| *
'* |6 7 s 4 5 c s w 0| - + *
'* '-------------------' 5 5 *
'* ":::::::::::::::::::" V V *
'* ":::::LCD::16*2:::::" *
'* ":::::::::::::::::::" *
'* """"""""""""""""""""" *
'* *
'* *
'****************************************************************************
$regfile = "m8def.dat"
$crystal = 8000000 'Internal RC oscillator 8 MHz
$lib "lcd4.lbx"
Config Lcdpin = Pin , Rs = Portb.0 , E = Portb.2 , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7
Config Lcd = 16 * 2
Config Pind.2 = Input
Config Portc.2 = Output
Config Timer0 = Timer , Prescale = 256 '8000000/256=31250 Hz
Config Int0 = Falling 'Interruption on Falling
Stop Timer0
Enable Timer0
Enable Int0
Enable Interrupts
On Timer0 Tikers 'work on timer
On Int0 Infrared 'work on interruption
Dim Tik As Word 'counter of teaks of timer
Dim Byt As Byte 'counter accepted bit
Dim Repeat_flag As Bit 'flag of repetition
Dim Start_flag As Bit 'flag of start condition
Dim Address As Byte 'byte of address
Dim Command As Byte 'byte of command
Dim Address_1 As Byte 'direct byte of address
Dim Command_1 As Byte 'direct byte of command
Dim Address_0 As Byte 'indirect byte of address
Dim Command_0 As Byte 'indirect byte of command
Dim Summa As Word
Cursor Off 'Switch Off cursor
'################################################################################################################
Do 'Main cycle
Cls 'Clean LCD
Lcd Address ; " " ; Command 'Lcd ADDRESS and COMMAND
If Command = 8 Then Portc.2 = 1 'If pressed key "P+" - to include LED
If Command = 240 Then Portc.2 = 0 'If pressed key "P-" - to switch off LED
Waitms 10 'Delay 10 ěń
Loop
End 'End of main cycle
'################################################################################################################
Tikers: 'work on timer
Timer0 = 253 '31250/(256-253)=10416,66 Hz (96 ěęń)
Incr Tik
If Tik >= 1200 Then 'if 1200 teaks, have thrown all in source condition
Tik = 0
Repeat_flag = 0
Start_flag = 0
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
Address = 0
Command = 0
Stop Timer0
End If
Return
'################################################################################################################
Infrared: 'work on interruption
Start Timer0
If Tik >= 139 And Tik < 150 Then 'if has happenned from 139 before 150 teaks - "START"
Address = 1
Repeat_flag = 0
Start_flag = 1
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
End If
If Tik >= 116 And Tik < 139 Then 'if has happenned from 116 before 138 teaks - "REPETITION"
Address = 2
Repeat_flag = 1
Start_flag = 0
End If
If Tik >= 22 And Tik < 116 And Start_flag = 1 Then 'if has happenned from 22 before 115 teaks - have taken "1"
Incr Byt
If Byt < 9 Then
Shift Address_1 , Left
Address_1 = Address_1 + 1
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
Address_0 = Address_0 + 1
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
Command_1 = Command_1 + 1
End If
If Byt >= 25 Then
Shift Command_0 , Left
Command_0 = Command_0 + 1
End If
End If
If Tik >= 10 And Tik < 22 And Start_flag = 1 Then 'if has happenned from 10 before 21 teaks - have taken "0"
Incr Byt
If Byt < 9 Then
Shift Address_1 , Left
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
End If
If Byt >= 25 Then
Shift Command_0 , Left
End If
End If
Tik = 0
If Byt = 32 Then 'if have taken 4 bytes, check correctness a receiving a command
'if address or command 16-bit, check does not pass
'Summa = Address_0 + Address_1
'If Summa = 255 Then
Address = Address_1
'Else
'Address = 0
'End If
'Summa = Command_0 + Command_1
'If Summa = 255 Then
Command = Command_1
'Else
'Command = 0
'End If
Byt = 0
Repeat_flag = 0
Start_flag = 0
Stop Timer0
End If
Return
'################################################################################################################ |