#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sfr P0M1=0x94;
sfr P0M0=0x93;
sfr P2M1=0x96;
sfr P2M0=0x95;
sfr P1M1=0x92;
sfr P1M0=0x91;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0~9数组
uchar buff[4]={0,0,0,0}; //位
uchar code bittab[]={0x01,0x02,0x04,0x08}; //控制位
uchar temp,unm,unm1;
void xs();
uchar key();
void delay(uchar);
void main()
{
P0M1=0xff;
P0M0=0x00;
P2M1=0xff;
P2M0=0x00;
P1M1=0xff;
P1M0=0x00;
P0=0XFF;
P2=0X00;
while(1)
{ buff[0]=0;
xs(key());
}
}
void xs() //显示
{ uchar i;
P2=0x00;
for(i=0;i<4;i++)
{
P2=bittab
;
P0=table[buff];
delay(1);
}
}
void delay(uchar z)
{
uchar x,y;
for(x=0;x<124;x++)
for(y=0;y<z;y++);
}
/*uchar key()
{
P3=0XFE;
temp=P3; //读取
temp=temp&0xf0; //是否有键按下
while(temp!=0xf0)
{
delay(20);
temp=P3; //读取
temp=temp&0xf0; //是否有键按下
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe0:unm=1;break;
} } }
return unm;
}
*/
uchar key()
{
uchar huoqu;
P3|=0x07;
temp=P3;
if(temp&0x07==0x07){huoqu=0xff;return(huoqu);}
delay(20);
P3|=0x07;
if(temp&0x07==0x07){huoqu=0xff;return(huoqu);}
huoqu=temp&0x07;
while(temp!=0X07)
{
switch(temp)
{
case 0x06:unm=2;break;
}
}
return huoqu;
}
此帖出自小平头技术问答
一周热门 更多>