#include "reg52.h"
sfr P4 = 0xc0;
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C4 = P3^4;
sbit C3 = P3^5;
sbit C2 = P4^2;
sbit C1 = P4^4;
unsigned char code table[] =
{
0xc0,//0
0xf9,//1
0xa4,//2
0xb0,//3
0x99,//4
0x92,//5
0x82,//6
0xf8,//7
0x80,//8
0x90,//9
0x88,//A
0x83,//B
0xc6,//C
0xa1,//D
0x86,//E
0x8e,//F
0xbf,//-
0x7f//.
};
void HC138(unsigned char n)
{
switch(n)
{
case 4:
P2 = (P2&0x1f) | 0x80;
break;
case 5:
P2 = (P2&0x1f) | 0xa0;
break;
case 6:
P2 = (P2&0x1f) | 0xc0;
break;
case 7:
P2 = (P2&0x1f) | 0xe0;
break;
}
}
void display(unsigned char val)
{
HC138(6);
P0 = 0x01;
HC138(7);
P0 = table[val];
}
unsigned char key_num = 0;
void scan()
{
R1 = 0;
R2 = 1;
R3 = 1;
R4 = 1;
C1 = 1;
C2 = 1;
C3 = 1;
C4 = 1;
if(C1 == 0)
{
while(C1 == 0);
key_num = 0;
display(key_num);
}
else if(C2 == 0)
{
while(C2 == 0);
key_num = 1;
display(key_num);
}
else if(C3 == 0)
{
while(C3 == 0);
key_num = 2;
display(key_num);
}
else if(C4 == 0)
{
while(C4 == 0);
key_num = 3;
display(key_num);
}
R1 = 1;
R2 = 0;
R3 = 1;
R4 = 1;
C1 = 1;
C2 = 1;
C3 = 1;
C4 = 1;
if(C1 == 0)
{
while(C1 == 0);
key_num = 4;
display(key_num);
}
else if(C2 == 0)
{
while(C2 == 0);
key_num = 5;
display(key_num);
}
else if(C3 == 0)
{
while(C3 == 0);
key_num = 6;
display(key_num);
}
else if(C4 == 0)
{
while(C4 == 0);
key_num = 7;
display(key_num);
}
R1 = 1;
R2 = 1;
R3 = 0;
R4 = 1;
C1 = 1;
C2 = 1;
C3 = 1;
C4 = 1;
if(C1 == 0)
{
while(C1 == 0);
key_num = 8;
display(key_num);
}
else if(C2 == 0)
{
while(C2 == 0);
key_num = 9;
display(key_num);
}
else if(C3 == 0)
{
while(C3 == 0);
key_num = 10;
display(key_num);
}
else if(C4 == 0)
{
while(C4 == 0);
key_num = 11;
display(key_num);
}
R1 = 1;
R2 = 1;
R3 = 1;
R4 = 0;
C1 = 1;
C2 = 1;
C3 = 1;
C4 = 1;
if(C1 == 0)
{
while(C1 == 0);
key_num = 12;
display(key_num);
}
else if(C2 == 0)
{
while(C2 == 0);
key_num = 13;
display(key_num);
}
else if(C3 == 0)
{
while(C3 == 0);
key_num = 14;
display(key_num);
}
else if(C4 == 0)
{
while(C4 == 0);
key_num = 15;
display(key_num);
}
}
void main()
{
while(1)
{
scan();
}
}
标签:06,矩阵,键盘,while,num,key,C3,C2,display From: https://www.cnblogs.com/Unclesundada/p/17003739.html