'{$STAMP BS2p} ' {$PBASIC 2.5} '*********************************************************** '** ** '** Serial Routines for the Basic Stamp BS2p ** '** Controlling RLY08 ** '** ** '** Copyright 2006 - Devantech Ltd ** '** Commercial use of this software is prohibited ** '** Private and educational use only is permitted ** '** ** '** Written by Chris Clarke Dec 2006 ** '** ** '*********************************************************** Key VAR Byte addr VAR Byte ver VAR Byte states VAR Byte command VAR Byte new_address VAR Byte Tx CON 0 ' To Rx Rx CON 1 ' To Tx BAUD CON 240 ' BS2sx & BS2p - 9600 - see parallax documentation GET_VER CON 90 GET_STATES CON 91 SET_STATES CON 92 ALL_ON CON 100 RELAY1_ON CON 101 RELAY2_ON CON 102 RELAY3_ON CON 103 RELAY4_ON CON 104 RELAY5_ON CON 105 RELAY6_ON CON 106 RELAY7_ON CON 107 RELAY8_ON CON 108 ALL_OFF CON 110 RELAY1_OFF CON 111 RELAY2_OFF CON 112 RELAY3_OFF CON 113 RELAY4_OFF CON 114 RELAY5_OFF CON 115 RELAY6_OFF CON 116 RELAY7_OFF CON 117 RELAY8_OFF CON 118 Main: ver = 0 scan: 'scan searches all the possible addresses of the RLY08 and looks for a response to the GETVER command DO WHILE (ver = 0) DEBUG CRSRXY,0, 0, "RLY08 addr = ", DEC addr DEBUG CRSRXY,0, 1,"Version = ", DEC ver IF(ver = 0) THEN addr = addr+1 IF(addr > 9) THEN addr = 1 SEROUT Tx, BAUD, [addr,GET_VER] SERIN Rx, BAUD, 100, scan, [ver] LOOP ' Loop mainLoop: 'scan routine has found a RLY08 so we arrive in mainloop SEROUT Tx, BAUD, [addr,GET_VER] 'ask RLY08 for its version SERIN Rx, BAUD, 100, scan, [ver] 'and retrieve response, no response will return us to the scan routine SEROUT Tx, BAUD, [addr,GET_STATES] 'ask RLY08 for its states SERIN Rx, BAUD, 100, scan, [states] 'and retrieve response, no response will return us to the scan routine command = 101 'first command that turns relay1 on DO WHILE(command < 109) 'loop runs through all the individual relays turning one on at a time SEROUT Tx, BAUD, [addr,command] SEROUT Tx, BAUD, [addr,GET_STATES] 'ask RLY08 for its states SERIN Rx, BAUD, 100, scan, [states] 'and retrieve response, no response will return us to the scan routine DEBUG CRSRXY,0, 2,"command = ", DEC command," " DEBUG CRSRXY,0, 3,"Relay States = ", DEC states," " command = command + 1 PAUSE 50 LOOP PAUSE 500 command = 111 'first command that turns relay1 off DO WHILE(command < 119) 'loop runs through all the individual relays turning one on at a time SEROUT Tx, BAUD, [addr,command] SEROUT Tx, BAUD, [addr,GET_STATES] 'ask RLY08 for its states SERIN Rx, BAUD, 100, scan, [states] 'and retrieve response, no response will return us to the scan routine DEBUG CRSRXY,0, 2,"command = ", DEC command," " DEBUG CRSRXY,0, 3,"Relay States = ", DEC states," " command = command + 1 PAUSE 50 LOOP PAUSE 500 GOTO MainLoop