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.

MSP430F5510: Using HW timers to do an efficient blocking delay

Part Number: MSP430F5510

Hello,

I am trying to do an efficient blocking delay for my university project. What i have right now working:

volatile int32_t counter;

void timers_init(void) {

TA0CTL |= TASSEL__SMCLK | MC__UP;
TA0CCR0 = 16000;                                      //SMCLK is 16MHz so this way we count to 1 ms every interrupt

}

void wait_ms(uint32_t delay) {

counter = delay;
TA0CTL |= TACLR;
TA0CCTL0 |= CCIE;

__bis_SR_register(LPM0_bits + GIE);
while(counter > 0);

}

#pragma vector=TIMER0_A0_VECTOR
__interrupt void timer0_A0_ISR(void) {

--counter;
if (counter == 0) {

__bic_SR_register_on_exit(LPM0_bits);
TA0CCTL0 &= ~(CCIE);

}

TA0CCTL0 &= ~(TA0IV_TA0IFG);

}

Is there any way to make it more efficient? The precision of the delay is not a priority but i don't want to settle by only "it's working"

Any help will be appreciated!

  • I'm not sure the "efficient" is a major concern when you're spending almost all of your time waiting in LPM, but I do see some holes:
    -----------------------
    > TA0CCTL0 &= ~(TA0IV_TA0IFG);

    If the goal here was to clear TA0CCTL0:CCIFG, then (1) this doesn't do it and (2) you don't need to do it since TA0CCTL0:CCIFG (specifically) auto-clears when you take the interrupt. I suspect you should just remove this line.
    -----------------------
    > __bis_SR_register(LPM0_bits + GIE);
    >while(counter > 0);

    If you believe that the (only possible) wakeup happens when counter==0, then the second line is unnecessary. If you think some other entity in your program might also do a wakeup, it should probably look more like:

    >while(counter > 0)
    > __bis_SR_register(LPM0_bits + GIE);

    (For that matter, if there are multiple wakeup agents in the program, I would probably make this more fail-safe by moving the clearing of CCIE from the ISR to here, so the ISR just keeps issuing wakeups -- typically only one -- until this code recognizes it.)
    -----------------------
    >volatile int32_t counter;

    A 32-bit variable can't be atomically fetched/stored. If you can't use an int16_t (32+ seconds), you should disable interrupts around the fetches and stores.
  • Hi Jordi,

    I haven’t heard from you for 5 days, so I’m assuming you were able to resolve your issue. If this isn’t the case, please click the "This did NOT resolve my issue" button and reply to this thread with more information. If this thread locks, please click the "Ask a related question" button and in the new thread describe the current status of your issue and any additional details you may have to assist us in helping to solve your issues.

**Attention** This is a public forum