在做2x5的矩阵键盘时,写完一段代码后检测的时候,行的那个红蓝指示灯狂闪是为什么啊哥,是定义出错了吗
下面是代码:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit H1 = P1^0;
sbit H2 = P1^1;
sbit L1 = P1^2;
sbit L2 = P1^3;
sbit L3 = P1^4;
sbit L4 = P1^5;
sbit L5 = P1^6;
sbit LATCH = P2^2;
sbit CLK = P2^1;
sbit DIO = P2^0;
uchar code LED_NUM[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
void delay(uint t)
{
uint i, j;
for (i = 0; i < t; i++)
{
for (j = 0; j < 120; j++);
}
}
uchar get_key()
{
uchar key = 0xff;
H1 = 0;
if (L1==0) key = 1;
if (L2==0) key = 2;
if (L3==0) key = 3;
if (L4==0) key = 4;
if (L5==0) key = 5;
H1 = 1;
H2 = 0;
if (L1==0) key = 6;
if (L2==0) key = 7;
if (L3==0) key = 8;
if (L4==0) key = 9;
if (L5==0) key = 0;
H2 = 1;
return key;
}
void display(uchar ten, uchar one)
{
LATCH = 0;
DIO = LED_NUM[ten];
CLK = 0;
CLK = 1;
DIO = LED_NUM[one];
CLK = 0;
CLK = 1;
LATCH = 1;
}
void main()
{
uchar key, ten, one;
P0 = 0x00;
P1 = 0xff;
P2 = 0x00;
while (1)
{
key = get_key();
if (key!=0xff)
{
ten = key / 10;
one = key % 10;
display(ten, one);
}
}
}
|