I am working with a STM32F030R8T6 and the HAL libraries. I use the STM32CubeMX for all the initialization code.
我正在使用STM32F030R8T6和HAL库。我使用STM32CubeMX作为所有初始化代码。
When I use a "if" statement inside the infinite loop it does not work.
Eg.
当我在无限循环中使用“if”语句时,它不起作用。例.
while (1) {
if ((Seconds - oldSec) >= 10) {
printf("Entramos 10sec\r\n");
oldSec = Seconds;
}
}
but if a use a HAL_Delay() function it works eg.
但如果A使用HAL_Delay()函数,则它可以工作,例如。
while (1) {
HAL_Delay(1);
if ((Seconds - oldSec) >= 10) {
printf("Entramos 10sec\r\n");
oldSec = Seconds;
}
}
I do not know, what is the problem?
我不知道,有什么问题吗?
I use the arm-gcc compiler with makefile.
我使用带有Makefile的ARM-GCC编译器。
更多回答
优秀答案推荐
CubeMX provides optimization level 3 by default and this causes problems if variables are not volatile but changed outside context (like in interrupts).
CubeMX默认情况下提供优化级别3,如果变量不是易失性的,而是在外部环境(如在中断中)更改的,则会出现问题。
Solution is to have Seconds
and oldSeconds
as volatile
and make sure Seconds
is increased in SysTick_IRQHandler
or in HAL_SYSTICK_Callback
functions.
解决方案是将Seconds和oldSeconds设置为volatile,并确保Seconds在SysTick_IRQ中或HAL_SYSTICK_Callback函数中增加。
Solution is also to set optimizations to level 0. But as you can see what can happen if you don't follow volatile
rule, it is better to stay at level 3.
解决方案也是将优化设置为0级。但正如你所看到的,如果你不遵循不稳定的规则,会发生什么,最好是保持在3级。
I had that problem for many times, then I realized that, if there is a single if clause in while loop problem occurs. I changed optimization level to 0 on Keil, then problem gone.
我多次遇到这个问题,然后我意识到,如果While循环中有一个If子句,就会出现问题。我在Keil上将优化级别更改为0,然后问题就消失了。
更多回答
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。
我是一名优秀的程序员,十分优秀!