Shop OBEX P1 Docs P2 Docs Learn Events
Writing numeric data to an SD card — Parallax Forums

Writing numeric data to an SD card

simonlsimonl Posts: 866
edited 2007-06-19 13:18 in Propeller 1
Hi,

Last night I finally hooked-up an SD card to my PChip - GREAT WORK ROCKIKI cool.gif

Using rockiki's excellent code I easily managed to write strings to a file, but I'm having trouble getting my head round writing numerics blush.gif

Here's the latest version of my code (if you run it,; you'll probably need to change the XTAL settings ;-)

CON
'    _clkmode        = xtal1 + pll16x
'    _xinfreq        = 5_000_000
    _clkmode = xtal2 + pll8x                           
    _xinfreq = 10_000_000

obj
'    term: "tv_text"
    term        : "PC_text"
    sdfat       : "fsrw"
    Num         : "Numbers"

var
    byte tbuf[noparse][[/noparse]20]
 
pub go | x
    num.init
    
    x := \start
    
    term.str(string("Returned from start : "))
    term.dec(x)
    term.out(13)
    waitcnt(1000 + cnt)
    reboot
    
pub start | status, sta, bytes, lcnt, Temp
    term.start(12)
    term.str(string("Mounting.", 13))        
    sdfat.mount(0)
     
    term.str(string("Mounted.", 13))
    term.str(string("===============================", 13))
    term.out(13)
       
    { --- WRITE THE FILE ------------------------------- }
    status := sdfat.popen(string("data.csv"), "w")
    term.str(string("Opening returned : "))
    term.dec(status)
    term.out(13)
    
    sta   := cnt
    bytes := 0
    lcnt  := 1
    Temp  := 1
    
    repeat 10
      sdfat.pwrite(Num.ToStr(lcnt, Num#DEC), 4)
      sdfat.pputc(string(","))
      waitcnt(1000 + cnt)
      sdfat.pwrite(Num.ToStr(Temp, Num#IHEX), 4)
      sdfat.pputc(string(","))
      waitcnt(1000 + cnt)
      sdfat.pwrite(Num.ToStr(Temp, Num#DEC), 4)
      
      sdfat.pputc(13)
      waitcnt(1000 + cnt)
      
      Temp := Temp + 2
      lcnt++
      term.str(string("Data written.", 13))
      waitcnt(1000 + cnt)
    
    sdfat.pclose
    term.str(string("File closed.", 13))

    { --- READ IT BACK --------------------------------- }
    status := sdfat.popen(string("data.csv"), "r")
    if status == 0
      term.str(string(13, "========", 13, "Opening returned : "))
      term.dec(status)
      term.out(13)
      repeat
        status := sdfat.pgetc
        if status < 0
          quit
        term.out(status)
      term.str(string("FILE READ OK.", 13, "========", 13, 13))
    else 
      term.str(string("Opening file FAILED with : "))
      term.dec(status)
      term.out(13)
       
    term.str(string("That's, all, folks!", 13))

As you can see I'm using pwrite - giving it the ToStr string and 4 bytes - but it's not writing as I'd hoped (see below)...
[code]
1

Comments

  • KaioKaio Posts: 253
    edited 2007-06-19 13:14
    Simon,

    you have to pass a character and not a string in method pputc.

          sdfat.pputc(",")
    
    



    Thomas
  • simonlsimonl Posts: 866
    edited 2007-06-19 13:18
    Doh! Do I feel silly now blush.gif

    Many thanks Kaio

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon
    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif
Sign In or Register to comment.