|
給力,經(jīng)過(guò)改進(jìn)可以正常跑了,而且8個(gè)管顯示都正常
- /*-----------------------------------------------
- 名稱(chēng):8位數(shù)碼管動(dòng)態(tài)掃描顯示變化數(shù)據(jù)
- 內(nèi)容:8位數(shù)碼管分別顯示不同數(shù)字,這種掃描顯示方式成為動(dòng)態(tài)掃描,并不停變化賦值
- ------------------------------------------------*/
- #include<reg52.h> //包含頭文件,一般情況不需要改動(dòng),頭文件包含特殊功能寄存器的定義
- #define DataPort P0 //定義數(shù)據(jù)端口 程序中遇到DataPort 則用P0 替換
- sbit DUAN=P2^6;//定義鎖存使能端口 段鎖存
- sbit WEI=P2^7;// 位鎖存
- /***********************************
- 數(shù)碼管碼表
- ***********************************/
- unsigned char code LedDuan[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //LED段碼
- /*****該程序位碼表沒(méi)有使用*****
- unsigned char code LedWei[10]={
- 0xfe, //0 第1位---從右側(cè)開(kāi)始
- 0xfd, //1 第2位
- 0xfb, //2 第3位
- 0xf7, //3 第4位
- 0xef, //4 第5位
- 0xdf, //5 第6位
- 0xbf, //6 第7位
- 0x7f, //7 第8位
- };
- ***************************/
- unsigned char Temp[8]; //臨時(shí)數(shù)組,用于顯示函數(shù)
- void Display(A,B,C,D,E,F,G,H); //顯示函數(shù)
- /*****************************
- 延時(shí)函數(shù)
- *****************************/
- void delay(unsigned int t)
- {
- unsigned int i,j;
- for(i = 0; i < t; i++)
- for(j = 0; j < 125; j++);
- }
- /***********************************
- 主函數(shù)
- ***********************************/
- void main(void){
- unsigned char num=0,t=0;
- unsigned int i=0;
- //unsigned int a,b,c,d,e,f,g,h;
- while(1){
- if(t==80){ //用于延時(shí)一段時(shí)間顯示,類(lèi)似延時(shí)函數(shù),
- i++;
- t=0;
- }
- if(Temp[0]==9){ //判斷數(shù)碼管是不是跑到 99999999 ,如果是,重新計(jì)數(shù)
- i=0;
- }
- t++;
- //數(shù)碼管斷碼分離計(jì)算
- Temp[0]=i/10000000;
- Temp[1]=i/1000000%10;
- Temp[2]=i/100000%10;
- Temp[3]=i/10000%10;
- Temp[4]=i/1000%10;
- Temp[5]=i/100%10;
- Temp[6]=i/10%10;
- Temp[7]=i%10;
- //顯示函數(shù)
- Display(Temp[0],Temp[1],Temp[2],Temp[3],Temp[4],Temp[5],Temp[6],Temp[7]);
- }
- }
- /*******************************
- 顯示函數(shù):
- 8個(gè)參數(shù)分別對(duì)應(yīng)8個(gè)數(shù)碼管
- *******************************/
- void Display(A,B,C,D,E,F,G,H){
- //打開(kāi)第一位
- DUAN=1;
- DataPort=LedDuan[A];
- DUAN=0;
-
- WEI=1;
- DataPort=0xfe;
- WEI=0;
- delay(1);
- //打開(kāi)第二位
- DUAN=1;
- DataPort=LedDuan[B];
- DUAN=0;
- WEI=1;
- DataPort=0xfd;
- WEI=0;
- delay(1);
- //打開(kāi)第三位
- DUAN=1;
- DataPort=LedDuan[C];
- DUAN=0;
- WEI=1;
- DataPort=0xfb;
- WEI=0;
- delay(1);
- //第四位
- DUAN=1;
- DataPort=LedDuan[D];
- DUAN=0;
-
- WEI=1;
- DataPort=0xf7;
- WEI=0;
- delay(1);
- //第五位
- DUAN=1;
- DataPort=LedDuan[E];
- DUAN=0;
-
- WEI=1;
- DataPort=0xef;
- WEI=0;
- delay(1);
- //第六位
- DUAN=1;
- DataPort=LedDuan[F];
- DUAN=0;
-
- WEI=1;
- DataPort=0xdf;
- WEI=0;
- delay(1);
- //第七位
- DUAN=1;
- DataPort=LedDuan[G];
- DUAN=0;
-
- WEI=1;
- DataPort=0xbf;
- WEI=0;
- delay(1);
- //第八位
- DUAN=1;
- DataPort=LedDuan[H];
- DUAN=0;
- WEI=1;
- DataPort=0x7f;
- WEI=0;
- delay(1);
- }
復(fù)制代碼
|
|