#include <reg52.h>
#define Segment P2
#define sl P0 ;
sbit addr0=P1^0; // 定义数据端口地址,000时16分频,100 分频
sbit addr1=P1^1;
sbit addr2=P1^2;
sbit clear=P1^3; //计数器清零
sbit ledM=P1^4; //档位led显示
sbit ledK=P1^5;
sbit ledB=P1^6;
unsigned char cnt; //定时1s计数
unsigned char d[4]; //对应数码管的各位
unsigned char level; //档位
unsigned int tN; //不同档位计时值
unsigned int fCnt; //脉冲下降沿次数
unsigned int regCnt; //脉冲次数暂存
unsigned int pCnt; //频率显示
unsigned char Num[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void Delay(unsigned int DelayTime)
{
while(DelayTime--);
}
void disp()
{
if(level==0) //B位档
{
d[3]=0;
d[2]=pCnt/100;
d[1]=(pCnt%100)/10;
d[0]=pCnt%10;
sl=0xfe;Delay(1);Segment=Num[d[3]];Delay(300);
sl=0xfd;Delay(1);Segment=Num[d[2]];Delay(300);
sl=0xfb;Delay(1);Segment=Num[d[1]];Delay(300);
sl=0xf7;Delay(1);Segment=Num[d[0]];Delay(300);
}
else if(level==1)
{
d[3]=pCnt/1000;
d[2]=(pCnt%1000)/100;
d[1]=(pCnt%100)/10;
d[0]=pCnt%10;
sl=0xfe;Delay(1);Segment=Num[d[3]];Delay(300);
sl=0xfd;Delay(1);Segment=Num[d[2]];Delay(300);
sl=0xfb;Delay(1);Segment=Num[d[1]]+0x80;Delay(300); //带小数点
sl=0xf7;Delay(1);Segment=Num[d[0]];Delay(300);
}
else if(level==2)
{
d[3]=pCnt/1000;
d[2]=(pCnt%1000)/100;
d[1]=(pCnt%100)/10;
d[0]=pCnt%10;
sl=0xfe;Delay(1);Segment=Num[d[3]]+0x80;Delay(300); //带小数点
sl=0xfd;Delay(1);Segment=Num[d[2]];Delay(300);
sl=0xfb;Delay(1);Segment=Num[d[1]];Delay(300);
sl=0xf7;Delay(1);Segment=Num[d[0]];Delay(300);
}
else
{
d[3]=0;
d[2]=0;
d[1]=0;
d[0]=0;
}
}
void intial()
{
clear=1; //分频器初始化
addr0=0;
addr1=0;
addr2=1;
tN=20; //档位初始化为Hz档
level=0;
ledM=1; //显示Hz档
ledK=1;
ledB=0;
TMOD=0x61; //定时器0,工作方式1,计数器1,工作方式2(8位自动重装)
TH0=(65536-50000)/256; //计数50ms
TL0=(65536-50000)%256;
TH1=256-1;//计数器1计数初值
TL1=256-1;
TR0=1; //开定时器0中断
ET0=1; //定时器0允许
TR1=1; //开计数器1中断
ET1=1; //允许计数器1中断
EA=1; //总中断允许
}
void main()
{
intial();
while(1)
{
disp();
}
}
void timer0() interrupt 1
{
cnt++;
if(cnt==tN)
{
ET1=0; //计时1s时,暂停外部计数1
TR1=0;
cnt=0;
regCnt=fCnt; //更新显示
fCnt=0; //频率计数清零
clear=0; //硬件分频器清零
clear=1;
if(level==0) //档位切换
{
pCnt=regCnt;
if(regCnt>999)
{
pCnt=0; //显示清零
level=1;
tN=1; // 定时50ms
addr2=0;//进行分频处理
TH1=0xfb; //50分频 256-5
TL1=0xfb;
}
}
else if(level==1)
{
if(regCnt<1)
{
level=0;
tN=20; //定时1s
addr2=1; //不分频
TH1=0xff; // 256-1
TL1=0xff; // 256-1
ledM=1; //显示HZ档
ledK=1;
ledB=0;
}
else if(regCnt<1000) //KHz档
{
level=1;
tN=1; //定时50ms
addr2=0; //硬件分频处理
TH1=0xfb; //50分频 256-5
TL1=0xfb; //256-5
pCnt=regCnt*10; //换算成相应的显示:f=regCnt*100
ledM=1; //显示KHz档
ledK=0;
ledB=1;
}
else
{
level=2;
tN=1; //定时50ms
addr2=0; //分频
TH1=0xf6; // 100分频 256-10
TL1=0xf6; //256-10
pCnt=0; //换算成相应的显示:f=regCnt*100
ledM=0; //显示MHz档
ledK=1;
ledB=1;
}
}
else if(level==2)
{
if(regCnt<500) //KHz档
{
level=1;
tN=1; //定时50ms
addr2=0; //分频
TH1=0xfb; // 50分频 256-5
TL1=0xfb; //256-5
pCnt=0; //换算成相应的显示
ledM=1; //显示KHz档
ledK=0;
ledB=1;
}
else if(regCnt<5000)
{
level=2;
tN=1; //定时50ms
addr2=0; //分频
TH1=0xf6; //100分频 256-10
TL1=0xf6; //256-10
pCnt=regCnt*2; //换算成相应的显示:f=regCnt*100
ledM=0; //显示MHz档
ledK=1;
ledB=1;
}
else
{
level=3; //超出范围
ledM=1; //全灭
ledK=1;
ledB=1;
}
}
ET1=1; //恢复外部计数1
TR1=1;
}
TH0=0x3c; //(65536-50000)/256;
TL0=0xb0; //(65536-50000)%256;
}
void enterFre() interrupt 3 //外部频率输入
{
fCnt++;
}
出现如下错误
compiling cl.c...
CL.C(37): warning C275: expression with possibly no effect
CL.C(37): error C141: syntax error near '='
CL.C(38): error C141: syntax error near '='
CL.C(39): error C141: syntax error near '='
CL.C(40): error C141: syntax error near '='
CL.C(49): error C141: syntax error near '='
CL.C(50): error C141: syntax error near '='
CL.C(51): error C141: syntax error near '='
CL.C(52): error C141: syntax error near '='
CL.C(61): error C141: syntax error near '='
CL.C(62): error C141: syntax error near '='
CL.C(63): error C141: syntax error near '='
CL.C(64): error C141: syntax error near '='
cl.c - 12 Error(s), 1 Warning(s). |