gpt4 book ai didi

c - Timer a在msp430中高级编译优化模式下的使用

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:33 25 4
gpt4 key购买 nike

我在具有高编译器优化的 MSP430 中使用了定时器 A,但发现在使用高编译器优化时我的定时器代码失败了。当未使用任何优化时,代码工作正常。

此代码用于实现1 ms timer tick。 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,使其在优化模式下工作正常?还是我用错了实现1ms中断?

最佳答案

是否在中断处理程序中修改了任何变量(例如 timeOutCNT)?

如果是这样,请确保将它们声明为volatile,例如

volatile int timeOutCNT;

这可以防止编译器进行假设 timeOutCNT 未被中断处理程序或其他线程修改的优化。

关于c - Timer a在msp430中高级编译优化模式下的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3031533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com