Shop OBEX P1 Docs P2 Docs Learn Events
reading measurments off hitachi hm55b compass module and writing to usb data lo — Parallax Forums

reading measurments off hitachi hm55b compass module and writing to usb data lo

ilyailya Posts: 13
edited 2008-03-09 03:34 in BASIC Stamp
How can i change the code below to read an angle measurement from a Hitachi hm55b compass module? The compass module is connected to a BS2 Basic stamp and the angle measurement is "outputed" from one of the I/O pins. Thanks.

' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================


'
[noparse][[/noparse] Program Description ]

' Before running this program be sure your Memory Stick Datalogger is
' connected as per the Quick Start Circuit in the documentation and that
' a formatted USB Thumb Drive is connected to the USB connector.
'
' This program tests the Memory Stick Datalogger (#27937) by writing data
' to a connected USB Thumb Drive. First a file called, 'SEEDFILE.TXT' is
' created to hold the seed value for generating sets of 5 random numbers.
' The random numbers are stored in a file called, 'DATAFILE.TXT' which is
' stored in a sub-folder called, 'DATADIR' on the drive. Once data has
' been stored on the drive it can be connected to a PC and read. Data
' could be formatted during writes in a format that allows it to be loaded
' into a spreadsheet and charted or graphed.
'
' NOTE: The drive is put into Standby (Sleep) Mode for ~10 seconds between
' writes. This is a good time to turn off the power and disconnect the
' drive from the Datalogger. You should not disconnect the drive while
' the Datalogger is writing to it or the data and/or file/folder structure
' could be corrupted. For troubleshooting see the documentation.


'
[noparse][[/noparse] I/O Definitions ]

TX PIN 8 ' Transmit Data --> 27937.4 (RXD)
RTS PIN 9 ' Request To Send --> 27937.6 (CTS)
RX PIN 10 ' Receive Data <-- 27937.5 (TXD)
CTS PIN 11 ' Clear To Send <-- 27937.2 (RTS)


'
[noparse][[/noparse] Constants ]

Baud CON 84 ' Serial Baud Rate 9600 bps (BS2)


'
[noparse][[/noparse] Variables ]

timeout VAR Bit ' Timeout Inidicator
buffer VAR Byte(15) ' Input Buffer
index VAR Byte ' Index Variable
ioByte VAR Byte ' Input/Output Storage
temp VAR Byte ' Temp Variable
counter VAR Word ' Counter Variable
result VAR Word ' Random Number Storage


'
[noparse][[/noparse] Initialization ]

DEBUG CLS, "Memory Stick Datalogger Test V1.0", CR, CR
PAUSE 2000 ' Allow Time To Settle

HIGH TX ' Initialize Transmit Line
LOW RTS ' Take Vinculum Out Of Reset

PAUSE 2000 ' Allow Time To Settle

GOSUB Purge ' Purge Buffer
DEBUG "Synchronizing...", CR
index = 0

' For Synchronization send E until echoed back
DO WHILE (index < 1)
PAUSE 250
SEROUT TX\CTS, Baud, [noparse][[/noparse]"E", CR] ' Transmit "E CR"
GOSUB Get_Serial_Bytes ' Get Returned Data
LOOP
GOSUB Purge ' Purge Buffer

' Send e to complete synchronization
PAUSE 500
SEROUT TX\CTS, Baud, [noparse][[/noparse]"e", CR] ' Transmit "e CR"
GOSUB Get_Serial_Bytes

' Send CR to see if drive is present
PAUSE 500
GOSUB Purge ' Purge Buffer
DEBUG "Checking for USB Flash drive...", CR
SEROUT TX\CTS, Baud, [noparse][[/noparse]CR] ' Send Carriage Return
GOSUB Get_Serial_Bytes ' Wait for D:\>

IF (buffer(0) = "N") THEN
DEBUG "No drive present!"
STOP
ELSE
DEBUG "Drive D: Ready...", CR
ENDIF
GOSUB Purge ' Purge Buffer


'
[noparse][[/noparse] Program Code ]

Main:
result = 11000 ' Initial "seed" Value For Random
RANDOM result ' Generate Random Number
DEBUG "Random Number (Seed) = ", DEC result, CR

' Delete SEEDFILE.TXT if one exists, ignore error if no file exists
PAUSE 500
SEROUT TX\CTS, Baud, [noparse][[/noparse]"DLF seedfile.txt", CR]
GOSUB Get_Serial_Bytes
GOSUB Purge ' Purge Buffer

' Open SEEDFILE.TXT for output (write)
PAUSE 250
SEROUT TX\CTS, Baud, [noparse][[/noparse]"OPW seedfile.txt", CR]
GOSUB Get_Serial_Bytes

' Write random seed value to SEEDFILE.TXT
PAUSE 250
SEROUT TX\CTS, Baud, [noparse][[/noparse]"WRF ", $00, $00, $00, $05, CR, DEC5 result, CR]
GOSUB Get_Serial_Bytes

' Close SEEDFILE.TXT
PAUSE 250
SEROUT TX\CTS, Baud, [noparse][[/noparse]"CLF seedfile.txt", CR]
GOSUB Get_Serial_Bytes
GOSUB Purge ' Purge Buffer

