仿真电路四-独立按键

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
#include "reg51.h"
sbit key=P0^0;
char num[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int n=0;
void delay(unsigned int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<500;j++);
}

void keyfun()
{
if(key==0)
{
delay(50);
if(key==0)
n++;
}
if(n==10)
n=0;
}

void smg()
{
P2=num[n];
}

int main()
{
while(1)
{
keyfun();
smg();
}
}
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
#include "reg51.h"
sbit key=P0^0;
char num[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int n=0,sign=0;
void delay(unsigned int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<500;j++);
}

void keyfun()
{
if(key==0&&sign==0)
{
delay(10);
if(key==0&&sign==0)
{
sign=1;
n++;
}
}
if(key==1&&sign==1)
{
delay(10);
if(key==1&&sign==1)
{
sign=0;
}
}

if(n==10)
n=0;
}

void smg()
{
P2=num[n];
}

int main()
{
while(1)
{
keyfun();
smg();
}
}