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.

MSP430F5438A: Programming a Msp430 to calculate the time difference between two external signals

Part Number: MSP430F5438A

I am attempting to calculate the difference between two external signals. In my code I have set up timer A and interrupts to capture the timestamps when each of the external signals rise. I am using RF3 pins 1.2 and 1.4 as inputs. When the time difference is calculated it should print the value on the LCD screen. However, when testing out the code, the input signals are not detected and nothing seems to happen. I am not sure where I have went wrong.

#include "msp430.h"
#include "hal_lcd.h"
#include "hal_lcd_fonts.h"

void TiePins(void);
void PinInit(void);
void TimerA0Init(void);
void LCDInit(void);
void format_numerical_string();

volatile unsigned long pulse1_timestamp = 0;
volatile unsigned long pulse2_timestamp = 0;
volatile unsigned long time_diff = 0;

void format_numerical_string(unsigned int n);
char LCD_string[12]="DeltaT:xxx\0";
/**
* main.c
*/
int main(void)
{
volatile unsigned int Delta_T, time_diff;

//HARDWARE CONFIGURATION
WDTCTL = WDTPW | WDTHOLD; // stop watch dog timer
/* Initialise tie pins */
TiePins();
/* Initialise GPIO to minimum current consumption */
PinInit();
//Initialise LCD and back light
LCDInit();
/* Initialise Timer A0.1 */
TimerA0Init();

__enable_interrupt(); // Enable global interrupts

while(1)
{
Delta_T = time_diff;
//Update LCD. Print message string
format_numerical_string(Delta_T);
halLcdPrintLineCol(LCD_string, 5, 1, OVERWRITE_TEXT);

// Bit-set (LPM3_bits + GIE) in SR register to enter LPM3 mode
__bis_SR_register(LPM3_bits + GIE);

}

return 0;
}

//MSP430 pins initialization
void TiePins()
{
// Tie unused ports
PAOUT = 0; PADIR = 0xFFFF; PASEL = 0;
PBOUT = 0; PBDIR = 0xFFFF; PBSEL = 0;
PCOUT = 0; PCDIR = 0xFFFF; PCSEL = 0;
PDOUT = 0; PDDIR = 0xFFFF; PDSEL = 0;
// P10.0 to USB RST pin, if enabled with J5
PEOUT = 0; PEDIR = 0xFEFF; PESEL = 0;
P11OUT = 0; P11DIR = 0xFF; P11SEL = 0;
PJOUT = 0; PJDIR = 0xFF;
P6OUT = 0x40; // Shut down audio output amp
}

void LCDInit()
{
halLcdInit();

halLcdBackLightInit();
halLcdSetBackLight(8);
halLcdSetContrast(90);
halLcdClearScreen();
halLcdPrintLine(" ASL", 0, 0);
halLcdPrintLineCol(LCD_string, 5, 1, 0);
}

//MSP430 pins initialization
void PinInit()
{
// Configure input pins for pulse detection
// Configure P1.2 as an input
P1SEL |= BIT2; // Select RF3 for P1 bit 2
P1DIR &= ~BIT2; // Set P1.2 as an input
P1REN |= BIT2; // Enable pull-up/pull-down resistors
P1IN |= BIT2; // Set pull-up resistor

// Configure P1.4 as an input
P1SEL |= BIT4; // Select RF3 for P1 bit 4
P1DIR &= ~BIT4; // Set P1.4 as an input
P1REN |= BIT4; // Enable pull-up/pull-down resistors
P1IN |= BIT4; // Set pull-up resistor


// Enable interrupt for rising edges on P1.2 and P1.4
P1IES &= ~BIT2; //Set interrupt on rising edges
P1IFG &= ~BIT2; //Clear any pending interrupts
P1IE |= BIT2; //Enable interrupts for P1.2


P1IES &= ~BIT4; //Set interrupt on rising edges
P1IFG &= ~BIT4; //Clear any pending interrupts
P1IE |= BIT4; //Enable interrupts for P1.4

}

void TimerA0Init()
{
//Set up capture for Timer A, Synchronised capture source
TA0CCTL0 = CM_1 + CCIS_0 + SCS + CAP + CCIE + CCI + CCIFG;
//Set up capture for Timer A, Synchronised capture source
TA0CCTL1 = CM_1 + CCIS_1 + SCS + CAP + CCIE + CCI + CCIFG;
//Timer source SMCLK, Continuous mode, Clear timer, enables interrupt flag
TA0CTL = TASSEL_2 + MC_2 + TACLR + TAIE + TAIFG;
//TA0CCR0 = 0xFFFF; // Set the timer to count to its maximum value
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer0_A0_ISR(void)
{
pulse1_timestamp = TA0CCR0; // Capture timestamp for pulse 1

P1IFG &= ~BIT2; //Clear any pending interrupts
}

#pragma vector = TIMER0_A1_VECTOR
__interrupt void Timer0_A1_ISR(void)
{
if (TA0IV == TA0IV_TACCR1) // Check which capture event occurred
{
pulse2_timestamp = TA0CCR1; // Capture timestamp for pulse 2
time_diff = pulse2_timestamp - pulse1_timestamp; // Calculate time difference

P1IFG &= ~BIT4; //Clear any pending interrupts
}
}

// create string with voltage measurement
void format_numerical_string(unsigned int n)
{
unsigned char ones = '0';
unsigned char tenths = '0';
unsigned char hundredths = '0';

while(n >= 10)
{
ones++;
n -= 10;
}
while(n >= 1)
{
tenths++;
n -= 1;
}
hundredths += n;
LCD_string[7] = ones;
LCD_string[8] = tenths;
LCD_string[9] = hundredths;

//LCD_string[7] = '1';
//LCD_string[8] = '2';
//LCD_string[9] = '3';

}

  • Hi Ciaran,

    In part of your code you execute this line:

    __bis_SR_register(LPM3_bits + GIE);

    This puts the device into low power mode 3, which disables a few things, including some of the clocks and the device's CPU. Details on this can be found in section 9.2 of the device datasheet. Try changing this line to:

    __bis_SR_register(GIE);

    Which will prevent the device from going into low power mode. Try this and let me know if you need further advice.

  • Hi Dylan,

    Thank for responding. Unfortunately this edit did not change anything, there is still no output value being produced

  • Per data sheet (SLAS655H) Tables 9-43 and 9-54:

    TA0CCTL0 = CM_1 + CCIS_0 + SCS + CAP + CCIE + CCI + CCIFG;

    This requests TA0.CCI0A, which is on P1.1 and

    > TA0CCTL1 = CM_1 + CCIS_1 + SCS + CAP + CCIE + CCI + CCIFG;

    this requests TA0.CCI1B, which is on P8.1.

    P1.2 and P1.4 are TA0.CCI1A and TA0.CCI3A respectively. Which pins are actually connected?

    -----------

    That said, I would expect (once you remove the LPM setting) an output of "000", not no output at all. Are you fairly certain your LCD display functions are working?

  • Hi Ciaran,

    Were you able to solve your issue? 

  • Hi Bruce,

    I had already noticed that error and corrected it. I want to connect pins P1.2 and P1.4. The LCD display function is working correctly 

  • Ciaran,

    Are you saying that your device never enters either of the timer-capture ISRs? I've been looking through your code and comparing it to a timer capture example that we posted and I can't find any code breaking discrepancies so far.

    Have you tried a version of the project without the LCD first? Starting with one of our timer capture examples and building your application code from there may reveal the issue and help you move forward faster.

**Attention** This is a public forum