|
初学者,编的程序在下面,求大佬们看看为甚么我的程序幅值可以测量出来但是频率测不出来/*
#include "msp430g2553.h"
#include "user.h"
char data_display[10];
uint U_sample[20];
uint index=0;
float U1=0;
uint f=0;
uint num2=0;
uint period=0;
uint temp=0;
uint overfloat=0;
void GPIO_Init(void)
{
P1DIR &=~BIT2;
P1SEL |= BIT2;
P1REN |= BIT2;
P1DIR &=~BIT1;
P1SEL |= BIT1;
P1REN |= BIT1;
}
void BCS_Init(void)
{
if(CALBC1_8MHZ ==0xFF)
{
while(1);
}
DCOCTL = 0;
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ;
BCSCTL2 |= DIVM_3;
__delay_cycles(100);
}
void Frequency(void)
{
TACTL = TASSEL_2 + MC_1 + TAIE + TACLR; //SMCLK,Up to CCR0,Timer A counter clear
TACCTL1 = CM_1 + CCIS_0 + SCS + CAP + CCIE; //Capture mode: 1 - pos. edge,Capture input select: 0 - CCIxA
if(period > 0)
f = 1000000/period;
floatTochar(f,data_display,1);
StrDisplay(data_display,3,0,0);
}
void ADC_DY(void)
{
ADC10CTL0 = SREF_1 + ADC10SHT_2 + ADC10ON + REFON + REF2_5V; // ADC10ON, interrupt enabled
ADC10CTL1=INCH_1; // input A1
ADC10AE0 |= 0x02; // P1.1 ADC option select
while(1)
{
num2++;
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
U_sample[index] = ADC10MEM;
index ++;
if(index >= 20)
{
index = 0;
U1 = M_Filter(U_sample,20)*2.5/1023;
U1 = U1*5;
floatTochar(U1,data_display,2);
StrDisplay(data_display,1,0,0);
}
if(num2>40)
{
num2=0;
break;
}
}
}
void main(void)
{
WDTCTL = WDTPW +WDTHOLD;
BCS_Init();
GPIO_Init();
_EINT();
Lcd_Init();
Lcd_Clear();
StrDisplay("电压值:",0,0,0);
StrDisplay("频率:",2,0,0);
while(1)
{
ADC_DY();
Frequency();
}
}
#pragma vector = TIMER0_A1_VECTOR //Timer_A中断向量
__interrupt void Timer_A(void)
{
switch (TA0IV)
{
case 2:
temp++;
if(temp == 1)
{
overfloat = 0;
TA0CTL = TACLR;
}
if(temp == 2)
{
period = CCR1;
period = overfloat*65536 + period;
temp = 0;
}
break;
case 4:break;
case 10: overfloat++;break; // TimerA溢出中断;
}
}
TAT感觉自己写的不是特别清楚但是真的尽力写了,希望有大神解答,感激不尽!!!!!
|
|