完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
大神求救~~!!小白已经绝望!!
实现的功能: 1、LCD1602显示当前日期和时间; 2、LCD1602实现秒表功能。 打开电源,LCD1602实现日期及时间显示, 第一行显示年月日,,格式为2017/03/24,第二行 显示当前时间,格式为22:34:43。按键123可以调节时间。 当按下独立按键4时,LCD1602处于秒表模式,当按下独立按 键5时,启动秒表,再按按键5,秒表停止,再按 按键6,返回日期和时间显示界面。 程序: #include #include #define uchar unsigned char #define uint unsigned int void LCD_Initialize(); void LCD_Set_POS(uchar); void LCD_Write_Data(uchar); void Display_String(uchar*,uchar); void LCD_Write_Command(uchar); ***it K1=P3^1; ***it K2=P3^0; ***it K3=P3^2; ***it K4=P3^3; ***it k5=P3^4; ***it k6=P3^5; ***it lcd_rs=P2^6; ***it lcd_rw=P2^5; ***it lcd_en=P2^7; uchar KeyCount=0; uchar code firstline[]="2017-03-27"; uchar code secondline[]="16-34-00"; uchar code msg1[]={" Second Watch 0 "}; uchar code msg2[]={">>>> "}; uchar code Prompts[][16]= { {"::1----> "}, {"::1----> ::2 "}, {"::1->2 ::3--> "}, {"::1->2 ::3-->4"} }; uchar Time_Buffer[]={0,0,0,0}; uchar LCD_Display_Buffer[]={"00:00:00:00"}; uchar i; uchar hour,minutes,seconds; uchar count,num; uchar cnt; void delay(uint z) { uint x,y; for(x=z;x>0;x--) { for(y=0;y<=112;y++) { } } } void LCD_Write_Command(uchar cmd) { lcd_rs=0; lcd_rw=0; lcd_en=0; P0=cmd; delay(100); lcd_en=1; delay(100); lcd_en=0; } void LCD_Write_Data(uchar str) { lcd_rs=1; lcd_rw=0; lcd_en=0; P0=str; delay(5); lcd_en=1; delay(5); lcd_en=0; } void LCD_Initialize() { lcd_en=0; delay(5);LCD_Write_Command(0x38); delay(5);LCD_Write_Command(0x0f); delay(5);LCD_Write_Command(0x06); delay(5);LCD_Write_Command(0x80); delay(5); } void Time1init() { TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void Clockinit() { hour=0; minutes=0; seconds=0; count=0; num=0; } void display(uchar hour,uchar minutes,uchar seconds) { uchar hourge,hourshi,minutesshi,minutesge,secondsshi,secondsge; hourshi=hour/10; hourge=hour%10; LCD_Write_Command(0x80+0x40); LCD_Write_Data(hourshi+48); delay(1); LCD_Write_Data(hourge+48); delay(1); minutesshi=minutes/10; minutesge=minutes%10; LCD_Write_Command(0x80+0x43); LCD_Write_Data(minutesshi+48); delay(1); LCD_Write_Data(minutesge+48); delay(1); secondsshi=seconds/10; secondsge=seconds%10; LCD_Write_Command(0x80+0x46); LCD_Write_Data(secondsshi+48); delay(1); LCD_Write_Data(secondsge+48); delay(1); } void revise() { if(K1==0) { delay(5); if(K1==0) { cnt++; TR0=0; } while(!K1); delay(5); while(!K1); } if(cnt==1) { LCD_Write_Command(0x80+0x46); LCD_Write_Command(0x0f); if(K2==0) { delay(5); if(K2==0) { seconds++; if(seconds==60) { seconds=0; } } while(!K2); delay(5); while(!K2); } if(K3==0) { delay(5); if(K3==0) { seconds--; if(seconds==-1)//here is a bug { seconds=59; } } while(!K3); delay(5); while(!K3); } } if(cnt==2) { LCD_Write_Command(0x80+0x43); LCD_Write_Command(0x0f); if(K2==0) { delay(5); if(K2==0) { minutes++; if(minutes==60) { minutes=0; } } while(!K2); delay(5); while(!K2); } if(K3==0) { delay(5); if(K3==0) { minutes--; if(minutes==-1) { minutes=59; } } while(!K3); delay(5); while(!K3); } } if(cnt==3) { LCD_Write_Command(0x80+0x40); LCD_Write_Command(0x0f); if(K2==0) { delay(5); if(K2==0) { hour++; if(hour==24) { hour=0; } } while(!K2); delay(5); while(!K2); } if(K3==0) { delay(5); if(K3==0) { hour--; if(hour==-1) { hour=23; } } while(!K3); delay(5); while(!K3); } } if(cnt==4) { TR0=1; cnt=0; } } void Show_Second() { uchar i; LCD_Set_POS(0x45); for(i=3;i!=0xff;i--) { LCD_Display_Buffer[2*i+1]=Time_Buffer[i]/10+'0'; LCD_Display_Buffer[2*i ]=Time_Buffer[i]%10+'0'; LCD_Write_Data(LCD_Display_Buffer[2*i+1]); LCD_Write_Data(LCD_Display_Buffer[2*i]); LCD_Write_Data(':'); } } void Time0() interrupt 1 { TH0=-10000/256; TL0=-10000%256; Time_Buffer[0]++; if(Time_Buffer[0]==100) { Time_Buffer[0]=0; Time_Buffer[1]++; } if(Time_Buffer[1]==60) { Time_Buffer[1]=0; Time_Buffer[2]++; } if(Time_Buffer[2]==60) { Time_Buffer[2]=0; Time_Buffer[3]++; } if(Time_Buffer[3]==24) Time_Buffer[3]=0; } void Time1() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++; if(count==20) { count=0; seconds++; if(seconds==60) { seconds=0; minutes++; if(minutes==60) { minutes=0; hour++; if(hour==24) { hour=0; } } } } } void main() { LCD_Initialize(); Time1init(); Clockinit(); while(1) { revise(); display(hour,minutes,seconds); for(i=0;i<10;i++) { LCD_Write_Data(firstline[i]); delay(1); } LCD_Write_Command(0x80+0x40); for(i=0;i<8;i++) { LCD_Write_Data(secondline[i]); delay(1); } if(K4==0) { uchar i; IE=0x82; TMOD=0x01; TH0=-10000/256; TL0=-10000%256; LCD_Initialize(); Display_String(msg1,0x00); Display_String(msg2,0x40); while(1) { if(k5==0) { delay(100); i=++KeyCount; switch(i) { case 1: case 3:TR0 =1; Display_String(Prompts[i-1],0); break; case 2: case 4:TR0 =0; Display_String(Prompts[i-1],0); break; default:TR0=0; break; } while (k5==0); } else if(k6==0) { TR0=0; KeyCount=0; for(i=0;i<4;i++) Time_Buffer[i]=0; Display_String(msg1,0); delay(100); while(k6==0) ; } Show_Second(); } } } } void LCD_Set_POS(uchar Position) { LCD_Write_Command(Position|0x80); } void Display_String(uchar*str,uchar LineNo) { uchar k; LCD_Set_POS(LineNo); for(k=0;k<16;k++) LCD_Write_Data (str[k]); } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 06:43 , Processed in 0.589325 second(s), Total 55, Slave 41 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号