资源描述:
《lcd1602液晶屏-i2c接口实验--时间显示》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、党湾一小学习资料LCD1602液晶屏I2C接口实验时间显示#include#include//LCD1602显示头文件LiquidCrystal_I2Clcd(0x27,20,4);//settheLCDaddressto0x27fora16charsand2linedisplayunsignedlongseconds;ints=0,m=0,h=0,d=0,mon=0,y=0;//时间进位intsecond=0,minute=0,hour=0,d
2、ay=0,month=0,year=0;//当前时间intSECOND=0,MINUTE=0,HOUR=0,DAY=0,MONTH=0,YEAR=0;//初始时间voidsetup(){lcd.init();lcd.backlight();lcd.begin(16,2);lcd.setCursor(2,1);lcd.print(":");lcd.setCursor(5,1);lcd.print(":");lcd.setCursor(4,0);lcd.print('-');lcd.setCursor(7,
3、0);lcd.print('-');for(inti=2;i<=13;i++){pinMode(i,OUTPUT);}set(2017,1,11,9,58,32);//设置初始时间}voidFormatDisplay(intcol,introw,intnum)//格式化输出{lcd.setCursor(col,row);if(num<10)lcd.print("0");lcd.print(num);}voidtime()//计算时间{second=(SECOND+seconds)%60;//计算秒m=(
4、SECOND+seconds)/60;//分钟进位FormatDisplay(6,1,second);minute=(MINUTE+m)%60;//计算分钟h=(MINUTE+m)/60;//小时进位FormatDisplay(3,1,minute);hour=(HOUR+h)%24;//计算小时4/4党湾一小学习资料d=(HOUR+h)/24;//天数进位FormatDisplay(0,1,hour);}intDays(intyear,intmonth)//根据年月计算当月天数{intdays=0;i
5、f(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
6、
7、year%400==0)days=29;elsedays=28;}returndays;}voidDay()//计算当月天数{intdays=Days(year,month);intda
8、ys_up;if(month==1)days_up=Days(year-1,12);elsedays_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);}4/4党湾一小学习资料voidMonth()//计算月份{month=(MONTH+mon)%12
9、;if(month==0)month=12;y=(MONTH+mon-1)/12;FormatDisplay(5,0,month);}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);}voidWee
10、k(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: