|
|
View previous topic :: View next topic |
Author |
Message |
kalambraka
Joined: 24 Jan 2012 Posts: 4
|
SD fat32 write problem |
Posted: Tue Jan 24, 2012 10:37 pm |
|
|
Hi guys!
I'm interfacing a PIC18F2455 with a SD card using the mmc_spi_fat32.c library.
I send data coming from sensors to the card using the fwrite function, and then I can see it (so, it's seems that everything is OK), but I realize that it only writes the half of my data into the card. If I send 4 datas using fwrite I can only see 2 in the card. If I send 8, I see 4...
I write this test program sending the data in a loop:
Code: |
#include <18F2455.h>
#use delay(clock=4000000)
#include "mmc_spi_fat32.h"
#include <usb_cdc.h>
#include <string.h>
#include "mmc_spi_fat32.c"
#ZERO_RAM
void main(){
delay_ms(1000);
char file,gfilename[5];
int t[3];
int h[3];
int p[3];
int l=3,x=1;
int mesura=0;
usb_cdc_init();
usb_init();
while(!usb_cdc_connected()) {} // Espera fins detectar una transmisió USB
if (usb_enumerated()){ // El PC reconeix el dispositiu
usb_task();
delay_ms(1000);
MMCInit() == MMC_OK;
while(MMCInit() != MMC_OK){ //espera inicialitzacio
printf(usb_cdc_putc,"MMC init FAIL!!!!!\r\n");
printf(usb_cdc_putc,"Try again...\r\n");
delay_ms(100);
MMCInit() == MMC_OK;
}
printf(usb_cdc_putc,"MMC init OK!!!!!\r\n");
delay_ms(1000);
InitFAT();
printf(usb_cdc_putc,"FAT init OK!!!!!\r\n");
delay_ms(1000);
strcpy(gfilename,"D.LOG");
file = fopen(gfilename,'w'); // open EVENTS.LOG for append ////
if (file & MMC_ERROR) ////
{ ////
printf(usb_cdc_putc,"Couldn't open file!\r\n"); ////
if(file == MMC_NO_CARD_INSERTED) ////
printf(usb_cdc_putc,"Please, insert MMC!"); ////
else if(file == MMC_MAX_FILES_REACHED) ////
printf(usb_cdc_putc,"ops.. =)"); ////
} ////
else ////
{
//#ZERO_RAM
DELAY_MS(1000);
t[0]=02;
t[1]=02;
t[2]=02;
h[0]=03;
h[1]=03;
h[2]=03;
p[0]=04;
p[1]=04;
p[2]=04;
while(mesura<4){
printf(usb_cdc_putc,"Start writing...\r\n");
fwrite(t,l,file);
delay_ms(10);
fwrite(h,l,file);
delay_ms(10);
fwrite(p,l,file);
delay_ms(10);
delay_ms(10);
fclose(file);
delay_ms(10);
delay_ms(100);
printf(usb_cdc_putc,"Write complete...");
mesura=mesura+1;
}
delay_ms(100);
}
}
}
|
In the SD card I should see:
222333444
222333444
222333444
222333444
but I always see only half:
222333444
222333444
Can anyone help me?
Thank you. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Wed Jan 25, 2012 2:23 am |
|
|
I'm surprised you see half.
Don't you think the fact that you have closed the file after writing the first 9 bytes, and don't open it again, might have something to do with it.
Best Wishes |
|
|
kalambraka
Joined: 24 Jan 2012 Posts: 4
|
|
Posted: Wed Jan 25, 2012 11:55 am |
|
|
Thanks for the reply.
If I open again the file inside the loop, then I see nothing in the SD card...
It's really strange...
The only solution that I found is sending data with zeros in another loop after this... I think that maybe it's something related to cleaning the buffer afer writing again... |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|