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

发布于 2024-09-05 15:33:47 字数 1556 浏览 2 评论 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 ms 中断?

I have used timer A in MSP430 with high compiler optimization, but found that my timer code is failing when high compiler optimization used.
When none optimization is used code works fine.

This code is used to achieve 1 ms timer tick. timeOutCNT is increamented in interrupt.

Following is the code

   //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

Can anybody help me here to resolve this issue? Is there any other way we can use timer A so it works fine in optimization modes? Or do I have used is wrongly to achieve 1 ms interrupt?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角迷惘 2024-09-12 15:33:47

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

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

Are TIMER_A_TACTL and others volatile? If not, the compiler may reorder or combine reads and writes, under the assumption that these have no side effects.

You should be able to solve by either introducing barriers at appropriate positions, or by declaring these variables as volatile

桜花祭 2024-09-12 15:33:47

查看列表文件输出,看看汇编器输出是否包含该行的输出

while(timeOutCNT <= 1);

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

我怀疑 Hasturkun 位于正确的行,因为您可能没有将 timeOutCNT 声明为 <代码>易失性。如果您忘记了这一点,那么优化器将假设 while 语句将减少为

while (1) ;

Have a look at the list file output to see whether the assembler output contains output for the line

while(timeOutCNT <= 1);

If you are using the kickstart version of the compiler then the list file will not contain the assembler listing and you should load the code into C-Spy and look at the disassembler listing there.

I suspect that Hasturkun is on the right lines in that you have probably not declared timeOutCNT as volatile. If you forget this then the optimiser will assume that the while statement will reduce to

while (1) ;

递刀给你 2024-09-12 15:33:47

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

I can't comment specifically on your timer code, but I saw a similar problem in general. When high levels of optimization were used, code would break in various places in seemingly unrelated ways. We ultimately chalked it up to compiler bugs then disabled optimization completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文