IR Remote Control programs for the NEC Protocol and Variants.


Author Message
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3343
Posted: 08:00am 01 Sep 2024      

An update for the IR Receiver program above.
It now also does standard NEC codes with 8 and 16 bit Device codes using NECs LSB first format.
It saves the results to a file, including the variant number in a format that can be copied and pasted into the NECsend program.
' "NEC and Samsung32 IR code receiver.bas"
' tested on PicoMite v5, v6 & ArmMiteF4 v5.07.02
Dim Integer Dev, Key, V, IRpin = MM.Info(PinNo GP1) '<- set your pin - Pico
'Dim Integer Dev, Key, V, IRpin = 1 '= PE2 <- set your pin - F4
Dim string KN
Print
Open "NEC_Key_Codes.txt" For append As #3
Pause 55
Print "To save the data (NEC_Key_Codes.txt) press any RC key"
Print " and type Quit at the 'Enter Key name ?' prompt." : Print
Print "Device, Key, Variant, 'Function"
Print #3, "'Device, Key, Variant, 'Function,,NEC_Key_Codes.txt"
SetPin IRpin, INTL, IR_Rx_NEC_S32

Sub IR_Rx_NEC_S32
 SetPin IRpin, off :SetPin IRpin, DIN 'stop interrupt and prepare to get data
 Local float T=Timer, T1, T2, Tmax=300, Z=T+Tmax
 Local Integer n, m
 dev = 0 : key = 0 : V = 0 : KN = ""

 Do :Loop Until Pin(IRpin)OR(Timer>Z):T2=Timer-T:Do While Pin(IRpin)AND(Timer<Z):Loop :T1=Timer-T
 'wait for start sequence to complete then get time, falling edge to falling edge
 For n=0 To 31
  T=Timer :Do :Loop Until Pin(IRpin):Do While Pin(IRpin):Loop :T1=Timer-T

  If T1>1.4 AND T1<2.4 Then 'measure bits, add to word, LSB first into MSB and shifting down
    m=(m>>1) + &H80000000 'add 1
   Else
    m=m>>1 'add 0
  EndIf
 Next

 SetPin IRpin, off
'  Print :Print "Start pulse";T2;"mS  ";Bin$(m,32); " Bit order reversed so MSB is on the left":Print

'  Print Bin$(m And &Hff,8), Bin$(m>>16 And &Hff,8) 'show assembled bytes 1 & 3 (m is in reverse order)
'  Print Bin$(m>>8 And &Hff,8), Bin$(255-(m>>24) And &Hff,8) 'show assembled bytes 2 & inverted 4
  'error check. compare byte 1 and byte 2
 If ((m AND 255) = ((m>>8) AND 255)) AND (T2 > 4) AND (T2 < 5) Then
  V = 1 'Samsung32, LSB first
  dev = m AND &Hff 'get device code
  key = m >> 16  AND &Hff 'get key code
 EndIf

 If ((m AND 255) = (255-((m>>8)) AND 255)) AND (T2 > 7) AND (T2 < 10) Then
  V = 4 'NEC 8 bit, LSB first
  dev = m AND &Hff 'get device code
  key = m >> 16  AND &Hff 'get key code
 EndIf

 If ((255-(m>>24) AND 255) = ((m>>16) AND 255)) AND (T2 > 7) AND (T2 < 10) AND (V = 0) Then
  V = 3 'NEC 16 bit, LSB first
  dev = m AND &Hffff 'get device code
  key = m >> 16  AND &Hff 'get key code
 EndIf

 If dev=0 Then GoTo NoData 'restart the interupt and end sub

 Print Dev;","; Key;","; V,
 Input "  Enter the Key name "; KN

 If (KN = "Quit") OR (KN = "quit") OR (KN = "QUIT") Then
  Print #3, : Print #3,"To use NECsend without the Variant number"
  Print #3,"replace the Device code:- ";Dev;" with:- ";Dev + (V<<16):Print #3,
  Pause 55
  Close #3
  Print
  List "NEC_Key_Codes.txt"
  End
 EndIf

 Print #3, Dev;","; Key;","; V; "  '"; KN

 NoData:
 Pause 300 'prevent re-triggering, Samsung remote always sends code twice
 SetPin IRpin, INTL, IR_Rx_NEC_S32 'prepare for next input

End Sub

Do : Loop

'Samsung32 specification:-
 'coding: Pulse Distance
 'Frame: 1 start + 32 Coding + 1 Stop bits
 'Data: 8 bit address + 8 bit address + 8 bit command + inverted 8 bit command all LSB first
 'Start Bit: 4500uS pulse 4500uS pause
 'Data: 0 bit = 550uS pulse + 550uS pause, 1 bit = 550uS pulse + 1650uS pause
 'Stop Bit 550uS pulse
 'Repetition after 47mS
 'on the remote control I have a button press always sends the code at least twice
End

When you Quit it lists the content of the file.
> RUN

To save the data (NEC_Key_codes.txt) type Quit at the 'Enter Key name ?' prompt.
Device, Key, Variant, 'Function
29185, 3, 3  Enter the Key name ? up
29185, 2, 3  Enter the Key name ? left
29185, 95, 3  Enter the Key name ? ok
29185, 12, 3  Enter the Key name ? info
5, 72, 1  Enter the Key name ? on
5, 29, 1  Enter the Key name ? return
5, 67, 1  Enter the Key name ? ok
64769, 206, 3  Enter the Key name ? list
64769, 193, 3  Enter the Key name ? vol+
1, 5, 4  Enter the Key name ? power
1, 19, 4  Enter the Key name ? enter
1, 91, 4  Enter the Key name ? pause
1, 4, 4  Enter the Key name ? stop
1, 4, 4  Enter the Key name ? quit

