[STM32F1] 程序跑飞的问题

[复制链接]
3313|42
 楼主| 比神乐 发表于 2020-8-25 16:31 | 显示全部楼层 |阅读模式
我用STM32F103VCT6写程序。
有一下一段代码:
  1. void Window0_Wy(void)
  2. {
  3.         char s[8],j,i=0;
  4.         float ShiftValue0;                                                //位移值

  5.         switch(SP_Read.WyBdDs.Uint)
  6.         {
  7.                 case 1:
  8.                         if(Encoder.Uint<SP_Read.Encoder_Sample[0].Uint)
  9.                                 ShiftValue0=0.0;
  10.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[1].Uint)
  11.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[0].Uint)) * \
  12.                                                                 SP_Read.Encoder_Xsh1[0].Float;
  13.                         else
  14.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[1].Uint)) * \
  15.                                                                 SP_Read.Encoder_Xsh1[0].Float+(float)(SP_Read.WyBzz[0].Uint);
  16.                         break;
  17.                 case 2:
  18.                         if(Encoder.Uint<SP_Read.Encoder_Sample[0].Uint)
  19.                                 ShiftValue0=0.0;
  20.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[1].Uint)
  21.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[0].Uint)) * \
  22.                                                                 SP_Read.Encoder_Xsh1[0].Float;
  23.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[2].Uint)
  24.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[1].Uint)) * \
  25.                                                                 SP_Read.Encoder_Xsh1[1].Float+((float)SP_Read.WyBzz[0].Uint);
  26.                         else
  27.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[2].Uint)) * \
  28.                                                                 SP_Read.Encoder_Xsh1[1].Float+((float)SP_Read.WyBzz[1].Uint);
  29.                         break;
  30.                 case 3:
  31.                         if(Encoder.Uint<SP_Read.Encoder_Sample[0].Uint)
  32.                                 ShiftValue0=0.0f;
  33.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[1].Uint)
  34.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[0].Uint)) * \
  35.                                                                 SP_Read.Encoder_Xsh1[0].Float;
  36.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[2].Uint)
  37.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[1].Uint)) * \
  38.                                                                 SP_Read.Encoder_Xsh1[1].Float+((float)SP_Read.WyBzz[0].Uint);
  39.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[3].Uint)
  40.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[2].Uint)) * \
  41.                                                                 SP_Read.Encoder_Xsh1[2].Float+((float)SP_Read.WyBzz[1].Uint);
  42.                         else
  43.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[3].Uint)) * \
  44.                                                                 SP_Read.Encoder_Xsh1[2].Float+((float)SP_Read.WyBzz[2].Uint);
  45.                         break;
  46.                 case 4:
  47.                         if(Encoder.Uint<SP_Read.Encoder_Sample[0].Uint)
  48.                                 ShiftValue0=0.0f;
  49.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[1].Uint)
  50.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[0].Uint)) * \
  51.                                                                 SP_Read.Encoder_Xsh1[0].Float;
  52.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[2].Uint)
  53.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[1].Uint)) * \
  54.                                                                 SP_Read.Encoder_Xsh1[1].Float+((float)SP_Read.WyBzz[0].Uint);
  55.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[3].Uint)
  56.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[2].Uint)) * \
  57.                                                                 SP_Read.Encoder_Xsh1[2].Float+((float)SP_Read.WyBzz[1].Uint);
  58.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[4].Uint)
  59.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[3].Uint)) * \
  60.                                                                 SP_Read.Encoder_Xsh1[3].Float+((float)SP_Read.WyBzz[2].Uint);
  61.                         else
  62.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[4].Uint)) * \
  63.                                                                 SP_Read.Encoder_Xsh1[3].Float+((float)SP_Read.WyBzz[3].Uint);
  64.                         break;
  65.                 case 5:
  66.                         if(Encoder.Uint<SP_Read.Encoder_Sample[0].Uint)
  67.                                 ShiftValue0=0.0f;
  68.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[1].Uint)
  69.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[0].Uint)) * \
  70.                                                                 SP_Read.Encoder_Xsh1[0].Float;
  71.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[2].Uint)
  72.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[1].Uint)) * \
  73.                                                                 SP_Read.Encoder_Xsh1[1].Float+((float)SP_Read.WyBzz[0].Uint);
  74.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[3].Uint)
  75.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[2].Uint)) * \
  76.                                                                 SP_Read.Encoder_Xsh1[2].Float+((float)SP_Read.WyBzz[1].Uint);
  77.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[4].Uint)
  78.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[3].Uint)) * \
  79.                                                                 SP_Read.Encoder_Xsh1[3].Float+((float)SP_Read.WyBzz[2].Uint);
  80.                         else if(Encoder.Uint<SP_Read.Encoder_Sample[5].Uint)
  81.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[4].Uint)) * \
  82.                                                                 SP_Read.Encoder_Xsh1[4].Float+((float)SP_Read.WyBzz[3].Uint);
  83.                         else
  84.                                 ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[5].Uint)) * \
  85.                                                                 SP_Read.Encoder_Xsh1[4].Float+((float)SP_Read.WyBzz[4].Uint);
  86.                         break;
  87.                 default :
  88.                         break;
  89.         }
  90.        
  91.         sprintf(s, "%-10.2f", ShiftValue0); //产生:" 123 4567"
  92.        
  93.         for(j=0;j<6;j++)
  94.         {
  95.                 if(s[j]==0x20)
  96.                         s[j]=0;
  97.                 else if(s[j]==0x2d)
  98.                         s[j]=13;
  99.                 else if(s[j]==0x2e)
  100.                 {
  101.                         s[j]=14;
  102.                         i=j;
  103.                 }
  104.                 else
  105.                         s[j]-=0x30;
  106.         }
  107.         for(j=0;j<=(i+2);j++)
  108.         {
  109.                 dispchn4(196+j*6,40,&ALB_NUM[s[j]][0]);
  110.         }
  111.         for(j=(i+3);j<6;j++)
  112.                 dispchn5(196+j*6,40);


  113.        


  114.        
  115. }
