液晶显示的时钟
扫描二维码
随时随地手机看文章
液晶显示的时钟(初级版,不包括年月日)
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit lcden = P2^6;//这几个端口要和硬件电路先联系
sbit lcdrw = P2^5;
sbit lcdrs = P2^4;
sbit dula = P3^1;
sbit wela = P3^2;
sbit key1 = P3^5;
sbit key2 = P3^6;
sbit key3 = P3^7;
uchar code table[]=" 2013-10-01";
uchar code table1[]=" 23:59:53";
uchar miao,fen,shi;
uint num,key1num;
void delay(uint z)
{
uint x,y;
for(x= z;x>0;x--)
for(y =110;y>0;y--);
}
void write_com(uchar com)//写命令函数
{
lcdrs = 0;//使能端置0低脉冲写指令,高脉冲写数据
_nop_();//推迟一个机器周期
lcdrw = 0;//低电平往里面写数据
P0= com;
delay(5);
lcden = 1;//使能端给一个高电平读走数据
delay(5);
lcden = 0;//使能端给一个低电平回位
}
void write_date(uchar date)//写数据函数
{
lcdrs = 1;//使能端置0低脉冲写指令,高脉冲写数据
_nop_();//推迟一个机器周期
lcdrw = 0;
P0= date;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
void init()
{
dula = 0;
wela = 0;//关闭数码管
fen = 59;
miao = 53;
shi = 23;
lcden = 0;
write_com(0x38);//显示模式设置
write_com(0x0c);//显示开关以及光标设置
write_com(0x06);
write_com(0x80);
for(num=0;num<13; num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);//设置数据指针
for(num=0;num<12; num++)
{
write_date(table1[num]);
delay(5);
}
TMOD = 0x02;
TH0 = 6;
TL0 = 6;
EA = 1;
ET0 = 1;
TR0 = 1;
key1num = 0;
}
void write_sfm(uchar date,uchar addr)
{
uchar gw,sw;
gw = date%10;
sw = date/10;
write_com(0x80+0x40+addr);
write_date(0x30+sw);
write_date(0x30+gw);
}
void key_scan()
{
P3 = 0xff;
if(key1==0)
{
delay(5);
if(key1 == 0)
{
while(!key1);
key1num++;
if(key1num==1)
{
TR0 = 0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(key1num == 2)
{
write_com(0x80+0x40+8);
write_com(0x0f);
}
if(key1num == 3)
{
write_com(0x80+0x40+5);
write_com(0x0f);
}
if(key1num == 4)
{
key1num=0;
TR0 = 1;
write_com(0x0c);
}
}
}
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(key1num!=0)
{
switch(key1num)
{
case 1:
{
miao++;
if(miao == 60)
{
miao = 0;
}
write_sfm(miao,10);
write_com(0x80+0x40+11);
break;
}
case 2:
{
fen++;
if(fen == 60)
{
fen = 0;
}
write_sfm(fen,7);
write_com(0x80+0x40+8);
break;
}
case 3:
{
shi++;
if(shi == 24)
{
shi = 0;
}
write_sfm(shi,4);
write_com(0x80+0x40+5);
break;
}
}
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(key1num!=0)
{
switch(key1num)
{
case 1:
{
miao--;
if(miao == 0)