找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2170|回复: 4
打印 上一主题 下一主题
收起左侧

电子时钟课程设计

[复制链接]
跳转到指定楼层
楼主
代码
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P3^5;
sbit lcden=P3^4;
sbit s1=P3^0;  
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;
sbit beep=P2^2;
sbit led=P2^3; //定义 LED灯端
uchar count,s1num,s4num;
char year,month,day,week,miao,shi,fen,pshi;
uchar code table[]=" 20 - - ";
void delay(uint z) //延时 函数
{
      uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void zhuanhuan()
{
if(shi==0)
pshi=12;
if(shi<=12&&shi>=1)
pshi=shi;
if(shi>12)
pshi=shi-12;
}
void di()//蜂鸣器发声函数
{
beep=0;
delay(300);
beep=1;
delay(300);
}
void write_com(uchar com)//液晶写命令函数
{
lcdrs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)//液晶写数据函数
{
lcdrs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void_shi()
{
if(shi>=12)
{
write_com(0x80+0x40+1);
write_date('P');
write_date('M');
}
else
{
write_com(0x80+0x40+1);
write_date('A');
write_date('M');
}
}
void write_ymd(uchar add1,uchar date1)
{
uchar shi,ge;
shi=date1/10;
ge=date1%10;
write_com(0x80+add1);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_week(char week)
{
write_com(0x80+12);
switch(week)
{
case 1:write_date('M');delay(5);
       write_date('O');delay(5);
    write_date('N');
    break;
case 2:write_date('T');delay(5);
       write_date('U');delay(5);
    write_date('E');
    break;
case 3:write_date('W');delay(5);
       write_date('E');delay(5);
    write_date('D');
    break;
case 4:write_date('T');delay(5);
       write_date('H');delay(5);
    write_date('U');
    break;
   
case 5:write_date('F');delay(5);
       write_date('R');delay(5);
    write_date('I');
    break;
case 6:write_date('S');delay(5);
       write_date('A');delay(5);
    write_date('T');
    break;
case 7:write_date('S');delay(5);
       write_date('U');delay(5);
    write_date('N');
    break;
    }
    }
void writeym()
{
day=1;
month++;
if(month==13)
{
month=1;
year++;
if(year==100)
year=0;
write_ymd(3,year);
}
write_ymd(6,month);
}
void init()
{
uchar num;
lcden=0;
year=13;
month=6;
day=21;
week=5;
shi=13;
fen=59;
miao=58;
count=0;
s1num=0;
s4num=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
delay(5);
write_com(0x80+0x40+6);
write_date(':');
delay(5);
write_week(week);
write_ymd(3,year);
write_ymd(6,month);
write_ymd(9,day);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
  TR0=0;
  write_com(0x80+3);
  write_com(0x0f);
  }
  if(s1num==2)
  {
  write_com(0x80+6);
  }
  if(s1num==3)
  {
  write_com(0x80+9);
  }
  if(s1num==4)
  {
  write_com(0x80+12);
  }
  if(s1num==7)
  {
  write_com(0x80+0x40+10);
  }
  if(s1num==6)
  {
  write_com(0x80+0x40+7);
  }
  if(s1num==5)
  {
  write_com(0x80+0x40+4);
  }
  if(s1num==8)
  {
  s1num=0;
  write_com(0x0c);
  TR0=1;
  }
  }
  }
  if(s1num!=0)
  {
  if(s2==0)
  {
  delay(5);
  if(s2==0)
  {
  while(!s2);
  if(s1num==1)
  {
  year++;
  if(year==100)
  year=0;
  write_ymd(3,year);
  write_com(0x80+3);
  }
  if(s1num==2)
  {
  month++;
  if(month==13)
  month=1;
  write_ymd(6,month);
  write_com(0x80+6);
  }
  if(s1num==3)
  {
day++;
if(year%4==0&&month==2)
{
if(day==30)
day=1;
}
if(year%4!=0&&month==2)
{
if(day==29)
day=1;
}
if(month!=2&&month!=4&&month!=6&&month!=9&&month==11)
{
if(day==32)
day=1;
}
if(month==4||month==6||month==9||month==11)
{
if(day==31)
day=1;
}
write_ymd(9,day);
write_com(0x80+9);
}
if(s1num==4)
{
week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+12);
}
if(s1num==7)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==6)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==5)
{
shi++;
if(shi==24)
{
shi=0;
}
if(s4num==0)
{
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
if(s4num==1)
{
zhuanhuan();
_shi();
write_sfm(4,pshi);
write_com(0x80+0x40+4);
}
}
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
year--;
if(year==-1)
year=99;
write_ymd(3,year);
write_com(0x80+3);
}
if(s1num==2)
{
month--;
if(month==0)
month=12;
write_ymd(6,month);
write_com(0x80+6);
}
if(s1num==3)
{
day--;
if(year%4==0&&month==2)
{
if(day==0)
day=29;
}
if(year%4!=0&&month==2)
{
if(day==0)
day=28;
}
if(month!=2&&month!=4&&month!=6&&month!=9&&month!=11)
{
if(day==0)
day=31;
}
if(month==4||month==6||month==9||month==11)
{
if(day==0)
day=30;
}
write_ymd(9,day);
write_com(0x80+9);
}
if(s1num==4)
{
week--;
if(week==0)
week=7;
write_week(week);
write_com(0x80+12);
}
if(s1num==7)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==6)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==5)
{
shi--;
if(shi==-1)
shi=23;
if(s4num==0)
{
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
if(s4num==1)
{
zhuanhuan();
_shi();
write_sfm(4,pshi);
write_com(0x80+0x40+4);
}
}
}
}
if(s1num==0)
{
if(s4==0)
{
delay(5);
if(s4==0)
{
s4num++;
while(!s4);
if(s4num==1)
{
zhuanhuan();
_shi();
write_sfm(4,pshi);
}
if(s4num==2)
{
s4num=0;
write_com(0x80+0x40+1);
write_sfm(4,shi);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
if(s1num!=0)
led=0;
else
led=miao%2;
if((shi>7&&shi<23)&&(fen==0)&&(miao==0))
{
di();
}
}
}
void timer0()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
week++;
if(week==8)
{
week=1;
}
write_week(week);
day++;
if(year%4==0&&month==2)
{
if(day==30)
{
writeym();
}
write_ymd(9,day);
}
if(year%4!=0&&month==2)
{
if(day==29)
{
writeym();
}
write_ymd(9,day);
}
if(month!=2&&month!=4&&month!=6&&month!=9&&month!=11)
{
if(day==32)
{
writeym();
}
write_ymd(9,day);
}
if(month==4||month==6||month==9||month==11)
{
if(day==31)
{
writeym();
}
write_ymd(9,day);
}
}
if(s4num==1);
{
zhuanhuan();
_shi();
write_sfm(4,pshi);
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}

QQ图片20181127155832.png (522.46 KB, 下载次数: 50)

QQ图片20181127155832.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:544367 发表于 2019-6-10 14:07 | 只看该作者
代码没注释啊??
回复

使用道具 举报

板凳
ID:559597 发表于 2019-6-14 18:41 | 只看该作者
有这个的完整设计吗
回复

使用道具 举报

地板
ID:559597 发表于 2019-6-14 18:43 | 只看该作者
都是能实现什么功能啊
回复

使用道具 举报

5#
ID:659524 发表于 2019-12-11 15:26 来自手机 | 只看该作者
仿真出不来
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表