我用AT89S52做频率计,谁能帮我把这个程序添加详细的注释
Cy28ds7
2018-12-03
#include unsigned char code Tab[]={ 0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE, 0xF6,0xEE,0x3E,0x9C,0x7A,0x9E,0x8E,0x00,}; unsigned char cfDispPos=0; unsigned long ifTimes=0; unsigned long Freq=0; unsigned long DispNow;... #include
unsigned char code Tab[]={ 0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,
0xF6,0xEE,0x3E,0x9C,0x7A,0x9E,0x8E,0x00,};
unsigned char cfDispPos=0;
unsigned long ifTimes=0;
unsigned long Freq=0;
unsigned long DispNow;
bit bfComplete=0;
bit bfTesting=0;
unsigned char Method=0;
unsigned int ifms=0;
bit bfShow=1;
sbit KEY=P3^7;
void Delay(int t)
{ while(t--);
}
void StartFreq(unsigned char Method)
{ switch(Method)
{ case 0:
{EX0=1; //从中断开定时器,测周法
IT0=1;
TMOD=0x11; //Timer0 for precise Time
EA=1;
break;}
case 1:
{//计数测频法
TH0=0xf8;TL0=0x30; //1ms
ifTimes=0;
TMOD=0x51; //Time0 for precise Time ,Timer1 for counter
TH1=0;TL1=0;
EA=1; ET0=1; TR0=1;TR1=1;ET1=1;
PT1=1;
}default:break;
}
}
void timer0 (void) interrupt 1
{ switch(Method)
{ case 0: //用于计数功能
ifTimes++;
break;
case 1: //work with T1
TH0=0xf8; TL0=0x30; //1ms
ifTimes++;
if(ifTimes>2000) //2s
{ TR1=0;
TR0=0;
EA=0;
ET0=0;
ET1=0;
Freq+=(TH1*256+TL1);
Freq=Freq*10/2; //Keep One bit for '.'
if(Freq>900000)Freq=0;
bfShow=1;
bfComplete=1;
}
break;
default:break;
}
}
void timer1(void) interrupt 3 //测频测周
{ switch(Method)
{ case 0:
TH1=0xf8; TL1=0x30;
ifms++;
if(ifms>1000)
{ ifms=0;bfShow=1;
}
case 1:
Freq+=65536;break;
default:break;
}
}
void EX0_IRQ(void) interrupt 0 //测周法使用
{ if(!bfTesting) //first falling edge
{ TH0=0x0;TL0=0x0; //1ms
ifTimes=0;
ET0=1; TR0=1; //Timer0 for Time
TH1=0xf8;TL1=0x30;
TR1=1;ET1=1;
bfTesting=1;
}
else //second falling edge
{ EA=0;TR0=0; ET0=0; EX0=0; //ET0=0;
Freq=(ifTimes*65536+256*TH0+TL0)/2;// us S
Freq=10000000/Freq; //Keep one bit for '.'
bfComplete=1;
}
}
void SystemInitial(void)
{ SCON=0X00;
//TH0=0xfc;TL0=0x18; //0.5ms
StartFreq(Method);
}
void main(void)
{ SystemInitial();
while(1)
{ if(!KEY)
{ Delay(2000);
if(!KEY)
{ while(!KEY);
if(Method)
Method=0;
else Method=1;
DispNow=0;
StartFreq(Method);
}
}
if(bfComplete)//开始测量
{ if(bfShow)
{ bfShow=0;
DispNow=Freq;
}
Freq=0;
bfComplete=0;
bfTesting=0;
StartFreq(Method);
}
{//显示
switch(cfDispPos)
{ case 0:P2=0x6E; P0=0xFE;break;
case 1:P2=Tab[DispNow/1%10]; P0=0xFD;break;
case 2:P2=Tab[DispNow/10%10]|(~Tab[8]); P0=0xFB;break;
case 3:P2=Tab[DispNow/10]; P0=0xF7;break;
case 4:P2=Tab[DispNow/1000%10]; P0=0xEF;break;
case 5:P2=Tab[DispNow/10000%10]; P0=0xDF;break;
case 6:P2=Tab[DispNow/100000%10]; P0=0xBF;break;
case 7:if(Method)P2=0x02;else P2=0; P0=0x7F;break;
}
Delay(100);cfDispPos++;
if(cfDispPos>=8){cfDispPos=0;}
}
}
}
全部评论
您可能感兴趣的社区主题
评论
选购仪器 上yiqi.com
仪器网络推广
品牌网上传播
长按识别二维码查看信息详情