)竞赛计分计时器设计(LCD(或两个8位数码管)显示时间(分钟、秒、秒小数各2 位)和双方分数,有双方加减分数按钮,有计时启动、暂停、清0按钮,有初始时间设置按钮)。的代码
时间: 2024-02-03 17:02:32 浏览: 40
以下是基于Arduino平台的示例代码:
```C++
#include <LiquidCrystal.h> // 引入液晶库
// 定义引脚
const int BUTTON_START_STOP = 2; // 计时启动、暂停按钮
const int BUTTON_CLEAR = 3; // 计时清零按钮
const int BUTTON_ADD_SCORE_1 = 4; // 玩家1加分按钮
const int BUTTON_SUB_SCORE_1 = 5; // 玩家1减分按钮
const int BUTTON_ADD_SCORE_2 = 6; // 玩家2加分按钮
const int BUTTON_SUB_SCORE_2 = 7; // 玩家2减分按钮
const int BUTTON_SET_TIME = 8; // 初始时间设置按钮
const int DIGIT_1 = 9; // 第1个7段数码管
const int DIGIT_2 = 10; // 第2个7段数码管
const int DIGIT_3 = 11; // 第3个7段数码管
const int DIGIT_4 = 12; // 第4个7段数码管
const int SCORE_1 = A0; // 玩家1得分
const int SCORE_2 = A1; // 玩家2得分
// 定义液晶显示
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// 定义计时器
unsigned long start_time = 0; // 记录启动时间
unsigned long pause_time = 0; // 记录暂停时间
unsigned long clear_time = 0; // 记录清零时间
unsigned long set_time = 0; // 初始时间
bool is_running = false; // 计时器是否在运行中
// 定义分数
int score_1 = 0; // 玩家1得分
int score_2 = 0; // 玩家2得分
// 定义显示数组
byte digits[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B00111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
// 初始化
void setup() {
// 设置引脚模式
pinMode(BUTTON_START_STOP, INPUT_PULLUP);
pinMode(BUTTON_CLEAR, INPUT_PULLUP);
pinMode(BUTTON_ADD_SCORE_1, INPUT_PULLUP);
pinMode(BUTTON_SUB_SCORE_1, INPUT_PULLUP);
pinMode(BUTTON_ADD_SCORE_2, INPUT_PULLUP);
pinMode(BUTTON_SUB_SCORE_2, INPUT_PULLUP);
pinMode(BUTTON_SET_TIME, INPUT_PULLUP);
pinMode(DIGIT_1, OUTPUT);
pinMode(DIGIT_2, OUTPUT);
pinMode(DIGIT_3, OUTPUT);
pinMode(DIGIT_4, OUTPUT);
pinMode(SCORE_1, INPUT);
pinMode(SCORE_2, INPUT);
// 初始化液晶显示
lcd.begin(16, 2);
lcd.clear();
lcd.print("Scoreboard");
// 初始化计时器
set_time = 300000; // 初始时间为5分钟
clear_time = millis();
}
// 主循环
void loop() {
// 获取按钮状态
bool button_start_stop_state = digitalRead(BUTTON_START_STOP);
bool button_clear_state = digitalRead(BUTTON_CLEAR);
bool button_add_score_1_state = digitalRead(BUTTON_ADD_SCORE_1);
bool button_sub_score_1_state = digitalRead(BUTTON_SUB_SCORE_1);
bool button_add_score_2_state = digitalRead(BUTTON_ADD_SCORE_2);
bool button_sub_score_2_state = digitalRead(BUTTON_SUB_SCORE_2);
bool button_set_time_state = digitalRead(BUTTON_SET_TIME);
// 处理计时器按钮
if (!button_start_stop_state && is_running == false) { // 计时启动
start_time = millis();
is_running = true;
} else if (!button_start_stop_state && is_running == true) { // 计时暂停
pause_time = millis();
is_running = false;
} else if (!button_clear_state) { // 计时清零
clear_time = millis();
is_running = false;
}
// 处理分数按钮
if (!button_add_score_1_state) { // 玩家1加分
score_1++;
} else if (!button_sub_score_1_state) { // 玩家1减分
score_1--;
} else if (!button_add_score_2_state) { // 玩家2加分
score_2++;
} else if (!button_sub_score_2_state) { // 玩家2减分
score_2--;
}
// 处理初始时间设置按钮
if (!button_set_time_state) {
set_time = 300000; // 初始时间为5分钟
}
// 计算时间
unsigned long current_time = 0;
if (is_running) {
current_time = millis() - start_time;
} else {
current_time = pause_time - start_time;
}
unsigned long elapsed_time = clear_time + current_time;
unsigned long remaining_time = set_time - elapsed_time;
int minutes = remaining_time / 60000;
int seconds = (remaining_time % 60000) / 1000;
int fraction = (remaining_time % 1000) / 10;
// 显示时间和分数
lcd.setCursor(0, 1);
lcd.print("Time:");
lcd.print(minutes < 10 ? "0" : "");
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds < 10 ? "0" : "");
lcd.print(seconds);
lcd.print(".");
lcd.print(fraction < 10 ? "0" : "");
lcd.print(fraction);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("P1:");
lcd.print(score_1);
lcd.print(" ");
lcd.print("P2:");
lcd.print(score_2);
// 显示计时器
display_time(minutes, seconds, fraction);
}
// 显示时间
void display_time(int minutes, int seconds, int fraction) {
digitalWrite(DIGIT_1, HIGH);
digitalWrite(DIGIT_2, HIGH);
digitalWrite(DIGIT_3, LOW);
digitalWrite(DIGIT_4, LOW);
display_digit(minutes / 10);
delay(5);
digitalWrite(DIGIT_1, LOW);
digitalWrite(DIGIT_2, HIGH);
digitalWrite(DIGIT_3, LOW);
digitalWrite(DIGIT_4, LOW);
display_digit(minutes % 10);
delay(5);
digitalWrite(DIGIT_1, LOW);
digitalWrite(DIGIT_2, LOW);
digitalWrite(DIGIT_3, HIGH);
digitalWrite(DIGIT_4, LOW);
display_digit(seconds / 10);
delay(5);
digitalWrite(DIGIT_1, LOW);
digitalWrite(DIGIT_2, LOW);
digitalWrite(DIGIT_3, LOW);
digitalWrite(DIGIT_4, HIGH);
display_digit(seconds % 10);
delay(5);
}
// 显示数字
void display_digit(int digit) {
byte display = digits[digit];
for (int i = 0; i < 8; i++) {
bool bit = bitRead(display, i);
digitalWrite(i + 4, bit);
}
}
```
该代码使用了一个液晶显示、四个7段数码管和若干个按钮来实现竞赛计分计时器的功能。其中,液晶显示用于显示时间和分数,四个7段数码管用于显示时间。按钮用于启动、暂停、清零计时器,设置初始时间和加减分数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)