forked from MetalSeed/stc51_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.2.c
115 lines (108 loc) · 2.47 KB
/
4.2.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/********************************************************************
* 文件名 : 独立按键.c
* 描述 : 数码管上最开始显示“88”。
按S1,数码管上的值减1.
按S2,数码管上的值加1.
***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit KEY1 = P3^2;
sbit KEY2 = P3^3;
sbit KEY3 = P3^4;
uint i=88;
//数码管的段码编码
uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//下一行的数组可以显示数值外,还可以显示数码管的点
uchar table_d[16] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};
/********************************************************************
* 名称 : Delay_1ms()
* 功能 : 延时子程序,延时时间为 1ms * x
* 输入 : x (延时一毫秒的个数)
* 输出 : 无
***********************************************************************/
void Delay_1ms(uint i)
{
uint x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
/********************************************************************
* 名称 : Delay()
* 功能 : 延时,延时时间为 10ms * del
* 输入 : del
* 输出 : 无
***********************************************************************/
void Delay(uchar temp)
{
uchar j;
for(j=0;j<temp;j++)
{
P0 = table[(i/10)%10];
P2 = 6;
Delay_1ms(5);
P0 = table[i%10];
P2 = 7;
Delay_1ms(5);
}
}
/********************************************************************
* 名称 : Delay()
* 功能 : 实现按键功能,并在LED上显示
* 输入 : 无
* 输出 : 无
***********************************************************************/
uchar KEY(void)
{
if(KEY1==0 || KEY2==0 || KEY3==0)
{
Delay(2); //20毫秒软件防抖
if(KEY1 == 0)
{
Delay(10);
return 1;
}
if(KEY2 == 0)
{
Delay(10);
return 2;
}
if(KEY3 == 0)
{
Delay(10);
return 3;
}
Delay(10);
}
return 0;
}
/********************************************************************
* 名称 : Main()
* 功能 : 实现按键控制LED的显示
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Main(void)
{
uchar temp;
while(1)
{
temp = KEY();
if(temp == 1)
{
i--;
}
if(temp == 3)
{
i++;
}
P0 = 0;
P2 = 6;
P0 = table[(i/10)%10];
Delay_1ms(3);
P0 = 0;
P2 = 7;
P0 = table[i%10];
Delay_1ms(3);
}
}