求独立按键控制两位数码管的
proteus仿真图,k1加;k2减;k3清零
程序如下
#include"reg52.h"
typedef unsignedint u16;
typedef unsignedchar u8;
***it LSA=P2^2;
***it LSB=P2^3;
***it LSC=P2^4;
***it k1=P3^1;
***it k2=P3^0;
***it k3=P3^2;
***it k4=P3^3;
u8 codesmgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
#define KEY1 1
#define KEY2 2
#define KEY3 3
#define KEY4 4
char keynum;
u8DisplayData[8];
void delay(u16i)
{
while(i--);
}
u8 KeyScan(u8mode)
{
if(mode==1)
{
keyen=1;
}
if(keyen==1&&(k1==0||k2==0||k3==0||k4==0))
{
delay(1000);
keyen=0;
if(k1==0)return KEY1;
else if(k2==0)return KEY2;
else if(k3==0)return KEY3;
else if(k4==0)return KEY4;
}
elseif(k1==1&&k2==1&&k3==1&&k4==1)
{
keyen=1;
}
return 0;
}
void DigDisplay()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i)
{
case(0):
LSA=1;LSB=1;LSC=1;break;
case(1):
LSA=0;LSB=1;LSC=1;break;
case(2):
LSA=1;LSB=0;LSC=1;break;
case(3):
LSA=0;LSB=0;LSC=1;break;
case(4):
LSA=1;LSB=1;LSC=0;break;
case(5):
LSA=0;LSB=1;LSC=0;break;
case(6):
LSA=1;LSB=0;LSC=0;break;
case(7):
LSA=0;LSB=0;LSC=0;break;
}
P0=DisplayData;
delay(100);
P0=0x00;
}
}
void datapros()
{
u8 key=KeyScan(0);
switch(key)
{
case 1: keynum++;if(keynum==100)keynum=0; break;
case 2: keynum--;if(keynum<=0)keynum=99; break;
case 3: keynum=0; break;
case 4: break;
}
DisplayData[0]=smgduan[keynum/10];
DisplayData[1]=smgduan[keynum%10];
}
void main()
{
while(1)
{
datapros();
DigDisplay();
}
}