51单片机电子记分牌编程代码:实现加减分、复位功能
#include <reg51.h>
#define uchar unsigned char #define uint unsigned int
sbit beep = P1^0; sbit addBtn = P2^0; sbit subBtn = P2^1; sbit resetBtn = P2^2;
uchar code SEG_TABLE[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar score = 50;
void delay(uint ms) { uint i, j; for(i = ms; i > 0; i--) for(j = 110; j > 0; j--); }
void displayScore(uchar s) { uchar tensDigit = s / 10; uchar unitsDigit = s % 10;
P0 = SEG_TABLE[tensDigit];
P2 = 0xfe;
delay(5);
P0 = SEG_TABLE[unitsDigit];
P2 = 0xfd;
delay(5);
}
void init() { TMOD = 0x01; // 定时器0工作在模式1 TH0 = 0x3c; // 定时器初值 TL0 = 0xb0; EA = 1; // 允许中断 ET0 = 1; // 允许定时器0中断 TR0 = 1; // 启动定时器0 }
void main() { init();
while(1) {
if(addBtn == 0) {
delay(10);
if(addBtn == 0) {
while(addBtn == 0);
if(score < 99) {
score++;
beep = 0;
displayScore(score);
}
}
}
if(subBtn == 0) {
delay(10);
if(subBtn == 0) {
while(subBtn == 0);
if(score > 0) {
score--;
beep = 0;
displayScore(score);
}
}
}
if(resetBtn == 0) {
delay(10);
if(resetBtn == 0) {
while(resetBtn == 0);
score = 50;
beep = 0;
displayScore(score);
}
}
}
}
void timer0() interrupt 1 { TH0 = 0x3c; TL0 = 0xb0; beep = ~beep; }
// 使用定时器0控制蜂鸣器,按钮K1、K2分别控制加减分,K3用于复位分数。数码管显示当前分数。
原文地址: https://www.cveoy.top/t/topic/pAUr 著作权归作者所有。请勿转载和采集!