keil5 stm32 跳不出while循环解决过程及方法

问题现象:

在主函数main中,有等待某一状态变量改变的while循环,在调试模式下观测该状态已经发生了改变,但while判断就是过不去,在单步调试下尝试,判断是可以过去的。而全速运行过不去。

解决过程:

首先要确定程序是死在哪里了,最好的办法就是看汇编代码:view->Disassembly windows

发现程序死在了:CMP      r0,#0x00

这里的r0是cpu寄存器,其值一直为0,没有把该状态变量的值取到r0寄存器。

那么为什么没把该值存入r0寄存器呢,调试时,程序一直执行CMP命令,没有执行取值的命令,所以问题的原因就是编译器在编译时认为该状态变量只会在main函数中使用,所以只取了一次状态值到r0,然后状态值发生了变化,主函数却未能感知到,所以才出现这个问题。

印证我的猜想:在while中调用一个hal_delay(1);函数,那么当调用之后再判断时,就会再取一次状态值进行CMP。

下面就开始解决编译器的编译优化问题,走了很多弯路,改下面的配置,试了很多次,快绝望的时候,把optimization中的Level 3改为Level 0,测试后

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Version: 2.15.0 (2020-09-28) Keil.STM32F4xx_DFP.2.15.0.pack Download Updated Pack to STM32Cube_FW_F4 Firmware Package version V1.25.1 using HAL Drivers V1.7.9. STM32CubeMX integration (Version 6.0.1): Added support for Timebase Source TIMx (FrameworkCubeMX_gpdsc.ftl). Removed non-existent include path. CMSIS Flash Algorithm: Corrected STM32F42xxx_43xxx_OPT Algorithm. CMSIS SVD: Updated STM32F42*.svd, STM32F43*.svd files. CMSIS-Driver: I2C: Corrected 2 byte reception in master mode. MCI: Replaced empty delay loops with _NOP(). SPI: Corrected PowerControl function (to return error if Initialize was not called, to abort active transfer if power off was requested). Updated GetDataCount function to give accurate count in DMA mode. Corrected Control function (abort in DMA mode, software controlled slave select in slave mode, TI Frame Format selection, ignore bus speed for slave mode). Corrected Uninitialize function (to power off the peripheral if it is powered). Corrected SPI3_SCK pin configuration. Corrected DMA MemDataAlignment configuration. USART: Corrected DMA MemDataAlignment configuration. USBD_HS/USBH_HS: OTG_HS ULPI clock disabled in low power if internal PHY is used to enable proper operation of OTG_HS port in FS mode during CPU sleep. CAN/EMAC/USBD/USBH: Removed macros already provided by cmsis_compiler.h. Updated Boards Examples: Migrated CubeMX projects to V6.0.1 and updated config files. Changed variant selection to "MDK-Plus" where possible. Updated all USB Host/Device examples with user templates from MDK-Middleware v7.11.1. Terminating app_main thread with osThreadExit() to avoid endless loop Updated MS Windows UBS driver files.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值