下面是原程序
- #include"math.h"
- #include "reg51.h"
- #define uchar unsigned char
- uchar key_temp,key=0,i,j,k,t=0;
- uchar a=0,b=0,c=0,d=0;
- unsigned long qian,hou;
- uchar table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- static unsigned char state_key=0;
- static unsigned char state_dis=0;
- void keyscan()
- {
- switch(state_key)
- {
- case 0:
- key_temp=(P1&0x0f);
- if(key_temp!=0x0f)
- t=0;
- state_key=1;
- break;
- case 1:
- if(t>=10)
- {
- if(key_temp==(P1&0x0f))
- {
- if(P1^0==0)
- key=1;
- if(P1^1==0)
- key=2;
- if(P1^2==0)
- key=3;
- if(P1^3==0)
- key=4;
- }
- state_key=2;
- }
-
- else
- state_key=0;
- break;
-
- case 2:
- if(key_temp==(P1&0x0f))
- {
- state_key=0;
- break;
- }
- key=0;//default:break;
- }
- }
- void keyaction()
- {
- switch(key)
- {
- case 1:break;
- case 2:i=0;
- i++;
- if(i>9)
- break;
- case 3:j=0;
- if(j<=3)
- j++;
- switch(j)
- {
- case 0:P3=0xfe;i++;break;
- case 1:P3=0xfd;i++;break;
- case 2:P3=0xfb;i++;break;
- case 3:P3=0xf7;i++;break;
- }
- break;
- case 4: k=0;
- k++;
- if(k==1)
- hou=hou+qian;
- qian=0;
- if(k==2)
- hou=hou+qian;
- qian=0;
- k=0;
- break;
- }
- }
- void display()
- {
- switch(j)
- {
- case 0:P2=table[a];break;
- case 1:P2=table[b][b];break;
- case 2:P2=table[c];break;
- case 3:P2=table[d];break;
- }
- }
- void display0()
- {
- switch(state_dis)
- {
- case 0:a=i;b=0;c=0;d=0;break;
- case 1:b=i;c=0;d=0;break;
- case 2:c=i;d=0;break;
- case 3:d=i;break;
- }
- display();
- }
- void time0init()
- {
- TMOD=0x01;
- TH0=-50000/256;
- TL0=-50000%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void time0()interrupt 1
- {
- TH0=-50000/256;
- TL0=-50000%256;
- t++;
- if(state_dis<=3)
- state_dis++;
- else
- state_dis=0;
- }
- void main()
- {
- time0init();
- while(1)
- {
- keyscan();
- keyaction();
- display0();
- switch(j)
- {
- case 0:qian=a;break;
- case 1:qian=a+b*10;break;
- case 2:qian=a+b*10+c*100;break;
- case 3:qian=a+b*10+c*100+d*1000;break;
- }
- a=0;
- b=0;
- c=0;
- d=0;
- }
- }
復制代碼
數碼管只顯示四位0,而且按鍵無反應,應該是邏輯上的問題,實在找不到,求大神!
|