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.

MSP430F2618: Interrupts

Part Number: MSP430F2618


Hi all!

I am using a msp430f2618 board to take pulses from a water flow sensor and the program increments a counter whenever an interrupt occurs.

My program is working fine but I want to reset the counter value to '0' after every 5 seconds.

unsigned int count = 0;

void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

P2IE |= 0x10; // P2.4 interrupt enabled
P2IES |= 0x10; // P2.4 Hi/lo edge
P2IFG &= ~0x10; // P2.4 IFG cleared

while(1) {

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/interrupt

wait(5);

printf("Round Over");

_BIC_SR(LPM0_bits + GIE);

count = 0;
}
}

// Port 2 interrupt service routine
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void) {

count++; // Incrementing the counter when ever an interrupt comes

P2IFG &= ~0x10; // P2.4 IFG cleared
}

static void wait(uint8 sec){

uint8 n;
TACCTL0 &= ~(1<<0);
for(n=0;n < sec;n++){
CCR0 = 32768;
TACTL = TASSEL_1 + MC_1; // SMCLK, upmode
while(!(TACCTL0 & (1<<0)));
TACCTL0 &= ~(1<<0);
}
}

  • A few things jump out here:

    1) You're running at something like 1MHz, so 32K timer ticks is closer to 32ms than to one second. You'll need multiple iterations to get one second.
    2) You're going into LPM0, but I don't see a wakeup (LPM0_EXIT) anywhere. main() will stall after the first LPM0_bits setting. Suggestion: Put the wait() logic in a TIMER0_A0_VECTOR, and do the LPM0_EXIT when the timeout expires.
    3) "count" should be declared "volatile".

    Unsolicited:
    1) I'm supposing "(1 << 0)" is "CCIFG". (I didn't look it up.) I suggest you use the latter to reduce hazards from typos.
    2) You probably want to apply CAL[DCO/BC1]_1MHZ" to improve the accuracy of your counts. The default DCO setting is only sorta-kinda close to 1MHz.
  • Hi Amith,

    why you do not setup the TimerA for interrupt functionality and use ALCK which is sourced by a crystal. Divide the clock by 8 and count up to 20480 for a 5 s intervall.
    Let the device stay in LPM3 which gives you much more power saving and clear the count variable in the TimerA interrupt. Here is what I have in mind but I did not check it on silicon but it should work:

    CCTL0 = CCIE; // CCR0 interrupt enabled
    CCR0 = 20480;
    TACTL = TASSEL_1 + ID_3+ MC_1; // ACLK divided by 8, upmode

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

    // Timer A0 interrupt service routine
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector=TIMERA0_VECTOR
    __interrupt void Timer_A (void)
    #elif defined(__GNUC__)
    void __attribute__ ((interrupt(TIMERA0_VECTOR))) Timer_A (void)
    #else
    #error Compiler not supported!
    #endif
    {
    count = 0;
    }

**Attention** This is a public forum