Sign in to follow this  
Followers 0

MSP430g2553 Frequency/Period Measurement

2 posts in this topic

I'm new to MSP430 and I'm trying to do a frequency/period measurement. I've read about timers and get the main concepts though I'm still learning. To get an idea of how they work, I'm using this code. It outputs a PWM on one pin which I connect to another pin for capturing period: Code Examples/C/msp430g2xx3_ta_21.c 

After making some modifications (removing unneeded code to light led and adding calculations for frequency) it works with the original pins. However, I want to change which input pin will take measurements. I want pin 2.0 to do so. Currently, TimerA0 captures and TimerA1 outputs PWM. Since PIN 2.0 uses TimerA1 and not A0,  I assumed I would just need to flip the timers and pins. However, it's not working. I looked at the datasheet and can't figure out whats wrong. What am I missing? Here is my code:



#include <msp430.h>

unsigned char Count, First_Time;
unsigned int REdge1, REdge2, FEdge;

int main(void)
  unsigned int Period, ON_Period;
  unsigned char DutyCycle;

  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

//  P1SEL |= BIT0;
  if (CALBC1_8MHZ==0xFF)					// If calibration constant erased
    while(1);                               // do not load, trap CPU!!	
  DCOCTL = 0;                               // Select lowest DCOx and MODx settings
  BCSCTL1 = CALBC1_8MHZ;                    // Set DCO to 8MHz

  // Configure Port Pins
  P1DIR |= BIT2;                            // P2.1/TA1.1 Output
  P1SEL |= BIT2;                            // TA1.1 Option select
  P2DIR &= ~BIT0;                           // P1.1/TA0.1 Input Capture
  P2SEL |= BIT0;                            // TA0.1 option select

  // Configure TA1.1 to output PWM signal
  // Period = 82/32khz = 2.5ms ~ 400Hz Freq
  TA0CCR0 = 82-1;                          // Period Register
  TA0CCR1 = 21;                            // TA1.1 25% dutycycle
  TA0CCTL1 |= OUTMOD_7;                    // TA1CCR1, Reset/Set
  TA0CTL = TASSEL_1 + MC_1 + TACLR;        // ACLK, upmode, clear TAR

  // Configure the TA0CCR1 to do input capture
  TA1CCTL1 = CAP + CM_3 + CCIE + SCS + CCIS_0;
                                            // TA0CCR1 Capture mode; CCI1A; Both
                                            // Rising and Falling Edge; interrupt enable
  TA1CTL |= TASSEL_2 + MC_2 + TACLR;        // SMCLK, Cont Mode; start timer

  // Variable Initialization
  Count = 0x0;
  First_Time = 0x01;

      __bis_SR_register(LPM0_bits + GIE);   // Enter LPM0
      __no_operation();                     // For debugger
      // On exiting LPM0
      if (TA1CCTL1 & COV)                   // Check for Capture Overflow
          while(1);                         // Loop Forever

      Period = REdge2 - REdge1;             // Calculate Period
      ON_Period = FEdge-REdge1;             // On period
      DutyCycle = ((unsigned long)ON_Period*100/Period);


// TA0_A1 Interrupt vector
#pragma vector = TIMER1_A1_VECTOR
__interrupt void TIMER1_A1_ISR (void)
      case  TA1IV_NONE: break;              // Vector  0:  No interrupt
      case  TA1IV_TACCR1:                   // Vector  2:  TACCR1 CCIFG
        if (TA1CCTL1 & CCI)                 // Capture Input Pin Status
            // Rising Edge was captured
            if (!Count)
                REdge1 = TA1CCR1;
                REdge2 = TA1CCR1;
                __bic_SR_register_on_exit(LPM0_bits + GIE);  // Exit LPM0 on return to main

            if (First_Time)
                First_Time = 0x0;
            // Falling Edge was captured
                FEdge = TA1CCR1;
      case TA1IV_TACCR2: break;             // Vector  4:  TACCR2 CCIFG
      case TA1IV_6: break;                  // Vector  6:  Reserved CCIFG
      case TA1IV_8: break;                  // Vector  8:  Reserved CCIFG
      case TA1IV_TAIFG: break;              // Vector 10:  TAIFG
      default: 	break;

I checked and there is a pulse going into P2.0 but P2.0 is not capturing it. Is it just not possible with this pin or am I missing something obvious?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0