这个程序 起始也就是 3个按键控制 +1 +2+ 3 1个按键控制 红方蓝方的电路 很简单 #include<> #include<> #define uchar unsigned char #define uint unsigned int // 段码 uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; // 待显示的6位缓冲 uchar Num_Buffer[]={0,0,0,0,0,0}; // 按键代码,按键计数 uchar Key_Code,Key_Counts1=0; Key_Counts2=0;//1 2分别是红 蓝方// 延时 void DelayMS(uint x) { uchar i; while(x--) for(i=0;i<120;i++); } // 显示函数 void Show_Counts_ON_DSY() { uchar i,j=0x01; Num_Buffer[2]=Key_Counts1/100; Num_Buffer[1]=Key_Counts1/10%10; Num_Buffer[0]=Key_Counts1%10; Num_Buffer[5]=Key_Counts1/100; Num_Buffer[4]=Key_Counts1/10%10; Num_Buffer[3]=Key_Counts1%10; for(i=0;i<6;i++) { j=_cror_(j,1); P0=0xff; P0=DSY_CODE[Num_Buffer[i]]; P2=j; DelayMS(1); } } // 主程序 void main() { uchar i; P0=0xff; P1=0xff; P2=0x00; Key_Code=0xff; while(1) { Show_Counts_ON_DSY(); P1=0xff; Key_Code=P1; //有键按下时,数码管刷新显示 30 次,该行代码同时起到延时作用 if(Key_Code!=0xff) for(i=0;i<30;i++) Show_Counts_ON_DSY(); switch(Key_Code) { case 0xfe: if(color==1)Key_Counts1++; else Key_Counts2++; //左边加1还是右边加1 break; case 0xfd: Key_Counts1=Key_Counts1+2; else Key_Counts2=Key_Counts2+2; break; case 0xfb: Key_Counts=Key_Counts1+3; else Key_Counts2=Key_Counts2+3; case 0xf7; color=!color;//左边还是右边 } Key_Code=0xff; }//P1接数码官段码 P2接片选