'Data Stamp.bsp ' {$STAMP BS2p} ' {$PBASIC 2.5} ' {$PORT COM9} 'Version Info '======================================================== 'Data Stamp program, designed for BASIC stamp BS2P40 version 1.5 'Last Updated Tuesday, May 27, 2008 'version 0 Version CON 0 'Compiler directives '======================================================== #DEFINE DebugMode = 0 'Notes '======================================================== 'this program is the main code for the Data stamp. 'The Data stamp is responsible for reading in a 24-bit 'measurement stored on electronics within the Data envelope, and 'transmitting the data back to the master stamp when requested 'The stamp reads from the 24-bit data output by the following pins ' BASIC stamp pin 8254 pin ' 8 D0 ' 9 D1 ' 10 D2 ' 11 D3 ' 12 D4 ' 13 D5 ' 14 D6 ' 15 D7 ' X0 D8 ' X1 D9 ' X2 D10 ' X3 D11 ' X4 D12 ' X5 D13 ' X6 D14 ' X7 D15 ' X8 D16 ' X9 D17 ' X10 D18 ' X11 D19 ' X12 D20 ' X13 D21 ' X14 D22 ' X15 D23 'the stamp communicates with the Master stamp through the 'stamp bus ' BASIC stamp pin Function ' 0 Serial Data ' 1 Serial Flow (RTS) ' 2 Channel Advance (in) ' 3 Attention (in) ' 4 Attention Acknowledge (out) 'Packet format: ' ' Byte Content ' 0 Packet size ' 1 Command ' 2 Data ' ... 'The stamp uses the following pins to enable/disable the Data envelope ' BASIC stamp pin Function ' 5 Advance Enable (level) 'PINS '======================================================== 'pins for STAMP bus (Main port only) BusData PIN 0 BusFlow PIN 1 ChAdv PIN 2 Attention PIN 3 AttAck PIN 4 'pins for Data envelope (Main port only) AdvEnable PIN 5 'Variables and Registers '======================================================== 'unique ID assigned to Data Stamp from computer during initialization ID VAR Byte 'Latch variables, used to detect transitions of inputs AttLatch VAR Bit ChLatch VAR Bit 'variables used for data Packets VAR Byte(5) 'the measurement packet, a premade packet format which has its form mostly constructed MeasPacket VAR Byte(5) 'MeasData structure: 'Byte Function '0 Packet Size Byte (always set to 5) '1 Data Stamp Identifier Byte( = 0x80 + Data Stamp ID ) '2,3,4 24-bit measurement data, stored in little endian format (LSB first, MSB last) 'constants used for serial communication T38K4 CON 45 Inverted CON $4000 Baud CON T38K4 + Inverted 'indexers and dummy variables i VAR Byte 'Main Code '======================================================== Startup: #IF DebugMode >= 1 #THEN DEBUG "Data Stamp Initializing", CR, CR #ENDIF GOSUB Init 'pause to wait for other stamps to finish their startup PAUSE 100 Main: 'main loop waiting for commands #IF DebugMode >= 1 #THEN DEBUG "At main loop", CR #ENDIF DO 'reset attention latch IF Attention = 0 THEN AttLatch = 1 LOW AttAck 'drop the Acknowledge line so the master stamp can begin handshaking ' with this stamp ENDIF 'reset the channel advance latch IF ChAdv & AdvEnable THEN ChLatch = 1 'if a rising edge on attention is detected IF (Attention & AttLatch) THEN GOTO GetCommand 'if a falling edge on channel advance is detected and the envelope is enabled IF (ChAdv = 0 ) & ChLatch & AdvEnable THEN GOSUB ReadEnvelope LOOP 'TODO: check this and make sure it complies with the latest system 'Serial In Code '======================================================== 'this executes serial communication on the STAMP Bus GetCommand: AttLatch = 0 HIGH AttAck #IF DebugMode >= 1 #THEN DEBUG "Serial Communication with master stamp commencing", CR #ENDIF 'first, receive the packet size SERIN BusData\BusFlow, Baud, [Packets(0)] DIR1 = 0 DIR0 = 0 'then receive the packet depending on the packet size Packets(0) = Packets(0) - 1 #IF DebugMode >= 1 #THEN DEBUG "Receiving ", DEC Packets(0), " bytes", CR #ENDIF 'if packet data length is 0 IF Packets(0) = 0 THEN 'that means the Master Stamp is requesting the measurement data #IF DebugMode >= 1 #THEN DEBUG "Data Request received", CR #ENDIF GOSUB SendMeasData ELSE 'otherwise, continue with packet receipt #IF DebugMode >= 2 #THEN DEBUG "Data: " #ENDIF FOR i = 1 TO Packets(0) SERIN BusData\BusFlow, Baud, [Packets(i)] #IF DebugMode >= 1 #THEN DEBUG HEX2 Packets(i) #ENDIF NEXT #IF DebugMode >= 2 #THEN DEBUG CR #ENDIF ENDIF DIR1 = 0 DIR0 = 0 'here, parse the packet that was sent 'enable/disable command SELECT Packets(1) CASE $00 'disable #IF DebugMode >= 1 #THEN DEBUG "Disabling measurement status", CR #ENDIF LOW AdvEnable CASE $01 'enable #IF DebugMode >= 1 #THEN DEBUG "Enabling measurement status", CR #ENDIF HIGH AdvEnable CASE $02 ID = Packets(2) MeasPacket(1) = $40 + ID #IF DebugMode >= 1 #THEN DEBUG "Setting identification number", CR #ENDIF CASE $80 #IF DebugMode >= 1 #THEN DEBUG "Sending STAMP Identification", CR #ENDIF Packets(0) = 3 Packets(1) = $02 '$02 - Data Stamp identification code Packets(2) = Version 'version of clock stamp code currently installed 'respond with version report packet GOSUB SendPacket CASE $81 #IF DebugMode >= 1 #THEN DEBUG "Sending Status", CR #ENDIF Packets(0) = 2 Packets(1) = AdvEnable GOSUB SendPacket ENDSELECT GOTO Main: 'Subroutines '======================================================== 'This initializes the input pins so the stamp can read from the MCS envelope Init: AUXIO DIRS = $0000 'set data bus to input mode MAINIO DIRH = $00 DIR1 = 0 'default state is disabled LOW AdvEnable 'these values are constants MeasPacket(0) = 5 MeasPacket(1) = $40 'appended to this is the Data Stamp ID# which is sent during ' system init RETURN ReadEnvelope: ChLatch = 0 #IF DebugMode >= 1 #THEN DEBUG "ChAdv detected, reading envelope...", CR #ENDIF AUXIO MeasPacket(3) = INL 'NMSB MeasPacket(4) = INH 'MSB MAINIO MeasPacket(2) = INH 'LSB RETURN 'this code is used to send data on the Stamp Bus to the Master Stamp SendPacket: LOW AttAck DO : LOOP WHILE Attention = 0 HIGH AttAck #IF DebugMode >= 1 #THEN DEBUG "Transmitting data..." #ENDIF SEROUT BusData\BusFlow, Baud, [ STR Packets\Packets(0) ] #IF DebugMode >= 1 #THEN DEBUG "Done!", CR #ENDIF DIR1 = 0 DIR0 = 0 'wait for the master stamp to acknowledge receipt by setting attention line low DO : LOOP WHILE Attention RETURN 'this specialized send packet code is used to send the measurement data to the Master Stamp SendMeasData: LOW AttAck DO : LOOP WHILE Attention = 0 HIGH AttAck #IF DebugMode >= 1 #THEN DEBUG "Transmitting measurement data..." #ENDIF SEROUT BusData\BusFlow, Baud, [ STR MeasPacket\MeasPacket(0) ] #IF DebugMode >= 1 #THEN DEBUG "Done!", CR #ENDIF DIR1 = 0 DIR0 = 0 'wait for the master stamp to acknowledge receipt by setting attention line low DO : LOOP WHILE Attention RETURN