- 37
- 0
- 约4.05千字
- 约 7页
- 2017-08-15 发布于贵州
- 举报
程序:1
#includereg51.h
#define GPIO_KEY P1
#define uchar unsigned char
#define uint unsigned int
sbit wela =P3^7;
sbit dula =P3^6;
uchar LedCode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
uchar str[4]={0x00,0x00,0x00,0x00};
unsigned char KeyValue;
void Delay10ms(uint c);
void KeyDown();
void Display(void);
uchar KeyCheck(void);
void main(void)
{
uchar a, b;
uchar i=0;
while(1)
{
KeyDown();
a=KeyCheck() ;
if(a!=0)
{
Delay10ms(10);
if(a!=0)
{
b=KeyValue;
str[i]=b;
i++;
if(i3)i=0;
}
}
Display();
}
}
void Display(void)
{
uchar i,j;
j=0xf8;
for(i=0;i4;i++)
{
wela=1;
P2=j;
wela=0;
dula=1;
P0=LedCode[str[i]];
dula=0;
j=1;
j=0xf7;
Delay10ms(1);
}
}
void KeyDown(void)
{
uchar a = 0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)
{
Delay10ms(1);
if(GPIO_KEY!=0x0f)
{
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=3;break;
case(0X0b): KeyValue=2;break;
case(0X0d): KeyValue=1;break;
case(0X0e): KeyValue=0;break;
}
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue+12;break;
case(0Xb0): KeyValue=KeyValue+8;break;
case(0Xd0): KeyValue=KeyValue+4;break;
case(0Xe0): KeyValue=KeyValue;break;
}
while((a5) (GPIO_KEY!=0xf0))
{
Delay10ms(1);
a++;
}
}
}
}
void Delay10ms(unsigned int c)
{
unsigned char a, b;
for (;c0;c--)
{
for (b=38;b0;b--)
{
for (a=130;a0;a--);
}
}
}
uchar KeyCheck(void)
{
uchar tmp=0;
P1=0x0f; //P1.0~P1.3是列P1.4~P1.7是行(0输出的为行)
tmp=P1;
tmp=~tmp;
tmp=0x0f;
if( tmp == 0 ) return(0); //无键按下返回0
else return(255); //有键按下返回255
}
程序:2
#include reg52.h
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit wela =P3^7;
sbit dula =P3^6;
//共阳数码管译码表
uchar code LedCode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
原创力文档

文档评论(0)