P0驱动数码管,按键1按下时,数值加1,到9停止;按键2按下时,在当前数值上减一,到0停止。请帮忙看看这样写合适吗谢谢。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P2^0;
sbit key2=P2^1;
uchar num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delay(uint);
void keyscan();
void main()
{
P1=0xfe;
num=5;
P0=table[num];
while(1)
{
keyscan();
P0=table[num];
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan()
{
if(key1==0)
delay(10);
if(key1==0)
{
if(num<=8)
num++;
while(!key1);
}
if(key2==0)
delay(10);
if(key2==0)
{
if(num>=1)
num--;
while(!key2);
}
}
|