Parallax, Inc. • IR Buddy Demo Kit (#28016) • 11/2002
Page 17
GOSUB Show_TX_Packet
lastCmd = cmd ' save for RX check
TX_Packet:
GOSUB IR_Buddy_Reset
HIGH TxLED
SEROUT IRbSIO, IRbBaud, [IRbDataTx, IRbMod, STR buffer\8]
PAUSE 5 ' let IRB grab SIO line
TX_Wait:
IF (Ins.LowBit(IRbSIO) = Busy) THEN TX_Wait
LOW TxLED
RX_Packet:
GOSUB IR_Buddy_Reset
SEROUT IRbSIO, IRbBaud, [IRbDataRx] ' prep for 8-byte RX
SERIN IRbSIO, IRbBaud, 1000, TO_Error, [STR buffer\8]
GOSUB Show_RX_Packet ' display received bytes
DEBUG CrsrXY, 8, 6 ' prep for status report
Check_RX_Packet:
IF (header <> ACK) THEN NAK_Error ' check packet bytes
IF (cmd <> lastCmd) THEN Packet_Error
rxChkSum = chkSum ' save rx checksum
GOSUB Make_CheckSum ' calc checksum of rx packet
IF (rxChkSum <> chkSum) THEN Packet_Error ' compare checksum values
Good_Packet:
DEBUG "Good Packet", ClrEOL
counter = (c 1) & $0F ' update counter
PAUSE 500
GOTO Build_Packet ' build & send new packet
NAK_Error:
DEBUG "Slave returned NAK", ClrEOL
GOTO Build_Packet ' rebuild & resend
Packet_Error:
DEBUG "Packet error", ClrEOL
GOTO Build_Packet
TO_Error:
DEBUG CrsrXY, 8, 6
DEBUG "Timeout error", ClrEOL
PAUSE 250 ' give slave time to reset
GOTO Build_Packet
END
' -----[ Subroutines ]----------------------------------------------------------
' Reset the IR Buddy. This code is useful for clearing data from the RX
' buffer and prepping to switch modes. Timing specific; do not change.
IR_Buddy_Reset:
LOW IRbSIO ' signal reset
PAUSE 5
INPUT IRbSIO ' release reset signal
PAUSE 50 ' allow time for reset actions