基于單片機(jī)定時(shí)器中斷的時(shí)鐘程序
大家只要自行更改端口就可以使用,走時(shí)還是比較準(zhǔn)確的
#includereg51.h>#define uchar unsigned char#define uint unsigned intsbit RING1=P3^7; //P3^7接蜂鳴 sbit OPEN=P3^1; //鬧鐘開時(shí)LED亮 uchar idata buffer[8]={0,0,0,0,0,0,10,11};//定義兩段緩沖區(qū),buffer用于過程中時(shí)間BCD碼暫存并初始化uchar code LED[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X40,0X40}; //LED段譯存儲(chǔ)于程序存儲(chǔ)區(qū)uchar miao=0;fen=58;shi=10; //定義全局變量用于miao,fen,shi用于存儲(chǔ)過程時(shí)間uchar _20ms;//定義全局變量_20ms用于計(jì)數(shù)中斷個(gè)數(shù),50個(gè)為1秒uchar hour2,minutes2,second2;//鬧鈴設(shè)置時(shí)間存儲(chǔ)區(qū)uchar hour1,minutes1,second1;//修改時(shí)間時(shí)暫存區(qū)bit hour,minutes,second;//修改相應(yīng)時(shí)的位標(biāo)志 bit add,dec;//加減位標(biāo)志bit openring,cancelring;//鬧鈴開、關(guān)、取消標(biāo)志 bit setring,settime;//鬧鈴設(shè)置、時(shí)間設(shè)置標(biāo)志 bit ok;//確定變量void delay_1ms(uchar x)//延時(shí)1ms{uchar j;while(x--){for(j=0;j123;j++){;}}}void inital(void)//定時(shí)器初始化函數(shù){ RING1=1; //這里RING等于0時(shí)蜂鳴器響;TMOD=0x01; //定時(shí)器0工作于方式1,采用12M晶振,定時(shí)20msTH0=(65536-20000)/256;TL0=(65536-20000)%256; TR0=1; //TR0=1 啟動(dòng)定時(shí)器EA=1; //開總中斷ET0=1; //開定時(shí)器中斷}void timer0 (void) interrupt 1 using 1 //定時(shí)0中斷函數(shù){ TH0=(65536-20000)/256;TL0=(65536-20000)%256; _20ms++;if(50==_20ms) //判斷是否到了一秒{ miao++; //到了一秒,秒++_20ms=0; if(miao==60) //六十秒到了嗎{ fen++; //到了份++miao=0;if(fen==60) //六十分到了嗎{ shi++; //到了時(shí)++fen=0;if(shi==24) //24小時(shí)到了嗎{ shi=0; //時(shí)清零fen=0;miao=0;}}}}if(openring==1) //鬧鐘開了嗎{ if((shi==hour2)(fen==minutes2)(miao==second2)) //定時(shí)時(shí)間到 鬧鈴響RING1=0;if(cancelring==1) //鬧鐘關(guān){RING1=1;cancelring=0;}} }void timebcd (uchar m,uchar f,uchar s) //時(shí)間變動(dòng)函數(shù),當(dāng)計(jì)時(shí)到達(dá)1秒時(shí)進(jìn)行時(shí)間變動(dòng) { buffer[0]=m%10; //shi、fen、miao轉(zhuǎn)換成BCD碼,主要為了數(shù)碼管的顯示buffer[1]=m/10;buffer[2]=f%10;buffer[3]=f/10;buffer[4]=s%10;buffer[5]=s/10;}void keyscan(void) //鍵掃描并確定鍵值函數(shù),該部分是時(shí)鐘各種功能實(shí)現(xiàn)的核心{ uchar scode,recode,value; //定義行列變量P2=0XF0; // 注意鍵盤與P2的接法,4*4鍵盤if ((P2 0XF0)!=0XF0){ delay_1ms(10);if ((P20XF0)!=0XF0){scode=0xfe;while((scode 0x10)!=0){ P2=scode;if ((P20XF0)!=0XF0){recode=(P2 0XF0)|0X0f;value=((~scode)+(~recode));switch(value){ case 0x11: hour=1;minutes=0;second=0;break;//時(shí),按到的鍵是修改時(shí)的功能鍵,hour是修改時(shí)的標(biāo)志位case 0x21: hour=0;minutes=1;second=0;break;//分,按到的鍵是修改分的功能鍵,minutes是修改分的標(biāo)志位;case 0x41: hour=0;minutes=0;second=1;break;//秒 與上面類似;case 0x81: add=1;dec=0;break;// 加 case 0x12: dec=1;add=0;break;//減case 0x22: setring=1;settime=0;second1=second2;minutes1=minutes2;hour1= hour2;break;//鬧鐘設(shè)置,case 0x42: settime=1;setring=0;second1=miao;minutes1=fen;hour1=shi;break;//時(shí)間設(shè)置case 0x82: setring=0;settime=0;break;//返回case 0x14: ok=1;break;//確定case 0x24: openring=1;OPEN=0;break;//開鬧鐘case 0x44: openring=0;OPEN=1;break;//關(guān)鬧鐘case 0x84:cancelring=1;break;// 取消響鈴 default: break;}}else scode=(scode1)|0x01;}} } } 注意:這里將鍵盤的值進(jìn)行了編碼,12個(gè)鍵盤進(jìn)行了編碼 ,特別注意 鬧鐘設(shè)置和時(shí)間設(shè)置的實(shí)現(xiàn)方法 ,他們共用了函數(shù) resettime,沒什么難的。 void resettime (viod) {if(hour==1){if (add==1){hour1++;add=0;}if( hour1==24)hour1=0;if (dec==1){hour1--;dec=0;}if (hour1==-1)hour1=23;}if(minutes==1){if (add==1){minutes1++;add=0;}if (minutes1==60)minutes1=0;if(dec==1){ minutes1--;dec=0;}if (minutes1==-1)minutes1=59;}if(second==1){ if (add==1){second1++;add=0;}if(second1==60)second1=0;if (dec==1){second1--;dec=0;}if(second1==-1)second1=59;}}display (void) //顯示函數(shù)子程序{ uchar j,n;for(n=0,j=0XFE;n8;j=((j1)|0X01),n++){P1=j; //P1接數(shù)碼管位選P0=LED[buffer[n]]; //P0接數(shù)碼管段選delay_1ms(5);}}void main (void){ inital(); // 調(diào)用定時(shí)器初始化for(;;) { timebcd(miao,fen,shi); keyscan(); //調(diào)用鍵掃描,確定功能if(settime==1) //當(dāng)按下的鍵是時(shí)間設(shè)置時(shí){ resettime();timebcd ( second1, minutes1,hour1);if(ok==1) //當(dāng)確定被按下時(shí),表明重設(shè)時(shí)間要覆蓋原來的時(shí)間,所以將miao,fen,shi重置,_20ms清零{ shi=hour1;fen=minutes1;miao=second1;_20ms=0;}} if(setring==1){ resettime();timebcd( second1, minutes1,hour1);if(ok==1){ hour2=hour1;minutes2=minutes1;second2=second1;}}display(); //調(diào)用顯示函數(shù)}}
評論