Other Parts Discussed in Thread: MSP430WARE
Tool/software: Code Composer Studio
I am still fairly new to MSP430 and CCS and I'm currently working on trying to determine how long it takes a sine wave to hit 0 twice or the half cycle of the wave. The wave is generated by a function generator at 1KHz and is offset so there is no negative component. The ADC works fine and I can detect when the sine wave hit 0. I also have a TimerA interrupt that gets activated every 0.0001 secs at which point interuCounter is incremented and is how I keep track of time between the two 0's of the wave. However when I try to count the time in between those two instances of voltage = 0 I keep getting "difference=1" no mater what I make the frequency. With 1KHz I should expect 10. Am I implementing the timer wrong? Or is something else missing that I'm overlooking?
#include "msp430g2553.h" #include "intrinsics.h" int voltage=0; int flag1=1; int flag2=0; int count=0; int preCount=0; int difference=0; int flag3=0; int interuCounter; int sawZero; void main(void) { WDTCTL = WDTPW + WDTHOLD; // stop WDT DCOCTL = 0; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; //////////////////////////////////////////////////////// //TimerA setup TA0CCR0 |= 100; //count up to 100 (1/1MHz*100 = 0.0001 sec interrupt) TA0CCTL0 |= CCIE; TA0CTL |= TASSEL_2 + MC_1;//SMCLK clock, count up mode // _BIS_SR(LPM0_bits + GIE); // Sleep in LPM0 with interrupts enabled ////////////////////////////////////////////////////////////////////////////////////////// // ADC configuration,V+ref=3V,V-ref=0V,Channel=A0 ADC10CTL0 = ADC10ON + ADC10IE; // Vref Vr+=3v,Vr-=VSS, // S&Htime = 4 X ADCCLK,ADC10 on,ADC interrupts enabled ADC10CTL1 = ADC10DIV_7; // INCH =0000->A0,ADCCLK src = ADC10CLK, // ADCCLK/8,Single Channel Single Conversion ADC10AE0 = INCH_0; // channel A0 ADC10CTL0 |= ENC + ADC10SC; // Start Conversion _BIS_SR(LPM0_bits + GIE); // Sleep in LPM0 with interrupts enabled } /***************************************************************************/ /* interrupt vectors for MSP430 */ /***************************************************************************/ #pragma vector = ADC10_VECTOR __interrupt void adc10_interrupt(void) { ADC10CTL0 |= ENC + ADC10SC; // Start Conversion again voltage = ADC10MEM;//voltage will be whatever ADC reads P1DIR |= BIT6; P1OUT &= ~BIT6; } #pragma vector=TIMER0_A0_VECTOR // Timer0 A0 interrupt service routine __interrupt void Timer0_A0 (void) { interuCounter++;//increment every 0.0001 secs if(flag2==1)//Enter third if statment if I see voltage=0 again { flag3=1; } if(voltage==0 && flag1==1) { preCount =interuCounter; //count when I first saw voltage=0 flag1=0; //Don't enter this statement again unless I've already seen 0 twice flag2=1;//Enter third if statment if I see voltage=0 again } if(voltage==0 && flag3==1) { flag1=1;//RESET flag2=0;//RESET flag3=0;//RESET count=interuCounter;//count at second instance of voltage=0 difference = count-preCount; //Difference between first instance an second instance of 0 P1OUT |= BIT6; } }