c语言 怎样用两个独立按键控制一位数码管显示 就是按下其中一个按键就加1 按下另一个按键就减1 0~9显示

2024年11月20日 02:28
有3个网友回答
网友(1):

这个实现是很容易的,定义两个键,一个为加,一个为减,数码管因为只有一个,所以可以用静态显示。程序和仿真图如下:

#include

#define uchar unsigned char

uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f};

sbit keyup=P3^0;

sbit keydn=P3^1;

main()

{

uchar i;

i=0;

while(1)

{

P1=table[i];

if(keyup==0)

{

i++;

while(keyup==0);

}

if(keydn==0)

{

if(i>0)i--;

while(keydn==0);

}

i%=10;

}

}

网友(2):

#include
#define Duan P0
#define Wei P1
unsigned char CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//unsigned char PLACE[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned int num,shi,ge;
sbit K3=P3^0; //K3中断加一
sbit K4=P3^1; //K4中断减一
void delay(unsigned int z);
//void display(unsigned int z);
void main()
{
Duan=0x3f;
Wei=0x7f;
while(1)
{

if(K3==0)
{
delay(10);
if(K3==0)
{
num++;
Wei=0x7f;
Duan=CODE[num];
delay(5000);
}
}
if(K4==0)
{
delay(10);
if(K4==0)
{
if(num--)
{
Wei=0x7f;
Duan=CODE[num];
delay(5000);
}
else {
num=0;
Wei=0x7f;
Duan=CODE[num];
}
}
}
}
}

void delay(unsigned int z)
{
unsigned int y;
for(;z>0;z--)
for(y=10;y>0;y--);
}
/*void display(unsigned int z)
{
shi=z/10;
ge=z%10;
while(shi==0)
{
Wei=0x7f;
Duan=CODE[ge];
}
while(shi!=0)
{
Wei=0x7F;
Duan=CODE[ge];
delay(1);
Duan=0;
Wei=0xbf;
Duan=CODE[shi];
delay(1);
Duan=0x00;
}

} */
/*void int0() interrupt 0
{
if(K3==0)
Key=1;
}
void int1() interrupt 2
{
if(K4==0)
Key=1;
} */

网友(3):

没有人回答?