【问题标题】:Keil - my while loop doesn't not workKeil - 我的 while 循环不起作用
【发布时间】:2016-08-20 10:39:32
【问题描述】:

我在 Keil 上使用 STM32F 微控制器。我在whilefor 循环上有问题。共享代码是我的错误部分。我的 for 或 while 循环不起作用。我停留在“step = 2”和“counter = 0”。我尝试了发布和调试模式。调试模式我看到了这个结果监视屏幕; step = 1 (WaitData = 1) after systemtick 在那个 systemtick = 5000 after that step = 2 (systemtick = 0 waitdata = 0) 之后增加,但代码堆栈在 for 循环上。

#include "stm32f4xx_hal.h"
#include <stdio.h>
#include <stdlib.h>

int step = 0;
int waitdata = 0;
int systemtick1 = 0;
int counter = 0;
void HAL_SYSTICK_Callback(void)
{
    if (WaitData == 1)
    {
        systemtick1++;
        if (systemtick1 == 5000)
        {
            step = 2;
            systemtick1 = 0;
            WaitData = 0;
        }
    }
}

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    HAL_Delay(2000);
    step = 1;
    WaitData = 1;

    for (; WaitData==1 ; ) // Or while (WaitData == 1);
    {
        asc++;
    }

    step = 3;
    while (1)
    {
    }
}

【问题讨论】:

标签: while-loop arm keil


【解决方案1】:

你在中断中改变的变量需要设置为volatile,因为你不知道编译器会如何优化它。

对变量的访问大多是按顺序的、可预测的方式完成的。但是中断是不可预测的。因此,您需要使用volatile 才能在正常“程序”流程之外更改变量。

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 2018-06-12
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多