Jump to content
Sign in to follow this  
akfisherman

Using Timer to find incoming frequency

Recommended Posts

I'm trying to find the frequency of an incoming signal. I set the incoming signal (sine wave = 162 MHz, amp=3V) as the clock for TimerA0. TimerA2 will be used to keep count in order to calculate the frequency. Once the freq is calculated I'm setting the DCO for the SMCLK. Frequency should be around 20 MHz.  

The TIMER_A2 ISR is not accessed at all. Not sure if the correct ISR is being used or even if this is the best/simplest way to find the frequency. Thanks

void main(void)
{
Port_Mapping(); // Access port mapping code
 
SetVcoreUp (0x01); // Increase Vcore setting to level 3 to support 25MHz.
SetVcoreUp (0x02);
SetVcoreUp (0x03);
 
WDTCTL = WDTPW + WDTHOLD;   // Stop watchdog timer
 
/////// Clock Settings for SMCLK and ACLK //////////
UCSCTL1 |= DCORSEL_4;             // DCORSEL =4 (12.3 to 28.2 MHz range)
UCSCTL4 |= SELM__XT1CLK + SELS__DCOCLK + SELA__XT1CLK;         //Selects MCLK, ACLK, SMCLK
UCSCTL8 = SMCLKREQEN + ACLKREQEN + MCLKREQEN;
 
/////// Output Pins /////////
P2SEL |=BIT2;     // SMCLK Output Pin (ACLK is same pin as TA0CLK input)
P2DIR |=BIT2;
P1DIR &= ~BIT0;    // Input for TA0CLK
P1SEL |= BIT0;
 
///// // TimerA0 Settings  (Used with incoming signal from PSTAR) /////
TA0CTL= TASSEL__TACLK + MC__UP + ID_3 + TAIE;   //set External clock, up mode, divide by 8, enable interrupt
TA0CCTL1 = CM_1 + CAP + OUTMOD_4;        // capture on rising edge, capture mode enabled, Toggle output
TA0CCTL0 |=CCIE;         // capture compare interrupt enable
TA0CCR0 = 1000;          // 1000 rising edge samples (should take 50 us) 20MHz/(1/50us)
 
/////// TimerA2 Settings   (Used to calculate frequency of incoming signal) /////
TA2CTL= TASSEL__ACLK + MC__CONTINUOUS + TAIE;        // select ACLK, continuous mode, enable interrupt
TA2CCTL1 = CM_1 + CAP + OUTMOD_7;    // capture on rising edge, capture mode enabled, output set to reset/set
TA2CCTL0 |=CCIE;         // capture compare interrupt enable
 
_EINT();    //Global interrupt enable
_bis_SR_register(GIE);
}
 
/////// Timer_A2 ISR ////////
int static timer_count = 0; // Variables used in TIMER ISR
int frequency_input;
int frequency_output;
int n;
 
#pragma vector=TIMER2_A0_VECTOR
__interrupt void TIMER_A2 (void)
{
if (TA0CCR0 == 1)
{
timer_count++;    // Starts count and is associated with TIMER A2
}
 
if (TA0CCR0 == 1000)
{
//// Configure SMClK based on timer_count result ////
frequency_input = 1000/timer_count; // Calculation of input frequency based on 1000 rising edges
frequency_output = frequency_input/32768; // Multiplier calculation for DCOCLK register for SMCLK frequency
n = frequency_output - 1; // variable for register setting of DCOCLK
UCSCTL2 = n;              // for DCOCLK frequency multiplier used by SMCLK
P4DIR |= BIT7;  // LED 2 turns on to verify ISR is being accessed
P4SEL &= ~BIT7;
timer_count = 0;     // Reset timer_count for next TA0CCR0 up count
}
}

 

Share this post


Link to post
Share on other sites

I set the incoming signal (sine wave = 162 MHz, amp=3V) as the clock for TimerA0.

If that's not a typo - then it is probably way to high for the MSP430 to count reliably. You should have a front end circuit that presents a 16 MHz max square wave to the MSP430.

 

Here is a simple frequency counter that uses Timer A0 to do the counting, and the watchdog timer as the gate timer: http://forum.43oh.com/topic/1913-frequency-counter-using-launchpad-nokia-5110-lcd/

 

The bottom half of main() is the code that does the counting.

Share this post


Link to post
Share on other sites

It should be dividing the incoming signal by 8. Is 20 MHz to high of a frequency? Even if I lower the incoming freq to 5 MHz it still doesn't work. I'll look at the link you sent. Thanks 

TA0CTL= TASSEL__TACLK + MC__UP + ID_3 + TAIE;   //set External clock, up mode, divide by 8, enable interrupt

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...