本人小白,刚开始学习单片机,但就卡在矩阵键盘问题中,求助高手
程序:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit A1=P2^5;
sbit B1=P2^6;
sbit C1=P2^7;
sbit buzz=P0^6;
uint g=0;
void delay(uint t)
{
uchar i;
for(t;t>0;t--)
for (i=1200;i>0;i--);
}
void ms_delay(uint t)
{
uchar i;
for(t;t>0;t--)
for (i=120;i>0;i--);
}
unsigned char keyscan(void)//键盘扫描函数,使用行列反转扫描法 比如:行为低电位,列为高四位
{
uint i=0;
for(i=0;i<4;i++)
{
P3=0XFF;
P3=P3&0XFE;
if(P3!=0XFF)
{
ms_delay(1200);
if(P3!=0XFF)
{
switch(P3)
{
case 0xEE:g=1;break;
case 0xDE:g=2;break;
case 0xbE:g=3;break;
}
}
}
P3=0XFF;
P3=P3&0XFD;
if(P3!=0XFF)
{
ms_delay(1200);
if(P3!=0XFF)
{
switch(P3)
{
case 0xED:g=4;break;
case 0xDD:g=5;break;
case 0xbD:g=6;break;
}
}
}
P3=0XFF;
P3=P3&0XFB;
if(P3!=0XFF)
{
ms_delay(1200);
if(P3!=0XFF)
{
switch(P3)
{
case 0xEB:g=7;break;
case 0xDB:g=8;break;
case 0xbB:g=9;break;
}
}
}
P3=0XFF;
P3=P3&0XF7;
if(P3!=0XFF)
{
ms_delay(1200);
if(P3!=0XFF)
{
switch(P3)
{
case 0xE7:g=10;break;
case 0xD7:g=11;break;
case 0xb7:g=12;break;
}
}
}
}
return g;
}
void main()
{
while(1)
{
keyscan();
switch(g)
{
case 1:
A1=0;
B1=0;
C1=1;
P0=0xff;break;
case 2:
A1=0;
B1=0;
C1=1;
P0=0xfe;break;
case 3:
A1=0;
B1=0;
C1=1;
P0=0x88;break;
case 4:
A1=0;
B1=0;
C1=1;
P0=0xf7;break;
case 5:
A1=0;
B1=0;
C1=1;
P0=0xef;break;
case 6:
A1=0;
B1=0;
C1=1;
P0=0xBB;break;
case 7:
A1=0;
B1=0;
C1=1;
P0=0x7f;break;
case 8:
A1=0;
B1=0;
C1=1;
P0=0xf0;break;
case 9:
A1=0;
B1=0;
C1=1;
P0=0xAA;break;
case 10:
A1=0;
B1=0;
C1=1;
P0=0x3c;break;
case 11:
A1=0;
B1=0;
C1=1;
P0=0xaa;break;
case 12:
A1=0;
B1=0;
C1=1;
P0=0xCC;break;
}
}
}
可是在实验板上S11,S10,S9,S8不能起作用,其他都可以工作起来,但是我在isis professional上仿真则是可以实行的,求大神帮助。
写法见笑了,
|