小弟写了一个测量方波频率的程序,但是TAR中值一直为0.求帮助。下面是代码。
#include <msp430g2452.h>
long time[8];
double sum;
long period;
long frequency=1;
long i=0,j=-1;
void process (void);
void delay ();
void main (void)
{
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_8MHZ; // set DCO step + modulation
BCSCTL3 |= LFXT1S_2; // Set LFXT1
BCSCTL2 |= SELM_3 + DIVM_3 + DIVS_3;
P1DIR &= ~BIT2;
P1SEL = BIT2;
TACTL = TASSEL_2+ID_0+TAIE+MC_3;
CCTL1 = CM_1+SCS+CAP+CCIE;
_EINT();
delay();
process();
}
void process (void)
{ while(j<7);
for (i=0;i<=6;i++)
sum += time[i+1]-time
;
period=sum/7;
period=period/1000000;
frequency=1/period;
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void timer_a(void)
{
switch(TAIV)
{
case 2:
time[++j]=TACCR0;
break;
}
if(j==7)
CCTL1 &= ~CCIE;
}
void delay ()
{
unsigned int i;
unsigned int j=10;
for(i=10;i>0;i--)
{
while(j--);
}
}
此帖出自小平头技术问答
一周热门 更多>