【问题标题】:Loop condition does not stop the program循环条件不会停止程序
【发布时间】:2021-04-07 08:44:01
【问题描述】:
void initTimer (void);
void delay (unsigned long milli);

unsigned int counter;
unsigned int zero =  0b0000000000000000;
unsigned int one =   0b0000000001000000;   

int main (void)
{
    initTimer();

    TRISB = 0;

    LATB = 0;

   for (counter = 0; counter < 10; counter++) {
       LATB = zero;
       delay (SHORT_DELAY);
     
       LATB = one;
       delay (SHORT_DELAY);
      
   }
       while (1);
}

循环中的代码即使在计数器超过 10 后也会执行。LED 继续闪烁。所有变量都被声明,所有数字变量都是二进制的,不确定这是否意味着什么。

【问题讨论】:

  • 因为您每次都从for(;;) 循环中一遍又一遍地调用该函数?发布整个代码。
  • 请尝试创建minimal reproducible example 向我们展示。
  • 缺少 } 结尾的 main。有没有你没有显示的代码?目前的代码是否仍然会导致问题?
  • 你是否关闭了看门狗定时器?
  • @Mike 你应该把这些 cmets 总结在一个答案中,这很可能是问题所在。

标签: c embedded mplab


【解决方案1】:

您必须在配置中关闭看门狗定时器,例如:

__CONFIG _WDT_OFF

否则程序将始终重新启动并通过您的 for 循环再次运行

【讨论】:

    猜你喜欢
    • 2014-05-06
    • 1970-01-01
    • 2019-01-29
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多