Shop OBEX P1 Docs P2 Docs Learn Events
Prop C Learn - SD Datalogger lesson - problems read/write integer — Parallax Forums

Prop C Learn - SD Datalogger lesson - problems read/write integer

John KauffmanJohn Kauffman Posts: 653
edited 2013-09-12 19:08 in Learn with BlocklyProp
I'm in the C learning system http://learn.parallax.com/propeller-c-simple-devices/sd-card-data
I've modified from example: SD Datalogger.side.

I'm trying to save integers from an RCTime circuit to EEPROM. For test I am showing value measured, writing to EEPROM, reading and showing value read back. On read-back test I keep getting zeros read out to terminal and when I open in notepad I see gibberish, like the values are not lined up with ASCII codes.

I'm very suspicious of my args for the fwrite / fread lines. The "element of data" would be an integer so 4 bytes? And just one of those elements.
I'm having problems finding the documentation on this. I got as far as here but nothing on fwrite file:///C:/Users/John%20Kauffman/Documents/SimpleIDE/Learn/Simple%20Libraries/Utility/libsimpletools/Documentation%20simpletools%20Library.html

// Write to EEPROM
int DO = 22, CLK = 23, DI = 24, CS = 25; // SD card pins on Propeller BOE
sd_mount(DO, CLK, DI, CS); // Mount SD card
FILE* fp = fopen("003.txt", "w"); // Open a file for writing
fwrite(&temp, 4, 1, fp); // Add contents to the file
fclose(fp); // Close the file

// Read from EEPROM
int myOut; // var for value read out
fp = fopen("003.txt", "r"); // Reopen file for reading
fread(myOut, 4, 1, fp); // Read one int
fclose(fp); // Close the file
print(" > read: %d",myOut); // Display value
print("\n"); // With a newline at the end

Entire file attachedTempLightLogger 02.c

Comments

  • SRLMSRLM Posts: 5,045
    edited 2013-09-12 13:49
    Just a thought, but shouldn't you pass the address of myOut to fread?
  • John KauffmanJohn Kauffman Posts: 653
    edited 2013-09-12 14:07
    You mean a number address instead of the var name?
  • SRLMSRLM Posts: 5,045
    edited 2013-09-12 15:05
    Well, you're passing the value of myOut to fread (at this point the value is undefined since you don't initialize it to anything). That's probably not correct. What I think you want is to pass the address of myOut to the function so that it can be loaded with the data. This is all speculative though: you should confirm with the documentation.
    fread([b]&[/b]myOut, 4, 1, fp); // Read one int
    
  • SRLMSRLM Posts: 5,045
    edited 2013-09-12 15:06
    On another note you should be able to do the final print in one line:
    print(" > read: %d[b]\n[/b]",myOut); // Display value
    
  • SRLMSRLM Posts: 5,045
    edited 2013-09-12 15:07
    Something else: you're not writing to EEPROM, but to the SD card. Your comments are out of date...
  • John KauffmanJohn Kauffman Posts: 653
    edited 2013-09-12 19:08
    Perfect, thanks for taking the time to analyse it. And yes, I have a bad habit of thinking of data logging to EEPROM
Sign In or Register to comment.