基于8051和Proteus的數(shù)字時(shí)鐘 程序的編寫和仿真
本程序功能及特點(diǎn):通過8051芯片及數(shù)碼管實(shí)現(xiàn)數(shù)字時(shí)鐘,并可以通過如下圖中的兩個(gè)按鍵對(duì)時(shí)鐘進(jìn)行調(diào)節(jié),其中 “PAUSE”按鍵的作用是暫停/啟動(dòng)時(shí)鐘并選擇要修改的時(shí)間參數(shù)“時(shí)”,“分”,“秒”,被選擇參數(shù)例如下圖中從左到右依次為“時(shí),分,秒”,當(dāng)小時(shí)被選中并被修改時(shí)會(huì)有最下面兩點(diǎn)的點(diǎn)亮以作為提示,“CHANGE”按鍵用來對(duì)被選中的參數(shù)進(jìn)行修改,只能進(jìn)行加法修改,超過參數(shù)的上限會(huì)自動(dòng)歸零,
本文引用地址:http://www.ex-cimer.com/article/201610/311531.htm注:本程序僅供本人日后學(xué)習(xí)參考使用,不足之處請(qǐng)見諒,
#include
sbit dualock=P2^0;
sbit weilock=P2^1;
unsigned char code duacode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
unsigned char aa=0,hour=0,mint=0,sec=0,bb=0,*choose;
void display(unsigned char,unsigned char,unsigned char);
void delay();
void init();
void main()
{
init();
while(1)
{
display(hour,mint,sec);
}
}
void init() //初始化
{
EA=1; ET0=1;
EX0=1;IT0=1;
EX1=1;IT1=1;
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TR0=1;
}
void display(unsigned char hour,unsigned char mint,unsigned char sec) //顯示函數(shù)
{
weilock=1;
P0=~(1<<0);
weilock=0;
dualock=1;
P0=(bb==1)?duacode[sec%10]|0x80:duacode[sec%10];
dualock=0;
delay();
weilock=1;
P0=~(1<<1);
weilock=0;
dualock=1;
P0=(bb==1)?duacode[sec/10]|0x80:duacode[sec/10];
dualock=0;
delay();
weilock=1;
P0=~(1<<2);
weilock=0;
dualock=1;
P0=(bb==2)?duacode[mint%10]|0x80:duacode[mint%10];
dualock=0;
delay();
weilock=1;
P0=~(1<<3);
weilock=0;
dualock=1;
P0=(bb==2)?duacode[mint/10]|0x80:duacode[mint/10];
dualock=0;
delay();
weilock=1;
P0=~(1<<4);
weilock=0;
dualock=1;
P0=(bb==3)?duacode[hour%10]|0x80:duacode[hour%10];
dualock=0;
delay();
weilock=1;
P0=~(1<<5);
weilock=0;
dualock=1;
P0=(bb==3)?duacode[hour/10]|0x80:duacode[hour/10];
dualock=0;
delay();
}
void delay()
{
unsigned char i=30;
while(i--);
}
void ext0() interrupt 0 //外部中斷0 選中被修改參數(shù)
{
TR0=0; bb++;
switch(bb)
{
case 1: choose=&sec; break;
case 2: choose=&mint;break;
case 3: choose=&hour;break;
case 4: TR0=1;bb=0; break;
}
}
void timer0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
if(++aa==20) {aa=0;sec++;}
if(sec==60) {sec=0;mint++;}
if(mint==60) {mint=0;hour++;}
}
void ext1() interrupt 2 // 外部中斷2 對(duì)被選中參數(shù)進(jìn)行修改
{
*choose+=1;
if(sec==60) sec=0;
if(mint==60) mint=0;
if(hour==24) hour=0;
}
評(píng)論