单片机 16个按键每个键分别1~16的数字,在数码管显示,为什么显示乱码??

2024-11-23 09:26:02
推荐回答(2个)
回答1:

16个按键每个键分别0~15的数字,在数码管显示,

#include
#include

typedef unsigned char uchar;
typedef unsigned int uint;

sbit dula=P2^6;
sbit wela=P2^7;

uchar code table[] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//------------------------------------------------
void delay(uint x)
{
uint i, j;
for(i = x; i > 0; i--) for(j = 110; j > 0; j--);
}
//------------------------------------------------
void display(uchar one,uchar two)
{
P0 = table[one]; dula = 1; dula = 0;
P0 = 0xfe; wela = 1; wela = 0;
delay(10);
P0 = 0xff; wela = 1; wela = 0;

P0 = table[two]; dula = 1; dula = 0;
P0 = 0xfd; wela = 1; wela = 0;
delay(10);
P0 = 0xff; wela = 1; wela = 0;
}
//------------------------------------------------
uchar Key_Scan(void) //扫描
{
uchar i, temp1, temp2;
for(i = 0; i < 4; i++) {
P3 = ~(1 << i); //i = 0 : P1 = 1111 1110
temp1 = P3 & 0xF0;
if(temp1 != 0xF0) { //有键按下
delay(10); //延时
temp2 = P3 & 0xF0; //再读
if(temp1 == temp2) {
if (temp1 == 0x70) return i * 4 + 0;
if (temp1 == 0xb0) return i * 4 + 1;
if (temp1 == 0xd0) return i * 4 + 2;
if (temp1 == 0xe0) return i * 4 + 3;
} } }
return 16;
}
//------------------------------------------------
void main()
{
uchar key, shi, ge;
while(1) {
key = Key_Scan();
shi = key / 10;
ge = key % 10;
display(shi, ge);
}
}
//------------------------------------------------

楼主的键盘程序,显然是借鉴某个《十天...》视频教材上的。
这个教材上的键盘程序,实在太滥了,去掉一大半,就够用了。

回答2:

怎么样个乱法!你可能先测试一下0到9这几个按键有没有问题直接用一个数码管显示!试试