![]() |
發(fā)布時間: 2018-5-2 17:00
正文摘要:我的開發(fā)板是H0T51增強型開發(fā)板 我想讓數(shù)碼燈每秒從0跑到99999999,我知道long int跑不到99999999,但是我需要解決的是數(shù)字顯示位置不對,我用的是12M晶振。我的P0是管顯示數(shù)字的。P20.P21.P22是通過74HC138控制八 ... |
#include<reg51.h> unsigned long i;//êyÂë1üÏÔê¾êy×Ö unsigned char s;//¼ÆËãÖüÆú unsigned char number;//é¨Ãè°Ë¸öêyÂë1ü #define led P0 #define LED P2 sbit led1=P2^0;//74HC138A sbit led2=P2^1;//74HC138B sbit led3=P2^2;//74HC138C unsigned char code LedChar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//êyÂëμÆ′ó0-9 unsigned char LedBuff[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//½«°Ë¸öêyÂëμÆ¶¼3õê¼Öμ void time(unsigned char qq)//Ñó3ùoˉêy { unsigned char j; for(;qq>0;qq--) { for(j=110;j>0;j++); } } void main() { P1=0x00;//3õê¼ÖμP1 P2=0xff;//3õê¼ÖμO2 i=0;//3õê¼ÖμLEDμÆÏÔê¾Öμ s=0;//3õê¼ÖüÆú number=0;//3õê¼é¨Ãè TMOD=0x01;//¼Æê±Æ÷Ä£ê½1 TH0=0xff;//éèÖÃ0.001oáÃëò»ÖüÆú TL0=0xff; TR0=1;//¿aÆô¼ÆêyÆ÷ while(1) { if(TF0==1) { TF0=0; TH0=0xff; TL0=0xff; s++;//ò»¸öÖüÆúàۼƼóò» } if(s>=1)//×Ô¼o¸ù¾Y1oáÃëò»ÖüÆúμÄê±¼äè¥è¡×Ô¼oÏëòaμÄê±¼ä { s=0; i++; } if(i<10)//èç1ûÖ»Åüμ½¸öλêy£¬Ö»èøöλêyáá { LedBuff[0]=LedChar[i%10]; }else if(i>=10&&i<100) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; }else if(i>=100&&i<1000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; }else if(i>=1000&&i<10000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; }else if(i>=10000&&i<100000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; }else if(i>=100000&&i<1000000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; LedBuff[5]=LedChar[i/100000%10]; }else if(i>=1000000&&i<10000000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; LedBuff[5]=LedChar[i/100000%10]; LedBuff[6]=LedChar[i/1000000%10]; }else if(i>=10000000&&i<100000000) { LedBuff[0]=LedChar[i%10]; LedBuff[1]=LedChar[i/10%10]; LedBuff[2]=LedChar[i/100%10]; LedBuff[3]=LedChar[i/1000%10]; LedBuff[4]=LedChar[i/10000%10]; LedBuff[5]=LedChar[i/100000%10]; LedBuff[6]=LedChar[i/1000000%10]; LedBuff[7]=LedChar[i/10000000%10]; }else //èç1ûêy×ÖÅüÂú8¸öLEDéèÖÃéÏÏT { LedBuff[0]=LedChar[9]; LedBuff[1]=LedChar[9]; LedBuff[2]=LedChar[9]; LedBuff[3]=LedChar[9]; LedBuff[4]=LedChar[9]; LedBuff[5]=LedChar[9]; LedBuff[6]=LedChar[9]; LedBuff[7]=LedChar[9]; } if(number==0)//é¨Ãè1-8¸öledμÆ { led3=1; led2=1; led1=1; led=LedBuff[0]; number++; time(2);//èÃÿ¸öLED′óÔ¼áá2oáÃë P0=0x00;//ÿ¸öledáá2oáÃëÖ®oó3õê¼ÖμLED } if(number==1) { led3=1; led2=1; led1=0; led=LedBuff[1]; number++; time(2); P0=0x00; } if(number==2) { led3=1; led2=0; led1=1; led=LedBuff[2]; number++; time(2); P0=0x00; } if(number==3) { led3=1; led2=0; led1=0; led=LedBuff[3]; number++; time(2); P0=0x00; } if(number==4) { led3=0; led2=1; led1=1; led=LedBuff[4]; number++; time(2); P0=0x00; } if(number==5) { led3=0; led2=1; led1=0; led=LedBuff[5]; number++; time(2); P0=0x00; } if(number==6) { led3=0; led2=0; led1=1; led=LedBuff[6]; number++; time(2); P0=0x00; } if(number==7) { led3=0; led2=0; led1=0; led=LedBuff[7]; number=0; time(2); P0=0x00; } } } 這是我修改后的,謝謝大家點評。原因就是我沒有消隱。謝謝 |
參與人數(shù) 1 | 黑幣 +60 | 收起 理由 |
---|---|---|
![]() | + 60 | 回帖助人的獎勵! |
wulin 發(fā)表于 2018-5-3 15:13 是的,我的比較啰嗦點。不過現(xiàn)在可以了,謝謝指點。 |
給你一個8位數(shù)碼管時鐘示例程序,根據(jù)實際電路更改端口定義。 //8位數(shù)碼管時鐘+鬧鐘 //K1鍵調整選擇,K2鍵+,長按連+,K3鍵-,長按連-,K4鍵鬧鐘設置 //定時器初始化程序根據(jù)晶振頻率選擇 //用計數(shù)法代替軟件延時,消除按鍵干擾其它程序 //數(shù)碼管采用分時動態(tài)顯示,2ms顯示1位 #include <reg51.h> #define uint unsigned int #define uchar unsigned char #define key_S 300 //宏定義短按(約20ms) #define key_L 6000 //宏定義長按(約2/3s) #define key_M 3000 //宏定義長按(約1/3s) sbit K1 = P3^0; //時鐘調整選擇/退出 sbit K2 = P3^1; //++,長按連+ sbit K3 = P3^2; //--,長按連- sbit K4 = P3^3; //鬧鐘調整選擇/退出 sbit Buzzer=P2^5; //蜂鳴器 sbit led138c=P2^4; //38譯碼器位選 sbit led138b=P2^3; //38譯碼器位選 sbit led138a=P2^2; //38譯碼器位選 uchar code table[]={ //0~F數(shù)組 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; uint Cnt2ms; //定義2ms秒變量 uchar hour=0,min=0,sec=0; //定義時、分、秒變量 uchar hour_1=0,min_1=0; //定義鬧鐘時、分變量 uchar Choice=0,Choice_1=0; //調整走時的選擇變量、設置鬧鐘的選擇變量 bit Buzzer_sign; //蜂鳴器鬧鐘標志 bit Twinkle,second=1; //閃爍標志、秒標志 bit flag=0; //2ms數(shù)碼管動態(tài)顯示標志 /*************************************** 定時器T0/T1初始化程序 @12MHz ***************************************/ /* void Timer_Init() { TMOD = 0x21; //設置T0模式1,T1模式2 TL0 = 0x30; //設置定時初值2ms TH0 = 0xF8; //設置定時初值2ms TL1 = 0x06; //設置定時初值250微秒 TH1 = 0x06; //設置定時重載值250微秒 TR0 = 1; //定時器0開始計時 // TR1 = 1; //定時器1開始計時 EA=1; //開總中斷 ET0=1; //開定時器1中斷 ET1=1; //開定時器1中斷 }*/ /*************************************** 定時器T0/T1初始化程序 @11.0592MHz ***************************************/ void Timer_Init() { TMOD = 0x21; //設置T0模式1,T1模式2 TL0 = 0xCD; //設置定時初值2ms TH0 = 0xF8; //設置定時初值2ms TL1 = 0x1A; //設置定時初值250微秒 TH1 = 0x1A; //設置定時重載值250微秒 TR0 = 1; //定時器0開始計時 // TR1 = 1; //定時器1開始計時 EA=1; //開總中斷 ET0=1; //開定時器1中斷 ET1=1; //開定時器1中斷 } /************************* 按鍵掃描程序 *************************/ void key_scan() { static bit key1_sign,key4_sign; //按鍵自鎖標志變量 static uint count1,count2,count3=0,count4=0;//消抖計數(shù)變量 if(!K1) //檢測按鍵1按下 { count1++; //消抖計數(shù)1自+1 if((count1>=key_S)&&(key1_sign==0))//檢測消抖計數(shù)與按鍵1自鎖標志 { key1_sign=1; //按鍵1自鎖標志置1 Choice++; //調整選擇變量自+1 if(Choice>=4) //調整時間選擇0正常走時,1調時,2調分,3調秒 { Choice=0; //調整時間選擇清0 Cnt2ms=0; //時間變量Cnt2ms清0 } } } else { key1_sign=0; //按鍵1自鎖標志清0 count1=0; //消抖計數(shù)count1清0 } if(!K2) { count2++; if(count2>=key_L) //長按快調 { if(Choice==1) //選擇變量1調時 { hour++; if(hour>=24) hour=0; } if(Choice==2) //選擇變量2調分 { min++; if(min>=60) min=0; } if(Choice==3) //選擇變量3調秒 { sec++; if(sec>=60) sec=0; } if(Choice_1==1) //選擇變量1調時 { hour_1++; if(hour_1>=24) hour_1=0; } if(Choice_1==2) //選擇變量2調分 { min_1++; if(min_1>=60) min_1=0; } count2=key_M; } } else //按鍵抬起 { if(count2>key_S && count2<key_L)//短按 { if(Choice==1) //選擇變量1調時 { hour++; if(hour>=24) hour=0; } if(Choice==2) //選擇變量2調分 { min++; if(min>=60) min=0; } if(Choice==3) //選擇變量3調秒 { sec++; if(sec>=60) sec=0; } if(Choice_1==1) //選擇變量1調時 { hour_1++; if(hour_1>=24) hour_1=0; } if(Choice_1==2) //選擇變量2調分 { min_1++; if(min_1>=60) min_1=0; } } count2=0; //count2清0 } if(!K3) { count3++; if(count3>=key_L) //長按 { if(Choice==1) //選擇變量 { hour--; if(hour>=24) hour=23; } if(Choice==2) //選擇變量 { min--; if(min>=60) min=59; } if(Choice==3) //選擇變量 { sec--; if(sec>=60) sec=59; } if(Choice_1==1) //選擇變量 { hour_1--; if(hour_1>=24) hour_1=23; } if(Choice_1==2) //選擇變量 { min_1--; if(min_1>=60) min_1=59; } count3=key_M; } } else //按鍵抬起 { if(count3>key_S && count3<key_L)//短按 { if(Choice==1) //選擇變量 { hour--; if(hour>=24) hour=23; } if(Choice==2) //選擇變量 { min--; if(min>=60) min=59; } if(Choice==3) //選擇變量 { sec--; if(sec>=60) sec=59; } if(Choice_1==1) //選擇變量 { hour_1--; if(hour_1>=24) hour_1=23; } if(Choice_1==2) //選擇變量 { min_1--; if(min_1>=60) min_1=59; } } count3=0; //count3清0 } if(!K4) //檢測按鍵1按下 { count4++; //消抖計數(shù)1自+1 if((count4>=key_S)&&(key4_sign==0))//檢測消抖計數(shù)與按鍵1自鎖標志 { key4_sign=1; //按鍵1自鎖標志置1 Choice_1++; //調整選擇變量自+1 if(Choice_1>=4) //調整鬧鐘時間選擇0鬧鐘關閉,1調時,2調分,3開啟鬧鐘 { Choice_1=0; //調整時間選擇清0 } } } else { key4_sign=0; //按鍵1自鎖標志清0 count4=0; //消抖計數(shù)count1清0 } } /******************************** 數(shù)碼管顯示程序 ********************************/ void display() { static uchar num=0; //分時顯示變量 if(flag==1) { flag=0; if((Choice_1>0)&&(Choice_1<3))//鬧鐘設置顯示部分 { switch(num) { case 0: P0=0x00; //消隱 led138a=0;led138b=0;led138c=0;//時十位位碼 P0=table[hour_1/10]; //時十位段碼 num++; break; case 1: P0=0x00; //消隱 led138a=1;led138b=0;led138c=0;//時個位位碼 if((Twinkle==1)&&(Choice_1==1))//時點閃爍 P0=table[hour_1%10]|0x80; else P0=table[hour_1%10];//時個位段碼+點 num++; break; case 2: P0=0x00; //消隱 led138a=0;led138b=1;led138c=0;//- P0=0x40; //- num++; break; case 3: P0=0x00; //消隱 led138a=1;led138b=1;led138c=0;//分十位位碼 P0=table[min_1/10]; //分十位段碼 num++; break; case 4: P0=0x00; //消隱 led138a=0;led138b=0;led138c=1;//分個位位碼 if((Twinkle==1)&&(Choice_1==2))//分點閃爍 P0=table[min_1%10]|0x80; else P0=table[min_1%10];//分個位段碼+點 num++; break; case 5: P0=0x00; //消隱 led138a=1;led138b=0;led138c=1;//- P0=0x40; //- num++; break; case 6: P0=0x00; //消隱 led138a=0;led138b=1;led138c=1;//秒十位位碼 P0=0x40; //秒十位段碼 num++; break; case 7: P0=0x00; //消隱 led138a=1;led138b=1;led138c=1;//秒個位位碼 P0=0x40;//秒個位段碼 num=0; break; } } else //正常走時顯示部分 { switch(num) { case 0: P0=0x00; //消隱 led138a=0;led138b=0;led138c=0;//時十位位碼 P0=table[hour/10]; //時十位段碼 num++; break; case 1: P0=0x00; //消隱 led138a=1;led138b=0;led138c=0;//時個位位碼 if((Twinkle==1)&&(Choice==1))//時點閃爍 P0=table[hour%10]|0x80; else P0=table[hour%10];//時個位段碼+點 num++; break; case 2: P0=0x00; //消隱 led138a=0;led138b=1;led138c=0;//- P0=0x40; //- num++; break; case 3: P0=0x00; //消隱 led138a=1;led138b=1;led138c=0;//分十位位碼 P0=table[min/10]; //分十位段碼 num++; break; case 4: P0=0x00; //消隱 led138a=0;led138b=0;led138c=1;//分個位位碼 if((Twinkle==1)&&(Choice==2))//分點閃爍 P0=table[min%10]|0x80; else P0=table[min%10];//分個位段碼+點 num++; break; case 5: P0=0x00; //消隱 led138a=1;led138b=0;led138c=1;//- P0=0x40; //- num++; break; case 6: P0=0x00; //消隱 led138a=0;led138b=1;led138c=1;//秒十位位碼 P0=table[sec/10]; //秒十位段碼 num++; break; case 7: P0=0x00; //消隱 led138a=1;led138b=1;led138c=1;//秒個位位碼 if(Choice_1==3) P0=table[sec%10]|0x80;//鬧鐘啟動后秒個位+點 else if((Twinkle==1)&&(Choice==3))//秒點閃爍+點 P0=table[sec%10]|0x80; else P0=table[sec%10];//秒個位段碼 num=0; break; } } } } /******************************** 鬧鐘程序 ********************************/ void Buzzer_nz() { if((Choice_1==3)&&(hour_1==hour)&&(min_1==min)&&(sec<30)) Buzzer_sign=1; else Buzzer_sign=0; if((second==1)&&(Buzzer_sign==1)) //計時周期1s { TR1 = 1; //定時器1開,蜂鳴器響一下 second=0; //定時周期1s變量清0 } } /******************************** 主程序 ********************************/ void main(void) { Timer_Init(); //初始化定時器 while(1) { key_scan(); //按鍵掃描 display(); //顯示 Buzzer_nz(); //鬧鐘 } } /*----------------------------- 定時器0中斷服務程序 200微秒 ------------------------------*/ void timer0() interrupt 1 { TL0 = 0xCD; //設置定時初值2ms TH0 = 0xF8; //設置定時初值2ms Cnt2ms++; //時間變量Cnt2ms自+1 flag=1; if((Cnt2ms>125 && Cnt2ms<250)||(Cnt2ms>375 && Cnt2ms<500))//閃爍頻率2Hz Twinkle=1; //閃爍標志 else Twinkle=0; if(Cnt2ms>=500) //在此可以調整精度 { Cnt2ms=0; //變量Cnt2ms清0 second=1; if(Choice!=3) //調整選擇變量為3停止走秒 sec++; //秒自+1 if(sec>=60) //如果秒>=60 { sec=0; //秒清0 min++; //分自+1 if(min>=60) //分>=60 { min=0; //分清0 hour++; //小時自+1 if(hour>=24) //小時>=24 hour=0; //小時清0 } } } } /*----------------------------- 定時器1中斷服務程序 250微秒 (無源蜂鳴器驅動程序) ------------------------------*/ void timer1() interrupt 3 { static uint count3=0; //中斷計數(shù)變量 count3++; //中斷計數(shù)變量count3自+1 Buzzer=~Buzzer; //蜂鳴器端口取反 if(count3>=500) //0.1秒時間到500 { count3=0; //計數(shù)清0 Buzzer=1; //蜂鳴器端口清0 TR1 = 0; //定時器1關閉 } } |
參與人數(shù) 1 | 黑幣 +5 | 收起 理由 |
---|---|---|
![]() | + 5 | 絕世好帖! |
zlzzlf 發(fā)表于 2018-5-2 17:32 if(s>=50) { s=0; i++; LedBuff[0]=0x00; LedBuff[1]=0x00; LedBuff[2]=0x00; LedBuff[3]=0x00; LedBuff[4]=0x00; LedBuff[5]=0x00; LedBuff[6]=0x00; LedBuff[7]=0x00; } if(i<10) { LedBuff[1]=0x00; LedBuff[2]=0x00; LedBuff[3]=0x00; LedBuff[4]=0x00; LedBuff[5]=0x00; LedBuff[6]=0x00; LedBuff[7]=0x00; LedBuff[0]=LedChar[i%10]; 我清除了,還是那樣子。 |
你的程序寫的太啰嗦,發(fā)生問題不易查找,簡化一下即正常。
![]() #include<reg51.h> #define led P0 #define LED P2 sbit led1=P2^0; sbit led2=P2^1; sbit led3=P2^2; unsigned int i=0,j=0; unsigned char k=0,s=0; unsigned char code LedChar[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char code LedArr[]={0,1,2,3,4,5,6,7}; unsigned char LedBuff[8]; void main() { TMOD=0x01; TH0=0xb1; TL0=0xe0; TR0=1; while(1) { if(TF0==1) { TF0=0; TH0=0xb1; TL0=0xe0; s++; } if(s>=50) { s=0; i++; if(i>9999) { i=0; j++; if(j>9999) j=0; } } /************數(shù)據(jù)分解到緩存************/ LedBuff[0]=LedChar[j/1000]; LedBuff[1]=LedChar[j%1000/100]; LedBuff[2]=LedChar[j%100/10]; LedBuff[3]=LedChar[j%10]; LedBuff[4]=LedChar[i/1000]; LedBuff[5]=LedChar[i%1000/100]; LedBuff[6]=LedChar[i%100/10]; LedBuff[7]=LedChar[i%10]; /************8位數(shù)碼管顯示************/ led=0x00; //消隱 LED&=0xF8; //清除P2低3位,保留高5位狀態(tài)不變 LED|=LedArr[k]; //送位碼 led=LedBuff[k]; //送段碼 k++; if(k>=8) k=0; } } |
參與人數(shù) 1 | 黑幣 +5 | 收起 理由 |
---|---|---|
![]() | + 5 | 回帖助人的獎勵! |
Powered by 單片機教程網(wǎng)