|
楼主 |
发表于 2009-11-11 16:30:12
|
显示全部楼层
附上程序(源程序是CVAVR的,改成ICC的):
程序大致意思:
测量频率,定时器0工作在计数模式,定时器2记录中断次数,
在限定的脉冲内计算周期,
时间=中断次数*计数一次时间+最后的TCNT2的值。
#include"iom16v.h"
#include"macros.h"
#define uchar unsigned char
#define uint unsigned int
const uchar led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
const uchar pos[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uchar disbuf[8],posit;
uchar freq_ok;
uchar time2_new;
uint freq;
long freq_temp;
void display()
{
PORTC=0xff;
PORTA=led[disbuf[posit]];
//if(posit==5) PORTA|=0x80;
PORTC=pos[posit];
if(++posit>=8) posit=0;
}
#pragma interrupt_handler time0_comp:20
void time0_comp()
{
time2_new=TCNT2;
TCNT2=0;
TIFR|=0x02; //由此语句引发出的疑惑。。。。
freq_temp=freq; //中断次数
freq=0;
freq_ok=1;
}
#pragma interrupt_handler time2_comp:4
void time2_comp()
{
freq++;
SEI();
display();
}
void freq_con()
{
uchar i,j=7;
for(i=0;i<=7;i++)
{
disbuf[j-i]=freq_temp%10;
freq_temp=freq_temp/10;
}
}
void main()
{
uchar i;
DDRA=0xff;
DDRC=0xff;
TCCR2=0x0a;
TCNT2=0;
OCR2=0xf9;
TCCR0=0x0e;
OCR0=0xf9;
TIMSK=0x82;
for(i=0;i<=7;i++) disbuf=0;
SEI();
while(1)
{
if(freq_ok)
{
freq_temp=freq_temp*250+time2_new;
freq_temp=(long)(125000000/freq_temp);
freq_con();
freq_ok=0;
}
}
} |
|