![]() |
發布時間: 2017-6-21 22:45
正文摘要:想用一個四位共陽數碼管做一個單片機計分器 只要讓第四位顯示除了零的數 第四位就不亮 或者整個數碼管都閃爍 不管如何改延時的時間都無法解決 求解 if(mm==0x01&&PB==1) &n ... |
看了你的要求,我手里沒有共陽數碼管,只有共陰數碼管,我還是專門研究了一下你的要求。 你的要求是不是:第一位不能顯示0? 很好辦,我已經在我的板子上調試成功了,程序附上。我的板子電路圖如圖所示。 我的程序功能:數碼管顯示9999以內的數據,按鍵1讓數據加1,按鍵2讓數據加10,按鍵3讓數據加100,按鍵4讓數據加1000,用于調試顯示是否正確。 顯示功能:首位不能顯示0,不論是千位,或是百位、十位。 #include<reg51.h> #define smg P0 sbit a=P2^2; sbit b=P2^3; sbit c=P2^4; sbit k1=P3^1; sbit k2=P3^0; sbit k3=P3^2; sbit k4=P3^3; unsigned char code xs[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d, 0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; unsigned char t=0; unsigned int j=0; void Delay10ms(unsigned int c); void Delay(unsigned char c); void play(void); void main(void) { unsigned char i; t=10; smg=0x00; while(1) { if(k1==0)//掃描鍵盤k1 { Delay10ms(1); if(k1==0) { j=j+1; if (j>9999) { j=0; } while((i<50)&&(k1==0)) { Delay10ms(1); i++; } i=0; } } if(k2==0)//掃描鍵盤k2 { Delay10ms(1); if(k2==0) { j=j+10; if (j>9999) { j=0; } while((i<50)&&(k2==0)) { Delay10ms(1); i++; } i=0; } } if(k3==0)//掃描鍵盤k3 { Delay10ms(1); if(k3==0) { j=j+100; if (j>9999) { j=0; } while((i<50)&&(k3==0)) { Delay10ms(1); i++; } i=0; } } if(k4==0)//掃描鍵盤k4 { Delay10ms(1); if(k4==0) { j=j+1000; if (j>9999) { j=0; } while((i<50)&&(k4==0)) { Delay10ms(1); i++; } i=0; } } play(); } } void play(void) { c=0;b=1;a=1; if (((j%10000)/1000)!=0) { smg=xs[(j%10000)/1000]; } else { smg=0x00; } Delay(t); smg=0x00; Delay(t); c=0;b=1;a=0; if (((j%1000)/100)!=0) { smg=xs[(j%1000)/100]; } else { if (((j%10000)/1000)!=0) { smg=xs[(j%1000)/100]; } else { smg=0x00; } } Delay(t); smg=0x00; Delay(t); c=0;b=0;a=1; if (((j%100)/10)!=0) { smg=xs[(j%100)/10]; } else { if ((((j%10000)/1000)!=0)||(((j%1000)/100)!=0)) { smg=xs[(j%100)/10]; } else { smg=0x00; } } Delay(t); smg=0x00; Delay(t); c=0;b=0;a=0; smg=xs[(j%10)/1]; Delay(t); smg=0x00; Delay(t); } void Delay(unsigned char c) { unsigned char b; for(c;c>0;c--) for(b=10;b>0;b--); } void Delay10ms(unsigned int c) { unsigned char a,b; for(c;c>0;c--) for(b=38;b>0;b--) for(a=130;a>0;a--); } |