矩阵键盘扫描程序,请前辈们帮我看看哪里出错了
时间:10-02
整理:3721RD
点击:
功能:按下键盘上任意键,在数码管上显示相应的编码。
数码管的位选端是用 三八译码器控制的
程序如下,编译没有问题,但下载到开发板上不运行,按键没有任何反应,求前辈们帮忙看看哪里写的不对,不胜感激。
#include "MAIN.H"
#include "IO.H"
unsigned char hang_data;
unsigned char lie_data;
unsigned char num,i;
unsigned int j;
#define lie_bit(bit0,bit1,bit2){\
P3_0=bit0;\
P3_1=bit1;\
P4_4=bit2;}
const unsigned char Led_Code[16] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e} ;
unsigned char Led_weixuan[6]={0x01,0x00,0x03,0x02,0x05,0x04};
typedef union bit_oprte_format
{
struct
{
unsigned char bit0 : 1;
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned char bit3 : 1;
unsigned char bit4 : 1;
unsigned char bit5 : 1;
unsigned char bit6 : 1;
unsigned char bit7 : 1;
} flag;
unsigned char bit_all;
}bit_format;
bit_format hang;
unsigned char hang_read(unsigned char hang_data)
{
hang.flag.bit0=P3_7 ; //hang0
hang.flag.bit1=P3_6 ; //hang1
hang.flag.bit2=P4_3 ; //hang2
hang.flag.bit3=P1_5 ; //hang3
hang_data =hang.bit_all ;
return hang_data;
}
void display(unsigned char key_num)
{
wx_write(Led_weixuan[0]);
io_write(key_Code[key_num]);
delay(1);
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void key_scan(void)
{
unsigned char temp,key_data;
lie_bit(0,1,1);
temp=hang_read();
temp=temp&0x0f;
if(temp!= 0x0f)
{
delay(10);
if(temp!= 0x0f)
{
switch(temp)
{
case 0x0e: {key_data = 1;break;}
case 0x0d: {key_data = 4;break;}
case 0x0b: {key_data = 7;break;}
case 0x07: {key_data = 10;break;}
}
while(temp!= 0x0f);
display(key_data);
}
lie_bit(1,0,1);
temp=hang_read();
temp=temp&0x0f;
if(temp!= 0x0f)
{
delay(10);
if(temp!= 0x0f)
{
switch(temp)
{
case 0x0e: {key_data =2;break;}
case 0x0d: {key_data =5;break;}
case 0x0b: {key_data =8;break;}
case 0x07: {key_data =11;break;}
}
while(temp!= 0x0f);
display(key_data);
}
}
lie_bit(1,1,0);
temp=hang_read();
temp=temp&0x0f;
if(temp!= 0x0f)
{
delay(10);
if(temp!= 0x0f)
{
switch(key_oprate_hang())
{
case 0x0e: {key_data = 3;break;}
case 0x0d: {key_data = 6;break;}
case 0x0b: {key_data = 9;break;}
case 0x07: {key_data = 12;break;}
}
while(temp=temp&0x0f);
display(key_data);
}
}
}
}
void main()
{
while(1)
{
key_scan();
}
}
数码管的位选端是用 三八译码器控制的
程序如下,编译没有问题,但下载到开发板上不运行,按键没有任何反应,求前辈们帮忙看看哪里写的不对,不胜感激。
#include "MAIN.H"
#include "IO.H"
unsigned char hang_data;
unsigned char lie_data;
unsigned char num,i;
unsigned int j;
#define lie_bit(bit0,bit1,bit2){\
P3_0=bit0;\
P3_1=bit1;\
P4_4=bit2;}
const unsigned char Led_Code[16] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e} ;
unsigned char Led_weixuan[6]={0x01,0x00,0x03,0x02,0x05,0x04};
typedef union bit_oprte_format
{
struct
{
unsigned char bit0 : 1;
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned char bit3 : 1;
unsigned char bit4 : 1;
unsigned char bit5 : 1;
unsigned char bit6 : 1;
unsigned char bit7 : 1;
} flag;
unsigned char bit_all;
}bit_format;
bit_format hang;
unsigned char hang_read(unsigned char hang_data)
{
hang.flag.bit0=P3_7 ; //hang0
hang.flag.bit1=P3_6 ; //hang1
hang.flag.bit2=P4_3 ; //hang2
hang.flag.bit3=P1_5 ; //hang3
hang_data =hang.bit_all ;
return hang_data;
}
void display(unsigned char key_num)
{
wx_write(Led_weixuan[0]);
io_write(key_Code[key_num]);
delay(1);
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void key_scan(void)
{
unsigned char temp,key_data;
lie_bit(0,1,1);
temp=hang_read();
temp=temp&0x0f;
if(temp!= 0x0f)
{
delay(10);
if(temp!= 0x0f)
{
switch(temp)
{
case 0x0e: {key_data = 1;break;}
case 0x0d: {key_data = 4;break;}
case 0x0b: {key_data = 7;break;}
case 0x07: {key_data = 10;break;}
}
while(temp!= 0x0f);
display(key_data);
}
lie_bit(1,0,1);
temp=hang_read();
temp=temp&0x0f;
if(temp!= 0x0f)
{
delay(10);
if(temp!= 0x0f)
{
switch(temp)
{
case 0x0e: {key_data =2;break;}
case 0x0d: {key_data =5;break;}
case 0x0b: {key_data =8;break;}
case 0x07: {key_data =11;break;}
}
while(temp!= 0x0f);
display(key_data);
}
}
lie_bit(1,1,0);
temp=hang_read();
temp=temp&0x0f;
if(temp!= 0x0f)
{
delay(10);
if(temp!= 0x0f)
{
switch(key_oprate_hang())
{
case 0x0e: {key_data = 3;break;}
case 0x0d: {key_data = 6;break;}
case 0x0b: {key_data = 9;break;}
case 0x07: {key_data = 12;break;}
}
while(temp=temp&0x0f);
display(key_data);
}
}
}
}
void main()
{
while(1)
{
key_scan();
}
}
按键接口没有初始化,如果51单片机,io口作为输入先置1
把这两句去掉
while (temp != 0x0f);
while (temp = temp & 0x0f);