|
|
我這個(gè)程序功能很簡(jiǎn)單:通過(guò)tx-1c實(shí)驗(yàn)班的第一個(gè)矩陣鍵盤(pán)按鍵開(kāi)始計(jì)時(shí)
1、燒寫(xiě)在板子上按鍵下去可以計(jì)時(shí),但是數(shù)碼管只有當(dāng)個(gè)位數(shù)變動(dòng)時(shí),十位數(shù)的顯示就閃一下,
就是說(shuō)個(gè)位數(shù)的數(shù)字顯示正常,但是十位數(shù)的數(shù)字一秒閃一次,想請(qǐng)教一下是什么問(wèn)題?
2、我想讓按鍵能實(shí)現(xiàn)開(kāi)始和暫停的功能,請(qǐng)問(wèn)我應(yīng)該怎么改?
3、還有想問(wèn)一個(gè)小知識(shí)點(diǎn),keil中的bit關(guān)鍵字定義的變量如果沒(méi)有初始化,那么這個(gè)變量初始值是1還是0呢?
剛開(kāi)始學(xué)單片機(jī),很多東西不是很懂,希望各位大佬能幫我解答一下,如果我的代碼寫(xiě)得不好也希望能給我一些小建議吧,謝謝了。
單片機(jī)源程序如下:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code led[]={0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f}; //數(shù)碼管段選信息
- uchar single, tenth;
- uchar flag;
- sbit dula=P2^6;
- sbit wela=P2^7;
- uint num=1;
- void delay(uchar ms)
- {
- uchar i,j;
- for(i=ms;i>0;i--)
- for(j=110;j>0;j--) ;
- }
- void display(uchar tenth, uchar single) //數(shù)碼管顯示函數(shù)
- {
- dula=1;
- P0=led[tenth];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfe;
- wela=0;
- delay(5);
- dula=1;
- P0=led[single];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfd;
- wela=0;
- delay(5);
- }
- void keyscan() //鍵盤(pán)檢測(cè)
- {
- uchar temp;
- P3=0xfe;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xee: flag=1;
- break;
-
- }
-
- while(temp!=0xf0)
- {
- temp=P3;
- temp=0xf0;
- }
- }
- }
- }
- void main()
- {
- single=0;
- tenth=0;
- display(tenth,single);
- TMOD=0x02; //利用定時(shí)器0方式2計(jì)數(shù)
- TL0=6; //方式2為初值自動(dòng)重裝的8位定時(shí)器
- TH0=6;
- keyscan(); //檢測(cè)按鍵是否被按下
- EA=1;
- ET0=1;
- TR0=1;
- while(flag) //當(dāng)檢測(cè)按鍵被按下開(kāi)始計(jì)時(shí)
- {
- if(num==3686) //中斷3686次相當(dāng)于1s
- {
- num=1;
- single++;
- if(single==10)
- {
- single=0;
- tenth++;
- }
- display(tenth,single);
- }
-
- }
- }
- void T0_time() interrupt 1 //中斷函數(shù)
- {
- num++;
- }
復(fù)制代碼
|
|