51单片机时钟代码带秒表闹钟功能.docx
- 文档编号:27959121
- 上传时间:2023-07-06
- 格式:DOCX
- 页数:20
- 大小:15.95KB
51单片机时钟代码带秒表闹钟功能.docx
《51单片机时钟代码带秒表闹钟功能.docx》由会员分享,可在线阅读,更多相关《51单片机时钟代码带秒表闹钟功能.docx(20页珍藏版)》请在冰豆网上搜索。
51单片机时钟代码带秒表闹钟功能
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitbeep=P1^5;//蜂鸣器
sbitLED1=P1^6;//LED灯
sbitep=P2^7;//1602使能端
sbitrs=P2^6;//1602
sbitrw=P2^5;//1602
sbits0=P3^4;//停止闹铃和小灯
sbits1=P3^5;//功能键
sbits2=P3^6;//增大键
sbits3=P3^7;//减小键
sbits4=P3^1;//
sbits5=P3^2;
sbits6=P3^3;
sbits7=P3^0;
uchars1num,s4num,count,count1,judge=0;
charsec,min,hour,miao,fen,shi,sec1,min1,diwei;
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
voiddi()
{
beep=0;
delay(50);
beep=1;
}
bitlcd_bz()//测试LCD忙碌状态
{
bitresult;
rs=0;
rw=1;
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
result=(bit)(P0&0x80);
ep=0;
returnresult;
}
voidwrite_com(ucharcom)//写入指令到LCD
{
while(lcd_bz());
rs=0;
rw=0;
ep=0;
P0=com;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
voidwrite_data(uchardat)//写入字符显示数据到LCD
{
while(lcd_bz());//等待LCD空闲
rs=1;
rw=0;
ep=0;
P0=dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
voidwrite_time(ucharadd,uchardat)
{
ucharshi,ge;
shi=dat/10;
ge=dat%10;
write_com(0x80+0x40+add+2);
write_data(0x30+shi);
write_data(0x30+ge);
}
voidwrite_alarm(ucharadd,uchardat)
{
ucharshi1,ge1;
shi1=dat/10;
ge1=dat%10;
write_com(0x80+0x00+add+2);
write_data(0x30+shi1);
write_data(0x30+ge1);
}
voidinit()
{
ep=0;
min=0;
sec=0;
hour=0;
s1num=0;
count=0;
write_com(0x38);delay
(1);//clear
write_com(0x0c);delay
(1);
write_com(0x06);delay
(1);
write_com(0x01);delay
(1);
write_com(0x80);delay(5);
write_com(0x80+0x40+0);delay(5);
write_data('T');delay(5);
write_com(0x80+0x40+1);delay(5);
write_data('I');delay(5);
write_com(0x80+0x40+2);delay(5);
write_data('M');delay(5);
write_com(0x80+0x40+3);delay(5);
write_data('E');delay(5);
write_com(0x80+0x40+4);delay(5);
write_data(':
');delay(5);
write_com(0x80+0x40+8);delay(5);
write_data(':
');delay(5);
write_com(0x80+0x40+11);delay(5);
write_data(':
');delay(5);
write_com(0x80+0x00+0);delay(5);
write_data('A');delay(5);
write_com(0x80+0x00+1);delay(5);
write_data('L');delay(5);
write_com(0x80+0x00+2);delay(5);
write_data('A');delay(5);
write_com(0x80+0x00+3);delay(5);
write_data('R');delay(5);
write_com(0x80+0x00+4);delay(5);
write_data('M');delay(5);
write_com(0x80+0x00+5);delay(5);
write_data(':
');delay(5);
write_com(0x80+0x00+8);delay(5);
write_data(':
');delay(5);
write_com(0x80+0x00+11);
write_data(':
');delay(5);
sec=25;
min=15;
hour=17;
miao=30;
fen=15;
shi=17;
write_time(10,sec);delay(5);
write_time(7,min);delay(5);
write_time(4,hour);delay(5);
write_alarm(10,miao);delay(5);
write_alarm(7,fen);delay(5);
write_alarm(4,shi);delay(5);
TMOD=0x01;
TL0=0xb0;
TH0=0x3c;
EA=1;
ET0=1;
TR0=1;
}
voidkeyscan()//按键扫描
{
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!
s1);
di();
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+13);
delay(5);
write_com(0x0f);
delay(5);
}
if(s1num==2)
{
write_com(0x80+0x40+10);delay(5);
}
if(s1num==3)
{
write_com(0x80+0x40+7);delay(5);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);delay(5);
TR0=1;
}
}
}
if(s1num!
=0)
{
if(s2==0)
{delay(5);
if(s2==0)
{
while(!
s2);
di();
if(s1num==1)
{
sec++;
if(sec==60)
sec=0;
write_time(10,sec);
write_com(0x80+0x40+13);delay(5);
}
if(s1num==2)
{
min++;
if(min==60)
min=0;
write_time(7,min);
write_com(0x80+0x40+10);delay(5);
}
if(s1num==3)
{
hour++;
if(hour==24)
hour=0;
write_time(4,hour);
write_com(0x80+0x40+7);delay(5);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!
s3);
di();
if(s1num==1)
{
sec--;
if(sec<0)
sec=59;
write_time(10,sec);
write_com(0x80+0x40+13);delay(5);
}
if(s1num==2)
{
min--;
if(min<0)
min=59;
write_time(7,min);
write_com(0x80+0x40+10);delay(5);
}
if(s1num==3)
{
hour--;
if(hour<0)
hour=23;
write_time(4,hour);
write_com(0x80+0x40+7);delay(5);
}
}
}
}
}
voidkeyscan1()
{
if(s4==0)
{
delay(5);
if(s4==0)
{
s4num++;
while(!
s4);
di();
if(s4num==1)
{TR0=0;
write_com(0x80+0x00+13);
write_com(0x0f);
delay(25);
}
if(s4num==2)
{
write_com(0x80+0x00+10);
}
if(s4num==3)
{
write_com(0x80+0x00+7);
}
if(s4num==4)
{
s4num=0;
write_com(0x0c);
TR0=1;
}
}
}
if(s4num!
=0)
{
if(s5==0)
{
delay(5);
if(s5==0)
{
while(!
s5);
di();
if(s4num==1)
{
miao++;
if(miao==60)
miao=0;
write_alarm(10,miao);
write_com(0x80+0x00+13);
}
if(s4num==2)
{
fen++;
if(fen==60)
fen=0;
write_alarm(7,fen);
write_com(0x80+0x00+10);
}
if(s4num==3)
{
shi++;
if(shi==24)
shi=0;
write_alarm(4,shi);
write_com(0x80+0x00+7);
}
}
}
if(s6==0)
{
delay(5);
if(s6==0)
{
while(!
s6);
di();
if(s4num==1)
{
miao--;
if(miao<0)
miao=59;
write_alarm(10,miao);
write_com(0x80+0x00+13);
}
if(s4num==2)
{
fen--;
if(fen<0)
fen=59;
write_alarm(7,fen);
write_com(0x80+0x00+10);
}
if(s4num==3)
{
shi--;
if(shi<0)
shi=23;
write_alarm(4,shi);
write_com(0x80+0x00+7);
}
}
}
}
}
voidtimer0()interrupt1
{
if(s7==0)
{delay(5);
if(s7==0)
{while(!
s7)
di();
judge++;
}
}
if(judge==2)
{TL0=0xb0;
TH0=0x3c;
write_time(10,sec);
write_time(7,min);
write_time(4,hour);
write_alarm(10,diwei);
write_alarm(7,sec1);
write_alarm(4,min1);
count++;
if(count==20)
{count=0;
sec++;
if(sec==60)
{sec=0;min++;
if(min==60)
{min=0;
hour++;
if(hour==24)
{hour=0;
}
}
}
}
}
if(judge==3)
{judge=0;
sec1=0;
min1=0;
diwei=0;
write_alarm(10,miao);
write_alarm(7,fen);
write_alarm(4,shi);
write_alarm(10,miao);
write_alarm(7,fen);
write_alarm(4,shi);
}
if(judge==0)
{TL0=0xb0;
TH0=0x3c;
write_time(10,sec);
write_time(7,min);
write_time(4,hour);
count++;
if(count==20)
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}}}}}
if(judge==1)
{TL0=0xb0;
TH0=0x3c;
count++;
diwei=diwei+5;
if(diwei==100)
{diwei=0;
sec1++;
if(sec1==60)
{sec1=0;
min1++;
if(min1==60)
{min1=0;
}}}
write_alarm(4,min1);
write_alarm(7,sec1);
write_alarm(10,diwei);
if(count==20)
{count=0;
sec++;
if(sec==60)
{sec=0;min++;
if(min==60)
{min=0;
hour++;
if(hour==24)
{hour=0;
}
}
}
}
write_time(10,sec);
write_time(7,min);
write_time(4,hour);
}
}
voidmain()
{
init();
while
(1)
{inti=2,t=0;
keyscan();
keyscan1();
if(hour==shi&&fen==min&&miao==sec)
{t++;
while(t)
{for(;i>1;i++)
{if(s0!
=0)
{LED1=0;
beep=0;
delay(50);
beep=1;
delay(50);
beep=0;
delay(50);
beep=1;
delay(500);
}
if(s0==0)
delay(5);
if(s0==0)
{
i=0;
beep=1;
LED1=1;
t=0;
}
}
}
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 时钟 代码 秒表 闹钟 功能