0

我在 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毫秒的中断?

4

3 回答 3

1

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

您应该能够通过在适当位置引入障碍或将这些变量声明为volatile

于 2010-06-14T14:44:30.763 回答
1

查看列表文件输出以查看汇编程序输出是否包含该行的输出

而(timeOutCNT <= 1);

如果您使用的是编译器的 kickstart 版本,那么列表文件将不包含汇编程序列表,您应该将代码加载到 C-Spy 并查看那里的反汇编程序列表。

我怀疑Hasturkun是正确的,因为您可能没有声明timeOutCNTvolatile. 如果您忘记了这一点,那么优化器将假定 while 语句将减少到

while (1) ;

于 2010-06-14T15:00:34.957 回答
0

我无法具体评论您的计时器代码,但我看到了一个类似的问题。当使用高级别的优化时,代码会以看似不相关的方式在不同的地方中断。我们最终将其归结为编译器错误,然后完全禁用了优化。

于 2010-06-14T14:30:46.620 回答