这段代码会跑飞。
其中:
  1. case 5:
  2. if(Encoder.Uint<SP_Read.Encoder_Sample[0].Uint)
  3. ShiftValue0=0.0f;
  4. else if(Encoder.Uint<SP_Read.Encoder_Sample[1].Uint)
  5. ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[0].Uint)) * \
  6. SP_Read.Encoder_Xsh1[0].Float;
  7. else if(Encoder.Uint<SP_Read.Encoder_Sample[2].Uint)
  8. ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[1].Uint)) * \
  9. SP_Read.Encoder_Xsh1[1].Float+((float)SP_Read.WyBzz[0].Uint);
  10. else if(Encoder.Uint<SP_Read.Encoder_Sample[3].Uint)
  11. ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[2].Uint)) * \
  12. SP_Read.Encoder_Xsh1[2].Float+((float)SP_Read.WyBzz[1].Uint);
  13. else if(Encoder.Uint<SP_Read.Encoder_Sample[4].Uint)
  14. ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[3].Uint)) * \
  15. SP_Read.Encoder_Xsh1[3].Float+((float)SP_Read.WyBzz[2].Uint);
  16. else if(Encoder.Uint<SP_Read.Encoder_Sample[5].Uint)
  17. ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[4].Uint)) * \
  18. SP_Read.Encoder_Xsh1[4].Float+((float)SP_Read.WyBzz[3].Uint);
  19. else
  20. ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[5].Uint)) * \
  21. SP_Read.Encoder_Xsh1[4].Float+((float)SP_Read.WyBzz[4].Uint);
  22. break;
  23. default :
  24. break;
  25. }
把这段注释掉,就不跑飞了,加上就不行。
大神看看,哪里有问题。谢谢
LcwSwust 发表于 2020-8-25 16:42 | 显示全部楼层
看得眼花,看是不是数组越界了。
 楼主| 比神乐 发表于 2020-8-25 17:01 | 显示全部楼层
LcwSwust 发表于 2020-8-25 16:42
看得眼花,看是不是数组越界了。

