MCU / test /probe5 /5.c
KeLe-open-code's picture
Publish reviewed course materials
37e076f verified
Raw History Blame Contribute Delete
1.23 kB
#include <REG51.H>
/* ���н� P3.6��P3.7�����д����ҽ� P3.3��P3.2��P3.1��P3.0 */
sbit row1 = P3^7;
sbit row2 = P3^6;
sbit col1 = P3^3;
sbit col2 = P3^2;
sbit col3 = P3^1;
sbit col4 = P3^0;
void delayMs(unsigned int i)
{
unsigned char t;
while (i--)
for (t = 0; t < 120; t++);
}
unsigned char scanKey(void)
{
unsigned char key = 0;
P3 = 255; /* ����������д 1 */
row1 = 0; /* ɨ���һ�� */
if (col1 == 0) key = 1;
else if (col2 == 0) key = 2;
else if (col3 == 0) key = 3;
else if (col4 == 0) key = 4;
row1 = 1;
row2 = 0; /* ɨ��ڶ��� */
if (key == 0)
{
if (col1 == 0) key = 5;
else if (col2 == 0) key = 6;
else if (col3 == 0) key = 7;
else if (col4 == 0) key = 8;
}
row2 = 1;
return key; /* û�а������� 0 */
}
void main(void)
{
unsigned char key;
P2 = 255; /* LED �͵�ƽ��������ʼȫ�� */
while (1)
{
key = scanKey();
if (key != 0)
{
delayMs(20); /* �������� */
if (scanKey() == key)
P2 = 255U << key; /* ��λ�� 0��������Ӧ�����ĵ� */
}
}
}