我用51做了一个记分牌,但是上电后数码管全亮,按键操作无效。检查了硬件
电路,应该是没问题的。程序
仿真也没错,不知道哪里出了问题
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit K0=P1^0;
- sbit K1=P1^1;
- sbit K2=P1^2;
- sbit K3=P1^3;
- sbit K4=P1^4;
- sbit LED1=P3^4;
- sbit LED2=P3^5;
- sbit LED3=P3^6;
- sbit LED4=P3^7;
- unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar cz=24;
- uchar timebegin =0;
- uchar bittime=0,bit1s=0;
- //uchar score=0;
- uchar mode=0;
- uchar score1=1;
- uchar score2=2;
- void delay10ms(void)
- {
- unsigned char i,j,k;
- for(i=5;i>0;i--)
- for(j=4;j>0;j--)
- for(k=248;k>0;k--);
- }
- void delay_key(void)
- {
- delay10ms();
- delay10ms();
- delay10ms();
- }
- void display_time()
- {
- TR0=1;
- P2=0XFb;
- P0=tab[cz/10];
- delay(10);
- P2=0XF7;
- P0=tab[cz%10];
- delay(10);
- }
- void display_1(uint z)
- {
- P2=0XFd;
- P0=tab[z%10];
- delay(10);
- P2=0XFe;
- P0=tab[z/10];
- delay(10);
- }
- void display_2(uint z)
- {
- P2=0XF7;
- P0=tab[z%10];
- delay(10);
- P2=0XFb;
- P0=tab[z/10];
- delay(10);
- }
- void mode_set()
- {
- if(K0==0)
- {
- delay_key();
-
- if(K0==0)
- {
- while(~K0);
- timebegin = 0;
- mode++;
- if(mode >= 3) mode=1;
- }
- }
-
- if(K4==0)
- {
- delay_key();
- if(K4==0)
- {
- while(~K4)
- cz = 24 ;
- mode = 4;
- timebegin = 1;
- }
- }
- }
- int inc(uchar num)
- {
- if(~K1)
- {
- delay_key();
- if(~K1) num +=1;
- while(~K1);
- }
- if(~K2)
- {
- delay_key();
- if(~K2) num +=2;
- while(~K2);
- }
- if(~K3)
- {
- delay_key();
- if(~K3) num +=3;
- while(~K3);
- }
- return num ;
- }
- void init_timer0(void)
- {
- TMOD = 0x01;
- TH0 = 0x3C;
- TL0 = 0x0b0;
- EA = 1;
- ET0 = 1;
- TR0 = 0;
- }
- void main(void)
- {
- init_timer0();
- P2=0XF0;
- P0=0X3F;
- while(1)
- {
- mode_set();
-
- if(mode==1)
- {
- LED1=0;
- LED2=1;
- LED3=1;
- LED4=1;
- score1 = inc(score1);
- display_1(score1);
- display_2(score2);
- }
-
- if(mode==2)
- {
- LED2=0;
- LED1=1;
- LED3=1;
- LED4=1;
- score2 = inc(score2);
- display_1(score1);
- display_2(score2);
- }
-
- if(mode == 4)
- {
- LED1=1;
- LED2=1;
- LED3=0;
- display_time();
- }
- }
- void timer0 (void) interrupt 1 using 0
- {
- TH0=0X3C;
- TL0=0X0B0;
-
- if(bittime == 20)
- {
- bittime = 0;
- if(timebegin) bit1s = 1;
- else bit1s = 0;
- }
- else bittime++;
-
- if(timebegin)
- {
- if(bit1s == 1)
- {
- bit1s = 0;
-
- cz--;
-
- if(cz == 0)
- {
- cz=0;
- LED4=0;
- timebegin = 0;
-
- }
- }
- }
- TH0=0X3C;
- TL0=0X0B0;
- }
复制代码
一周热门 更多>