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.

Simple Timer/Counter question



Hello guys; just need verification on my assumption about this code:

This is taken from John Davies' MSP book:

TACCR0 = 49999;

TACCTL0 = CCIE;

TACTL = MC_1|ID_3|TASSEL_2|TACLR;

__enable _interrupt();

for(;;){

}

//-----------------

#pragma vector = TIMERA0_VECTOR

__interrupt void TAO_ISR(void)

{

    P2OUT ^= LED1|LED2;

}

 

In the description it says "toggles LEDs with period of about 1s. Toggle LEDs in ISR using interrupts from timer A CCR0 in Up mode with period of about .5s."

 

My question is the period .5s  because the default of the SMCLK is 1Mhz and since we choose SMCLK divided by 8 with the code, it's 1000000/8 = 125000. Since the counter is counting 49999 cycles it's 49999/125000 = .39s.

Is that why the comment is saying "about .5s period"?

I don't want to make any assumptions.

 

Thanks,

 

 

 

 

  • Brock,

    your analysis is correct.

    Now assuming the SMCLK is with the default setting, running @ 1MHz

    TimerA is configured in Up mode (counts to TACCR0 = 49999), TimerA effective clock  = SMCLK / 8 (assuming default SMCLK = 1MHz) = 125kHz.

    Therefore interrupt is triggered every 49999/125000 ~= .39s. Quite different from .5s. If my goal were to have a more precise .5s period, I'd go with TACCR0 = 612500 (=125000/2).Not sure why 49999 was chosen.

    Could you double check what the actual frequency of the SMCLK is? Maybe that holds the key to why the 50000 # was selected.

     

    Regards,

    Dung

     

  • If the SMCLK is exactly 1.000000MHz and you want the interrupt interval to be exactly every 0.500000 second apart, then you should set:

    TACCR0 = 62499;

    But the SMCLK is only ABOUT 1MHz and you only want the interrupt interval to be ABOUT every 0.5 second, you could set:

    TACCR0 = ABOUT 62499;

    But the compiler will not like it. The question is, do you consider 49999 to be ABOUT 62499?

    By the way, do you know why I said 62499 instead of 62500? And why that book uses 49999 instead of 50000?

    Here is a clue: how many counts are there when you count "0, 1, 2, 3, 4, 5, 6, 7, 8, 9"?

     

     

  • I'm not really sure what the point of your post is but congrats on the ego?

    My question although not explicitly stated was why is he using 49999 when he can use 62499 as you so gladly pointed out.

    Have a good weekend.

  • Brock said:
    My question although not explicitly stated was why is he using 49999 when he can use 62499 as you so gladly pointed out.

    Do you know which MSP has been used for the example?

    I don't know any MSP with exactly 1 MHz at startup. The 2x or 4x required loading the DCO with some constants first, teh 5x has an FLL with reference which will by default initialize the MCLK (and SMCLK) to ~1MHz.

    I guess, the MSP used when this example was written, was one of the 1x series, which starts with a DCO frequency of 600..800kHz., but well below 1Mhz.

  • Hello Brock

    Your analysis is correct .

    But the value 49999 is correct in following case

    Default DCO value ~ 800 KHz (View any sample code for 8MHz series of MSP) and SMCLK=DCO then after dividing SMCLK by 8 will yield a value 100KHz and the value 49999 will give you .5s value.

     

     

  • HI,
    I FOLLOW THE SAME BOOK...AND I HAD THE SAME DOUBT..I AM NEW TO THIS MICROCONTROLLER AND JusT WANT TO KNOW WHY IS IT MC_1 ,ID_3,TASSEL_2 all the time...also..i've seen DIVA_3 for ACLK to divide by 8..why is it..and what do i do to divide the timer by 2 or 4?kindly help me out...
  • On your MSPs product page, you can download the users guide (and the datasheet and errata sheet) for this MSP.
    The users guide describes all hardware modules available for any MSP of this family. The answers you seek are found in the chapter about Timer A and and the clock system chapter. There are many similarities but also some sometimes significant differences between different MSP families. So be sure to get the right users guide. There are about 500 different MSP430 processors in ~9 different families.

**Attention** This is a public forum