因為存為TXT可以實現,但是讀取TXT里邊的數據總是不盡如人意,所以,最終存為bin文件了。
先摘幾個觀點:
http://www.openedv.com/posts/list/36712.htm “文本文件存儲的都是ASCII內容,如果你以16進制格式顯示出來,那就是ASCII的內碼。”
http://www.openedv.com/posts/list/58089.htm“ 你這種不要存.txt了,直接存.bin就是了。你先學會存一個字節數據,... ”
stm32 Fatfs 讀寫SD卡(http://www.ichanging.org/stm32-fatfs-sd.html)
f_open(http://elm-chan.org/fsw/ff/en/open.html)
實現的部分代碼(讀寫bin文件)(其他可以借鑒 原子的FATFS相關實例等 http://www.openedv.com/forums/show/0/2/0/55.htm)
u32 sd_size;
FIL file;
FRESULT res;
UINT bw; UINT br;//ʵ¼Ê¶ÁÈ¡µ½µÄ×Ö½ÚÊý
// SD ¿¨ while(SD_Initialize())//¼ì²â²»µ½SD¿¨ { //LCD_ShowString(60,170,200,16,16,"SD Card Error!"); printf("\r\nSD Card Error!\r\n"); delay_ms(500); //LCD_ShowString(60,170,200,16,16,"Please Check! "); printf("\r\nSD Card Error!\r\n"); delay_ms(500); } printf("\r\nSD Card OKr\n"); printf("\r\nSD Card Size: MB\r\n"); sd_size=SD_GetSectorCount();//µÃµ½ÉÈÇøÊý printf("\r\nsd_size:%d\r\n",sd_size); f_mount(fs[0],"0:",1); //¹ÒÔØSD¿¨ res=f_open(&file,"0:/Test.bin",FA_CREATE_ALWAYS | FA_WRITE ); if(res != FR_OK) { printf("\r\nOpen file error!\r\n"); } else { if(res == FR_OK) { iTemp=13; res = f_write(&file, &iTemp, sizeof(iTemp), &bw); /* Write it to the dst file */ //res = f_write(&file, "\r\n",2, &bw); /* Write it to the dst file */ iTemp=1; res = f_write(&file, &iTemp, sizeof(iTemp), &bw); /* Write it to the dst file */ printf("\r\nwrite data ok!\r\n"); } else { printf("\r\nwrite data error!\r\n"); } } f_close(&file); res=f_open(&file,"0:/Test.bin",FA_OPEN_EXISTING|FA_READ);// if(res!=FR_OK) { printf("\r\n f_open() fail .. \r\n"); } else { printf("\r\n f_open() success .. \r\n"); } while(!f_eof(&file)) { iBuf=0; res = f_read(&file, &iTemp, sizeof(u16), &br);//¶Áȡһ¸ö16λµÄÊý¾Ý if(res==FR_OK) { iBuf++; printf("%d \r\n",iTemp); } else { printf("\r\n f_read() fail .. \r\n"); } } f_close(&file); f_mount(fs[0],"0:",NULL);