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.

Timers. I need more timers!

Other Parts Discussed in Thread: MSP430F5659

Hello chaps.

In my current project, I try to utilize timers as much as possible.
From the ADC, to menu timeouts.

Since there are around four timers in a typical MSP430, I use the CCRx IRQs as well, since the TxIFGs were soon exhausted.
This however, was a bit cumbersome, and soon lead to problems. (Timers not firing, misfiring, etc)

I am now considering rearranging my code to use only TxIFGs with the aid of some flags to distinguish what function used the specific timer and for what purpose.

I would like to see how you guys address this issue.

Thanks in regards.

  • Hi Christos,
    What MSP are you using? Some other MSP's have even more timers, such as MSPFR6959. Some with 21 (perhaps more) CCR's.

    You can also configure WDT and RTC to function as timers.

    Can you give us more details on what you are doing, and what sort of errors you are seeing? I think you'll get a better response from the community that way!


    Best regards,
    Cameron
  • The part I'm using is the MSP430F5659.
    The project is an radio alarm clock, so the RTC does the calendar stuff, the WDT is currently unused, but I will implement a watchdog in the end, so there goes that.

    The part has TIMER0_A5, TIMER1_A3, TIMER2_A3 and TIMER0_B7.

    However, I would like a broader answer to my question.
    I mean what do you guys do when you're out of free timers?
    How do you cope with it?

    As for the errors I am encountering, is more that the code is starting to look like spaghetti, and some timers do not fire, because they are hijacked by another function :)

  • The first one I made used a CCRx for each task to itself, so total of 6 time-independent task on a G2553 (both timers)
    plus a 7th task used IFG that is the software-RTC.

    The new one I'm coming up with is that 16 tasks have to share one timer,
    it can choose (and change at will) from: 10ms, 250ms, and 1sec and 16sec.
    Those are the CCR0, and CCR1,  CCR2 and IFG (on a 32K crystal with adiv8)

    As to not waste power each task have to put its bit in a ccr_active-var, and when done it clears its bit flag.
    if the CCRx IRQ notice that no one is using it, it turns itself off.

    So when a task wants to use it and it's not already running, it will have to safe-copy TA0R to CCRx

    void initCCRx(char CCRx, int taskmask){
        int temp;
        do {temp = TA0R;}				      // not same freq domain, read twice and cmp
        while(temp != TA0R);				  // it's safe as 4KHz is way slower than 1MHz
        switch (CCRx){
        case 0: if (!ccr0active)
                  CCR0 = temp + 328/aclkdiv;  // aclkdiv is a #defined value incase aclk change
                ccr0active |= taskmask;
                CCTL0 = CCIE; break;	

    Here is  CCR0:

    #pragma vector=TIMER0_A0_VECTOR			// Timer A0_0 interrupt service routine
    __interrupt void Timer_A (void)
    {    
      if (ccr0active){
         CCR0 += 328/aclkdiv;				// Add 10ms to CCR0		
        taskclk |= BIT0;
        __bic_SR_register_on_exit(LPM3_bits);
      }
      else CCTL0 = 0;                       // turn IRQ off
    }

    I may release the complete system for "donations", soon.

  • Christos Birbas said:
    I mean what do you guys do when you're out of free timers?

    For slow (alarm clock application) timers we just implement them in software, as much as needed. - Run single timer (ticks timer) at frequency like 1000Hz or 100Hz or 1Hz and implement timer counters and capture functionality in ticks timer ISR.

    If we need lot of high speed timers - we just use chips having what we need. For example many ARM microcontrollers happen to have loads of hardware timers each having enough capture/compare registers.

  • Thank you very much guys.
    I 'll try to implement Tony's solution and see how it goes.

    Regards.
  • Did this solution work for you?
  • Yes it did mate!
    I consolidated most of my timing stuff into TIMER_B7 and some into A5 :)

    The paper about multiple time bases, did the job.
    Regards.
  • Awesome! And thanks for the feedback, we always appreciate knowing what is and isn't working for you.

    Best,
    Cameron

**Attention** This is a public forum