SP RAM help
Alonzo
Posts: 3
I have the following data in SP RAM.
·
Data········· Slot
1····························· 0
2····························· 1
3····························· 2
4····························· 3
·
·
How do I retrieve the data and put the data into a single variable as a decimal number of 1234?
·
Data········· Slot
1····························· 0
2····························· 1
3····························· 2
4····························· 3
·
·
How do I retrieve the data and put the data into a single variable as a decimal number of 1234?
Comments
single_variable = (DataByte0 * 1000) + (DataByte1 * 100) + (DataByte2 * 10) + DataByte3
Make sure single_variable is a word!· Also, if you wanted it as a 4-byte string with the ASCII values (I'm not quite sure what you want), just add $30 to each of the bytes (that's the ASCII '0' character offset).
Otherwise, the solution is quite good as posted.
'extract the data as variables, see the code and question below:
' {$STAMP BS2p}
' {$PBASIC 2.5}
'
'**********************************************************************************************************************
'Constants
SD_Data_OUT······ CON··· 0················ 'Serial data TO SD Module
SD_Data_IN······· CON··· 1················ 'Serial data FROM SD Module
Right_OE········· CON··· 5················ 'Right motor enable, HIGH turns motor OFF, LOW enables motor
Left_OE·········· CON··· 6················ 'Left motor enable, HIGH turns motor OFF, LOW enables motor
SD_Fpin·········· CON··· 7················ 'Fpin for MMC/SD Module
LCD·············· CON··· 8················ 'Serial LCD Data
DAC_LOAD········· CON··· 9················ 'CS
DAC_DATA········· CON··· 11··············· 'Digital to Analog Data line, Data is shifted in on the falling edge
DAC_CLK·········· CON··· 10··············· 'Digital to Analog Clock line, Data is shifted in on the falling edge
MSB_FIRST········ CON··· 1················ 'MSB First
LSB_FIRST········ CON··· 0················ 'LSB First
ADC_CS··········· CON··· 12··············· 'Analog to Digital Chip Select line, A LOW enables the ADC
ADC_Din·········· CON··· 13··············· 'Analog to Digital Data Into AD Convertor, load channel configuration data into the ADC
ADC_Dout········· CON··· 14··············· 'Analog to Digital Data Out of AD Convertor, data changes on falling edge of clock
ADC_CLK·········· CON··· 15··············· 'Analog to Digital Clock line
N9600············ CON··· $40F0············ 'Use with BS2-SX for BPI-216 2x16 LCD display
I················ CON··· 254·············· 'Instruction prefix value for BPI-216 2x16 LCD display
CLR·············· CON··· 1················ 'Clear Screen command for the LCD
Right_Motor······ CON··· 15
Left_Motor······· CON··· 7
ADC_Right_Press·· CON··· 24··············· 'CH0 of ADC
ADC_Right_Flow··· CON··· 25··············· 'CH1 of ADC
ADC_Left_Press··· CON··· 26··············· 'CH2 of ADC
ADC_Left_Flow···· CON··· 27··············· 'CH3 of ADC
BaudMode········· CON··· 240·············· '8 bit no parity true for BS2-sx and BS2-p
'**********************************************************************************************************************
'Variables
Left_Flow········ VAR······ Word
Right_Flow······· VAR······ Word
Rx_Pressure······ VAR······ Word
Left_Pressure···· VAR······ Word
Right_Pressure··· VAR······ Word
Status··········· VAR······ Nib
Burdened_Delta··· VAR······ Nib
Bilevel_Mode····· VAR······ Bit··············· '0 = OFF, 1 = ON
x················ VAR······ Word
y················ VAR······ Byte
·'FC··········· CON···· $89
PAUSE 500
Rx_Pressure = 1311
Burdened_Delta = 6
Bilevel_Mode = 1
'FW = File Write, Open the SD Card AND WRITE a file name TO it
DEBUG "....................",CR
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FW ","PatData.txt",CR]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "File write: ",STR x \2, CR
PAUSE 200
'PL = Put Line, put a line of data on the SD card
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"PL ","1234","5","6","78901","23456","78901","\r",CR]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "Line wrote: ",STR x \2, CR
PAUSE 500
'FC = File Closed, close the file
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FC",CR ]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "File closed:",STR x \2, CR
PAUSE 200
'FR = File Read, read the PatData.txt file
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FR ","PatData.txt",CR ]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "Read Status:",STR x \2, CR
PAUSE 200
'GL = Get Line, get the line of data that was written
'on the SD card above
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"GL",CR]
SERIN SD_Data_IN,240,10000,No_Data,[noparse][[/noparse]SPSTR 21]
PAUSE 200
'display the data on the debug screen
FOR y = 0 TO 20
GET y , Rx_Pressure
DEBUG "Data: ",Rx_Pressure,CR
NEXT
'Question:· At this point of the code, how do I put the data into the SP RAM and then
'extract the data as variables.· The variable sizes are
'separated by quotes as indicated in the PL command above?
'x = (DataByte0 * 1000) + (DataByte1 * 100) + (DataByte2 * 10) + DataByte3
'FS = File Size, indicate the size of the PatData.txt file
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FS ","PatData.txt",CR ]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "File size:· ",STR x \2, CR
PAUSE 200
GOTO Close_File
No_Data:
DEBUG "****************",CR
DEBUG "Time-out Error",CR
DEBUG "****************",CR
PAUSE 200
'FC = File Closed, close the file
Close_File:
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FC",CR]
SERIN SD_Data_IN,240,2000,Not_Closed,[noparse][[/noparse]STR x \2]
DEBUG "File closed:",STR x \2, CR
GOTO Completed
Not_Closed:
DEBUG "File may not have closed"
Completed:
DEBUG "....................",CR,CR
END
Many thanks!
LOW Enable··································· ' activate the reader
Alonzo, here is a snippet of code from my RFID program:
SERIN RX, baud1, [noparse][[/noparse]WAIT($0A),· SPSTR 10]·······HIGH enable
GOTO Display_Tag
Display_Tag:
· GET 0, a
· GET 1, b
· GET 2, c
· GET 3, d
· GET 4, e
· GET 5, f
· GET 6, g
· GET 7, h
· GET 8, i
· GET 9, j
· DEBUG "Tag number is : ",· a, b, c, d, e, f, g, h, i, j, "· ",·· asc ? Device, cr, cr
The address read with GET and the data stored in a, b, c, etc.
I'm not sure but you could probably use the WORD modifier if you wanted to store something larger than a byte.· If you do use WORD, remember that WORD will take two bytes of storage space.
The SPRAM has 164 bytes of storage.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Do you have a Stamp Tester yet?
http://hometown.aol.com/newzed/index.html
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
·· Actually there are 128 bytes available.·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Alonzo,
Alonzo, it sounds like you have an ambitions SD card project! To read that data out of SPRAM with the widths you stated, it would be something like this, calling the subroutine DecodeSP that follows:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
·· Right you are...I guess I think of it (127) as useful since I frequently check it in different code modules.·· Then again, I can't remember really using more than 64 bytes of it, tops.· As for the OP, I believe he's only trying to read the first 4 bytes into a Word variable.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
' {$STAMP BS2p}
' {$PBASIC 2.5}
'
'**********************************************************************************************************************
'Constants
SD_Data_OUT CON 0 'Serial data TO SD Module
SD_Data_IN CON 1 'Serial data FROM SD Module
Right_OE CON 5 'Right motor enable, HIGH turns motor OFF, LOW enables motor
Left_OE CON 6 'Left motor enable, HIGH turns motor OFF, LOW enables motor
SD_Fpin CON 7 'Fpin for MMC/SD Module
LCD CON 8 'Serial LCD Data
DAC_LOAD CON 9 'CS
DAC_DATA CON 11 'Digital to Analog Data line, Data is shifted in on the falling edge
DAC_CLK CON 10 'Digital to Analog Clock line, Data is shifted in on the falling edge
MSB_FIRST CON 1 'MSB First
LSB_FIRST CON 0 'LSB First
ADC_CS CON 12 'Analog to Digital Chip Select line, A LOW enables the ADC
ADC_Din CON 13 'Analog to Digital Data Into AD Convertor, load channel configuration data into the ADC
ADC_Dout CON 14 'Analog to Digital Data Out of AD Convertor, data changes on falling edge of clock
ADC_CLK CON 15 'Analog to Digital Clock line
N9600 CON $40F0 'Use with BS2-SX for BPI-216 2x16 LCD display
I CON 254 'Instruction prefix value for BPI-216 2x16 LCD display
CLR CON 1 'Clear Screen command for the LCD
Right_Motor CON 15
Left_Motor CON 7
ADC_Right_Press CON 24 'CH0 of ADC
ADC_Right_Flow CON 25 'CH1 of ADC
ADC_Left_Press CON 26 'CH2 of ADC
ADC_Left_Flow CON 27 'CH3 of ADC
BaudMode CON 240 '8 bit no parity true for BS2-sx and BS2-p
'**********************************************************************************************************************
'Variables
Left_Flow VAR Word
Right_Flow VAR Word
Rx_Pressure VAR Byte
Left_Pressure VAR Word
Right_Pressure VAR Word
Status VAR Nib
Burdened_Delta VAR Nib
Bilevel_Mode VAR Bit '0 = OFF, 1 = ON
x VAR Word
y VAR Word
'FC CON $89
Nbytes VAR Byte
sPointer VAR Byte
result VAR Word
idx VAR Byte
ascii VAR Byte
PAUSE 500
Rx_Pressure = 1311
Burdened_Delta = 6
Bilevel_Mode = 1
'FW = File Write, Open the SD Card AND WRITE a file name TO it
DEBUG "....................",CR
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FW ","PatData.txt",CR]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "File write: ",STR x \2, CR
PAUSE 200
'PL = Put Line, put a line of data on the SD card
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"PL ","1234","5","6","12345","23456","45678","\r",CR]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "Line wrote: ",STR x \2, CR
PAUSE 500
'FC = File Closed, close the file
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FC",CR ]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "File closed:",STR x \2, CR
PAUSE 200
'FR = File Read, read the PatData.txt file
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FR ","PatData.txt",CR ]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "Read Status:",STR x \2, CR
PAUSE 200
'GL = Get Line, get the line of data that was written
'on the SD card above
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"GL",CR]
SERIN SD_Data_IN,240,10000,No_Data,[noparse][[/noparse]SPSTR 21]
PAUSE 200
'display the data on the debug screen
FOR y = 0 TO 20
GET y, Rx_Pressure
DEBUG "Data: ",Rx_Pressure,CR
NEXT
DEBUG "....................",CR,CR
'Question was: At this point of the code, how do I put the data into the SP RAM and then
'extract the data as variables. The variable sizes are
'separated by quotes as indicated in the PL command above?
'Answer below:
Nbytes=4 : sPointer=0 : GOSUB DecodeSP
x = result ' 1234
DEBUG "1234 Data: ",DEC x,CR,CR
Nbytes=1 : sPointer=4 : GOSUB DecodeSP
x = result ' 5
DEBUG "5 Data: ",DEC x,CR,CR
Nbytes=1 : sPointer=5 : GOSUB DecodeSP
x = result ' 6
DEBUG "6 Data: ",DEC x,CR,CR
Nbytes=5 : sPointer=6 : GOSUB DecodeSP
x = result ' 12345 ' <-- changed to 12345
DEBUG "12345 Data: ",DEC x,CR,CR
Nbytes=5 : sPointer=11 : GOSUB DecodeSP
x = result ' 23456
DEBUG "23456 Data: ",DEC x,CR,CR
Nbytes=5 : sPointer=16 : GOSUB DecodeSP
x = result ' 45678 ' changed to 45678
DEBUG "45678 Data: ",DEC x,CR,CR
GOTO File_Size
DecodeSP: ' enter with sPointer, Nbytes
result=0
FOR idx=0 TO Nbytes-1
GET idx+sPointer, ascii
result = result*10 + (ascii - $30) ' convert ascii to number and accumulate
NEXT
RETURN
File_Size:
'FS = File Size, indicate the size of the PatData.txt file
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FS ","PatData.txt",CR ]
SERIN SD_Data_IN,240,[noparse][[/noparse]STR x \2]
DEBUG "File size: ",STR x \2, CR
PAUSE 200
GOTO Close_File
No_Data:
DEBUG "****************",CR
DEBUG "Time-out Error",CR
DEBUG "****************",CR
PAUSE 200
'FC = File Closed, close the file
Close_File:
SEROUT SD_Data_OUT,240,[noparse][[/noparse]"FC",CR]
SERIN SD_Data_IN,240,2000,Not_Closed,[noparse][[/noparse]STR x \2]
DEBUG "File closed:",STR x \2, CR
GOTO Completed
Not_Closed:
DEBUG "File may not have closed"
Completed:
DEBUG "....................",CR,CR
END
·· No problem!· A lot of helpful knowledgeable people here.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com