This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553 DCO CLOCK FREQUENCY VARIES WITH FOR LOOP

Hi. I'm trying to blink led with 1second frequency using SMCLK sourcing from DCO. I set clock frequency to 1Mhz. When i set the CCRO=50000 and interrupt counter(led_counter)  to 20 the led blinks wiith a faster frequency(faster than 1 second). If i change CCR0=25000 and interrupt counter to 40 the blinks significantly slower (approx 2 second) than the previous situation. Again with CCR0=12500 and interrupt counter = 80 blinking rate falls nearly 3-4 second.

I couldn't find out where the fault is. Can someone explain this problem ?

include <msp430.h>
volatile int led_counter=0; // Interrupt counter

void main(void)
{
   WDTCTL = WDTPW | WDTHOLD; .
   BCSCTL1 = CALBC1_1MHZ; 
   DCOCTL = CALDCO_1MHZ; 
   P1DIR |= BIT0;                        // Port1.0 output.
   CCTL0 = CCIE; 
   CCR0 = 50000; // 
   TACTL = TASSEL_2 + MC_1; 
   _BIS_SR(LPM0_bits + GIE);
   while(1);
   return 0;
}

#pragma vector=TIMER0_A0_VECTOR 
__interrupt void timer_isr(void)
{
     led_counter++;    
     if(led_counter==20)
     {
         P1OUT ^= BIT0; // Port1.0'ı tersle.
         led_counter = 0; 
     } 
     CCR0 += 50000; 
}

  • You're mixing two methods here:

    1) Use Up mode (MC_1) and don't increment CCR0

    2) Use Continuous mode (MC_2) and increment CCR0 on each interrupt

    (1) is usually simpler, but sometimes you have to use (2).

    I suggest picking one or the other: either (1) remove the CCR0 increment in the ISR or (2) switch to MC_2.

  • I agree with Bruce,

    you are mixing things up. Up-mode and incrementing will give you another period every time. Remember the interrupt occurs when TAR hits CCR.

    If you set CCR to 50000, then it will take 50ms until TAR hits CCR (when TAR starts from zero). Then you add another 50000. CCR is 16 bit so it rolls over at 65535 which leads to CCR being set to 34464. TAR was set to zero when hitting CCR and it takes one clock cycle to set it to zero, so the next interrupt would appear in (1 / 1000000) * 34465 = 34.465ms and the next interrupt will be another period again and so on.

    Using the continuous mode and incrementing the CCR value in the ISR (you should do this before anything else) with 50000 will work, since you always set the next hit 50000 cycles away from the actual TAR again.

    Dennis

  • Thank you all for your advices and explanations. I tried (1) in Bruce's answer and my code worked.

**Attention** This is a public forum