|
此程序可使用不同P口的任意單片機引腳驅(qū)動數(shù)碼管顯示。包含程序和仿真,仿真已驗證OK。
分享給各位學(xué)習(xí)參考。
圖片1.jpg (13.99 KB, 下載次數(shù): 60)
下載附件
2018-1-22 16:22 上傳
單片機源程序如下:
- #include "reg51.h"
- #define uchar unsigned char //重命名關(guān)鍵字
- #define uint unsigned int //重命名關(guān)鍵字
- sbit K1=P3^0; //開始/暫停
- sbit K2=P3^1; //復(fù)位
- sbit GPIO_A=P1^0; //數(shù)據(jù)端a
- sbit GPIO_B=P0^1; //數(shù)據(jù)端b
- sbit GPIO_C=P1^1; //數(shù)據(jù)端c
- sbit GPIO_D=P1^2; //數(shù)據(jù)端d
- sbit GPIO_E=P0^2; //數(shù)據(jù)端e
- sbit GPIO_F=P1^3; //數(shù)據(jù)端f
- sbit GPIO_G=P0^3; //數(shù)據(jù)端g
- sbit GPIO_H=P1^4; //數(shù)據(jù)端dp
- sbit L1=P2^0; //數(shù)碼管1位選
- sbit L2=P2^1; //數(shù)碼管2位選
- sbit L3=P2^2; //數(shù)碼管3位選
- unsigned char code DIG_CODE[10] = {0,1,2,3,4,5,6,7,8,9};
- //0、1、2、3、4、5、6、7、8、9的顯示碼
- unsigned char DisplayData[3];
- //用來存放要顯示的3位數(shù)的值
- uchar i,Key_Flag_Idx; //定義變量
- uint Second_Counts; //定義變量
- bit Key_State; //定義變量
- void DelayMS(uint ms) //延時子函數(shù)
- {
- uchar t; //定義變量
- while(ms--)
- for(t=0;t<120;t++);
- }
- void DuanXuan(unsigned char dat) //發(fā)送段碼0~9
- {
- if(dat==0)
- {GPIO_H=0;GPIO_G=0;GPIO_F=1;GPIO_E=1;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'0'
- if(dat==1)
- {GPIO_H=0;GPIO_G=0;GPIO_F=0;GPIO_E=0;GPIO_D=0;GPIO_C=1;GPIO_B=1;GPIO_A=0;} //'1'
- if(dat==2)
- {GPIO_H=0;GPIO_G=1;GPIO_F=0;GPIO_E=1;GPIO_D=1;GPIO_C=0;GPIO_B=1;GPIO_A=1;} //'2'
- if(dat==3)
- {GPIO_H=0;GPIO_G=1;GPIO_F=0;GPIO_E=0;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'3'
- if(dat==4)
- {GPIO_H=0;GPIO_G=1;GPIO_F=1;GPIO_E=0;GPIO_D=0;GPIO_C=1;GPIO_B=1;GPIO_A=0;} //'4'
- if(dat==5)
- {GPIO_H=0;GPIO_G=1;GPIO_F=1;GPIO_E=0;GPIO_D=1;GPIO_C=1;GPIO_B=0;GPIO_A=1;} //'5'
- if(dat==6)
- {GPIO_H=0;GPIO_G=1;GPIO_F=1;GPIO_E=1;GPIO_D=1;GPIO_C=1;GPIO_B=0;GPIO_A=1;} //'6'
- if(dat==7)
- {GPIO_H=0;GPIO_G=0;GPIO_F=0;GPIO_E=0;GPIO_D=0;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'7'
- if(dat==8)
- {GPIO_H=0;GPIO_G=1;GPIO_F=1;GPIO_E=1;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'8'
- if(dat==9)
- {GPIO_H=0;GPIO_G=1;GPIO_F=1;GPIO_E=0;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'9'
- if(dat==10)
- {GPIO_H=0;GPIO_G=0;GPIO_F=0;GPIO_E=0;GPIO_D=0;GPIO_C=0;GPIO_B=0;GPIO_A=0;} //全滅
- }
- void DuanXuan2(unsigned char dat) //發(fā)送段碼0.~9.
- {
- if(dat==0)
- {GPIO_H=1;GPIO_G=0;GPIO_F=1;GPIO_E=1;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'0.'
- if(dat==1)
- {GPIO_H=1;GPIO_G=0;GPIO_F=0;GPIO_E=0;GPIO_D=0;GPIO_C=1;GPIO_B=1;GPIO_A=0;} //'1.'
- if(dat==2)
- {GPIO_H=1;GPIO_G=1;GPIO_F=0;GPIO_E=1;GPIO_D=1;GPIO_C=0;GPIO_B=1;GPIO_A=1;} //'2.'
- if(dat==3)
- {GPIO_H=1;GPIO_G=1;GPIO_F=0;GPIO_E=0;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'3.'
- if(dat==4)
- {GPIO_H=1;GPIO_G=1;GPIO_F=1;GPIO_E=0;GPIO_D=0;GPIO_C=1;GPIO_B=1;GPIO_A=0;} //'4.'
- if(dat==5)
- {GPIO_H=1;GPIO_G=1;GPIO_F=1;GPIO_E=0;GPIO_D=1;GPIO_C=1;GPIO_B=0;GPIO_A=1;} //'5.'
- if(dat==6)
- {GPIO_H=1;GPIO_G=1;GPIO_F=1;GPIO_E=1;GPIO_D=1;GPIO_C=1;GPIO_B=0;GPIO_A=1;} //'6.'
- if(dat==7)
- {GPIO_H=1;GPIO_G=0;GPIO_F=0;GPIO_E=0;GPIO_D=0;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'7.'
- if(dat==8)
- {GPIO_H=1;GPIO_G=1;GPIO_F=1;GPIO_E=1;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'8.'
- if(dat==9)
- {GPIO_H=1;GPIO_G=1;GPIO_F=1;GPIO_E=0;GPIO_D=1;GPIO_C=1;GPIO_B=1;GPIO_A=1;} //'9.'
- }
- void DigDisplay() //數(shù)碼管顯示子函數(shù)
- {
- unsigned int j; //定義變量
- L1=0; //位選
- DuanXuan(DisplayData[0]);//發(fā)送段碼
- j = 100; //掃描間隔時間設(shè)定
- while(j--);
- L1=1; //消隱
- L2=0; //位選
- DuanXuan2(DisplayData[1]);//發(fā)送段碼
- j = 100; //掃描間隔時間設(shè)定
- while(j--);
- L2=1;
- L3=0; //位選
- DuanXuan(DisplayData[2]); //發(fā)送段碼
- j = 100; //掃描間隔時間設(shè)定
- while(j--);
- L3=1;
- }
- void Key_Event_Handle() //處理按鍵事件
- {
- if(Key_State==0)
- {
- Key_Flag_Idx=(Key_Flag_Idx+1)%2; //讓變量Key_Flag_Idx保持在小于2的值
- switch(Key_Flag_Idx)
- {
- case 1:
- ET0=1;
- TR0=1; //開啟定時器
- break;
- case 0:
- ET0=0;
- TR0=0; //關(guān)閉定時器
- break;
- }
- }
- }
- void main()
- {
- DuanXuan(10);
-
- TMOD=0x01; //定時器0方式1
- TH0=(65536-50000)/256; //定時器0:50ms
- TL0=(65536-50000)%256;
- EA=1;
- while (1)
- {
- if(Key_State!=K1) //開始按鍵按下時
- {
- DelayMS(10); //按鍵去抖
- Key_State=K1;
- Key_Event_Handle(); //執(zhí)行按鍵處理函數(shù)
- }
- if(K2==0) //復(fù)位按鍵按下時
- {
- ET0=0;
- TR0=0; //關(guān)閉定時器
- i=0; //清零計數(shù)
- Second_Counts=0; //清零秒
- Key_Flag_Idx=0;
- }
- DisplayData[0] = DIG_CODE[Second_Counts/100%10]; //顯示百位
- DisplayData[1] = DIG_CODE[Second_Counts/10%10]; //顯示十位
- DisplayData[2] = DIG_CODE[Second_Counts%10]; //顯示個位
- DigDisplay(); //執(zhí)行數(shù)碼管顯示函數(shù)
- }
- }
- //T0中斷函數(shù)
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
不同P口數(shù)碼管顯示.zip
(87.99 KB, 下載次數(shù): 38)
2018-1-22 16:24 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|