OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 8491|回复: 6

串口printf出一长串冗余的数字

[复制链接]

1

主题

6

帖子

0

精华

新手上路

积分
30
金钱
30
注册时间
2014-8-25
在线时间
0 小时
发表于 2014-10-13 21:38:09 | 显示全部楼层 |阅读模式
5金钱
定义一个最大数组,按键按下 ,系统进入USART1接收中断接收MPU6050数据,释放,USART6通过串口模块在串口助手打印数据,结果只有第二次按下按键并松开时才能printf出正常的数据,每次复位后的第一次按键操作都会printf出一堆冗余的数字(每次的冗余数据好像都是一样的)。有人见过类似的情况吗? 
万分感谢~

下面的数据都是冗余的数据
0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  -35027851575571492000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -7143479014884154400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  0.000000  0.000000  0.000000  -3214208162333086000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  0.000000  -2101101478197202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -16992370489940172000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -97128321627121392000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -687509828315948880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  -0.000107  -50165882999059984000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -3294898827850357600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -0.000000  9463522718676524800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -16921942405281130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  28712007706444560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -12879792785683522000000000000000000000000000000000000000000000000000000000000000000.000000  -506852412323269920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -500823466265924480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  0.000000  -980518762077523840000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -1572240958923033400000000000000000000000000000000000000000000000000000000000000.000000  -3741545358609749600000000000000000000000000000000000000000000000000000000000000.000000  -745716349173255040000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  -3135653458042396400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -0.000000  0.000000  113179680331128480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -0.000000  -0.000000  -3174967742764795200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -5164215306530506400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  -3111022594187276400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  220204601932743880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  0.000000  -5500075960908372000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  13762799703262644000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -802656383858611360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -1013529414354822600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  -548417660673516880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -619245503665048880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  13548594133066222.000000  0.000000  -548417634510103120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -300244939643826240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  -878269505182414080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  0.000000  0.000000  0.000000  1688584158288991600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000  0.000000  -22913888023114176000000000000000000000000000000000000000000000000000000000000000000.000000  651562659803798000000000000000.000000 

datapro_BETA.7z

2.39 MB, 下载次数: 559

最佳答案

查看完整内容[请看2#楼]

哦   找到问题了     貌似是用了emx_Array_real_T这个结构体的原因    换成普通的double型数组就可以    但是不知道为什么 结构体是这样定义的: emxArray_real_T {     double *data;     int *size;     int allocatedSize;     int nu ...
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
30
金钱
30
注册时间
2014-8-25
在线时间
0 小时
 楼主| 发表于 2014-10-13 21:38:10 | 显示全部楼层
哦   找到问题了     貌似是用了emx_Array_real_T这个结构体的原因    换成普通的double型数组就可以    但是不知道为什么

结构体是这样定义的:
emxArray_real_T
{
    double *data;
    int *size;
    int allocatedSize;
    int numDimensions;
    boolean_T canFreeData;
};

初始化函数:
void emxInit_real_T(emxArray_real_T **pEmxArray, int numDimensions)
{
  emxArray_real_T *emxArray;
  int i;
  *pEmxArray = (emxArray_real_T *)malloc(sizeof(emxArray_real_T));
  emxArray = *pEmxArray;
  emxArray->data = (double *)NULL;
  emxArray->numDimensions = numDimensions;
  emxArray->size = (int *)malloc((unsigned int)(sizeof(int) * numDimensions));
  emxArray->allocatedSize = 0;
  emxArray->canFreeData = true;
  for (i = 0; i < numDimensions; i++) 
{
    emxArray->size = 0;
  }
}

分配大小函数:
emxArray_real_T *emxCreate_real_T(int rows, int cols)
{
  emxArray_real_T *emx;
  int size[2];
  int numEl;
  int i;
  size[0] = rows;
  size[1] = cols;
  emxInit_real_T(&emx, 2);
  numEl = 1;
  for (i = 0; i < 2; i++) {
    numEl *= size;
    emx->size = size;
  }

  emx->data = (double *)calloc((unsigned int)numEl, sizeof(double));
  emx->numDimensions = 2;
  emx->allocatedSize = numEl;
  return emx;
}

emxInit_real_T(&struct_a,2);  
struct_a=emxCreate_real_T(600,3); 

请问这样初始化然后分配大小之后的struct_a->data跟普通的double型数组有什么不一样吗
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2014-10-13 23:09:19 | 显示全部楼层
估计是你使用printf格式有问题。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
30
金钱
30
注册时间
2014-8-25
在线时间
0 小时
 楼主| 发表于 2014-10-14 10:34:36 | 显示全部楼层
谢谢原子哥        

 格式问题?

struct_a->data是个600*3的二维数组   

for(i0=0;i0<loop_ub-1;i0++)
{
for(i=0;i<3;i++)
printf("%f  ",struct_a->data[i+i0*3]);
}  
或者
for(i=0;i<3;i++)
{
     for(i0=0;i0<loop_ub-1;i0++)
       printf("%f  ",struct_a->data[i+i0*3]);
}
结果一样呢


然后printf是用microlib的
 
int fputc(int ch, FILE *f)
{
USART_SendData(USART6, (uint8_t) ch);

while (USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET) {}
   
    return ch;
}
int GetKey (void)  { 

    while (!(USART1->SR & USART_FLAG_RXNE));

    return ((int)(USART1->DR & 0x1FF));
}
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2014-10-14 23:23:26 | 显示全部楼层
回复【4楼】ALCHEMIST:
---------------------------------
地址一定要4字节对齐!!
估计你分配给data的内存没有4字节对齐,导致这个问题。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
30
金钱
30
注册时间
2014-8-25
在线时间
0 小时
 楼主| 发表于 2014-10-16 10:19:40 | 显示全部楼层
 vc里面不是自动对齐的吗?   keil的编译器不一样吗?          我尝试着将data换成小一点的3*1或3*10然后再用动态内存分配   一开始倒是有地址了  一接收数据之后地址就变为0x00000000了       
struct emxArray_real_T
{
    double *data;
    int *size;
    int allocatedSize;
    int numDimensions;
    boolean_T canFreeData;
};
结构体是这样定义的   明明*size也是用emxArray->size = (int *)malloc((unsigned int)(sizeof(int) * numDimensions));分配成功了   
double *data用emx->data = (double *)calloc((unsigned int)numEl, sizeof(double));直接分配3*600的话地址就直接是0x00000000(难道一开始不能分配这么大)   就算像我前面用3*10可以分配地址了  但一接收数据又变成0x00000000了。。

42.PNG
32.PNG
回复

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
30
金钱
30
注册时间
2014-8-25
在线时间
0 小时
 楼主| 发表于 2014-10-16 10:20:30 | 显示全部楼层
截图42是接收数据之后的   32是接收数据之前的
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-23 04:56

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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