接上一篇的正点原子战舰读取sd卡文本文件的主程序。

#include “sys.h”
#include “delay.h”
#include “usart.h”
#include “led.h”
#include “lcd.h”
#include “key.h”
#include “usmart.h”
#include “malloc.h”
#include “sdio_sdcard.h”
#include “w25qxx.h”
#include “ff.h”
#include “exfuns.h”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/************************************************
************************************************/

FIL fp0;
FILE* fp1;
FRESULT res0;
unsigned int brf;
unsigned int *admin_p;
int iii;
int j;
UINT br0,bw0;
u8 ReadBuf[500];
char buf99[100];
char str99[100];
double doublexy[400];
int mm;
double StrToDouble(char *s);

double xx[100];
double yy[100];
int ttt;
int ggg;

//8
//
***
//*****************************************************************
int main(void)
{
u32 total,free;
u8 t=0;
u8 res=0;
u8 tbuf[500];
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
usmart_dev.init(72); //初始化USMART
LED_Init(); //初始化与LED连接的硬件接口
KEY_Init(); //初始化按键
LCD_Init(); //初始化LCD
my_mem_init(SRAMIN); //初始化内部内存池
POINT_COLOR=RED; //设置字体为红色

while(SD_Init())//检测不到SD卡
{
	LCD_ShowString(30,150,200,16,16,"SD Card Error!");
	delay_ms(500);					
	LCD_ShowString(30,150,200,16,16,"Please Check! ");
	delay_ms(500);
	LED0=!LED0;//DS0闪烁
}
exfuns_init();							//为fatfs相关变量申请内存				 

f_mount(fs[0],“0:”,1); //挂载SD卡

 memset(str99,0,sizeof(str99));

memset(buf99,0,sizeof(buf99));

LCD_Fill(30,150,240,150+16,WHITE);		//清除显示			  
while(exf_getfree("0",&total,&free))	//得到SD卡的总容量和剩余容量
{
	LCD_ShowString(30,150,200,16,16,"SD Card Fatfs Error!");
	delay_ms(200);
	LCD_Fill(30,150,240,150+16,WHITE);	//清除显示			  
	delay_ms(200);
	LED0=!LED0;//DS0闪烁
}	

res0=f_open(&fp0,“1.txt”, FA_READ );
if(res0!=FR_OK)
{
LCD_ShowString(30,150,200,16,16,“f_open() fail!”);
}
else
{
LCD_ShowString(30,150,200,16,16,“f_open() okok!”);
}
f_read(&fp0,ReadBuf,500,&br0);
sprintf((char*)tbuf,“br0:%d”,br0);
LCD_ShowString(30,210,210,16,16,tbuf);
delay_ms(1000);
LCD_Clear(WHITE);

		LCD_Clear(WHITE);
		 
		 
		 j=0;mm=0;
	  for(iii=0;iii<br0;iii++)
  {   
			delay_ms(5);
			 if(ReadBuf[iii]!=',')
			 { LCD_ShowString(30,170,200,16,16,"1234");	
					str99[j]=ReadBuf[iii];
				 sprintf((char*)tbuf,"%s",str99);
				 LCD_ShowString(30,210,210,16,16,tbuf);	
				 delay_ms(1000);
				 LCD_Clear(WHITE);
					j++;
			 }

			 else if(ReadBuf[iii]==',')//读取到一个字符数组;放到里面。把字符数组,变为浮点数
			 {
				 LCD_ShowString(30,170,200,16,16,"ef0f");	
		     doublexy[mm]=StrToDouble(str99);
			   mm++;
	       memset(str99,0,sizeof(str99));
	       j=0;
			 }
					 
}
				sprintf((char*)tbuf,"mm:%d",mm); 
  LCD_ShowString(30,210,210,16,16,tbuf);	
     for(iii=0;iii<mm;iii++)
			 {
					sprintf((char*)tbuf,"Time:%.2f",doublexy[iii]); 
					LCD_ShowString(30,210,210,16,16,tbuf);
					delay_ms(500);
				  LCD_Clear(WHITE);
				
			 }
			 
     ttt=0;ggg=0;
			 for(iii=0;iii<br0;iii++)
			 {
				  if(iii%2==0)//偶数,放到y坐标
					{xx[ttt]=doublexy[iii];ttt++;	}
        else 
					{ yy[ggg]=doublexy[iii];ggg++;}	
         					
				}
			 
				for(iii=0;iii<br0/12;iii++)
			    {
						sprintf((char*)tbuf,"xx:%.2f",xx[iii]); 
						LCD_ShowString(30,210,210,16,16,tbuf);
						delay_ms(1000);
						LCD_Clear(WHITE);
					}
					
					for(iii=0;iii<br0/12;iii++)
			    {
						sprintf((char*)tbuf,"yy:%.2f",yy[iii]); 
						LCD_ShowString(30,210,210,16,16,tbuf);
						delay_ms(1000);
						LCD_Clear(WHITE);
					}
			 
		 
 res0 = f_close(&fp0);
 while(1);

}

//字符串转double
double StrToDouble(char *s)
{
int i = 0;
int k = 0;
double j;
int flag =1;
double result = 0.0;
if (s == NULL)
{
// printf(“输入字符串为空”);
// exit(-1);
return 0;

}

while (s[i] == ' ')
{
	i++;
}
if (s[i] == '+')
{
	i++;
}
if (s[i] == '-')
{
	i++;
	flag = -1;
}
while (s[i] != '\0' && s[i] != '.')
{
	if (s[i] < '0' || s[i] > '9')
	{
	//	printf("字串含有非数字字符,无法转换!\n");
		return 0;
	//	exit(-1);
	}
	j = (s[i] - '0')*1.0;
	result = result * 10 + j;
     if((result>0x7fffffff))
    {
  	return 0;
    }
	i++;
}
if (s[i] == '.')
{
	i++;
	while (s[i] != '\0')
	{
		if (s[i] < '0' || s[i] > '9')
		{
			//printf("字串含有非数字字符,无法转换!\n");
			return 0;
		}
		k++;
		j = s[i] - '0';

	result = result + (1.0 * j) / pow(10.0, k);    //编译时候此处有警告

		i++;
	}
}
result = flag * result;
return result;

}

猜你喜欢

转载自blog.csdn.net/lmf666/article/details/106179298