| Posted: 09:20am 07 Nov 2022 |
|
|
|
Some example code based on TassyJim's bitbang...
38kHz carrier frequency is 26us period time. The high time is 13us, the low time is 13us.
The set of pulses is 0.5ms of 38kHz pulses, this is 19 pulses, resulting in 38 high times and 38 low times.
This is exploited in below (blocking) code written around BITBANG BITSTREAM and PAUSE statements.
'bitbang_IR.bas test for bitbang bitstream
option default none option explicit on
const carrier% = int(1e6/38e3) '38kHz 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%,data%,keycode% dim puls%(37):for i%=0 to 37:puls%(i%)=phase%:next i%
'Init GP0 as the transmit pin setpin gp0,dout
'here we define the data to be sent keycode% = 42 'just any code between 0 and 63 (6 bit codes)
'repeatedly send the code in "keycode%" least significant bit first do 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 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
loop end
an IR driving circuit can be as this one:

EDIT: documentation is a bit vague on "0" and "1" bit times. I thinki I got it correct in above code now. Edited 2022-11-07 20:23 by Volhout |