我用單片機(jī)寫(xiě)的電子時(shí)鐘
學(xué)習(xí)單片機(jī)半個(gè)月了,學(xué)得也差不多了,今天寫(xiě)一個(gè)寫(xiě)程序練習(xí)練習(xí)哈哈
跟電子表的功能一模一樣,可以進(jìn)行設(shè)置,準(zhǔn)確度非常高哈哈,用的是1602型號(hào)的液晶顯示器,大家只需要簡(jiǎn)單的修改一下端口就能應(yīng)用到你自己的設(shè)計(jì)上去了.
下面是源代碼:
#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;}}
評(píng)論