#define uchar unsigned char
uchar distab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff}
void delay(unsigned int a)
{
unsigned int i,j
for(i=0i<ai++)
for(j=0j<1000j++)
}
uchar kbscan(void)
{
unsigned char sccode,recode
P2=0x0f //发0扫描,列线输入
if ((P2 &0x0f) != 0x0f) //有键按下
{
delay(20) //延时去抖动
if ((P2&0x0f)!= 0x0f)
{
sccode = 0xef //逐行扫描初值
while((sccode&0x01)!=0)
{
P2=sccode
if((P2&0x0f)!=0x0f)
{
recode=(P2&0x0f)|0xf0
return((~sccode)+(~recode))
}
else
sccode=(sccode<<1)|0x01
}
}
}
return 0 //无键按下,返回0
}
void getkey(void)
{
unsigned char key
key=kbscan()
if(key==0)return
switch(key)
{
case 0x11:P1=distab[0]break
case 0x12:P1=distab[1]break
case 0x14:P1=distab[2]break
case 0x18:P1=distab[3]break
case 0x21:P1=distab[4]break
case 0x22:P1=distab[5]break
case 0x24:P1=distab[6]break
case 0x28:P1=distab[7]break
case 0x41:P1=distab[8]break
case 0x42:P1=distab[9]break
case 0x44:P1=distab[10]break
case 0x48:P1=distab[11]break
case 0x81:P1=distab[12]break
case 0x82:P1=distab[13]break
case 0x84:P1=distab[14]break
case 0x88:P1=distab[15]break
default:P1=distab[16]break
}
}
main()
{
while(1)
{
getkey()
}
}
没有下拉电阻,如果没有下拉电阻,没有被按键时,引脚是高组态,这个时候不能用来读取数据,必须将用来读的引脚加上下拉电阻拉到地,这样才能读数据,所以你需要4个下拉电阻在键盘上。。。。 记住引脚悬空,又没有上拉或下拉电阻的时候不要读数据,这时候读的东西是不会出现你想要的结果,可能1,可能0 ,有上拉无输入时,是1,下拉式0,你这里面希望是无输入时引脚为0,所以用下拉。。。。#include <reg51.h>#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code Led_Show[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x00}
void delayms(void)
uchar kbscan(void)
void main()
{
uchar key
while(1)
{
key=kbscan()
if(key!=0)
{
switch(key)
{
case 0x18:P0 = Led_Show[0]
break
case 0x14:P0 = Led_Show[1]
break
case 0x12:P0 = Led_Show[2]
break
case 0x11:P0 = Led_Show[3]
break
case 0x28:P0 = Led_Show[4]
break
case 0x24:P0 = Led_Show[5]
break
case 0x22:P0 = Led_Show[6]
break
case 0x21:P0 = Led_Show[7]
break
case 0x48:P0 = Led_Show[8]
break
case 0x44:P0 = Led_Show[9]
break
case 0x42:P0 = Led_Show[14]
break
case 0x41:P0 = Led_Show[15]
break
default:break
}
}
}
}
uchar kbscan(void)
{
unsigned char sccode,recode
P2=0x0f //发0扫描,列线输入
if ((P2 &0x0f) != 0x0f) //有键按下
{
delayms() //延时去抖动
if ((P2&0x0f)!= 0x0f)
{
sccode = 0xef //逐行扫描初值
while((sccode&0x01)!=0)
{
P2=sccode
if((P2&0x0f)!=0x0f)
{
recode=(P2&0x0f)|0xf0
return((~sccode)+(~recode))
}
else
sccode=(sccode<<1)|0x01
}
}
}
return 0 //无键按下,返回0
}
void delayms(void)
{
unsigned char k,y
for (k=200k>0k--)
for (y=50y>0y--)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)