varnon
03-24-2012, 04:06 AM
The tutorial in reference is here:
http://www.gadgetgangster.com/tutorials/414
'
'SD Read Example 2 by Jeff Ledger
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
DO = 0 'Set these pins to match your SD connections.
CLK = 1
DI = 2
CS = 3
OBJ
sdfat : "fsrw"
pst : "Parallax Serial Terminal"
num : "Numbers"
VAR
BYTE DATA
PUB demo | mount, r, counter, number
'Start the required code to communicate with Parallax Serial Terminal.
'Wait four seconds for the user to launch PST and press the Enable button.
pst.Start(115_200)
waitcnt(clkfreq*4 + cnt) 'Increase the 4 if more time is required.
'Attempt to mount the SD card.
'Report a failure to mount if the card is not found.
'Halt if the card fails to mount.
mount := \sdfat.mount_explicit(DO, CLK, DI, CS)
if mount < 0
pst.str( string( 13, "Failed to mount", 13 ) )
abort
'Report a message that the card was found and mounted.
pst.str(string(13,"SD was card found and mounted fine.",13,13))
sdfat.popen(string("data.csv"),"r")
counter:=0 'Setup a counter starting at zero.
repeat 'Start a repeat loop.
r := sdfat.pgetc 'Get a character from data.csv
if r < 0 'If the character is "less than zero"
pst.newline ' send a newline to the terminal
quit ' and quit.
if r == 44 or r == 13 'If character is a comma or carriage return
pst.str(string("String: ")) ' Display line of text.
pst.str(@data) ' Display data.
number:=num.FromStr(@data, num#dec) ' Convert data string to number.
pst.str(string(" Number: ")) ' Display line of text.
pst.dec(number) ' Display the number.
pst.str(string(" +")) 'A little test to make sure
pst.dec(number) ' we are dealing with
pst.str(string("=")) ' actual number data.
pst.dec(number+number) 'Display number + number
pst.newline 'Send a carriage return to PST
bytefill(@data,0,counter) 'Fill data with zeros again.
counter:=0 'Reset counter to negative 0.
if r > 44 'If the character "greater than 44"
data[counter]:=data[counter]+r ' send character to data string.
counter++ ' increment the counter.
pst.str(string("Closing file and unmounting SD card.",13))
sdfat.pclose
'Unmount the card and end program.
sdfat.unmount
There are a few things that I don't understand.
First, I am a little confused to how "data" is used.
In the VAR section, it is declared as a byte...
But the way it is referenced in the program (data[counter]) suggests that "data" is an array of bytes.
Is this correct? By declaring "byte data" is a byte sized and byte aligned array being created?
Or is it that no array is created, but that to "data[1]" refers to a non-named byte that is 1 byte away from "data"?
Second, I do not understand the line "data[counter]:=data[counter]+r"
Why is "r" added to the existing value? The existing value should be 0, correct? For me, the program has worked identically with "data[counter]:=r"
Finally, why do you need to refer to the address of data in "pst.str(@data)"? Not referring to the address obviously does not work, but I am not sure why. The other time the address is referred to is in the "bytefill" command. This makes a little more sense to me. The bytes are set to 0 from the starting address (@data or data[0] should be equivalent I think) to whatever the last address that was written to is, determined by the counter.
For my purposes, I encountered errors during the file reading phase of my program. I found that this occurred when there were around 30 comma separated values in my text file. I found that declaring the variable data as an array such as "byte data[100]" eliminated all problems. But really, I have no idea why that worked.
Can anyone clarify this questions a little? I do have working code, but I really want to understand how and why things work so that I can correct any bugs that come up.
Thanks
http://www.gadgetgangster.com/tutorials/414
'
'SD Read Example 2 by Jeff Ledger
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
DO = 0 'Set these pins to match your SD connections.
CLK = 1
DI = 2
CS = 3
OBJ
sdfat : "fsrw"
pst : "Parallax Serial Terminal"
num : "Numbers"
VAR
BYTE DATA
PUB demo | mount, r, counter, number
'Start the required code to communicate with Parallax Serial Terminal.
'Wait four seconds for the user to launch PST and press the Enable button.
pst.Start(115_200)
waitcnt(clkfreq*4 + cnt) 'Increase the 4 if more time is required.
'Attempt to mount the SD card.
'Report a failure to mount if the card is not found.
'Halt if the card fails to mount.
mount := \sdfat.mount_explicit(DO, CLK, DI, CS)
if mount < 0
pst.str( string( 13, "Failed to mount", 13 ) )
abort
'Report a message that the card was found and mounted.
pst.str(string(13,"SD was card found and mounted fine.",13,13))
sdfat.popen(string("data.csv"),"r")
counter:=0 'Setup a counter starting at zero.
repeat 'Start a repeat loop.
r := sdfat.pgetc 'Get a character from data.csv
if r < 0 'If the character is "less than zero"
pst.newline ' send a newline to the terminal
quit ' and quit.
if r == 44 or r == 13 'If character is a comma or carriage return
pst.str(string("String: ")) ' Display line of text.
pst.str(@data) ' Display data.
number:=num.FromStr(@data, num#dec) ' Convert data string to number.
pst.str(string(" Number: ")) ' Display line of text.
pst.dec(number) ' Display the number.
pst.str(string(" +")) 'A little test to make sure
pst.dec(number) ' we are dealing with
pst.str(string("=")) ' actual number data.
pst.dec(number+number) 'Display number + number
pst.newline 'Send a carriage return to PST
bytefill(@data,0,counter) 'Fill data with zeros again.
counter:=0 'Reset counter to negative 0.
if r > 44 'If the character "greater than 44"
data[counter]:=data[counter]+r ' send character to data string.
counter++ ' increment the counter.
pst.str(string("Closing file and unmounting SD card.",13))
sdfat.pclose
'Unmount the card and end program.
sdfat.unmount
There are a few things that I don't understand.
First, I am a little confused to how "data" is used.
In the VAR section, it is declared as a byte...
But the way it is referenced in the program (data[counter]) suggests that "data" is an array of bytes.
Is this correct? By declaring "byte data" is a byte sized and byte aligned array being created?
Or is it that no array is created, but that to "data[1]" refers to a non-named byte that is 1 byte away from "data"?
Second, I do not understand the line "data[counter]:=data[counter]+r"
Why is "r" added to the existing value? The existing value should be 0, correct? For me, the program has worked identically with "data[counter]:=r"
Finally, why do you need to refer to the address of data in "pst.str(@data)"? Not referring to the address obviously does not work, but I am not sure why. The other time the address is referred to is in the "bytefill" command. This makes a little more sense to me. The bytes are set to 0 from the starting address (@data or data[0] should be equivalent I think) to whatever the last address that was written to is, determined by the counter.
For my purposes, I encountered errors during the file reading phase of my program. I found that this occurred when there were around 30 comma separated values in my text file. I found that declaring the variable data as an array such as "byte data[100]" eliminated all problems. But really, I have no idea why that worked.
Can anyone clarify this questions a little? I do have working code, but I really want to understand how and why things work so that I can correct any bugs that come up.
Thanks