#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
typedef unsigned char uint8;
typedef unsigned int uint16;sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
uint num[16]={0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f};//数码管数字
uint count1,count0;
ulong f;
uint8 a[16]="Its frequency is";
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void wrc(uint8 c)
{
delay(100);
rs=0;
rw=0;
e=0;
P0=c;
e=1;
delay(10);
e=0;
}
void wrd(uint8 dat)
{
delay(100);
rs=1;
rw=0;
e=0;
P0=dat;
e=1;
delay(10);
e=0;
rs=0;
}
void init()
{
delay(100);
wrc(0x38);
wrc(0x38);
wrc(0x38);
wrc(0x0c);
wrc(0x01);
}
void display(long int Hz)
{
uint8 i;
long int datas[] = {0, 0, 0, 0, 0,0,0,0};
wrc(0x00+0x80);
for(i=0;i<16;i++)
{
wrd(a);
}
if(Hz>1000)
{
datas[0] = Hz/10000000;
datas[1] = Hz%10000000/1000000;
datas[2] = Hz%1000000/100000;
datas[3] = Hz%100000/10000;
wrc(0x40+0x80);
wrd('0'+datas[0]);
wrd('0'+datas[1]);
wrd('0'+datas[2]);
wrd('0'+datas[3]);
wrc(0X40+0x84);
wrd('K');
wrd('H');
wrd('z');
}
else
datas[0] = Hz/1000;
datas[1] = Hz%1000/100;
datas[2] = Hz%100/10;
datas[3] = Hz%10;
wrc(0x40+0x80);
wrd('0'+datas[0]);
wrd('0'+datas[1]);
wrd('0'+datas[2]);
wrd('0'+datas[3]);
wrc(0X40+0x84);
wrd(' ');
wrd('H');
wrd('z');
}
/*中断服务特殊功能寄存器配置*/
void init1()
{
TMOD = 0x15; //定时器16为计数工作模式
TH1 =0x3c;
TL1 =0xaf; //50ms
TH0=0x0;
TL0=0x0;//计数器0的初值为0
ET0 = 1; //开定时器0中断
ET1=1;//开定时器1中断
TR0=1;//启动定时器0
TR1=1; //启动定时器1
EA = 1; //开总中断
}void main()
{
init1();
init();
count1=0;
count0=0;
f=0;
while(1)
{
display(f);
}
} void int1() interrupt 1 //计数器0中断
{
TH0=0x0;
TL0=0x0;
count0++;
}
void int3() interrupt 3//定时器1中断
{
TH1=0x3c;
TL1=0xaf;
count1++;
if(count1==20)
{
f=TH0*256+TL0+count0*65536;
TH0=0;
TL0=0;
count0=0;
count1=0;
}
}
一周热门 更多>