#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
uchar Ncount=0,c;
uchar b,q,w,m ;
//uchar code table[]="0123456789";
void delay(uchar m) ;
void write_com(uchar j);
void write_data(uchar j) ;
void init() ;
void init1(void);
void btod(void);
void bintobcd(void);
void display(void);
void main(void)
{
init();
init1();
while (1)
{
delay(5);
display();
}
}
void delay(uchar m)
{ uchar i,j;
for (i=0;i<m;i++)
for (j=0;j<100;j++);
}
void Busy()
{
uchar busy;
do
{
E=0; //0为关闭使能端,1为打开使能端
RS=0; //1为数据 ,0为指令
RW=1; //1为读,0为写 1
delay(5);
P1=0xFF;//未采用P0口不需要啦!
E=1; //0为关闭使能端,1为打开使能端
busy=P1; //读取P1状态
delay(5);
E=0; //0为关闭使能端,1为打开使能端
}
while(busy&0x80);
}
void write_com(uchar j)
{E=0;
RS=0;
RW=0;
E=1;
P1=j;
Busy();
E=0;
}
void write_data(uchar j)
{ E=0;
RS=1;
RW=0;
E=1;
P1=j;
Busy();
E=0;
}
void init()
{E=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void init1()
{
EA=0;
TMOD=0x05;
T0=0x0000;
TR0=1;
}
/*void btod(void)
{long p=0,a=0x0001;
uchar i,j;
for(i=1;i<8;i++)
{if(c & a)
p=p+2^(i-1);
a<<1;
}
c=p;
}*/
void bintobcd(void)
{
Ncount=T0;
//c=Ncount;
//btod();
//Ncount=c;
m=Ncount/1000;
w=Ncount%1000/100;
q=Ncount%1000%100/10;
b=Ncount%1000%100%10;
}
void display(void)
{
bintobcd();
write_com(0xc0);
write_data('F');
write_data('r');
write_data('e');
write_data('q');
write_com(0xc8);
write_data(m+0x30);
write_data(w+0x30);
write_data(q+0x30);
write_data(b+0x30);
write_com(0xce);
write_data('H');
write_data('Z');
}
C:\Documents and Settings\Administrator\桌面 |