' Create folder DATADIR and ignore error if folder exists
PAUSE 200
SEROUT TX\CTS, Baud, [noparse][[/noparse]"MKD datadir", CR]
GOSUB Get_Serial_Bytes
GOSUB Purge ' Purge Buffer

counter = 0 ' Loop Counter

DO
' Open SEEDFILE.TXT for input (read)
PAUSE 200
SEROUT TX\CTS, Baud, [noparse][[/noparse]"OPR seedfile.txt", CR]
GOSUB Get_Serial_Bytes

' Read random seed into variable
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"RDF ", $00, $00, $00, $05, CR]
SERIN RX\RTS, Baud, 500, No_Data2, [noparse][[/noparse]DEC5 result]
DEBUG "Seed:", DEC result, CR ' Display stored seed value

' Close SEEDFILE.TXT
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"CLF seedfile.txt", CR]
GOSUB Get_Serial_Bytes

' Change to DATA folder
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"CD datadir", CR]
GOSUB Get_Serial_Bytes

' Open DATAFILE.TXT for output (write)
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"OPW datafile.txt", CR]
GOSUB Get_Serial_Bytes

counter = counter + 1 ' Increment Counter

' Write loop counter value and data separator to DATAFILE.TXT
PAUSE 120
DEBUG "Writing data separator now... ", CR
SEROUT TX\CTS, Baud, [noparse][[/noparse]"WRF ", $00, $00, $00, $09, CR,
DEC5 counter, "**", CR, LF, CR]
GOSUB Get_Serial_Bytes

' Write 5 random numbers to DATAFILE.TXT
PAUSE 120
DEBUG "Writing 5 random numbers now... ", CR
FOR temp = 1 TO 5
RANDOM result
DEBUG "Random Number = ", DEC result, CR
SEROUT TX\CTS, Baud, [noparse][[/noparse]"WRF ", $00, $00, $00, $07, CR,
DEC5 result, CR, LF, CR]
GOSUB Get_Serial_Bytes
PAUSE 120
NEXT

' Close DATAFILE.TXT
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"CLF datafile.txt", CR]
GOSUB Get_Serial_Bytes

' Change to root folder
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"CD ..", CR]
GOSUB Get_Serial_Bytes

' Delete SEEDFILE.TXT
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"DLF seedfile.txt", CR]
GOSUB Get_Serial_Bytes

' Open SEEDFILE.TXT for output (write)
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"OPW seedfile.txt", CR]
GOSUB Get_Serial_Bytes

' Write new random seed value
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"WRF ", $00, $00, $00, $05, CR, DEC5 result, CR]
GOSUB Get_Serial_Bytes

' Close SEEDFILE.TXT
PAUSE 120
SEROUT TX\CTS, Baud, [noparse][[/noparse]"CLF seedfile.txt", CR]
GOSUB Get_Serial_Bytes

' USB Suspend Mode (Power Saving Mode)
PAUSE 120
DEBUG "Putting drive to sleep...", CR
SEROUT TX\CTS, Baud, [noparse][[/noparse]"SUD", CR]
GOSUB Get_Serial_Bytes

PAUSE 10000

' Wake Drive (Full Power)
PAUSE 120
DEBUG "Waking drive now", CR
SEROUT TX\CTS, Baud, [noparse][[/noparse]"WKD", CR]
GOSUB Get_Serial_Bytes
LOOP


'
[noparse][[/noparse] Subroutines ]

Get_Serial_Bytes:
timeout = 1 ' Set Timeout Indicator Flag
index = 0 ' Initialize Index
DO WHILE (timeout > 0) ' While Timeout Has Not Occurred
ioByte = 0 ' Clear Temporary Storage
SERIN RX\RTS, Baud, 100, No_Data, [noparse][[/noparse]ioByte]
buffer(index) = ioByte ' Save Byte Received To Array
index = index + 1 ' Increment Index
IF (index > 14) THEN ' Check For Overflow
index = 14 ' Prevent Overflow
ENDIF
LOOP
RETURN

Purge:
timeout = 1 ' Set Timeout Indicator Flag
DO WHILE (timeout > 0) ' While Timeout Has Not Occurred
PAUSE 50
SERIN RX\RTS, Baud, 500, No_Data, [noparse][[/noparse]ioByte]
LOOP
RETURN

No_Data:
timeout = 0 ' Timeout, Clear Flag
RETURN

No_Data2:
DEBUG CR, "Error reading the seed file -- Halting execution!", CR
PAUSE 1000
GOSUB Purge ' Purge Buffer
STOP
RETURN

Comments

  • FranklinFranklin Posts: 4,747
    edited 2008-03-09 03:34
    You need to read a value from the compas into a variable on the stamp and then write it to the logger. the code for these two things should be on the Parallax site under the page in the store for the device. You need to pare the code posted down to just OPEN, WRITE and CLOSE. The main method is to simplify and do one step at a time. Write the code to read the compass and display the result in debug. then write code to read a number you type (with debugin) and store it to the logger. You will then have the two parts to accomplish your task.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.