数码管可以显示0-999三位数字,按一下加按键,数字加1;按一下减按键,数字减1。初始化显示为学号后三位。提交C文件
时间: 2024-05-08 15:16:48 浏览: 158
51单片机仿真,三位数码管显示功能。具体功能描述如下:数码管可以显示0-999三位数字,按一下加按键,数字加1;按一下减按键,数
```c
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit keyAdd = P3 ^ 1;
sbit keySub = P3 ^ 0;
sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;
uchar code segCode[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
uchar cnt = 0;
uchar num[3] = {0, 1, 6};
void delay(uint i)
{
while (i--)
;
}
void display()
{
uchar i;
for (i = 0; i < 3; i++)
{
wela = 1 << i;
P0 = segCode[num[i]];
delay(1000);
P0 = 0x00;
wela = 0x00;
dula = 1;
dula = 0;
}
}
void keyScan()
{
if (keyAdd == 0)
{
delay(10);
if (keyAdd == 0)
{
cnt++;
if (cnt > 999)
{
cnt = 0;
}
num[0] = cnt / 100;
num[1] = cnt / 10 % 10;
num[2] = cnt % 10;
display();
}
while (!keyAdd)
;
}
if (keySub == 0)
{
delay(10);
if (keySub == 0)
{
cnt--;
if (cnt < 0)
{
cnt = 999;
}
num[0] = cnt / 100;
num[1] = cnt / 10 % 10;
num[2] = cnt % 10;
display();
}
while (!keySub)
;
}
}
void main()
{
display();
while (1)
{
keyScan();
}
}
```
阅读全文