Part Number: MSP430FR2311
Other Parts Discussed in Thread: MSP430F5529,
Tool/software: Code Composer Studio
Hello,
I have written some code to capture the count values from a water flow sensor when water flows through it. I based my code from another example written for an msp430f5529 which is here e2e.ti.com/.../461042.
#include <msp430.h>
//initialize variables
volatile unsigned int counter=0;
volatile unsigned int prev_counter=0;
unsigned int difference=0;
double pulse_freq;
double flow_rate=0.0;
__attribute__((ramfunc));
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //Stop watchdog timer
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// Using Pin 2.1 / TB1.2
P2DIR &= ~BIT1;//Bit 1 Set as 1
P2SEL1 |= BIT1;//Bit 1 Set as 1
// TB1CCR2 (Configure timer_B capture/compare register2);
TB1CCTL2 = CAP + CM_3 + CCIE + SCS + CCIS_1;//capture mode + on both rising&falling edge + enable interrupts + synchronize capture source + Capture input select CCIxB
// SMCLK, Cont Mode; start timer
TB1CTL |= TBSSEL_2 + MC_2 + TBCLR;
while(1) {
__bis_SR_register(LPM0_bits + GIE); //enter low power mode0 with interrupt enable
__no_operation();
__delay_cycles (60000000);//delay for 60 sec before calculating L/min
difference = counter - prev_counter;
pulse_freq = 1/difference;
flow_rate = (pulse_freq*60)/4.7; //multiply by delay (60sec) to get pulses/min and divide by constant from flow sensor datasheet.
//The COV bit is used by the timer module as notification in case of overflow condition
//where a second timer capture event occurs before the first/previous capture event can be read through the CCIFG bit
if(TB1CCTL2 & COV) //Check for capture overflow
{
TB1CCTL2 &= ~COV; // Clear the COV bit when it has been detected
}
}
}
//Timer_B1 TBCCR1 Interrupt Vector Handler Routine
#pragma vector = TIMER1_B0_VECTOR
__interrupt void TIMER1_B0_ISR (void)
{
switch(__even_in_range(TB1IV,0x0E))
{
case TB1IV_NONE: break; //Vector 0: No interrupt
case TB1IV_TBCCR1: //Vector 2: TBCCR1 CCIFG. Interrupt source:capture/compare R1. Interrupt Flag: TBxCCR1 CCIFG.
prev_counter = counter;
counter = TB1CCR1; // 'Counter' value is copied to TB1CCR1 register
__bic_SR_register_on_exit(LPM0_bits + GIE); //exit LPM0?
break;
case TB1IV_TBCCR2: break;//Vector 4: TBCCR2 CCIFG
//case TB1IV_TBCCR3: break;
//case TB1IV_TBCCR4: break;
//case TB1IV_5: break;
//case TB1IV_6: break;
case TB0IV_TBIFG: break; //Vector 6: TBIFG. CCIFG should be set, TBxCCRy register should be set as 0xCAFE
default: break;
}
}
I am using TimerB1.2 and SMCLK. I setup TB1CCTL2 and TB1CTL accordingly, input capture mode, both edges rising and falling. My flow sensor is connected to Pin 2.1.
My problem is that my code will not go into the interrupt. I've been debugging for a while and when I make water flow through the sensor, it just stays in the '__bis_SR_register(LPM0_bits + GIE)' line. Also, the values of 'counter' and 'prev_counter' stay at 0.
I made it work using the F5529 launchpad. But I need it to work for the FR2311. I'm thinking there is a problem with how my interrupt vector is setup or my timerB1.2 is not configured correctly.
Any and all help would be greatly appreciated.
Thanks