1602显示日期时间(jiange)
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar date[]="2017-06-02"; //10 4-7
uchar sfm[]="00-00-00"; //8 2-5
sbit lcden=P2^2;
sbit lcdrs=P2^1;
sbit lcdrw=P2^0;
sbit dula=P2^6;
sbit wela=P2^7;
sbit D1=P3^3;
uint i;
uint tt=0;
uint j=7;
uint flag=0;
uint shi,fen,miao;
uint shi =22 ; //change 小时
uint fen =59 ; //change 分钟
uint miao =55 ; //change 秒钟
void firstshow()
{
sfm[6]=(miao/10)+0x30;
sfm[7]=(miao%10)+0x30;
sfm[3]=(fen/10)+0x30;
sfm[4]=(fen%10)+0x30;
sfm[0]=(shi/10)+0x30;
sfm[1]=(shi%10)+0x30;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timeinit()
{
TMOD=0x01;
EA=1;
ET0=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
void write_com(uchar com) //写指令
{
lcdrs=0;
lcdrw=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) //写数据
{
lcdrs=1;
lcdrw=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0c); //开显示,显示光标;
write_com(0x06); //读或写一个字符后地址指针加1;
write_com(0x01); //关显示,不显示光标,光标不闪烁;
// write_com(0x80);
}
void main()
{
init();
timeinit();
firstshow();
write_com(0x80+0x02);
for(i=0;i<10;i++)
{
write_date(year);
delay(10);
D1=1;
}
while(1)
{
write_com(0x80+0x42);
for(i=0;i<8;i++) {
TR0=1;
if(tt==20){
tt=0;
miao++;
if(miao==60){ //miao -----------------------------------------
miao=0;
fen++;
if(fen==60){ //fen-------------------------------
fen=0;
shi++;
if(shi==24){ //shi --------------
shi=0;
} //shi---------------
sfm[0]=(shi/10)+0x30;
sfm[1]=(shi%10)+0x30;
} //fen ------------------------------------
sfm[3]=(fen/10)+0x30;
sfm[4]=(fen%10)+0x30;
} //miao ----------------------------------------------
sfm[6]=(miao/10)+0x30;
sfm[7]=(miao%10)+0x30;
}
write_date(sfm);
delay(5);
}
}
}
void time0() interrupt 1
{
D1=0;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
tt++;
}