当 IAR 配置为进行最大优化时,我的计时器代码失败
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TIMER_A_TACTL
和其他易失性
吗?如果不是,编译器可能会重新排序或组合读取和写入,并假设这些没有副作用。您应该能够通过在适当的位置引入 障碍 或将这些变量声明为来解决
易变
Are
TIMER_A_TACTL
and othersvolatile
? 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
查看列表文件输出,看看汇编器输出是否包含该行的输出
如果您使用的是 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
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
asvolatile
. If you forget this then the optimiser will assume that the while statement will reduce towhile (1) ;
我无法具体评论您的计时器代码,但我总体上看到了类似的问题。当使用高级别的优化时,代码会在不同的地方以看似不相关的方式被破坏。我们最终将其归因于编译器错误,然后完全禁用优化。
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.