定时器a在高编译优化模式下在msp430中的使用

发布于 2024-09-05 23:34:47 字数 1766 浏览 4 评论 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技术交流群

发布评论

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

评论(1

初懵 2024-09-12 23:34:47

您的变量(例如 timeOutCNT)是否在中断处理程序中被修改?

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

volatile int timeOutCNT;

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

Are any of your variables (e.g. timeOutCNT) modified in an interrupt handler?

If so, ensure that you declare them as volatile, e.g.

volatile int timeOutCNT;

This prevents the compiler from making optimisations that assume that timeOutCNT is not modified by interrupt handlers or other threads.

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