LCD1602电子时钟.docx
- 文档编号:25089429
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:7
- 大小:35.51KB
LCD1602电子时钟.docx
《LCD1602电子时钟.docx》由会员分享,可在线阅读,更多相关《LCD1602电子时钟.docx(7页珍藏版)》请在冰豆网上搜索。
LCD1602电子时钟
Arduino LCD1602电子时钟
利用LCD显示时间,日期、星期,并能通过按键修改时间
Proteus ISIS仿真图:
程序如下:
/**LCDRSpintodigitalpin12
*LCDEnablepintodigitalpin11
*LCDD4pintodigitalpin5
*LCDD5pintodigitalpin4
*LCDD6pintodigitalpin3
*LCDD7pintodigitalpin2
*LCDR/Wpintoground
*LCDVSSpintoground
*LCDVCCpinto5V */
#include
LiquidCrystallcd(12,11,5,4,3,2);
#definechoose 8 //选择端口
#defineadd 6 //加
#defineminus 7 //减
unsignedlongseconds;
int s=0,m=0,h=0,d=0,mon=0,y=0; //时间进位
int second=0,minute=0,hour=0,day=0,month=0,year=0; //当前时间
int SECOND=0,MINUTE=0,HOUR=0,DAY=0,MONTH=0,YEAR=0; //初始时间
intchose=0,ButtonDelay=10;
voidsetup()
{
for(inti=2;i<=13;i++)
pinMode(i,OUTPUT);
digitalWrite(add,HIGH);
digitalWrite(minus,HIGH);
digitalWrite(choose,HIGH);
lcd.begin(16,2); //初始化LCD1602
set(2015,5,17,12,30,50);
}
voidFormatDisplay(intcol,introw,intnum) //格式化输出
{
lcd.setCursor(col,row);
if(num<10) lcd.print("0");
lcd.print(num);
}
voidtime() //计算时间
{
second=(SECOND+seconds)%60; //计算秒
m=(SECOND+seconds)/60; //分钟进位
FormatDisplay(6,1,second);
minute=(MINUTE+m)%60; //计算分钟
h=(MINUTE+m)/60; //小时进位
FormatDisplay(3,1,minute);
hour=(HOUR+h)%24; //计算小时
d=(HOUR+h)/24; //天数进位
FormatDisplay(0,1,hour);
lcd.setCursor(2,1); lcd.print(":
");
lcd.setCursor(5,1); lcd.print(":
");
}
intDays(intyear,intmonth) //根据年月计算当月天数
{
intdays=0;
if(month!
=2)
{
switch(month)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
days=31; break;
case4:
case6:
case9:
case11:
days=30; break;
}
}
else//闰年
{
if(year%4==0&&year%100!
=0||year%400==0) days=29;
else days=28;
}
returndays;
}
voidDay() //计算当月天数
{
intdays=Days(year,month);
intdays_up;
if(month==1) days_up=Days(year-1,12);
else days_up=Days(year,month-1);
day=(DAY+d)%days;
if(day==0) day=days;
if((DAY+d)==days+1)
{
DAY-=days;
mon++;
}
if((DAY+d)==0)
{
DAY+=days_up;
mon--;
}
FormatDisplay(8,0,day);
}
voidMonth() //计算月份
{
month=(MONTH+mon)%12;
if(month==0) month=12;
y=(MONTH+mon-1)/12;
FormatDisplay(5,0,month);
lcd.setCursor(7,0); lcd.print('-');
}
voidYear() //计算年份
{
year=(YEAR+y)%9999;
if(year==0)year=9999;
lcd.setCursor(0,0);
if(year<1000) {lcd.print("0");}
if(year<100) {lcd.print("0");}
if(year<10) {lcd.print("0");}
lcd.print(year);
lcd.setCursor(4,0); lcd.print('-');
}
voidWeek(inty,intm,intd) //根据年月日计算星期几
{
if(m==1)m=13;
if(m==2)m=14;
intweek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7+1;
Stringweekstr="";
switch(week)
{
case1:
weekstr="Mon."; break;
case2:
weekstr="Tues."; break;
case3:
weekstr="Wed."; break;
case4:
weekstr="Thur."; break;
case5:
weekstr="Fri."; break;
case6:
weekstr="Sat."; break;
case7:
weekstr="Sun."; break;
}
lcd.setCursor(11,0); lcd.print(weekstr);
}
voidDisplay() //显示时间、日期、星期
{
time();
Day();
Month();
Year();
Week(year,month,day);
}
voidDisplayCursor(introl,introw) //显示光标
{
lcd.setCursor(rol,row);
lcd.cursor();
delay(100);
lcd.noCursor();
delay(100);
}
voidset(inty,intmon,intd,inth,intm,ints) //设置初始时间
{
YEAR=y;
MONTH=mon;
DAY=d;
HOUR=h;
MINUTE=m;
SECOND=s;
}
voidSet_Time(introl,introw,int&Time) //通过按键设置时间
{
DisplayCursor(rol,row);
if(digitalRead(add)==LOW)
{
delay(ButtonDelay);
if(digitalRead(add)==LOW)
Time++;
Display();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- LCD1602 电子 时钟