I copied a simple test program from MSP430 examples that uses Timer_A for frequency capture. Basically the captured_clocks buffer should contain the pulse widths between the input pulses. So if I feed 1 kHz square wave into the P1.3 (pin 3) and set the clock for 1 MHz the buffer should have values [1000,1000,1000]. However when I run the program I only get garbage in the buffer (e.g. [62,26,52678]). Any ideas what goes wrong? I'm using MSP430G2553 with Launchpad.
#include "msp430g2553.h"
unsigned int previosly_captured_clock = 0;
unsigned int captured_clocks[10];
unsigned char i=0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased
// do not load, trap CPU!!
}
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ;
P1SEL |= BIT1; // P1.1 timer input
TACTL = TASSEL_2 + MC_2; // SMCLK, contmode
TACCTL0 = CM_1+CCIS_0+SCS+CAP+CCIE; // Positive edge + Capture input select 0 + Synchronous capture + Capture mode + Capture interrupt enable
_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
}
// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
unsigned int captured_clock = TACCR0;
captured_clocks[i++] = captured_clock - previosly_captured_clock;
previosly_captured_clock = captured_clock;
if (i>10) {
__no_operation(); // Breakpoint here
i = 0;
}
}