#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS = P2^1;
sbit R_CLK = P2^2;
sbit S_CLK = P2^0;
sbit LED = P2^3;
sbit BUZZER = P2^6;
uchar place_cnt = 0;
bit updat = 0;
uint cnt = 0;
uchar bt = 0;
uchar score1 = 0;
uchar score2 = 0;
uchar match = 0;
uchar soud = 0;
uchar cont = 0;
uchar code table[] = {
0x3f, 0x06, 0x5b, 0x4f, // 0,1,2,3
0x66, 0x6d, 0x7d, 0x07, // 4,5,6,7
0x7f, 0x6f, 0x77, 0x07, // 8,9,A,B
0x39, 0x5e, 0x79, 0x71, // C,D,E,F
0x40 // ??
};
uchar number;
uchar KeyValue = 0;
// ???????????
bit confirm_mode = 0; // 0:????, 1:??????
uchar pending_team = 0; // 0:A?, 1:B?
uchar pending_points = 0; // ??????
uchar pending_key = 0; // ???????
uint timeout_cnt = 0; // ?????
void send595(uchar dat) {
uchar i;
for(i = 0; i < 8; i++) {
if((dat << i) & 0x80)
DS = 1;
else
DS = 0;
S_CLK = 0;
S_CLK = 1;
}
}
void out595(void) {
R_CLK = 0;
R_CLK = 1;
}
void vision(uchar dat, uchar pos) {
send595(pos);
send595(~table[dat]);
out595();
}
void Timer_init(void) {
TMOD = 0x11; // ???0?1??????1
TH0 = (65535 - 60000) / 256; // 60ms??
TL0 = (65535 - 60000) % 256;
TR0 = 1; // ?????0
ET0 = 1; // ?????0??
EA = 1; // ?????
}
void timer0() interrupt 1 {
TH0 = (65535 - 60000) / 256; // ??60ms??
TL0 = (65535 - 60000) % 256;
updat = 1; // ????????
// ????
if(confirm_mode) {
timeout_cnt++;
if(timeout_cnt >= 33) { // ?2??? (33*60ms=1980ms)
timeout_cnt = 0;
confirm_mode = 0;
LED = 1; // ??LED
}
}
}
void delay(uint i) {
uint j, k;
for(j = 0; j < i; j++) {
for(k = 0; k < 120; k++);
}
}
// ??????(??????)
void handle_key(uchar key) {
// ???????????
if(key == 12 || key == 14) { // A???B??
if(key == 12) score1 = 0; // A??
if(key == 14) score2 = 0; // B??
confirm_mode = 0;
LED = 1; // ??LED
return;
}
if(confirm_mode) {
// ???????
if(key == pending_key) {
// ??????
if(pending_team == 0) { // A?
if(score1 + pending_points <= 99) {
score1 += pending_points;
} else {
score1 = 99;
}
} else { // B?
if(score2 + pending_points <= 99) {
score2 += pending_points;
} else {
score2 = 99;
}
}
confirm_mode = 0;
LED = 1; // ??LED
timeout_cnt = 0;
} else {
// ?????????,???????
confirm_mode = 0;
LED = 1; // ??LED
timeout_cnt = 0;
}
} else {
// ???????
switch(key) {
case 0: // A+1
pending_key = 0;
pending_points = 1;
pending_team = 0;
confirm_mode = 1;
LED = 0; // ??LED
timeout_cnt = 0;
break;
case 4: // A+2
pending_key = 4;
pending_points = 2;
pending_team = 0;
confirm_mode = 1;
LED = 0; // ??LED
timeout_cnt = 0;
break;
case 8: // A+3
pending_key = 8;
pending_points = 3;
pending_team = 0;
confirm_mode = 1;
LED = 0; // ??LED
timeout_cnt = 0;
break;
case 2: // B+1
pending_key = 2;
pending_points = 1;
pending_team = 1;
confirm_mode = 1;
LED = 0; // ??LED
timeout_cnt = 0;
break;
case 6: // B+2
pending_key = 6;
pending_points = 2;
pending_team = 1;
confirm_mode = 1;
LED = 0; // ??LED
timeout_cnt = 0;
break;
case 10: // B+3
pending_key = 10;
pending_points = 3;
pending_team = 1;
confirm_mode = 1;
LED = 0; // ??LED
timeout_cnt = 0;
break;
// ????????
case 1:
match = match + 10;
break;
case 3:
match = match + 1;
break;
case 5:
match = match % 10;
break;
case 7:
match = match / 10 * 10;
break;
case 9:
soud = 1;
break;
case 13:
soud = 0;
break;
}
}
}
void ketscan() {
P1 = 0xF0;
if(P1 != 0xF0) {
delay(10); // ??
if(P1 != 0xF0) {
bt++;
}
if(bt == 2) {
bt = 0;
switch(P1) {
case 0xE0: KeyValue = 0; break;
case 0xD0: KeyValue = 1; break;
case 0xB0: KeyValue = 2; break;
case 0x70: KeyValue = 3; break;
}
P1 = 0x0f;
switch(P1) {
case 0x0e: KeyValue = KeyValue; break;
case 0x0D: KeyValue = KeyValue + 4; break;
case 0x0b: KeyValue = KeyValue + 8; break;
case 0x07: KeyValue = KeyValue + 12; break;
}
// ??????(??????)
handle_key(KeyValue);
while(P1 != 0x0f) { // ??????
}
}
}
}
void main(void) {
Timer_init();
LED = 1; // ???LED??
while(1) {
if(updat == 1) {
updat = 0;
if(place_cnt < 7) {
place_cnt++;
} else {
place_cnt = 0;
}
switch(place_cnt) {
case 0: vision(score1 / 10 % 10, 0x01); break; // A???
case 1: vision(score1 % 10, 0x02); break; // A???
case 2: vision(16, 0x04); break; // ???
case 3: vision(match / 10 % 10, 0x08); break; // ??????
case 4: vision(match % 10, 0x10); break; // ??????
case 5: vision(16, 0x20); break; // ???
case 6: vision(score2 / 10 % 10, 0x40); break; // B???
case 7: vision(score2 % 10, 0x80); break; // B???
}
}
ketscan();
}
}数码管数据一直闪 修改程序 使数码管稳定显示 其他地方程序不变