3×3 matrix keyboard digital tube display 0 to 8
Then choose the digital tube, assuming that the matrix keyboard connected to the P1 port, the digital tube connected to the P0 port, the digital tube displays 0 after power-up, and then the 3 * 3 keyboard control displays 1 to 9, the program in c language is as follows:/***************************************** *********************************************************************** scanning method to monitor the keyboard and display the value of the keys. I have a 4*4 board, so I didn’t debug it. If there is something wrong, I will adjust it for you again. I see that there are other people asking again, don’t mind if I go to earn some points~~~o(︶︶︶︶)o*************************************************************************** ***************************************/#include<reg52.h>//file include
#defineuintunsignedint
#defineucharunsignedchar/#defineuintunsignedint
#defineucharunsignedchar/#defineuintunsignedchar/ //Macro definitions for ease of use
ucharcodetable[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90};//digital tube display code
ucharnum,temp. num1;//global variable definition
/************************ delay subfunction ***********************************/voiddelay(uintz)
{
uintx,y;
< p>for(x=z;x>0;x–)
for(y=110;y>0;y–);
}
/************************** main function *********************************** **/ucharkeyscan();//define key scan function voiddisplay(ucharaa);//define display function voidmain()
{
num=0;
P0=0xc0;
while(1)
{
< p>display(keyscan());//call the keyscan subfunction
}
}
/*************************display subfunction **********************************/ voiddisplay(ucharaa)
{P0=table[aa];
}/************************ keyboard scanning subfunction *******************************/ucharkeyscan()
{
P1=0xfe;//P1 assigns a value and sends 0 low
temp=P1;//temp reads back the value of P1.
Microcontroller matrix keyboard key number discrimination
PA port connects to the column line, PC port connects to the line line
Sweep the surface of the column line, read the state of the line line …. The default line is high, when the line and the column line intersect, a column line is low, then if no key is pressed in this column, the state of the line is still high, if there is a key pressed, then the line is shorted to low, which line line outputs a low level, then it is the key is pressed in this line, and know which column is in, it will be completely sure that the key is pressed which key is pressed … That is, the first line number plus the column number is the key number (both line number and column number start from 0)
It should be that the first key number of the first line is 8, and the column number is 1, so the key number of the closed key is 9, right? (What you said yourself earlier is also the same: the key number of the key number of the closed key is equal to the column number of the low level plus the first key number of the line number of the line line that is low)
Scanning means that the scanning doesn’t start when the columns are all high and when scanning starts, all of the columns are low. When scanning, first column 0 becomes low, other columns are still high, then column 1 becomes low, other columns are still high (including column 0, which became low earlier, is now back to high), then column 2 becomes low, other columns are high, until column 7 becomes low, other columns are high, completing a scan…
And “when PA port output is 11111101, it means that the 1st column output is low, the other columns are high, the columns are input ports, not read its state, the row port is output port, read the row state, if pc0~pc3 output 1101, if pc0~pc3 corresponds to rows 0~3, then the 2nd row is low (). you said: then 1 row 1 column intersection key is closed, is pc0~pc3 corresponds to row 3~0 in turn), then 1 column 2 rows intersection key is closed, there are 8 columns in each row, the second row of the first line number (because the line number starts from 0, so the line number for the line number of 2 rows, according to the customary is the third line) is 16, the column number for the first, so the key number is 16 +1 = 17
The following have scanning the 2 * 4 keypad assembly program (yours is 4*8, that is, 4 rows and 8 columns of the keyboard), just for reference
Can’t read it without forcing it, I hope it will help you
DATASEGMENT
TABLEDB’A’,’B ‘,’C’,’D’,’E’,’F’. ‘G’,’H’
DATAENDS
CODESEGMENT
ASSUMECS:CODE,DS:DATA
START. MOVAX,DATA
MOVDS,AX
MOVAL,81H; make the 8255’s A and C ports and initialize
OUT63H,AL
START1:
CALLEXAM; subroutine to determine if there is a key press
JZSTART1
CALLDELAY; delay program to determine whether a key is pressed by mistake
CALLEXAM
JZSTART1; if it is pressed by mistake, return to the beginning, if it is not, the sequence will be executed
MOVCL,00H; start the judgment of key number
MOVCH,0FEH< /p>
CHECK:
MOVAL,CH
OUT60H,AL
INAL,62H; read data from the c-port, i.e., read the status of the line…
TESTAL,01H; determine if it is line 0
JNZOTHER; if not, turn to OTHER
MOVAL,00H; if yes, the line header number is set to 0
JMPKCODE; determine the key number
OTHER:; determine if it is line 1
TESTAL,02H
JNZNEXT; if not, jump to NEXT for next column
MOVAL,04H; first number of row 1 set to 4
KCODE:; determine key number
ADDAL,CL
MOVBX,OFFSETTABLE
ANDAX,0FFH
ADDBX,AX
MOVAL,[BX]
MOV[DI],AL
MOVAH,01H
MOVAL,[DI]
INT10H
NEXT:; make the judgment of the next column<
INCCL
MOVAL,CH
TESTAL,08H
JZSTART1
ROLAL,1
MOVCH,AL
JMPCHECK
EXAMPROC
MOVAL,[00H
NEXT. 00H
OUT60H,AL
INAL,62H
NOTAL
TESTAL,03H
RET
EXAMENDP
DELAYPROC
PUSHCX
MOVCX, 0010H
T1:
MOVAX,1000H
T2:DECAX
JNZT2
LOOPT1
POPCX
RET
DELAYENDP
CODEENDS
ENDSTART
C51 Matrix Keyboard Scanning Please explain the program especially the key steps Urgent!!!!
C51P1 Port 4X4 Keyboard Explanation
This is a 4X4 keyboard made with the P1 port of a C51 microcontroller. The lower 4 bits of the p1 port are the keyboard column scan lines, and the higher 4 bits are the keyboard row scan lines.
The column scan lines are the outputs and the row scan lines are the inputs.
The following program to make a description
(***) indicates that attention
1, the first to determine the entire keyboard there is no key pressed, as long as the line scan line input is not all 1, (1111) that is, there is a key pressed;
P1=0xf0;if ((P1&0xf0)! =0xf0)if no key pressed, all 1, then return return-1;
If there is a key pressed then delay, again to determine whether there is a key pressed, Delay();if((P1&0xf0)! =0xf0)If no key is pressed then return return-1.
A key is pressed then continue, this process is to judge the key to eliminate the jitter, to avoid reading the key value many times,*** or because of the key jitter to read the key value of the key is not pressed, an error occurs,*** column scanning line is the output of the full 0, P1 = 0xf0.
2, into the reading of the key value of the key value, and the above is different, and the key value is not read. Each judgment,*** column scan line only one output is 0, that is, P1=0xfe, 0xfd,0xfb,0xf7;
first column scan line P1.0,sCode=0xfe; if the row scan line is all 1, then no key pressed in this column, scanning the next column
sCode=_crol_(sCode,1);*** sCode left shift one bit, that is 0xfd, so scan 4 times, row scan line are all 0, then no key pressed,
return return-1;
If the row scan line is not all 0, that is, there is a key pressed, now you can read the key value
kCode=~P1;//P1=EE,ED …
for(i=0;i<16;i++)
{
if(kCode==KeyCodeTable[i])
returni;
}
1. First kCode=~P1;***p1 value take the inverse row of the scan line may be 1, 2,4,8; the same column scan line corresponds to the value 1,2,4,8
Together p1 has 16 values, that is, KeyCodeTable[i] table x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,
0x41,0x42,0x44,0x48,0x81, 0x82,0x84,0x88
If (kCode==KeyCodeTable[i]) holds, the corresponding i value is the key number.
2. Return the i value is the key number,returni;.
ucharKeys_Scan()
{
ucharsCode,kCode,i,k;
P1=0xf0;
if((P1&0xf0)! =0xf0)//scan column
{
Delay();
if((P1&0xf0)! =0xf0)//dithering
{
sCode=0xfe;
for(k=0;k<4;k++)
{
P1=sCode;//find the low
if((P1&0xf0)! =0xf0)//only equal to perform elseP1 and 0xf0 for and for 0xf0 with the same true for true, one false for false
{
kCode=~P1;//P1=EE,ED…
for(i=0;i<16;i++)
{
if(kCode==KeyCodeTable[i])
returni;
}
}
}
else
sCode=_crol_( sCode,1);
}
}
}
return-1;
}
arduino4*4 matrix keyboard how to use
Here is a brief introduction to a matrix keyboard works, 4*4 matrix keyboard has 8 pins, 4 a group, corresponding to the rows and columns, through the method of key scanning, the different rows (columns), respectively, the input of high and low levels, and then read different columns (rows) on the level, so as to know that a certain key on the keyboard is pressed.
For example, when row 1 outputs a low level, and the other rows output a high level, read the status on the sequential columns respectively, if column 1 is low, the result will be (1,1), and the key will be 1, if column 2 is low, the result will be (1,2) and the key will be 2
Installing a 4×4 Matrix Keyboard
The 4×4 Matrix Keyboard has an 8-hole row motherboard, which theoretically can be directly Inserted into the 0-7 feet, but 0,1 feet for serial communication, so you can only choose 2 to 13 feet, here the choice of 2-9 feet.
First of all, select a 16PIN double-row pin, insert the long side of the double-row pin into the motherboard
The other side is inserted into the 8P line, the other end of the 8P line according to the order of the front of the keyboard from left to right, the line connects to the 2PIN pin, and then connects to the 5PIN pin,
the 2PIN pin is inserted into the Arino’s 8,9 feet, and the 5PIN pin is inserted into the Arino’s 8,9 feet, and the 5PIN pin is inserted into the Arino’s 8,9 feet.
Define the Arino IO ports
byterowPins[ROWS]={9,8,7,6};//connect to the pins of the row keypad
bytecolPins[COLS]={5,4,3,2};//connect to the pins of the column keypad
bytecolPins[COLS]={5,4,3,2};//connect to the pins of the column keypad
This is the example of how to connect the 2PIN pins to the Arino’s 8,9 pins, then connect to 5PIN pins
This is a good example of how to connect the 2PIN pins to the Arino’s 8,9 pins, then connect to 5PIN pins. /p>
Example Program
[C++]
#include<Keypad.h>
constbyteROWS=4;//fourrows
constbyteCOLS=4;//fourcolumns<
//definethecymbolsonthebuttonsofthekeypads
charhexaKeys[ROWS][COLS]={
{‘1′,’2′,’3′,’A’},
{‘4′,’5′,’6′,’B’} ,
{‘7′,’8′,’9′,’C’},
{‘*’,’0′,’#’,’D’}
};
byterowPins[ROWS]={9,8,7,6};//connecttotherowpinoutsofthekeypad< /p>
bytecolPins[COLS]={5,4,3,2};//connecttothecolumnpinoutsofthekeypad
//initializeaninstanceofclassNewKeypad
KeypadcustomKeypad=Keypad(makeKeymap(hexaKeys),rowPins,colPins,ROWS,COLS);
voidsetup(){
Serial.begin(9600);
}
voidloop(){
charcustomKey=customKeypad.getKey();
if(customKey){
Serial.println( customKey);
}
}