Posted: 11:14am 25 Mar 2024 |
|
|
|
Tuned up with reversal code mentioned above. Device and key code match original micromite input and output codes.
Transmit-
'Using 'Bit Twiddling Hacks' (Search on the Internet) pointed to by phil99 ,matherp '22/3/2024 - new NECSend routine to replace original due problem with 4.5mS space generation 'uses the same dev code and key code as the micromite ' IR Transmit Const IRpin = MM.Info$(pinno PE0) 'output pin, change as needed SetPin IRpin, dout : Pin(IRpin) = 0 ' IR code output ' IR receiver input, fixed as PE2 for F4 ' Do 'Your program here. Input "Enter Device and Command codes "; dev, key NECsend IRpin, dev, key pause 1000 Loop Sub NECsend IRpin As integer, dev As integer, key As integer Local integer word, d(1389), m, n = 1 'need to reverse bits in a byte as NEC IR code is sent LSB first If dev < 256 Then ' 8 bit address, so add inverse of address DevCode = ((dev * &H0202020202 and &H010884422010) mod 1023)<<8 DevCode = DevCode + (255-((dev * &H0202020202 and &H010884422010) mod 1023)) Else ' extended or 16 bit address DevCode = (((dev and 255) * &H0202020202 and &H010884422010) mod 1023) << 8 DevCode = DevCode + ((dev>> 8) * &H0202020202 and &H010884422010) mod 1023 end if KeyCode = (key * &H0202020202 and &H010884422010) mod 1023 'Check operation of bit reverse code print print "Input- device ";dev;" "; bin$(dev);" key ";key;" ";Bin$(key) print "Output- DevCode "; DevCode;" ";BIN$(DevCode,16);" ";" KeyCode ";KeyCode;" ";Bin$(KeyCode,8) Print ' word = (DevCode<<16) + (KeyCode<<8) + 255-KeyCode 'assemble 32 bits to transmit ' Math set 14 , d() '14=1000/(38*2)uS duration of a half cycle @ 38kHz BitBang bitstream IRpin, 670, d() 'send 9ms start 38kHz Timer =0 For m = 43 To 1387 Step 42 'load data sent after start sequence d(m) = (((word>>(32-n)) AND 1)*2 + 1) * 562.5 'convert bits to duration (short = 0, long = 1) Inc n 'step to next data bit Next Do : Loop Until Timer > 4.3 'send 4.5ms start space BitBang bitstream IRpin, 1387, d() 'send the data Pin(IRpin) = 0 End Sub
IR received decoder-
'Using 'Bit Twiddling Hacks' (Search on the Internet) pointed to by phil99 ,matherp 'uses the same device code and key code as the micromite 'IR receiver input, fixed as PE2 for F4 foreCol% = RGB(white) backCol% = rgb(BLACK) CLs IR dev, key , irint 'start the decoder Print " Waiting for IR input" text 1,20," Waiting for IR input",LT,1,1,foreCol%,backCol% Do 'Your program here. pause 1000 Loop 'IR input detected irint: If (dev And &HFF) = ((dev>>8) Xor &HFF) Then ' 8 bit address Print "device address 8 bit " DevCode = ((dev>>8) * &H0202020202 and &H010884422010) mod 1023 Else ' extended or 16 bit address Print "device address 16 bit " DevCode = (((dev and 255) * &H0202020202 and &H010884422010) mod 1023) << 8 DevCode = DevCode + ((dev>> 8) * &H0202020202 and &H010884422010) mod 1023 EndIf KeyCode = (key * &H0202020202 and &H010884422010) mod 1023 ' print "DevCode ";DevCode," "; bin$(DevCode,8);" KeyCode ";KeyCode;" ";Bin$(KeyCode,8) print cls text 1,20,"Device Code:"+STR$(DevCode)+" Key Code:"+STR$(KeyCode),LT,1,1,foreCol%,backCol% ' IReturn
Tony
Footnote added 2024-06-06 17:00 by morgs67 Update for transmit code:
Footnote added 2024-03-24 07:25 by phil99 In the event that the program using the NECsend Sub is also using the Timer odd things may hapen when the Sub resets it. These mods fix that. T = Timer + 4.3 'Replaces Timer = 0 For ... Next Do : Loop Until T < Timer 'Replaces Do : Loop Until Timer > 4.3
Also add this line. Local Float T |