写了个单片机矩阵按键控制数码管显示数字的程序,只需要显示0到9就好了。但是自己写的调不出来,谢谢帮忙
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90";
void play(uchar num)
{
P0=table[num];
}
void delay(uint z)
{
int i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void scan()
{
uchar M,key;
P2=0Xfe;
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
delay(5);
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
M=P2;
switch(M)
{
case 0xee:key=0;break;
case 0xde:key=1;break;
case 0xbe:key=2;break;
case 0x7e:key=3;break;
}
while(M!=0XF0);
{
M=P2;
M=M&0XF0;
}
play(key);
}
}
P2=0Xfd;
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
delay(5);
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
M=P2;
switch(M)
{
case 0xed:key=4;break;
case 0xdd:key=5;break;
case 0xbd:key=6;break;
case 0x7d:key=7;break;
}
while(M!=0XF0);
{
M=P2;
M=M&0XF0;
}
play(key);
}
}
P2=0Xfb;
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
delay(5);
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
M=P2;
switch(M)
{
case 0xeb:key=8;break;
case 0xdb:key=9;break;
case 0xbb:key=10;break;
case 0x7b:key=11;break;
}
while(M!=0XF0);
{
M=P2;
M=M&0XF0;
}
play(key);
}
}
P2=0Xf7;
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
delay(5);
M=P2;
M=M&0XF0;
if(M!=0XF0)
{
M=P2;
switch(M)
{
case 0xe7:key=12;break;
case 0xd7:key=13;break;
case 0xb7:key=14;break;
case 0x77:key=15;break;
}
while(M!=0XF0);
{
M=P2;
M=M&0XF0;
}
play(key);
}
}
}
void main()
{
P0=0XC0;
P1=0xf0;
while(1)
{
scan();
}
} 展开
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]=
{
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e
} ;//共阳极数码管0 为亮
sbit wei=P1^0;
void delay(uchar t)//毫秒延
{
uint i,j;
for(i=0; i<t; i++)
for(j=0; j<110; j++);
}
void display(uchar d)
{
P1=0x00;
wei=1; //第一个数码管点亮
P0=table[d];
}
void main()
{
uchar v,temp;
uchar key_l,key_h;// 定义列和行
uchar key;
while(1)
{
P2=0xf0;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0) //检测到有按键按下
{
delay(10);//消抖
if(temp!=0xf0)//再次确认一下有否真的有按下
{
temp=P2;//把这次的检测值作为真值
key_l=temp&0xf0;
P2=key_l|0x0f;//让低四位置1 1110 1111
temp=P2;//赶紧读取P口值
key_h=temp&0x0f;//得到行值 0000 1110
key=key_l+key_h;//1110 0000
//+ 0000 1110
//ee 1110 1110
}
switch(key)
{
case 0xee:display(0);break;
case 0xde:display(1);break;
case 0xbe:display(2);break;
case 0x7e:display(3);break;
case 0xed:display(4);break;
case 0xdd:display(5);break;
case 0xbd:display(6);break;
case 0x7d:display(7);break;
case 0xeb:display(8);break;
case 0xdb:display(9);break;
case 0xbb:display(10);break;
case 0x7b:display(11);break;
case 0xe7:display(12);break;
case 0xd7:display(13);break;
case 0xb7:display(14);break;
case 0x77:display(15);break;
}
}
}
}
把行列扫描的值相加 key=key_l+key_h; 我也是调了很长时间 你要把原理彻底搞明白了 呵呵
{
P2=0xfe;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xee:num=0;
break;
case 0xde:num=1;
break;
case 0xbe:num=2;
break;
case 0x7e:num=3;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
flag=flag+1;
}
}
P2=0xfd;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xed:num=4;
break;
case 0xdd:num=5;
break;
case 0xbd:num=6;
break;
case 0x7d:num=7;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
flag=flag+1;
}
}
P2=0xfb;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xeb:num=8;
break;
case 0xdb:num=9;
break;
case 0xbb:num=10;
break;
case 0x7b:num=11;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
flag=flag+1;
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:num=12;
break;
case 0xd7:num=13;
break;
case 0xb7:num=14;
break;
case 0x77:num=15;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
flag=flag+1;
}
}
}
这是键扫程序,肯定有用的