c - 当 IAR 配置为进行最大优化时,我的计时器代码失败

标签 c timer msp430

我在 MSP430 中使用了高度编译器优化的定时器 A,但发现当使用高度编译器优化时我的定时器代码失败。 当不使用优化时,代码可以正常工作。

此代码用于实现 1 ms 定时器滴答。 timeOutCNT 在中断中增加。

下面是代码

   //Disable interrupt and clear CCR0
   TIMER_A_TACTL = TIMER_A_TASSEL |                       // set the clock source as SMCLK
    TIMER_A_ID |                           // set the divider to 8
    TACLR |                                // clear the timer
    MC_1;      // continuous mode
   TIMER_A_TACTL &= ~TIMER_A_TAIE;                        // timer interrupt disabled
   TIMER_A_TACTL &= 0;                        // timer interrupt flag disabled

   CCTL0 = CCIE;                               // CCR0 interrupt enabled
   CCR0 = 500;
   TIMER_A_TACTL &= TIMER_A_TAIE;    //enable timer interrupt
   TIMER_A_TACTL &= TIMER_A_TAIFG;    //enable timer interrupt
   TACTL = TIMER_A_TASSEL + MC_1 + ID_3;                   // SMCLK, upmode

   timeOutCNT = 0;

   //timeOutCNT is increased in timer interrupt
   while(timeOutCNT <= 1); //delay of 1 milisecond 

   TIMER_A_TACTL = TIMER_A_TASSEL |                       // set the clock source as SMCLK
   TIMER_A_ID |                             // set the divider to 8
   TACLR |                                  // clear the timer
   MC_1;        // continuous mode
   TIMER_A_TACTL &= ~TIMER_A_TAIE;                        // timer interrupt disabled
   TIMER_A_TACTL &= 0x00;                        // timer interrupt flag disabled

有人可以帮我解决这个问题吗?我们是否可以通过其他方式使用计时器 A,使其在优化模式下正常工作?还是我错误地使用了 1 ms 中断?

最佳答案

TIMER_A_TACTL 和其他 volatile 吗?如果不是,编译器可能会重新排序或组合读取和写入,假设这些没有副作用。

您应该能够通过引入 barriers 来解决在适当的位置,或者通过将这些变量声明为 volatile

关于c - 当 IAR 配置为进行最大优化时,我的计时器代码失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3031435/

相关文章:

c - 从 C 中的十六进制字符串获取 uint

c - 使用组播 UDP 的 C 网络记录器?

Javascript同步-等待setTimeOut完成,如果可能的话不回调

iOS:如何使 swift 3 中事件中的每个计时器失效或终止?

java - swing GUI 组件的计时器问题

c - 跳过模拟器中的指令

c - MSP430 (msp430-gcc) 链接器 undefined reference

c - 液晶屏坏了

c - 指向指针的指针和指向二维数组的指针之间的区别

c - 需要帮助重组循环/让简单的平均代码正常工作。