野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9700|回复: 2

mpu6050数据存储到sd卡,为什么是乱码?求解

[复制链接]
发表于 2015-10-20 15:25:57 | 显示全部楼层 |阅读模式
#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"
#define uchar unsi
                   #define TASK_ENABLE 0
                                                  extern unsigned int Task_Delay[NumOfTask];
FIL fnew;
                                                                                                        /* file objects */
FATFS fs;                                                                                                        /* Work area (file system object) for logical drives */
FRESULT res;
UINT br, bw;                                                    /* File R/W count */
BYTE buffer[4096]={0};                         /* file copy buffer */
BYTE textFileBuffer[] = "Welcome to use Wildfire iso stm32 Development Board today is a good day";
short  Accel[3];
short Gyro[3];
short Temp;
void delay (int x)
{  int i,j;
    for (i=0;i<x;i++)                  
       for(j=0;j<600;j++);
}
int main(void)
{                                 

    i2c_GPIO_Config();
  //MPU6050初始化
        MPU6050_Init();
   /* USART1 config */
                USART1_Config();
                printf("\r\n 飞行数据采集系统 \r\n");
       
                /* Sdio Interrupt Config */
                NVIC_Configuration();
       
                /* Register work area for each volume (Always succeeds regardless of disk status) */
                f_mount(0,&fs);
                /* function disk_initialize() has been called in f_open */
       
                /* Create new file on the drive 0 */
                res = f_open(&fnew, "0:飞行数据.txt", FA_CREATE_ALWAYS | FA_WRITE );
                 while(1)
                 {
                delay(5000);         
                         if(Task_Delay[1]==0)
                   {
                        MPU6050ReadAcc(Accel);                       
                //        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("温度: %d",Temp);
               
                        Task_Delay[1]=100;//此值每1ms会减1,减到0才可以重新进来这里,所以执行的周期是100ms
               }

                if ( res == FR_OK )
                {
                         
                        res = f_write(&fnew, Accel, sizeof(Accel), &bw);
                        res = f_write(&fnew, Gyro, sizeof(Gyro), &bw);
                                //         f_sync(&fnew);
                                   f_lseek(&fnew, 500);//移动文件指针
                                 // f_lseek(&fnew, f_size(&fnew));
                        f_close(&fnew);      
                }

                res = f_open(&fnew, "0飞行数据.txt", FA_OPEN_EXISTING | FA_READ);          
                res = f_read(&fnew, buffer, sizeof(buffer), &br);

                printf("\r\n %s ", buffer);
                           }
                /* Close open files */
                f_close(&fnew);                                              
                 
    /* Unregister work area prior to discard it */
    f_mount(0, NULL);

          
       
}

          


回复

使用道具 举报

发表于 2015-10-20 19:56:32 | 显示全部楼层
你直接往txt格式的文件写入原始数据,自然是会错了。
你要先用sprintf函数把数据转化成字符串,再进行写入,这样打开txt文件才不会错
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-20 21:49:23 | 显示全部楼层
flyleaf 发表于 2015-10-20 19:56
你直接往txt格式的文件写入原始数据,自然是会错了。
你要先用sprintf函数把数据转化成字符串,再进行写入 ...

我试试,多谢了啊哈
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-19 20:37 , Processed in 0.048036 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表