51单片机数码管显示0-99,按下按键1家1,按下按键2件1,按下按键3归零
时间: 2023-09-06 14:11:25 浏览: 241
这个问题可以通过以下步骤来解决:
1. 初始化:将数码管全部显示为0。
2. 不断读取按键的状态,如果按键1被按下,则将当前数码管的值加1;如果按键2被按下,则将当前数码管的值减1;如果按键3被按下,则将数码管的值归零。
3. 每次修改数码管的值后,需要检查是否超出了0-99的范围,如果超出,则将数码管的值重置为0或99。
4. 将修改后的数码管的值显示在数码管上。
以下是代码示例(假设数码管使用共阴极):
```c
#include <reg51.h>
// 数码管的端口定义
sbit digit1 = P2^0;
sbit digit2 = P2^1;
sbit segmentA = P1^0;
sbit segmentB = P1^1;
sbit segmentC = P1^2;
sbit segmentD = P1^3;
sbit segmentE = P1^4;
sbit segmentF = P1^5;
sbit segmentG = P1^6;
sbit segmentDP = P1^7;
// 按键的端口定义
sbit button1 = P3^0;
sbit button2 = P3^1;
sbit button3 = P3^2;
// 数码管的显示表
unsigned char displayTable[] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
0x07, // 7
0x7f, // 8
0x6f // 9
};
// 当前数码管的值
unsigned char value = 0;
// 数码管的位选择
void selectDigit(unsigned char digit) {
switch (digit) {
case 1:
digit1 = 0;
digit2 = 1;
break;
case 2:
digit1 = 1;
digit2 = 0;
break;
default:
digit1 = 1;
digit2 = 1;
break;
}
}
// 数码管的段选择
void selectSegment(unsigned char segment) {
switch (segment) {
case 0:
segmentA = 0;
segmentB = 0;
segmentC = 0;
segmentD = 0;
segmentE = 0;
segmentF = 0;
segmentG = 1;
segmentDP = 1;
break;
case 1:
segmentA = 1;
segmentB = 0;
segmentC = 0;
segmentD = 1;
segmentE = 1;
segmentF = 1;
segmentG = 1;
segmentDP = 1;
break;
case 2:
segmentA = 0;
segmentB = 0;
segmentC = 1;
segmentD = 0;
segmentE = 0;
segmentF = 1;
segmentG = 0;
segmentDP = 1;
break;
case 3:
segmentA = 0;
segmentB = 0;
segmentC = 0;
segmentD = 0;
segmentE = 1;
segmentF = 1;
segmentG = 0;
segmentDP = 1;
break;
case 4:
segmentA = 1;
segmentB = 0;
segmentC = 0;
segmentD = 1;
segmentE = 1;
segmentF = 0;
segmentG = 0;
segmentDP = 1;
break;
case 5:
segmentA = 0;
segmentB = 1;
segmentC = 0;
segmentD = 0;
segmentE = 1;
segmentF = 0;
segmentG = 0;
segmentDP = 1;
break;
case 6:
segmentA = 0;
segmentB = 1;
segmentC = 0;
segmentD = 0;
segmentE = 0;
segmentF = 0;
segmentG = 0;
segmentDP = 1;
break;
case 7:
segmentA = 0;
segmentB = 0;
segmentC = 0;
segmentD = 1;
segmentE = 1;
segmentF = 1;
segmentG = 1;
segmentDP = 1;
break;
case 8:
segmentA = 0;
segmentB = 0;
segmentC = 0;
segmentD = 0;
segmentE = 0;
segmentF = 0;
segmentG = 0;
segmentDP = 1;
break;
case 9:
segmentA = 0;
segmentB = 0;
segmentC = 0;
segmentD = 0;
segmentE = 1;
segmentF = 0;
segmentG = 0;
segmentDP = 1;
break;
default:
segmentA = 1;
segmentB = 1;
segmentC = 1;
segmentD = 1;
segmentE = 1;
segmentF = 1;
segmentG = 1;
segmentDP = 0;
break;
}
}
// 延时
void delay(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 120; j++);
}
}
// 主函数
void main() {
// 初始化数码管
selectDigit(0);
selectSegment(10);
delay(100);
// 不断更新数码管的值
while (1) {
// 读取按键的状态
if (!button1) {
value++;
} else if (!button2) {
value--;
} else if (!button3) {
value = 0;
}
// 检查数码管的值是否超出范围
if (value > 99) {
value = 0;
} else if (value < 0) {
value = 99;
}
// 显示数码管的值
selectDigit(1);
selectSegment(displayTable[value / 10]);
delay(5);
selectDigit(2);
selectSegment(displayTable[value % 10]);
delay(5);
}
}
```
阅读全文