电子表决器 功能:可对某一个议题进行表决,表决人数为“0-9”,10人“A”表示赞成,“B”表示反对,预设通过表决条件,当符合条件时,LED亮绿色,否则亮红色,并显示赞成反对弃权的比例 要求: ①超级终端显示议题内容,表决结果后同步显示在议题后 3按键表决可以两步完成:1.编号,2.表决,不按者视为弃权 4数码管显示投票结果票数,LED显示表决结果如何用51单片机c语言实现代码
时间: 2024-02-28 16:54:32 浏览: 77
按键和LED.c按键 LED c语音 51单片机的按键LED程序,很好用。
以下是一个简单的电子表决器的代码实现,使用了51单片机和LCD屏幕:
```c
#include <reg51.h>
#include <stdio.h>
// 配置LCD屏幕
#define LCD_DB P0
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
void LcdWriteCmd(unsigned char cmd) {
RS = 0;
RW = 0;
LCD_DB = cmd;
EN = 1;
EN = 0;
}
void LcdWriteData(unsigned char dat) {
RS = 1;
RW = 0;
LCD_DB = dat;
EN = 1;
EN = 0;
}
void LcdInit() {
LcdWriteCmd(0x38);
LcdWriteCmd(0x0c);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}
// 定义投票结果
int agree = 0, oppose = 0, abstain = 0;
// 处理按键输入
void HandleKeypad(char key) {
if (key == 'A') {
agree++;
} else if (key == 'B') {
oppose++;
} else if (key >= '0' && key <= '9') {
abstain++;
}
}
// 显示投票结果
void ShowResult() {
int total = agree + oppose + abstain;
char buffer[16];
sprintf(buffer, "Agree: %d", agree);
LcdWriteCmd(0x80);
for (int i = 0; i < 8; i++) {
LcdWriteData(buffer[i]);
}
sprintf(buffer, "Oppose: %d", oppose);
LcdWriteCmd(0x90);
for (int i = 0; i < 8; i++) {
LcdWriteData(buffer[i]);
}
sprintf(buffer, "Abstain: %d", abstain);
LcdWriteCmd(0x88);
for (int i = 0; i < 8; i++) {
LcdWriteData(buffer[i]);
}
sprintf(buffer, "Total: %d", total);
LcdWriteCmd(0x98);
for (int i = 0; i < 8; i++) {
LcdWriteData(buffer[i]);
}
if ((float)agree / total >= 0.5) {
LcdWriteCmd(0xc0);
LcdWriteData('P');
LcdWriteData('a');
LcdWriteData('s');
LcdWriteData('s');
} else {
LcdWriteCmd(0xc0);
LcdWriteData('F');
LcdWriteData('a');
LcdWriteData('i');
LcdWriteData('l');
}
}
void main() {
LcdInit();
LcdWriteCmd(0x80);
LcdWriteData('V');
LcdWriteData('o');
LcdWriteData('t');
LcdWriteData('e');
LcdWriteData(' ');
LcdWriteData('N');
LcdWriteData('o');
LcdWriteData('w');
while (1) {
for (int i = 0; i < 10; i++) {
char key = GetKeypadInput();
if (key) {
HandleKeypad(key);
break;
}
}
ShowResult();
}
}
```
这里假设已经实现了一个名为 `GetKeypadInput` 的函数,可以获取按键输入。这个函数需要根据实际硬件情况进行实现,常见的方法是使用矩阵按键扫描。
阅读全文