看了你的要求,我手里没有共阳数码管,只有共阴数码管,我还是专门研究了一下你的要求。 你的要求是不是:第一位不能显示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--); } |