高中生
最后登录1970-1-1
在线时间 小时
注册时间2015-10-20
|
楼主 |
发表于 2015-10-26 10:38:59
|
显示全部楼层
#include "stm32f10x.h"
#include "bsp_sdio_sdcard.h"
#include "bsp_usart1.h"
#include "ff.h"
#include "mpu6050.h"
#include "bsp_i2c.h"
#include "stm32f10x_it.h"
#include "stdio.h"
#define uchar unsi
#define TASK_ENABLE 0
extern unsigned int Task_Delay[NumOfTask];
FIL fnew;
u8 i=0;
u8 byte; /* file objects */
FATFS fs; /* Work area (file system object) for logical drives */
FRESULT res;
UINT br, bw; /* File R/W count */
BYTE buffer[10000]; /* file copy buffer */
BYTE write_buf[10];
short Accel[3];
u8 flag=0,j=0;
short Gyro[3];
short Temp;
u16 pointer=0;
void delay (int x)
{ int i,j;
for (i=0;i<x;i++)
for(j=0;j<600;j++);
}
//void num2char(short *write_buf)
//{
// write_buf[0]=Accel[0]/10000+0X30;
// write_buf[1]=Accel[0]%10000/1000+0X30;
// write_buf[2]=Accel[0]%1000/100+0X30;
// write_buf[3]=Accel[0]%100/10+0X30;
// write_buf[4]=Accel[0]%10+0X30;
// write_buf[5]=',';
//
//}
void read(void)
{
res = f_open(&fnew, "0:飞行数据.txt", FA_OPEN_EXISTING | FA_READ);
pointer=fnew.fsize;
res=f_read(&fnew, buffer,sizeof(buffer) , &br);
printf("\r\n %s ", buffer);
printf("\r\n完成\r\n\r\n" );
f_close(&fnew);
}
int main(void)
{
i2c_GPIO_Config();
//MPU6050初始化
MPU6050_Init();
USART1_Config();
printf("\r\n 飞行数据采集系统 \r\n");
NVIC_Configuration();
f_mount(0,&fs);
res = f_open(&fnew, "0:飞行数据.txt", FA_CREATE_ALWAYS | FA_WRITE );
// pointer=fnew.fsize; //获得文件大小
// res=f_lseek(&fnew,pointer);//移动文件指针
// res=f_write(&fnew,buffer,byte,&br);
//
// res=f_sync(&fnew);
f_close(&fnew);
while(1)
{
delay(10000);
if(Task_Delay[1]==0)
{
MPU6050ReadAcc(Accel);
printf("\r\n加速度参考:%8d",Accel[0] );
write_buf[0]='*';
write_buf[1]='*';
write_buf[2]=':';
write_buf[3]=Accel[0]/10000+0X30;
write_buf[4]=Accel[0]%10000/1000+0X30;
write_buf[5]=Accel[0]%1000/100+0X30;
write_buf[6]=Accel[0]%100/10+0X30;
write_buf[7]=Accel[0]%10+0X30;
write_buf[8]=0x0d;
write_buf[9]=0x0a;
// write_buf[6]='\r' ;
// printf("\r\n加速度: %8d%8d%8d",Accel[0],Accel[1],Accel[2] );
// MPU6050ReadGyro(Gyro);
// printf("陀螺仪: %8d%8d%8d ",Gyro[0],Gyro[1],Gyro[2]);
// MPU6050_ReturnTemp(&Temp);
// printf("\r温度:%d",Temp);
// printf("\r\n采集成功%d\r\n",i );
// Task_Delay[1]=100;//此值每1ms会减1,减到0才可以重新进来这里,所以执行的周期是100ms
}
if ( res == FR_OK )
{
// sprintf(FLIGHTBUFF,"%s",&Accel);
//res = f_write(&fnew, Accel,2, &bw);
//res = f_write(&fnew, &Accel[1],2, &bw);
// res = f_write(&fnew, &Accel[2],2, &bw);
res = f_open(&fnew, "0:飞行数据.txt", FA_CREATE_ALWAYS | FA_WRITE );
pointer=fnew.fsize;
res=f_lseek(&fnew,pointer);
res=f_lseek(&fnew,pointer+j);
res = f_write(&fnew, write_buf, sizeof(write_buf), &bw);
res=f_sync(&fnew);
i++;
// f_lseek(&fnew, f_size(&fnew));
f_close(&fnew);
printf("\r\n写成功%d",i );
j=j+10;
read();
delay(50);
}
// res = f_open(&fnew, "0:飞行数据.txt", FA_OPEN_EXISTING | FA_READ);
// pointer=fnew.fsize;
// res=f_read(&fnew, buffer,sizeof(buffer) , &br);
// printf("\r\n %s ", buffer);
// printf("\r\n最后一句\r\n\r\n" );
//
//
// f_close(&fnew);
}
/* Close open files */
/* Unregister work area prior to discard it */
f_mount(0, NULL);
}
|
|