At the beginning, the digital tube is not displayed. After the matrix keyboard is pressed in sequence, 0 ~ F, six digital tubes are displayed statically at the same time.
Digital tube and matrix keyboard
First, disable the segment selection of all digital tubes so that the digital tubes do not display any numbers, and then select all digital tubes. Next, you only need to select all digital tubes, in the future, you only need to send the selected data to the segment, and then go to the while () loop to continuously scan the keyboard for pressing
# Include <reg52.h>
# Define uchar unsigned char
# Define uint unsigned int
Sbit Dula = P2 ^ 6;
Sbit wela = P2 ^ 7;
Uchar code table [] = {
0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79,0x71
};
Void delayms (uint XMS)
{
Uint I, J;
For (I = XMS; I> 0; I --)
For (j = 110; j> 0; j --);
}
Void display (uchar num)
{
P0 = table [num];
Dula = 1;
Dula = 0;
}
Void matrixkeyscan ()
{
Uchar temp, key;
P3 = 0xfe;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xEE:
Key = 0;
Break;
Case 0xde:
Key = 1;
Break;
Case 0xbe:
Key = 2;
Break;
Case 0x7e:
Key = 3;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Display (key );
}
}
P3 = 0xfd;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xed:
Key = 4;
Break;
Case 0xdd:
Key = 5;
Break;
Case 0xbd:
Key = 6;
Break;
Case 0x7d:
Key = 7;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Display (key );
}
}
P3 = 0xfb;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xeb:
Key = 8;
Break;
Case 0xdb:
Key = 9;
Break;
Case 0xbb:
Key = 10;
Break;
Case 0x7b:
Key = 11;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Display (key );
}
}
P3 = 0xf7;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Temp = P3;
Switch (temp)
{
Case 0xe7:
Key = 12;
Break;
Case 0xd7:
Key = 13;
Break;
Case 0xb7:
Key = 14;
Break;
Case 0x77:
Key = 15;
Break;
}
While (temp! = 0xf0)
{
Temp = P3;
Temp = temp & 0xf0;
}
Display (key );
}
}
}
Void main ()
{
P0 = 0;
Dula = 1;
Dula = 0;
P0 = 0;
Wela = 1;
Wela = 0;
While (1)
{
Matrixkeyscan ();
}
}
Here are a few
P3 = 0xfe;
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{
Delayms (10 );
Temp = P3;
Temp = temp & 0xf0;
If (temp! = 0xf0)
{.........
P3 = 0xfe: the first line is set to a low level, and all other lines are set to a high level.
Temp = P3 the current status value of reading the P3 port is assigned to the Temporary Variable temp
Temp = temp & 0xf0 calculate temp and 0xf0, and then assign the result to temp, mainly to determine whether the high 4 bits of temp have 0, if the 4-digit height of temp is not 0, the result after its operation with 0xf0 "and" is still equal to 0xf0. the high 4-bit data of temp is actually the four columns of the matrix keyboard, therefore, we can judge whether the result after the temp and 0xf0 operations is 0xf0 to determine whether the keys on the first line are pressed.
If (temp! = 0xf0) indicates that temp is the result of the calculation of the above P3 port data and 0xf0 "and". If the result temp is not equal to 0xf0, a key is pressed.
In addition
All P3 ports
Other pins to reset res \ vpd
EA \ VPP
Ale \ prog
This is all. Mine is AT89S52 (51 series)