Shop OBEX P1 Docs P2 Docs Learn Events
Write variables with FSRW or SD_MMC_FATEngine to an SD card — Parallax Forums

Write variables with FSRW or SD_MMC_FATEngine to an SD card

lfreezelfreeze Posts: 174
edited 2012-03-16 06:59 in Propeller 1
Please help !
I am using the FSRW object, and the “SD Cards write tutorial” provided by
Gadget Gangster. Everything worked fine with the tutorial. I am able to write strings to the SD card. I then tried to modify the Write program (original below) to include a variable. I did many, many trial and error attempts, mostly including the “Numbers” object, I was not successful. Can anyone provide a simple example of writing a variable to the SD card?

I have not tried the FAT/16/FAT32 full file system driver. Does a simple example
Of writing a variable to an SD card exist for this routine?
Many thanks for any help….

Larry

‘
WRITE STRINGS TO SD CARD
con
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
do = 27 '0
clk = 26 '1
di = 25 '2
cs = 24' 3
obj
sdfat : "fsrw"
pst : "Parallax Serial Terminal"
num : "numbers"
Pub demo | mount ,able
able:=1

pst.start(115_200)
waitcnt(clkfreq*4 + cnt)

mount := \sdfat.mount_explicit(do, clk, di, cs)
if mount < 0
pst.str( string( 13, "Failed to Mount", 13 ))
abort

pst.str(string(13, "SD card was found and mounted okay" ,13))

sdfat.popen(string("test.txt"),"w")
repeat 10
sdfat.pputs(string("Sending Strings is Easy",13,10 ))


sdfat.pclose
sdfat.unmount

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-06-27 09:19
    Take the "PUB dec" routine from FullDuplexSerial and change the "tx( )" calls to "sdfat.pputc( )" calls. You can also use the Format object from the ObEx to create strings from numbers, then use .pputs to output the strings to the file.
  • lfreezelfreeze Posts: 174
    edited 2011-06-27 13:06
    Thanks for the quick response. I modified the “PUB DEC” routine in full duplex serial as follows:

    PUB dec(value) | i
    '' Print a decimal number
    if value < 0
    -value
    sdfat.pputc("-")
    i := 1_000_000_000
    repeat 10
    if value => i
    sdfat.pputc(value / i + "0")
    value //= i
    result~~
    elseif result or i == 1
    sdfat.pputc("0")
    i /= 10
    I changed the write program to include and use the full duplex serial object.
    I created a variable, “able” and assigned it a value of “1”. I loaded, and ran the write program. I replaced this line:

    “ sdfat.pputs(string("Sending Strings is Easy",13,10 )) “

    With this line:

    “ ser.dec(able)”

    I got all blanks when I read the card
    Can you point me in the right direction? I probably misunderstood your suggestion
    Thanks

    Larry
  • KyeKye Posts: 2,200
    edited 2011-06-27 13:58
    Everything you need is right here: http://www.parallaxsemiconductor.com/an006

    You spend alot less time debugging.
  • rokickirokicki Posts: 1,000
    edited 2011-06-27 14:03
    You want to *copy* the dec routine from the serial object into your top-level object or
    into a modified sdfat.

    Chances are you have included the sdfat program multiple times, so they each get their
    own set of variables (which means their own file).

    Thus, the modified ser() routine is not writing to the file you think it is (it is writing to a
    non-existent file).
  • lfreezelfreeze Posts: 174
    edited 2011-06-27 14:30
    Thank you for the responses. I am going to try both suggestions.

    A question for KYE: I downloaded the AN006 application note and read it
    Thoroughly. I must admit that I am confused on how to modify the engine program to match the connections on my SD card.
    The current connection on the card I am using are:
    SD Propeller

    DO 27
    CLK 26
    DI 25
    CS 24

    These connections work okay using the FSRW object (writing strings)

    I notice on page 15 of the PDF , there are some examples
    Of how to create, open, and append a file. I did not find a simple example of how to write a byte or long variable to the card. If you have an example, please provide it.
    Thanks again……

    Larry
  • HShankoHShanko Posts: 402
    edited 2011-06-27 16:18
    @ Kye,

    Thank you for pointing out AN006. This subject is something I need to read up on and use.
  • KyeKye Posts: 2,200
    edited 2011-06-27 18:07
    The file system driver comes with a piece of demo source code. Read it and understand it. Also, inside of the AN006 is a page of file system acesss examples.

    Also, the file system works kinda like a serial port... All the information you need is there in the demo code included with the driver. Please read the demo code. It shows how to do everything you want.
  • lfreezelfreeze Posts: 174
    edited 2011-06-28 13:51
    Thanks Kye, I was successful, I took the ISD_card_profiler program and after studying it for a while it became obvious that “printdecimal” part of the program was what I needed. I did some cutting and pasting and was able to modify, and write five test variables to the SD card. I’m still trying to learn the many functions that are available in your excellent driver.

    Larry
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-15 12:25
    I am having the exact same problem (writing variables to an SD card). But a lot of this stuff is over my head (I have gone through the SD card tutorial on gadget gangster and am using the FSRW object). Lfreeze, could you explain to me in more detail what you did to be able to write variables to the SD card? Thanks.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-15 12:46
    Both FSRW and Kye's SD card driver deal with strings and with individual characters. In particular, FSRW has a method pputc that writes a single character (byte) to the currently open file and another method called pputs that writes a string of characters. What you need is to take a number and convert it to a series of characters and write those characters to the file. There's an object called Numbers that has several methods in it that will take a number and convert it to a string, then return the address of the string (in a buffer internal to Numbers). You can use that address to write the string using the pputs method. You can also do what I suggested above in post #2 making sure copy the dec method to your program making the changes I suggested (that's why I said "Take").
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-15 13:06
    Thanks for the quick response Mike.....I was thinking of that as a possible solution (converting a decimal to a string), I know in C there are quite a few functions, but i was at a loss in terms of spin. I will take a look at the numbers object and let you know how that goes.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2012-03-15 13:18
    /note to self...

    Update the GG tutorial with some demonstrations on how to write numbers... Thanks for pointing out this need.

    (The AN006 document is most excellent, but different folks learn in different ways, so alternatives are always a good idea...)

    OBC
  • lfreezelfreeze Posts: 174
    edited 2012-03-16 06:59
    Attached are two spin programs that I created for test purposes. They did work, and do demonstrate the use of variables.
    They are not fully commented, but give them a try. I am not very skilled at writing spin code but, persisted with a lot of trial and error
    and cutting and pasting.
    Larry
Sign In or Register to comment.