Parallax, Inc. • IR Buddy Demo Kit (#28016) • 11/2002
Page 11
Busy CON 0 ' IRB is transmitting
ErrorLevel CON 5 ' max errors before alarm
CrsrXY CON 2 ' DEBUG position command
ClrEOL CON 11 ' Clear DEBUG line to right
' -----[ Variables ]------------------------------------------------------------
randVal VAR Word ' pseudo-random value
sysOut VAR Byte ' system code for RC-5 TX
cmdOut VAR Byte ' command code for RC-5 TX
sysIn VAR Byte ' system code for RC-5 RX
cmdIn VAR Byte ' command code for RC-5 RX
errors VAR Nib ' error count
' -----[ Initialization ]-------------------------------------------------------
Setup:
GOSUB IR_Buddy_Reset ' reset TX and RX side
' -----[ Program Code ]---------------------------------------------------------
Main:
RANDOM randVal ' create pseudo-random value
sysOut = randVal.HighByte & %00011111 ' extract system value
cmdOut = randVal.LowByte & %00111111 ' extract command value
TX_Code:
SEROUT IRbTX, IRbBaud, [IRbRc5Tx, 0, IRbMod, sysOut, cmdOut]
PAUSE 5 ' let IRB grab SIO line
TX_Wait:
IF (Ins.LowBit(IRbTX) = Busy) THEN TX_Wait ' wait for TX to end
RX_Code:
SEROUT IRbRX, IRbBaud, [IRbRc5Rx, 10] ' get codes from other side
SERIN IRbRX, IRbBaud, [STR sysIn\2\254] ' expecting just two bytes
Display:
DEBUG Home ' display status
DEBUG "Out", TAB, DEC3 sysOut, TAB, DEC3 cmdOut, CR
DEBUG "In", TAB, DEC3 sysIn, TAB, DEC3 cmdIn, CR, CR
DEBUG "Errors: ", DEC errors, ClrEOL
Check_Codes:
IF (sysIn <> sysOut) THEN Codes_Bad ' check system code
IF (cmdIn <> cmdOut) THEN Codes_Bad ' check command code
Codes_Okay:
errors = 0 ' clear errors
LOW AlarmLED ' alarm off
GOTO Main
Codes_Bad:
errors = 1 ' update error count
IF (errors < ErrorLevel) THEN Main ' continue if error count okay