Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 9 months ago.
Why it does not work when I realise writing in state machine mode. I attach my program.
- include "mbed.h"
- include "SDFileSystem.h"
/* This library supports:
FAT12 / FAT16 / FAT32 SD / SDHC cards up to 32GB long filenames time stamp
- /
uint8_t stan_zapisu; uint16_t licznik=0; FILE *fp;
Serial pc(SERIAL_TX, SERIAL_RX);
SDFileSystem sd(p5, p6, p7, p8, "sd"); the pinout on the mbed Cool Components workshop board ST NUCLEO F030RB
SDFileSystem sd(PA_7, PA_6, PA_5, PB_5, "sd"); MOSI, MISO, SCLK, SSEL
void maszyna_zapisu(void);
int main() {
pc.baud(38400); zamiast domyslnie 9600 jest 38400 pc.printf("This program writes file on SD card - PW 2014-11-16 13:50\r\n"); printf("Hello World!\n");
mkdir("/sd/mydir", 0777);
/* This works */ /* FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { pc.printf("Could not open file for write\n"); } fprintf(fp, "Hello fun SD Card World!"); fclose(fp);
pc.printf("Goodbye World!\n");
- /
/* this not
Error fp is undefined Why?
- / FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); while(1) { maszyna_zapisu(); };
}
/////////// void maszyna_zapisu(void) {
switch(stan_zapisu) { case 0: {
/* if (fp == NULL) { error("Could not open file for write\n"); } else
- / stan_zapisu=1; }; break;
case 1:
{
fprintf(fp,"#
#
#
##\r\n");
pc.printf("
#
#
#
\r\n");
stan_zapisu=2;
}; break;
case 2: { licznik++; if (licznik==100) { stan_zapisu=3; } else stan_zapisu=1; }; break;
case 3: { fprintf(fp,"End of file.\r\n"); pc.printf("End of file.\r\n"); stan_zapisu=4; fclose(fp); }; break;
case 4: { }; break;
} }
Question relating to:
![](/media/cache/profiles/af7fc8485f40dc495bc626d6dbe7faf1.50x50_q85.png)
Please use
posted by Andy A 17 Nov 2014<<code>> and <</code>>
around the code blocks (each one on a line on it's own). Without it code doesn't display correctly and it's hard to tell which lines are commented out.OK, it looks like it should work but you never check that fopen returns a valid response. There is a chance that opening and closing the same file 1000 times as fast as you can is causing problems. It would be more normal to open the file once and keep it open. Make fp a static variable within maszyna_zapisu and only open and close it once.
posted by Andy A 17 Nov 2014Now works perfectly.