我用单片机写的电子时钟
时间:11-29
来源:互联网
点击:
学习单片机半个月了,学得也差不多了,今天写一个写程序练习练习哈哈
跟电子表的功能一模一样,可以进行设置,准确度非常高哈哈,用的是1602型号的液晶显示器,大家只需要简单的修改一下端口就能应用到你自己的设计上去了.
下面是源代码:
#include#define uchar unsigned char#define uint unsigned int uchar code year_table[]=" 2010-04-11 SUN";uchar code time_table[]=" 00:00:00";uchar time_count,key_count;char hour,minute,second,mouth,day,week;uint year;sbit rs=P3^5;sbit lcden=P3^4;sbit dula=P2^6;sbit wela=P2^7;sbit key_set=P3^0;sbit key_inc=P3^1;sbit key_dec=P3^2;sbit rd=P3^7;void delay(uint delay_time){uint x,y;for(x=delay_time;x>0;x--)for(y=110;y>0;y--);}void write_com(uchar com){rs=0;lcden=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;}void write_date(uchar date){rs=1;lcden=0;P0=date;delay(5);lcden=1;delay(5);lcden=0;}void timer_Init(){TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}write_time(uchar addr,uchar time){uchar shi,ge;shi=time/10;ge=time%10;write_com(0x80+0x40+addr);write_date(0x30+shi);write_date(0x30+ge);}write_day(uchar addr,uchar day){uchar shi,ge;shi=day/10;ge=day%10;write_com(0x80+addr);write_date(0x30+shi);write_date(0x30+ge);}write_year(uchar addr,uint year){uchar shi,ge,qian,bei;qian=year/1000;bei=year%1000/100;shi=year%100%100/10;ge=year%10;write_com(0x80+addr);write_date(0x30+qian);write_date(0x30+bei);write_date(0x30+shi);write_date(0x30+ge);}write_week(uchar addr,uchar week){write_com(0x80+addr);switch(week){case 1:write_date(M);write_date(O);write_date(N);break;case 2:write_date(T);write_date(U);write_date(E);break;case 3:write_date(W);write_date(E);write_date(D);break;case 4:write_date(T);write_date(H);write_date(U);break;case 5:write_date(F);write_date(R);write_date(I);break;case 6:write_date(S);write_date(A);write_date(T);break;case 7:write_date(S);write_date(U);write_date(N);break;}} void Init(){uchar num;dula=0;wela=0;lcden=0;year=2010;mouth=4;day=11;week=7;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80);for(num=0;num<16;num++){write_date(year_table[num]);delay(5);}write_com(0x80+0x40);for(num=0;num<12;num++){write_date(time_table[num]);delay(5);}timer_Init();}void keyscan(){rd=0;if(key_set==0){delay(5);if(key_set==0){key_count++;while(!key_set);//等待键盘释放if(key_count==1){TR0=0;write_com(0x80+0x40+10);write_com(0x0f);}if(key_count==2){write_com(0x80+0x40+7);}if(key_count==3){write_com(0x80+0x40+4);}if(key_count==4){write_com(0x80+13);}if(key_count==5){write_com(0x80+10);}if(key_count==6){write_com(0x80+7);}if(key_count==7){write_com(0x80+2);}if(key_count==8){key_count=0;write_com(0x0c);TR0=1;}}}if(key_count!=0){if(key_inc==0){delay(5);if(key_inc==0){while(!key_inc);if(key_count==1){second++;if(second==60){second=0;}write_time(10,second);write_com(0x80+0x40+10);}if(key_count==2){minute++;if(minute==60){minute=0;}write_time(7,minute);write_com(0x80+0x40+7);}if(key_count==3){hour++;if(hour==24){hour=0;}write_time(4,hour);write_com(0x80+0x40+4);}if(key_count==4){week++;if(week==8){week=1;}write_week(13,week);write_com(0x80+13);}if(key_count==5){day++;if(day==32){day=1;}write_day(10,day);write_com(0x80+10);}if(key_count==6){mouth++;if(mouth==13){mouth=1;}write_day(7,mouth);write_com(0x80+7);}if(key_count==7){year++;write_year(2,year);write_com(0x80+2);}}}if(key_dec==0){delay(5);if(key_dec==0){while(!key_dec);if(key_count==1){second--;if(second==-1){second=59;}write_time(10,second);write_com(0x80+0x40+10);}if(key_count==2){minute--;if(minute==-1)minute=59;write_time(7,minute);write_com(0x80+0x40+7);}if(key_count==3){hour--;if(hour==-1)hour=23;write_time(4,hour);write_com(0x80+0x40+4);}if(key_count==4){week--;if(week==0){week=7;}write_week(13,week);write_com(0x80+13);}if(key_count==5){day--;if(day==0){day=31;}write_day(10,day);write_com(0x80+10);}if(key_count==6){mouth--;if(mouth==0){mouth=12;}write_day(7,mouth);write_com(0x80+7);}if(key_count==7){year--;if(year==2009){year=2010;}write_year(2,year);write_com(0x80+2);}}}}}void main(){Init();while(1){keyscan(); }}void clock_timer() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;time_count++;if(time_count==18){time_count=0;second++;if(second==60){second=0;minute++;if(minute==60){minute=0;hour++;if(hour==24){hour=0;}write_time(4,hour);}write_time(7,minute);}write_time(10,second);}}
单片机电子时 相关文章:
- 51单片机电子时钟程序(12-01)
- 用单片机做的电子时钟(11-29)
- 51单片机电子时钟(11-29)
- 单片机电子时钟程序(11-29)
- 通过51单片机实现电子时钟功能并通过LCD1602显示(11-26)
- 用51单片机做的电子时钟(11-23)