检查了,没有越界。
 楼主| 比神乐 发表于 2020-8-25 17:11 | 显示全部楼层
  1. //                        else if(Encoder.Uint<SP_Read.Encoder_Sample[4].Uint)
  2. //                                ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[3].Uint)) * \
  3. //                                                                SP_Read.Encoder_Xsh1[3].Float+((float)SP_Read.WyBzz[2].Uint);
  4. //                        else if(Encoder.Uint<SP_Read.Encoder_Sample[5].Uint)
  5. //                                ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[4].Uint)) * \
  6. //                                                                SP_Read.Encoder_Xsh1[4].Float+((float)SP_Read.WyBzz[3].Uint);
  7. //                        else
  8. //                                ShiftValue0=((float)(Encoder.Uint-SP_Read.Encoder_Sample[5].Uint)) * \
  9. //                                                                SP_Read.Encoder_Xsh1[4].Float+((float)SP_Read.WyBzz[4].Uint);
又测试了一下,把上面这几句注释掉没问题。加上就不行,好奇怪
 楼主| 比神乐 发表于 2020-8-26 08:24 | 显示全部楼层
1.jpg
跑到这里面来了
sonicll 发表于 2020-8-26 11:01 | 显示全部楼层
进hardfault以后,看一下内核的SP值是多少,然后把这个SP值放进内存窗口查看,可以看到栈里的内容,然后向下找偏移第6个32位字的值,这个值就是进hardfault中断时,压进栈里的PC值,也是代码出错的地址
 楼主| 比神乐 发表于 2020-8-26 13:50 | 显示全部楼层
sonicll 发表于 2020-8-26 11:01
进hardfault以后,看一下内核的SP值是多少,然后把这个SP值放进内存窗口查看,可以看到栈里的内容,然后向 ...

谢谢!内核的SP值如何查看?
 楼主| 比神乐 发表于 2020-8-26 13:52 | 显示全部楼层
sonicll 发表于 2020-8-26 11:01
进hardfault以后,看一下内核的SP值是多少,然后把这个SP值放进内存窗口查看,可以看到栈里的内容,然后向 ...

3.jpg
是那个MSP吗?
 楼主| 比神乐 发表于 2020-8-26 13:57 | 显示全部楼层
4.jpg
哪个是那个地址啊
sonicll 发表于 2020-8-26 13:58 | 显示全部楼层

对,就是msp,在memory窗口输入地址0x200064D0,格式选择unisgned int
 楼主| 比神乐 发表于 2020-8-26 13:59 | 显示全部楼层
5.jpg
你帮我看看,谢谢
 楼主| 比神乐 发表于 2020-8-26 14:01 | 显示全部楼层
是080279AB吗?找到这个地址再怎么办呀?
ayb_ice 发表于 2020-8-26 14:09 | 显示全部楼层
char s[8],搞大些
sonicll 发表于 2020-8-26 14:09 | 显示全部楼层
在反汇编代码里找0x08002020这个位置,应该就是这里的指令出现了错误
岛屿失梦 发表于 2020-8-26 14:11 | 显示全部楼层
@一下版主问问看吧?
 楼主| 比神乐 发表于 2020-8-26 14:28 | 显示全部楼层
sonicll 发表于 2020-8-26 14:09
在反汇编代码里找0x08002020这个位置,应该就是这里的指令出现了错误

9.jpg
这个根本看不懂
sonicll 发表于 2020-8-26 14:43 | 显示全部楼层

太诡异了,按道理这个地址应该放的是指令,可你这图里看到的却是文字池之类的数据,不是指令
 楼主| 比神乐 发表于 2020-8-26 15:06 | 显示全部楼层

谢谢!我把s[8]改为s[18]就好了,可是我不懂为什么就好了。
你给我讲讲
ayb_ice 发表于 2020-8-26 15:09 | 显示全部楼层
比神乐 发表于 2020-8-26 15:06
谢谢!我把s[8]改为s[18]就好了,可是我不懂为什么就好了。
你给我讲讲

就是越界了而已
sonicll 发表于 2020-8-26 15:10 | 显示全部楼层
本帖最后由 sonicll 于 2020-8-26 15:14 编辑

因为 sprintf(s, "%-10.2f", ShiftValue0),10位长度2位小数,ShiftValue0的整数部分很大的话,这个格式化字符串的长度就超过8字节了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部