|
【求助】关于F149测正弦波频率,测量值在正常值和另外一个值之间跳
[复制链接]
用430单片机做一个课程设计,需要用单片机测量正弦波频率。
正弦波经过一个简单的处理将其负半波削掉,然后送入单片机的比较器输入端与二极管参考电压进行比较,输出结果送定时器A的捕获/比较器1,捕获比较寄存器设置为上升沿触发。
现在的问题是测量值一次是对的,下一次是错的,再下一次又是对的,反复这样,不知道是为什么。请各位大虾帮忙。
...
void main(void)
{
unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog Timer
P5DIR |= 0x30; // P5.5,P5.4 outputs
P5SEL |= 0x30; // P5.5,P5.4 options
P1DIR |= 0X07; // CS_7289 = P1.0 ; CLK_7289 = P1.1 ; DIO_7289 = P1.2
P1OUT = 0X01; // CS_7289 = 1;
BCSCTL1 &= ~XT2OFF; // XT2on
do
{
IFG1 &= ~OFIFG;
Delay(255);
}
while((IFG1&OFIFG) != 0); // Change the system clock
BCSCTL2 |= SELM1 + DIVM1 + SELS + DIVS0 + DIVS1; // MCLK = (1/4)XT2;SMCLK = (1/8)XT2
CACTL1 |= CARSEL + CAREF_3 + CAON;
CACTL2 |= P2CA0 + CAF;
for(i = 0; i < 200; i ++)
Delay(255); //waiting for zlg7289 ready;
DisInit();
CCTL1 = CM_1 + CCIS_1 + SCS + CAP + CCIE; // ²¶»ñ±&Egr××e;½Ï¿ØÖƼĴæÆ÷1
TACTL = TASSEL_2 + TACLR + TAIE; // Timer_A ¿ØÖƼĴæÆ÷
TACTL |= MC1;
_EINT();
while(1)
{
DataDisplay(MeasureF());
for(i = 0;i < 255;i ++)
Delay(255);
}
}
...
ULONG MeasureF(void)
{
ULONG tmp;
UINT *p;
while(finishFlag == 0);
finishFlag = 0;
if(timerMeasure.num == 0)
tmp = timerMeasure.timeEnd - timerMeasure.timeStart;
else
{
MPY = timerMeasure.num;
OP2 = 65535;
p = (UINT *) &(tmp);
*p = RESLO;
*(p+1) = RESHI;
tmp = tmp + timerMeasure.timeEnd - timerMeasure.timeStart;
}
return (tmp);
}
#pragma vector = TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
switch(TAIV)
{
case 2:
{
if(flag == 0)
{
flag = 1;
timerMeasure.timeStart = TACCR1; //存开始时间
CCTL1 &= ~COV;
}
else
{
timerMeasure.timeEnd = TACCR1; //存结束时间
CCTL1 &= ~COV;
timerMeasure.num = number; //存在开始和结束时间之间timerA溢出次数
flag = 0;
number = 0;
finishFlag = 1;
}
break;
}
case 10:
{
if(flag == 1)
number ++;
break;
}
}
}
|
|