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.

Timer A equation for the number into CCR0

Other Parts Discussed in Thread: MSP430F1611

Hi everybody,

 

I have a question about the Timer A in my MSP430F1611. I am using this timer as a counter and using the next example to start knowing how it works:

 

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
  P1DIR |= 0x01;                        // P1.0 output
  CCTL0 = CCIE;                         // CCR0 interrupt enabled
  CCR0 = 1000-1;
  TACTL = TASSEL_1 + MC_1;              // ACLK, upmode

  _BIS_SR(LPM3_bits + GIE);             // Enter LPM3 w/ interrupt
}

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{

  P1OUT ^= 0x01;                        // Toggle P1.0

CCR0 += 1000-1;                       //add offset to CCR0

}

 

I have three questions that i couldn't solve with the help of this forum or google...:

1. What does it means when it sais "add offset to CCR0"? Maybe that when the interrupt is executed, at the end we start again to count? Am i wrong? Please tell me the meaning of that line.

2. I need to know the formula to know the number that i must put into the CCR0 register to count. In the help of this code (I only added the last line about the offset to make the question 1) i found this:

Description: Toggle P1.0 using software and the TA_0 ISR. Timer_A is
//  configured for up mode, thus the timer overflows when TAR counts
//  to CCR0. In this example, CCR0 is loaded with 1000-1.
//  Toggle rate = 32768/(2*1000) = 16.384
//  ACLK = TACLK = 32768Hz, MCLK = SMCLK = default DCO ~800kHz

Is the formula in bold the one that i need to use to select the number into the CCR0 register?. I don't understand very well this formula. There is a factor 2 at the denominator... What does it mean?. How is the formula to find the number i am looking for? In my final application i will use ACLK = MCLK= 8MHz and need to trigger the interruption every 5 seconds... Can you help me with this calculation?

3. And the last question: why the example use CCR0 = 1000-1; I suppose that is because the counter starts in zero and must substract one count... Am i right?

Thank you very much for your help in advance.

Regars

Alejos

  • Alejos said:
    1. What does it means when it sais "add offset to CCR0"? Maybe that when the interrupt is executed, at the end we start again to count? Am i wrong? Please tell me the meaning of that line

    No, the timer continues to count up. CCR0, however, contains the same value that caused the interrupt. If we add 1000 (not 1000-1!) to the current content of CCR0, the next interrupt will happen exactly 1000 counts after the last, no matter how long the ISR took, or how long it took to come to the point where CCR0 is updated (provided that less than 1000 cycles have passed in between).

    Alejos said:
    2. I need to know the formula to know the number that i must put into the CCR0 register to count.

    You have the TA count frequency (ACLK). You have the desired output frequency (e.g. 1kHz). and you toggle the port each interrupt, so you'll need to call the ISR twice as fast. The formula what to init CCR0 with and add to it is  ACLK/2000. If it doesn't fit to an integer, you'll need to write some code that will add alternating values to CCR0 or live with the difference.

    This method requires TimerA running in continuous mode (MC_2) and not in UP mode (MC_1) ! It will work for CCR0, CCR1 and any other, so you can have different frequencies at the same time.

    The formula you listed is for a different kind of setup. There TimerA is running in up mode. It will count from 0 to CCR0 and start again at 0. Here you never need to change CCR0 or you'll change frequency. Again, if you toggle the port, you'll need twice the ISR frequency. This only works with CCR0.
    This operating mode is usually used for generating PWM signals (not for calling ISRs), where the CCR0 gives the PWM frequency and the other CCRs are used for generate hardware PWM outputs with varying duty cycle. This is a more complex theme and you'll find more about it in other threads (search for PWM).

    So in the above example code, the two methods have been mixed, not giving the desired result.

  • Hi !

    I have doubt as to what will happen if the offset is not added in the continuous mode?

    As in if only the CCR0 is initialized and not updated in the ISR?

  • If CCR0 is not updated (or updated while the next interval has already passed), then the next interrupt will happen 655536 timer ticks later, when the timer reaches this count the next time.
    For this reason, my own timer function includes code that checks whether TAR-CCR0 < interval. It continues adding the interval (and counting any software counters) until it is. This way, periods of disabled interrupts up to (65536+interval) counts can be bridged without losing the ‘beat’. However, for the period of disabled interrupts, no PWM signal changes are generated.

    In a properly written code, long periods of disabled interrupts shouldn’t happen (the only exception is flash erasing, as it takes several ms in which interrupts must be disabled)

**Attention** This is a public forum