zoukankan      html  css  js  c++  java
  • 【转】STM32利用FATFS读写数组

    因为存为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_openhttp://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("
    SD Card Error!
    ");
    		delay_ms(500);					
    		//LCD_ShowString(60,170,200,16,16,"Please Check! ");
    		printf("
    SD Card Error!
    ");
    		delay_ms(500);
    	}
    	printf("
    SD Card OKr
    ");	
    	printf("
    SD Card Size:     MB
    ");
    	sd_size=SD_GetSectorCount();//µÃµ½ÉÈÇøÊý
    	printf("
    sd_size:%d
    ",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("
    Open file error!
    ");
    	}
    	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, "
    ",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("
    write data ok!
    ");
    		}
    		else
    		{
    			printf("
    write data error!
    ");
    		}
    	 }
    	f_close(&file);
    	
    
    	 res=f_open(&file,"0:/Test.bin",FA_OPEN_EXISTING|FA_READ);//
    	if(res!=FR_OK)
    	{
    		printf("
     f_open() fail .. 
    ");
    	}
    	else
    	{
    		printf("
     f_open() success .. 
    ");
    	}
    	while(!f_eof(&file))
    	{
    		iBuf=0;
    		res = f_read(&file, &iTemp, sizeof(u16), &br);//¶Áȡһ¸ö16λµÄÊý¾Ý
    		if(res==FR_OK)
    		{
    			iBuf++;
    			printf("%d 
    ",iTemp);
    		}
    		else
    		{
    			printf("
     f_read() fail .. 
    ");  
    		}
    	}
    	f_close(&file);
      f_mount(fs[0],"0:",NULL);
  • 相关阅读:
    七牛云上传文件
    微博三方登录
    异步任务 --- django-celery
    阿里云短信服务
    Redis五大数据结构和使用方法
    千万不要买我们家的鞋子!
    Firebug控制台详解
    【转】android 按home键返回到桌面后,再按桌面应用图标又重新打开该应用的解决方法
    【转】android中webview使用的一些细节
    JSONException: Value of type java.lang.String cannot be converted to JSONObject
  • 原文地址:https://www.cnblogs.com/zhangbing12304/p/11375178.html
Copyright © 2011-2022 走看看