Configuring a msp430 to calculate the time difference between two input signals

50 views Asked by At

To calculate the time difference between two input signals. The idea is to use an msp430 that is connected to a function generator by two pins(P1.2 and P1.4). Timer A is set to continuous and when a input rises an interrupt should be triggered to capture the timestamp and once both input signals rise, then calculate the time difference and print the value on the LCD screen.

Using the code below does not produce any value and seems not to work.

#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 pulse1_timestamp = 0;
volatile unsigned pulse2_timestamp = 0;
volatile unsigned 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(LPM0_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_0 + 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;
}
0

There are 0 answers