【Proteus仿真】51单片机按键控制数码管加减

【Proteus仿真】51单片机按键控制数码管加减


  • 📺Proteus仿真演示:
    在这里插入图片描述

📋采用的是8位数码管,实际上只使用了3位数码管用来显示。

📝实例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

uchar code DSY_CODE[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff
};//共阳数码管断码
uchar Num_Buffer[] = {0,0,0};
uchar Key_Code,Key_Counts=0;

void DelayMS(uint x)
{
uchar t;
while(x--)
{
for(t=0; t<120; t++);
}
}

void Show\_Counts\_ON\_DSY()
{
uchar i,j=0x01;
Num_Buffer[2]=Key_Counts/100;
Num_Buffer[1]=Key_Counts/10%10;
Num_Buffer[0]=Key_Counts%10;
for(i=0; i<3; i++)
{
j = \_cror\_(j,1);
P2 = j;
P0 = DSY_CODE[Num_Buffer[i]];
DelayMS(5);
P2 = 0x00;//消隐
}

}

void main()
{
uchar i;
P0 = 0xff;
P1 = 0xff;
P2 = 0x00;
Key_Code = 0xff;
while(1)
{
Show\_Counts\_ON\_DSY();
P1 = 0xff;
Key_Code = P1;
if(Key_Code != 0xff)
{
for(i=0; i<30; i++)
{
Show\_Counts\_ON\_DSY();
}
}
switch(Key_Code)
{
case 0xfe:
if(Key_Counts<255) Key_Counts++;
break;
case 0xfd:
if(Key_Counts>0) Key_Counts--;
break;
case 0xfb:
Key_Counts=0;
}
Key_Code = 0xff;
}
}

📚程序源码和仿真资源

1
2
3
链接:https://pan.baidu.com/s/1KQQje51fgE966knzK8-TkQ 
提取码:1bbq