2010-06-13 43 views
0

我已经使用MSP430中的定时器A进行高编译器优化,但发现我的定时器代码在使用高编译器优化时失败。 当没有使用优化代码工作正常。在高编译器优化模式下定时器在msp430中的使用

该代码用于实现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毫秒中断?

回答

5

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

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

volatile int timeOutCNT; 

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

+0

我相信不同的编译器即使在高优化的情况下也会有不同的'volatile'。 – nategoose 2010-06-13 14:33:24

+0

嗨Artelius, 感谢您的解决方案,这对我工作。 Vishal N – Vishal 2010-06-15 04:07:00