|
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define wdm P0
sbit pulse = P3^4; //脈沖輸入端口
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4; //控制數(shù)碼管位
uchar code DM[] =
{
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f
}; //段表
/******全局變量定義******/
uchar f0[5],pwm[2]; //分別保存頻率、占空比
/******函數(shù)聲明******/
void Delay(uint n); //延時函數(shù)
void init(); //初始化函數(shù)
void handle1_f(uint f); //處理頻率函數(shù)
void handle2_p(uint p); //處理函數(shù)
void Display_f(); //顯示占空比頻率
void Display_p(); //顯示占空比
/******主函數(shù)******/
void main()
{
uint th1,tl1,tf1,th2,tl2,tf2;
uint f,p;
int i,j;
init(); //初始化
while(1)
{
TH0 = 0;
TL0 = 0;
while(pulse); //pulse為脈沖的輸入引腳
while(!pulse); //等待上升沿來臨
TR0=1; //打開定時器
while(pulse); //等待下降沿來臨
th1=TH0;
tl1=TL0;
tf1=TF0;
while(!pulse); //等待上升沿來臨
TR0=0; //關(guān)閉定時器
th2=TH0;
tl2=TL0;
tf2=TF0;
p = ((1.0)*(th1*256+tl1+tf1*64536)/(th2*256 + tl2+tf2*64536))*1000;
f = 1000000.0/((th2*256 + tl2+tf2*64536)*12/12);
handle1_f(f);
handle2_p(p);
//刷新一次
for(i=0;i<2;i++){
Display_f();
Display_p();
}
}
}
void init()
{
TMOD = 0x11; //定時器T0、T1工作在方式1
EA = 1;
ET0 = 1;
ET1 = 1;
TH0 = 0;
TL0 = 0;
}
void Delay(int x)
{
int i, j;
for(i = x; i > 0; i--)
for(j = 114; j > 0; j--);
}
void handle1_f(uint x)
{
f0[5] = x%10;
f0[4] = x/10%10;
f0[3] = x/100%10;
f0[2] = x/1000%10;
f0[1] = x/10000%10;
f0[0] = x/100000%10;
}
void handle2_p(uint x)
{
pwm[2] = x%10;
pwm[1] = x/10%10;
pwm[0] = x/100%10;
}
void Display_f()
{
int i,j;
for(i=0;i<6;i++)
{
switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//顯示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//顯示第7位
}
j=f0[i];
P0=DM[j];//發(fā)送段碼
Delay(5); //間隔一段時間掃描
}
}
void Display_p()
{
int i,j;
for(i=0;i<3;i++)
{
switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//顯示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//顯示第7位
}
j=pwm[i];
P0=DM[j];//發(fā)送段碼
Delay(5); //間隔一段時間掃描
}
}
|
|