📄 keyboard.c
字号:
//Keyboard Routine. This sends the ascii codce for the pressed key for a 4x4 matrix.
//Columns are connected to P1 whereas rows are connected to P0
unsigned char Keys[][] = {
{'0', '1', '2', '3'},
{'4', '5', '6', '7'},
{'8', '9', 'A', 'B'},
{'C', 'D', 'E', 'F'},
};
P0 = 0x00; //Port0 to made as input port
P1 = 0xFF; //Port1 to output port.
sfr Row = P0; //Rows are connected to P0.
sfr Col = P1; //P1 is an input port and Cols are connected to this port.
unsigned char ColLoc;
unsigned char RowLoc;
/*
Step: 1 Checking whether all keys are released
1. Ground all Rows at once.
2. Read the columns
3. Mask the used bits
*/
while (1) {
do{
Row = 0x00; //Grounding all the Rows
ColLoc = Col; //Reading the Columns
ColLoc &= 0x0F; //Mask the unused bits
} while (ColLoc == 0x0F); //Wait until all the keys are released.
/*
Step: 2 Checking the key press and debouncing
1. Check whether a key is pressed.
2. After that, check for the key debouncing. This is done by calling
certain delay about 20ms and then checking for the key press again.
*/
do {
do {
MSDelay (20); //Call delay, Just initially.
ColLoc = Col; //Read the Columns
ColLoc &= 0x0F; //Mask the unused bits
}while (ColLoc == 0x0F); //Wait until the key press
MSDelay (20); //Call delay for debounce
ColLoc = Col; //Reading the Columns
ColLoc &= 0x0F; //Mask the unused bits
}while (ColLoc == 0x0F); //Wait until the key press.
/*
Whether the key is pressed, has been monitored, Now the
next thing is to check, which key has been pressed.
For that, ground the first row, read the columns. If the key press is detected,
then break out of the loop to start converting to an equivalent ascii code
*/
//Here we are just reading the Columns connected latches many times.
while (1) {
Row = 0xFE; //Ground the first row
ColLoc = Col; //Read the Columns
ColLoc &= 0x0F; //Mask the unused bits
if (ColLoc != 0x0F) {
RowLoc = 0; //Ground Row 0
break;
}
Row = 0xFD; //Ground the second Row
ColLoc = Col; //Read the Columns
ColLoc &= 0x0F; //Mask the unused bits
if (ColLoc != 0x0F) {
RowLoc = 1; //Ground Row 1
break;
}
Row = 0xFB; //Ground the third row
ColLoc = Col; //Read the Columns
ColLoc &= 0x0F; //Mask the unused bits
if (ColLoc != 0x0F) {
RowLoc = 2; //Ground Row 2
break;
}
Row = 0xF7; //Gorunding the Fourth row
ColLoc = Col; //Read the Columns
ColLoc &= 0x0F; //Mask the unused bits
if (ColLoc != 0x0F) {
RowLoc = 3; //Ground Row 3
break;
}
//Checking the column number and sending the result to some display
if (ColLoc == 0xFE) Send(Keys[RowLoc][0]);
else if (ColLoc == 0xFD) Send(Keys[RowLoc][1]);
else if (ColLoc == 0xFB) Send(Keys[RowLoc][2]);
else (ColLoc == 0xF7) Send(Keys[RowLoc][3]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -