#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define dig P0
#define key P1
uchar code DIG_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //数码管显示编码
void keyscan(); //检测按键子程序
uchar keyvalue; //存放读取的键值
void Delay10ms(unsigned int c); //误差 0us 延时10ms
void main()
{
while(1)
{
keyscan(); //读取键值
dig=DIG_code[keyvalue];
}
}
void keyscan()
{
uchar i;
i=0;
while(key!=0x0f)
{
Delay10ms(1); //去抖
while(key!=0x0f)
{
key=0x0f; //给每一列赋高电平,如果有一个按下则那一列会变低电平
switch(key) //switch函数开始的时候会实时读取变量的值
{
case(0x07):
keyvalue=0;
break;
case(0x0b):
keyvalue=1;
break;
case(0x0d):
keyvalue=2;
break;
case(0x0e):
keyvalue=3;
break;
default:
break;
}
key=0xf0; //同理 检测行
switch(key)
{
case(0x70):
keyvalue+=0;
break;
case(0x0b):
keyvalue+=4;
break;
case(0x0d):
keyvalue+=8;
break;
case(0x0e):
keyvalue+=12;
break;
default:
break;
}
while(i<50&&key!=0x0f)
{
Delay10ms(1);
i++;
}
}
}
}
void Delay10ms(unsigned int c) //误差 0us 延时10ms
{
unsigned char a, b;
//--c已经在传递过来的时候已经赋值了,所以在for语句第一句就不用赋值了--//
for (;c>0;c--)
{
for (b=38;b>0;b--)
{
for (a=130;a>0;a--);
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
typedef unsigned char uint8;
typedef unsigned int uint16;
uint8 keyvalue;
uint8 smg[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delay(uint16 i)
{
uint8 j,k;
for(;i>0;i--)
for(j=38;j>0;j--)
for(k=130;k>0;k--);
}
void keyscan()
{
P1=0x0f;
if(P1!=0x0f)
{
delay(2);
if(P1!=0x0f)
{
switch(P1)
{
case 0x07: keyvalue=12;break;
case 0x0b: keyvalue=8;break;
case 0x0d: keyvalue=4;break;
case 0x0e: keyvalue=0;break;
}
P1=0xf0;
switch(P1)
{
case 0x70: keyvalue=keyvalue+3;break;
case 0xb0: keyvalue=keyvalue+2;break;
case 0xd0: keyvalue=keyvalue+1;break;
case 0xe0: keyvalue=keyvalue+0;break;
}
while(P1!=0xf0);
}
}
}
void main()
{
P0=smg[0];
while(1)
{
keyscan();
P0=smg[keyvalue];
}
}
一周热门 更多>