'Device, Key, Variant, 'Function
29185, 3, 3  'up
29185, 2, 3  'left
29185, 95, 3  'ok
29185, 12, 3  'info
5, 72, 1  'on
5, 29, 1  'return
5, 67, 1  'ok
64769, 206, 3  'list
64769, 193, 3  'vol+
1, 5, 4  'power
1, 19, 4  'enter
1, 91, 4  'pause
1, 4, 4  'stop
>


Edit.
Have edited the code above to improve the screen layout.

Edit 2
Same program but using PULSIN to get the data instead of waiting for the pin to change.
Benefits: nil
Disadvantages: nil
Reason: because you can
' "NEC JVC16 Samsung32 IR code receiver v3 - PicoMite & F4.bas"
' tested on PicoMite v6, 64MHz to 400MHz & ArmMiteF4 v5.07.02, 160MHz
Dim Integer Dev, Key, V, IRpin = MM.Info(PinNo GP1) '<- set your pin
'Dim Integer Dev, Key, V, IRpin = 1 '= PE2 <- set your pin

Sub IR_Rx_NEC_V
SetPin IRpin, off :SetPin IRpin, DIN 'stop interrupt and prepare to get data
Local float T=Timer, T1, T2, Tmax=300, Z=T+Tmax
Local Integer n, m
dev = 0 : key = 0 : V = 0 : KN = ""

Do :Loop Until Pin(IRpin)Or(Timer>Z):T2=Timer-T
'wait for start sequence to complete then get low pulses
For n=0 To 31
 T1=Pulsin(IRpin,1)
'  Print T1;

 If (T1 < 4000) And (T1 > 200) Then
   If T1>1000 And T1<2000 Then 'measure bits, add to word, LSB first into MSB and shifting down
     m=(m>>1) + &H80000000 'add 1
    Else
     m=m>>1 'add 0
   EndIf
  Else
   Exit For
 EndIf

Next
'  Print "m",Bin$(m>>16,16)

SetPin IRpin, off
'Print :Print "Start pulse";T2;"mS  ";Bin$(m,32); " Bit order reversed so MSB is on the left"
'Print T1;"uS last pulse"

'  Print Bin$(m And &Hff,8), Bin$(m>>16 And &Hff,8) 'show assembled bytes 1 & 3 (m is in reverse order)
'  Print Bin$(m>>8 And &Hff,8), Bin$(255-(m>>24) And &Hff,8) 'show assembled bytes 2 & inverted 4
 'error check. compare byte 1 and byte 2
If ((m And 255) = ((m>>8) And 255)) And (T2 > 4) And (T2 < 5) Then
 V = 1 'Samsung32, LSB first
 dev = m And &Hff 'get device code
 key = m >> 16  And &Hff 'get key code
EndIf

If ((m And 255) = (255-((m>>8)) And 255)) And (T2 > 7) And (T2 < 10) Then
 V = 4 'NEC 8 bit, LSB first
 dev = m And &Hff 'get device code
 key = m >> 16  And &Hff 'get key code
EndIf

If ((255-(m>>24) And 255) = ((m>>16) And 255)) And (T2 > 7) And (T2 < 10) And (V = 0) Then
 V = 3 'NEC 16 bit, LSB first
 dev = m And &Hffff 'get device code
 key = m >> 16  And &Hff 'get key code
EndIf

If ((m And &Hffff) = 0) And (T2 > 7) And (T2 < 10) Then
 V = 5 'JVC16, LSB first
 dev = (m >> 16) And &Hff 'get device code
 key = (m >> 24)  And &Hff 'get key code
EndIf

If dev<>0 And key<>0 Then 'output data

 Print Dev;","; Key;","; V,
 Input "  Enter the Key name "; KN

 If (KN = "Quit") Or (KN = "quit") Or (KN = "QUIT") Then
   Print #3, : Print #3,"To use NECsend without the Variant number"
   Print #3,"replace the Device code:- ";Dev;" with:- ";Dev + (V<<16):Print #3,
   Pause 55
   Close #3
   Print
   List "NEC_Key_Codes.txt"
   End
  Else
   Print #3, Dev;","; Key;","; V; "  '"; KN
 EndIf

EndIf
Pause 300 'prevent re-triggering, Samsung & JVC remotes always send code twice
SetPin IRpin, INTL, IR_Rx_NEC_V 'prepare for next input

End Sub

Dim string KN
Print
Open "NEC_Key_Codes.txt" For append As #3
Pause 55
Print "To save the data (NEC_Key_Codes.txt) press any RC key"
Print " and type Quit at the 'Enter Key name ?' prompt." : Print
Print "Device, Key, Variant, 'Function"
Print #3, "'Device, Key, Variant, 'Function,,NEC_Key_Codes.txt"
SetPin IRpin, INTL, IR_Rx_NEC_V
Do : Loop

End


Edit 3 9/9/2024
The code above has been edited again, adding the JVC 16 bit protocol. Close enough to NEC to count as Variant 5.

The received codes match the corresponding functions at the end of this document:-
JVC Protocol so I know it is working.
The main difference is it uses only 16 bits so there is no inherent error detection. The remote always sends the code at least twice so it would be possible to modify my code to read both copies and compare them.

Another difference is the remote control can have more than one Device Code to cater for multi function devices.
Edited 2024-09-10 11:55 by phil99