ArmMiteF4 BITBANG BITSTREAM Operation


Author Message
morgs67
Regular Member

Joined: 10/07/2019
Location: Australia
Posts: 77
Posted: 06:59am 23 Mar 2024      

Here is code that incorporates bit reversing as NEC protocol is LSB bit first.
This means that the ARMmiteF4 device and key codes match that of the Micromite.
'22/3/2024 - new NECSend routine to replace original due problem with 4.5mS space generation
 ' modified to use same dev code and key code as 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 "; devin, keyin    
   ConvInput ' use function revb to reverse  bits in a word as NEC IR code sent LSB first
   NECsend IRpin, DevCode, KeyCode
   
   pause 1000
 Loop
 
 
Sub NECsend IRpin As integer, dev As integer, key As integer
 Local integer word, d(1389), m, n = 1

 word = (DevCode<<16) + (KeyCode<<8) + 255-KeyCode 'assemble 32 bits
 
 Math set 14 , d() '13=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 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
 BitBang bitstream IRpin, 1387, d() 'send the data
 
 Pin(IRpin) = 0
End Sub
 '-----------------------------
sub ConvInput  ' use function revb to reverse  bits in a word as NEC IR code sent LSB first
 'Check if device code is 8 bit or 16 bit
 If devin  < 256 Then ' 8 bit address, so add inverse of address
   DevCode=(revb(devin,8)<<8) + 255-(revb(devin,8))'add inverse to form 16 bits
     Else ' extended or 16 bit address
   DevCode=revb(devin,16)
 end if
 KeyCode=revb(keyin,8)
 print
 print "Input- device ";devin;"   "; bin$(devin);"   keyin ";keyin;"   ";Bin$(keyin)
 print "Output- DevCode "; DevCode;"  ";BIN$(DevCode,16);"  ";"   KeyCode ";KeyCode;"   ";Bin$(KeyCode,8)
 Print
 
end sub
 
 ' Function to reverse  bits in byte as NEC IR code sent LSB first
Function revb(x As integer, y As integer) As integer
 Local integer z
 revb=0
 For z=1 To y
   'select bit starting from RHS to LHS
   If x And (1<<(z-1)) Then
     revb=revb Or (1<<(y-z))
   EndIf
 Next z
End Function


Also to read IR codes use:
'F4-NECRx-bit-reverse
 ' modified to use same dev code and key code as the micromite
 
 
 ' IR receiver input, fixed as PE2 for F4
 '
 IR dev, key , irint 'Rx.int 'start the decoder
 Print "Waiting for IR input"
 
 
 Do
   'Your program here.
   
   pause 1000
 Loop
 
 
 'process detected IR input
irint:
 If (dev And &HFF) = ((dev>>8) Xor &HFF) Then ' normal address
   Print "device address 8 bit "
   DevCode=revb((dev>>8),8)
   KeyCode=revb(key,8)
 Else ' extended address
   Print "device address 16 bit "
   DevCode=revb(dev,16)
   KeyCode=revb(key,8)
 EndIf
 'print "device ";dev;"   "; bin$(dev,16);"   command ";key;"   ";Bin$(key,8)
 print "DevCode ";DevCode,"   "; bin$(DevCode,8);"  KeyCode ";KeyCode;"   ";Bin$(KeyCode,8)
 print
 IReturn
 
 ' Function to reverse  bits in byte as NEC IR code sent LSB first
Function revb(x As integer, y As integer) As integer
 Local integer z
 revb=0
 For z=1 To y
   'select bit starting from RHS to LHS
   If x And (1<<(z-1)) Then
     revb=revb Or (1<<(y-z))
   EndIf
 Next z
End Function
 


Tony