|
最近學(xué)了數(shù)碼管的時(shí)鐘顯示和按鍵的控制,于是想到小時(shí)候戴過的幾塊錢的電子表,就想實(shí)現(xiàn)一個(gè),可以通過按鍵來手動(dòng)調(diào)時(shí)間!代碼寫好了,單片機(jī)也能跑起來,不按按鍵的時(shí)候,時(shí)鐘可以自動(dòng)實(shí)現(xiàn),但是有問題,就是當(dāng)我用按鍵實(shí)現(xiàn)對(duì)分鐘和時(shí)鐘的調(diào)整(這里是加1)后,這個(gè)時(shí)鐘就會(huì)停止運(yùn)行了!我的猜想是,按鍵實(shí)現(xiàn)后,程序沒有進(jìn)入到中斷里面了,所以時(shí)鐘停止!但不知道對(duì)不對(duì)!代碼如下- #include <reg52.h>
- sbit S5=P3^7;//按鍵控制分鐘的調(diào)整
- sbit S4=P3^6;//按鍵控制時(shí)鐘的調(diào)整
- sbit D1=P1^0;
- sbit D2=P1^1;
- sbit WELA=P2^7;
- sbit DULA=P2^6;
- #define uint unsigned int
- uint num ,a,b,c,d,e,f,g,x,y,z;
- void delay(uint z);//延時(shí)
- void display(uint a,uint b,uint c,uint d, uint e,uint f);//數(shù)碼管顯示函數(shù)
- unsigned char code table1[]=
- {
- 0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F
- };
- unsigned char code table2[]=
- {
- 0XDF,0XEF,0XF7,0XFB,0XFD,0XFE
- };
- void main()
- {
- TMOD=0X00;
- TH0=(8192-8000)/32;
- TL0=(8192-8000)%32;
- EA=1;
- ET0=1;
- TR0=1;
- while(1)
- {
- if(num==1)
- {
- num=0;
- a++;
- }
- if(S5==0)
- {
- delay(10);
- if(S5==0)
- {
- D1=0;
- c++;
- if(c==10)
- {
- d++;
- c=0;
- if(d==6)
- {
- e++;
- d=0;
- if(e==24)
- {
- e=0;
- }
- }
- }
- while(!S5);
- delay(10);
- while(!S5);
- }
- }
- if(S4==0)
- {
- delay(10);
- if(S4==0)
- {
- D2=0;
- e++;
- if(e==24)
- {
- e=0;
- }
- while(!S4);
- delay(10);
- while(!S4);
- }
- }
- else
- {
- D1=1;
- D2=1;
- }
- if(a==10)
- {
- b++;
- a=0;
- if(b==6)
- {
- c++;
- b=0;
- if(c==10)
- {
- d++;
- c=0;
- if(d==6)
- {
- e++;
- d=0;
- if(e==24)
- {
- e=0;
- }
- }
- }
- }
- }
- g=e%10;
- f=(e-g)/10;
- display(a,b,c,d,g,f);
- }
- }
- void display(uint a,uint b,uint c,uint d,uint g,uint f)
- {
- WELA=1;
- P0=table2[0];
- WELA=0;
- DULA=1;
- P0=table1[a];
- DULA=0;
- delay(2);
- WELA=1;
- P0=table2[1];
- WELA=0;
- DULA=1;
- P0=table1[b];
- DULA=0;
- delay(2);
- WELA=1;
- P0=table2[2];
- WELA=0;
- DULA=1;
- P0=table1[c];
- DULA=0;
- delay(2);
- WELA=1;
- P0=table2[3];
- WELA=0;
- DULA=1;
- P0=table1[d];
- DULA=0;
- delay(2);
- WELA=1;
- P0=table2[4];
- WELA=0;
- DULA=1;
- P0=table1[g];
- DULA=0;
- delay(2);
- WELA=1;
- P0=table2[5];
- WELA=0;
- DULA=1;
- P0=table1[f];
- DULA=0;
- delay(2);
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- {
- for(y=20;y>0;y--);
- }
- }
- void timero() interrupt 1
- {
- TH0=(8192-8000)/32;
- TL0=(8192-8000)%32;
- num++;
-
- }
復(fù)制代碼 代碼有點(diǎn)長(zhǎng),但邏輯上很簡(jiǎn)單,就是一個(gè)時(shí)鐘(時(shí)分秒的顯示),然后加上按鍵,出現(xiàn)的問題描述如上!請(qǐng)大家?guī)臀铱纯,感激不盡
|
|