基于51单片机的1602时钟
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
基于51单片机的1602时钟
基于51单片机的1602时钟,并使用1602显示倒计时。可通过按钮设置时间。经过调试验证。
源代码:
#include"reg52.h"
#include"def.h"
uchar code table3[] =" Time ";
uchar code table4[]=" 23:59:57 ";
uchar TH0_shu,TL0_shu;
uchar Tcount;
uchar shi,fen,miao;
sbit choice=P2^0;
sbit plus=P2^1;
sbit substract=P2^2;
uchar Kcount;
#include"LCD1602.h"
void TIMER0_Init()
{
TMOD=0x01;
TH0_shu=(65536-50000)/256;
TL0_shu=(65536-50000)%256;
TH0=TH0_shu;
TL0=TL0_shu;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
uchar shiw,ge;
LCD_Init();
TIMER0_Init(); xieshuzu(table3,0x80); xieshuzu(table4,0xc0);
shi=23;
fen=59;
miao=57;
Kcount=0;
for(;;)
{
if(Kcount==0)
{
if(Tcount>=20)
{
Tcount=0;
miao++;
}
if(miao>=60)
{
miao=0;
fen++;
}
if(fen>=60)
{
fen=0;
shi++;
}
if(shi>=24)
{
shi=0;
}
}
shiw=miao/10;
ge=miao%10;
xies(ge,0xca);
xies(shiw,0xc9);
shiw=fen/10;
ge=fen%10;
xies(ge,0xc7);
xies(shiw,0xc6);
shiw=shi/10;
ge=shi%10;
xies(ge,0xc4);
xies(shiw,0xc3);
if(choice==0)
delay(10);
{
if(choice==0)
Kcount++;
while(choice==0);
}
if(Kcount>=4)
Kcount=0;
if(Kcount==0)
{
TR0=1;
writec(0x0c);
}
else
{
TR0=0;
writec(0x0f);
}
if((Kcount>=1)&&(Kcount<=3))
{
if(plus==0)
delay(10);
if(plus==0)
{
switch(Kcount)
{
case(1):shi++;break;
case(2):fen++;break;
case(3):miao++;break;
default:break;
}
}
while(plus==0);
if(substract==0)
delay(10);
if(substract==0)
{
switch(Kcount)
{
case(1):shi--;break;
case(2):fen--;break;
case(3):miao--;break;
default:break;
}
}
while(substract==0);
if(fen>=60) fen=0;
if(miao>=60) miao=0;
if(shi>=24) shi=0;
}
};
}
void Intt0() interrupt 1
{
EA=0;
TH0=TH0_shu;
TL0=TL0_shu;
Tcount++;
EA=1;
}
sbit LCD_E=P3^4;
sbit LCD_RS=P3^5;
sbit LCD_RW=P3^6;
sfr LCD_DA TE=0x80;
uchar code table[] =" 0717014031 "; uchar code table2[]=" BYSJ ";
/***********************/
void writec(uint com)
{
LCD_E=0;
LCD_RS=0;
LCD_RW=0;
LCD_DA TE=com;
delay(1);
LCD_E=1;
delay(2);
LCD_E=0;
delay(1);
LCD_RW=1;
}
/***********************/
void writed(uint dat)
{
LCD_E=0;
LCD_RS=1;
LCD_RW=0;
LCD_DA TE=dat;
delay(1);
LCD_E=1;
delay(2);
LCD_E=0;
delay(1);
LCD_RW=1;
}
/***********************/
void xies(uchar a,uchar add)
{
writec(add);