|
C51用第三個(gè)定時(shí)器timer2掃描驅(qū)動(dòng)6位數(shù)碼管顯示clock的方式:
大家都知道:
1. 通常C51驅(qū)動(dòng)6-8段數(shù)碼管,都習(xí)慣用delay延時(shí)掃描顯示方式,那么有沒有更好的驅(qū)動(dòng)方式?
2. C51有三個(gè)定時(shí)器,timer0,timer1用的多,都比較熟悉,那么定時(shí)器3(timer2)怎么使用?
本實(shí)例程序?qū)崿F(xiàn)了采用定時(shí)器3(timer2)快速掃描驅(qū)動(dòng)6位數(shù)碼管,并顯示clock(時(shí)分秒)的新方式,程序更簡潔,無delay延時(shí)函數(shù),運(yùn)行效率更高,而且屏顯效果更好(刷新更快,且無delay方式驅(qū)動(dòng)顯示殘影問題)。
單片機(jī)源程序如下:
- #include <reg52.h>
- //#include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char //宏定義
- sbit dula=P2^6; //數(shù)碼管段控制鎖存引腳定義聲明
- sbit wela=P2^7; //數(shù)碼管位控制鎖存引腳定義聲明
- uchar AA,BB,CC,DD,EE,FF;
- uchar displaynum,displaytm; //數(shù)碼管顯示函數(shù)名定義
- void dispay(uchar AA,uchar BB,uchar CC,uchar DD,uchar EE,uchar FF);
- //void delay(uint); //函數(shù)聲明
- void init();
- uint temp; //注意數(shù)據(jù)類型
- uchar tm,kp,hnum,fnum,num,numwe; //函數(shù)名定義
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
- 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,
- 0x79,0x71,0x80,0x76,0x38,0x00}; //數(shù)碼管字符表
- uchar code tablewe[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //數(shù)碼管位選參數(shù)表
- void main()
- {
- init(); //調(diào)用初始化子函數(shù)
- while(1)
- {
- if(tm==20)
- {
- tm=0; //定時(shí)器清零
- temp++;
- P1=~P1;
- if(temp==60)
- {
- temp=0;
- fnum++;
- if(fnum==60)
- {
- fnum=0;
- hnum++;
- if(hnum=24)
- hnum=0;
- }
- }
- AA=hnum/10; //時(shí)
- BB=hnum%10; //時(shí)
- CC=fnum/10; //分
- DD=fnum%10; //分
- EE=temp/10; //秒
- FF=temp%10; //數(shù)位提取
- }
- dispay(AA,BB,CC,DD,EE,FF);
- }
- }
- /*void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=100;y>0;y--);
- } */
- void dispay(uchar AA,uchar BB,uchar CC,uchar DD,uchar EE,uchar FF) //六位數(shù)碼管顯示驅(qū)動(dòng)
- {
- if(displaytm==1)
- {
- displaytm=0;
- displaynum++;
- if(displaynum==1)
- {
- dula=1;
- P0=table[AA];
- dula=0;
- P0=0xff; //消數(shù)碼管殘影
- wela=1;
- P0=tablewe[0];
- wela=0;
- }
- if(displaynum==2)
- {
- dula=1;
- P0=table[BB];
- dula=0;
- P0=0xff; //消數(shù)碼管殘影
- wela=1;
- P0=tablewe[1];
- wela=0;
- }
- if(displaynum==3)
- {
- dula=1;
- P0=table[CC];
- dula=0;
- P0=0xff; //消數(shù)碼管殘影
- wela=1;
- P0=tablewe[2];
- wela=0;
- }
- if(displaynum==4)
- {
- dula=1;
- P0=table[DD];
- dula=0;
- P0=0xff; //消數(shù)碼管殘影
- wela=1;
- P0=tablewe[3];
- wela=0;
- }
- if(displaynum==5)
- {
- dula=5;
- P0=table[EE];
- dula=0;
- P0=0xff; //消數(shù)碼管殘影
- wela=1;
- P0=tablewe[4];
- wela=0;
- }
- if(displaynum==6)
- {
- dula=1;
- P0=table[FF];
- dula=0;
- P0=0xff; //消數(shù)碼管殘影
- wela=1;
- P0=tablewe[5];
- wela=0;
- }
- if(displaynum==7)
- displaynum=0;
- }
- }
- void init()
- {
- P0=0xff; //消除數(shù)碼管殘影
- dula=0; //初始化關(guān)閉段選
- wela=0; //初始化關(guān)閉位選
- temp=0;
- num=0xfe; //點(diǎn)亮第LED1
- P1=num; //點(diǎn)亮第LED1
- hnum=12;
- fnum=0;
- TMOD=0x01; //設(shè)置定時(shí)器T0,T1為定時(shí)器模式
- TH0=0x4C; //T0定時(shí)器高8位初始值設(shè)置計(jì)時(shí)50ms
- TL0=0x00; //T0定時(shí)器低8位初始值設(shè)置計(jì)時(shí)50ms
- //TH1=0x4c; //T1定時(shí)器高8位初始值設(shè)置計(jì)時(shí)50ms
- //TL1=0x00; //T1定時(shí)器低8位初始值設(shè)置計(jì)時(shí)50ms
- EA=1; //開cpu中斷
- ET0=1; //開定時(shí)器T0中斷
- TR0=1;
- //ET1=1; //開定時(shí)器T1中斷
- //TR1=1;
- TH2=0x0f4; //T2定時(shí)器高8位初始值設(shè)置計(jì)時(shí)0.4ms
- TL2=0x48; //T2定時(shí)器低8位初始值設(shè)置計(jì)時(shí)0.4ms
- ET2=1; //開定時(shí)器中斷
- TR2=1; //開啟定時(shí)器T2
- }
- void timer_p0() interrupt 1
- {
- TH0=0x4c; //T0定時(shí)器高8位初始值設(shè)置再計(jì)時(shí)50ms
- TL0=0x00; //T0定時(shí)器低8位初始值設(shè)置再計(jì)時(shí)50ms
- tm++;
- }
- /*void timer_p1() interrupt 3
- {
- TH1=0x4C; //T0定時(shí)器高8位初始值設(shè)置再計(jì)時(shí)50ms
- TL1=0x00; //T0定時(shí)器低8位初始值設(shè)置再計(jì)時(shí)50ms
- tt++;
- }*/
- void timer_p2() interrupt 5 //定時(shí)器timer1中斷設(shè)置
- {
- TF2=0; //定時(shí)器T2必須由軟件對標(biāo)志位清零
- TH2=0x0f4; //T2定時(shí)器高8位初始值設(shè)置再計(jì)時(shí)0.4ms
- TL2=0x48; //T2定時(shí)器低8位初始值設(shè)置再計(jì)時(shí)0.4ms
- displaytm++;
- }
- // 采用C52第三個(gè)定時(shí)器timer2定時(shí)掃描方式顯示6位數(shù)碼管
復(fù)制代碼
所有資料51hei提供下載:
C51timer2-clock.rar
(31.5 KB, 下載次數(shù): 9)
2020-12-27 14:52 上傳
點(diǎn)擊文件名下載附件
|
評分
-
查看全部評分
|