PicoMite IR Decoding / Sending


Author Message
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5994
Posted: 08:09am 08 Nov 2022      

Abstracted the IR transmite, preparation for IR receive.

'bitbang_IR.bas test for bitbang bitstream

option default none
option explicit

const carrier% = int(1e6/38e3)                '38kHz period time in us
const phase% = int(carrier%/2)                'half the carrier period time
const bits% = 6                               'number of bits to send


'0.5ms 38kHz carrier is 38e3*5e-4 = 19 cycles = 38 half cycles (phases)
'generate an array that contains the 38 time delays
dim i%,keycode%
dim a$
dim puls%(37):for i%=0 to 37:puls%(i%)=phase%:next i%


'setup ---------------------------------------------------------------------------------------

'Init GP0 as the transmit pin, and force low (IR LED off)
setpin gp0,dout : pin(gp0)=0

'Init GP1 for interrupt low (IR receiver modules are default high, low when IR detected)
setpin gp1,intl,IR_in


'main ----------------------------------------------------------------------------------------

'repeatedly send the code in "keycode%" least significant bit first
do

 'send keyboard presses through IR
 a$=inkey$
 if a$<>"" then                              'when a key is pressed, send it
   keycode%=ASC(a$) and 63                   'convert ASCII to 0...63
   send_IR
 end if
 
loop until a$=chr$(27)                        'stop when ESC pressed
end


'Interrupt ------------------------------------------------------------------------------------
sub IR_in
 'do nothing yet
end sub


'subroutines ----------------------------------------------------------------------------------

'send keycode% using carrier bursts coded in puls%()
sub send_IR

 local i%
 
 for i%=0 to bits%-1                         'cycle through the 6 bits
   bitbang bitstream gp0,38,puls%()          'leading pulse
   pause 1.2                                 '2ms - 0.5ms carrier - 0.3ms MMBasic loop delay
   if (keycode% and 2^i%) then pause 2       'when 1 bit then wait 2ms more until next pulse
 next i%
 
 bitbang bitstream gp0,38,puls%()            'terminating pulse
 pause 50                                    'interword gap (pause until next keycode sent
 
end sub