1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include <reg51.h> #define uchar unsigned char #define uint unsigned int #define WEI P1 #define KEY P2 char code table[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e };
int scoreA = 0,scoreB = 0; //A队,B队分数 void delay(int t) //延时函数 { int i,j; for(i = 0;i < t;i++) for(j = 0;j < 120;j++); }
void display(int A,int B) //数码管动态显示函数 { int shi,ge; shi = A/10; //十位分离 ge = A%10; //个位分离
P0 = 0xff; //消影处理 P1 = 0x01; //给位选 P0 = table[shi]; //给段选 delay(5); //延时显示
P0 = 0xff; P1 = 0x02; P0 = table[ge]; delay(5);
shi = B / 10; ge = B % 10; P0 = 0xff; P1 = 0x04; P0 = table[shi]; delay(5);
P0 = 0xff; P1 = 0x08; P0 = table[ge]; delay(5); } void keyscan() { int temp; //按键扫描临时变量 int t; //数据交换临时变量 temp = KEY; if((temp & 0x0f) != 0x0f) { delay(1); temp = KEY; if((temp & 0x0f) != 0x0f) { temp = KEY; temp = temp & 0x0f; switch(temp) { case 0x0e: //第一个键被按下 scoreA += 1; if(scoreA > 99) scoreA = 0; //A队分数加1 break; case 0x0d: //第二个键被按下 scoreB += 1; //B队分数加1 if(scoreB > 99) scoreB = 0; break; case 0x0b: //第三个键被按下 { t = scoreB; scoreB = scoreA; //交换场地 scoreA = t; break; } case 0x07: //第四个键被按下 scoreA = 0; //两队分数清零 scoreB = 0; break; default : break; } temp = KEY; while((temp & 0X0F) != 0x0f) { temp = KEY; } } } }
void main() { while(1) { display(scoreA,scoreB);//数码管动态显示 keyscan(); //按键扫描 }
}
|