The timers in the MSP430G2553 chip, such as Timer_A, have a capture function. How to use it?
[Copy link]
Author: Simplebob
Link: https://www.zhihu.com/question/20240825/answer/14973719
Source: Zhihu
Copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.
1. Before measuring, you need to convert the sine wave into a square wave. This is a very simple circuit.
2. To measure the frequency, the following code is written by me, and it can measure up to 100K. The accuracy is 0.01HZ. In general, use TIEMEA to generate a 2S interrupt, and read and calculate the frequency after 2S. TIMERA0 is the measurement of pulse width, and TIMERA1 is the processing of timer A interrupt.
void Init_Capture(void)
{
P1DIR&=~BIT1;
P1SEL|=BIT1;
BCSCTL2 |= SELS; // SMCLK=XT2=16M
BCSCTL2 |= DIVS_1; //SMCLK divided by 2, i.e. SMCL=8MHZ
TACTL |=TASSEL_1+TAIE+TACLR; //8-division, select ACLK as the clock source (ACLK) of timerA, open interrupt, count up mode
TACCTL0 |=CM_1+SCS+CAP+CCIS_0+CCIE; //Rising edge capture + synchronous capture + open capture + timerA is capture + open capture interrupt
TACTL |=MC_2;
}
int main()
{
Init_Capture();
while(1)
{
if(global_a.Conver==1)//Capture frequency
{
_DINT();
global_a.Conver=0;
global_a.CapCount=(float)((32768.0*global_a.pulse)/global_a.time);//Calculate frequency, pay attention to understanding!
Print_Fre();//Display frequency_EINT
();
}
}
}
#pragma vector=TIMERA0_VECTOR
__interrupt void timer_A(void)
{
if(global_a.Cap_Tar==0)
{
global_a.Cap_First = TACCR0;
global_a.Cap_Tar++;
}
else
{
global_a.Cap_Last = TACCR0;
global_a.Cap_Tar++;
}
}
#pragma vector=TIMERA1_VECTOR
__interrupt void timeA1(void)
{
switch(TAIV)
{case 2:
break;
case 4:
break;
case 10: if(global_a.Cap_Tar==0)
global_a.pulse=0;
else
{
global_a.pulse=global_a.Cap_Tar-1;
global_a.time = global_a.Cap_Last-global_a.Cap_First;
global_a.Cap_Tar=0;
TACTL &=~BIT0;
// BIC_SR_IRQ(LPM3_bits);
global_a.Conver=1;
_DINT();
}
break;
}
}
|