📄 1602.c
字号:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P1^2;
sbit lcdrw=P1^3;
sbit lcden=P1^4;
sbit shezhi=P3^5;
sbit add=P3^6;
sbit dec=P3^7;
uchar count,shezhinum;
char miao,shi,fen;
uchar code table[]=" wang shu mao ";
uchar code table1[]=" 12-00-00 ";
void delay(uchar z)
{
uchar x,y;
for(x=0;x<z;x++)
for(y=0;y<110;y++);
}
void write_com(uchar com)
{
lcdrs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_dat(uchar dat)
{
lcdrs=1;
lcden=0;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
uchar i;
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(i=0;i<15;i++)
{
write_dat(table[i]);
delay(5);
}
write_com(0x80+0x40);
for(i=0;i<12;i++)
{
write_dat(table1[i]);
delay(5);
}
TMOD=0x01;
TH0=0xdc;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
}
void write_sfm(uchar add,uchar dat)
{
uchar shi,ge;
shi=dat/10;
ge=dat%10;
write_com(0x80+0x40+add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void keyscan()
{
if(shezhi==0)
{
delay(5);
if(shezhi==0)
{
shezhinum++;
while(!shezhi);
if(shezhinum==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
}
if(shezhinum==2)
{
write_com(0x80+0x40+7);
}
if(shezhinum==3)
{
write_com(0x80+0x40+4);
}
if(shezhinum==4)
{
shezhinum=0;
write_com(0x0c);
TR0=1;
}
}
if(shezhinum!=0)
{
if(add==0)
{
delay(5);
if(add==0)
{
while(!add);
if(shezhinum==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(shezhinum==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(shezhinum==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(dec==0)
{
delay(5);
if(dec==0)
{
while(!dec);
if(shezhinum==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(shezhinum==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(shezhinum==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
}
}
void main()
{
init();
SCON=0XFF;
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=0xdc;
count++;
if(count==100)
{
count=0;
miao++;
write_sfm(10,miao);
if(miao==60)
{
miao=0;
fen++;
write_sfm(7,fen);
if(fen==60)
{
fen=0;
shi++;
write_sfm(4,shi);
if(shi==24)
{
shi=0;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -