如题,程序见下,问题是txt文本的数据不对。
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "sram.h"
#include "malloc.h"
#include "string.h"
#include "mmc_sd.h"
#include "ff.h"
#include "usmart.h"
#include "adc.h"
//////////
FIL fnew; /* file objects */
FATFS fs1; /* Work area (file system object) for logical drives */
FRESULT res;
extern UINT br, bw; /* File R/W count */
BYTE buffer[4096]={0}; /* file copy buffer */
BYTE textFileBuffer[] = "Welcome , today is a good day";
int main(void)
{
u8 t=0;
u16 adcx;
unsigned char temp0[400]={48,49,50,51,52,53,54,55,56,57,58}; //ASCII 实际为0 1 2 。。。
u16 temp1;
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600); //串口初始化为9600
LED_Init(); //LED端口初始化
LCD_Init(); //初始化液晶
KEY_Init(); //初始化按键
Adc_Init(); //ADC初始化
usmart_dev.init(72);
mem_init(SRAMIN); //初始化内部内存池
f_mount(0,&fs1);
res = f_open(&fnew, "0:newfile.txt", FA_CREATE_ALWAYS | FA_WRITE );
if ( res == FR_OK )
{
res = f_write(&fnew, textFileBuffer, sizeof(textFileBuffer), &bw);
f_close(&fnew);
}
res = f_open(&fnew, "0:armfly.txt", FA_OPEN_EXISTING | FA_READ);
res = f_read(&fnew, buffer, sizeof(buffer), &br);
/* Close open files */
f_close(&fnew);
f_mount(0, NULL);
while(1)
{
if(KEY2==0)
{
for(t=0;t<128;t++)
{
adcx=Get_Adc_Average(ADC_Channel_0,5);
temp0[t]= (char)adcx*(3300.0/4096)+'0' ;
adcx=Get_Adc_Average(ADC_Channel_1,5);
temp0[t+128]= (char)adcx*(3300.0/4096)+'0' ;
adcx=Get_Adc_Average(ADC_Channel_2,5);
temp0[t+256]= (char)adcx*(3300.0/4096)+'0' ;
LED1=!LED1;
}
}
if(KEY1==0)
{
f_mount(0,&fs1);
res = f_open(&fnew, "0:ADC.txt", FA_CREATE_ALWAYS | FA_WRITE );
if ( res == FR_OK )
{
res = f_write(&fnew, temp0, sizeof(temp0), &bw);
f_close(&fnew);
}
f_close(&fnew);
f_mount(0, NULL);
LED1=!LED1;
}
if(KEY0==0)
{
LED1=!LED1;
LCD_ShowString(0,250,200,16,16,buffer);
}
LED0=!LED0;
}
}
//注释:
KEY0 按下读取已有文本中字符串并显示。
KEY2按下为AD采集数据,转换放入char型数组
KEY1 按下char数组 存入TXT。
上电运行后,按KEY1 SD卡中TXT文本中为 0 1 2 3 4 5 6 7 8 9 :。正确;
按KEY2 AD采集后。 在按KEY1。数据就不对了。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>