The principle of keyboard
Keyboards are divided into coded keyboards (such as computer keyboards) and non-coded keyboards (identified by programs themselves).
Non-encoded keyboards are divided into: independent non-encoded keyboards (independent keys), row-column non-encoded keyboards (4*4 array keyboards)
Circuit diagram of a standalone keyboard.
Because the IO port of the 51 single-chip microcomputer is not a bidirectional port but a quasi-bidirectional port, in order to make the IO port have input function, the IO port must be set to 1. After setting it to 1, when the button is pressed, the level of the IO port will be pulled down, that is, it will be set to 0. When the IO port is detected to be 0, it can be determined that the button has been pressed. There will be a jitter process when the button is pressed (the spring will jitter). Since the speed of the single-chip microcomputer detecting the IO port is very fast, which exceeds the frequency of the spring jitter, when the single-chip microcomputer detects that the IO port is 0, it needs to delay for a short time and then detect whether the IO is 0. If it is still 0, it is confirmed that the button is pressed. Because there is a pull-up resistor in the IO port, when the button is released, the IO port is pulled high again.
Routine:
#include
#define uint unsigned int
#define uchar unsigned char
sbit Key = P3^4; // key
sbit Led = P1^0; // Ice 灯
void delay(uint z);
/******** Main function ********/
void main()
{
while(1)
{
if(!Key)
{
delay(10); // debounce operation
if(!Key)
Led = 0; // Led lights up when pressed
else
Led = 1;
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
Episode 15
4*4 matrix keyboard
In the above figure, one button occupies one IO port. If there are 16 buttons, 16 IO ports will be occupied. In order to reduce the use of IO ports, it is necessary to connect them in a matrix. As shown in the following figure
Matrix scanning principle
From the figure we can see that P30, P31, P32, P33 are rows (lower four bits), and P34, P35, P36, P37 are columns (upper four bits).
Suppose we press the S6 button. [page]
The first step is to determine the column and assign 0xF0 = 1111 0000 to P3 port, then P37, P36, P35, P34 are all set to 1, P33, P32, P31, P30 are all set to 0. When S6 is pressed, since P31 on one side of the S6 button is 0, P35 connected to the other side of S6 is pulled low, that is, equal to 0. As shown in the figure below
At this time, the column value P3 = 1101 0000 = 0xD0. In the program, we only need to determine whether P3 is equal to 0xF0. If it is not equal, it means that a key is pressed.
The second step is to not change the state of the upper four bits: 1101 Set all the lower four bits to 1 (P3 = P3 | 0x0F). At this time, it becomes 1101 1111. Since the microcontroller scans the columns very quickly, the key is still in the pressed state when scanning the rows (human reaction is not as fast as the microcontroller). S6 is pressed, and since P35 connected to S6 is at a low level (i.e. 0), P31 changes from a high level (i.e. 1) to a low level (i.e. 0) as shown in the figure below.
The value obtained at this time is P3 = 1101 1101 = S6 is pressed. This is the detection principle.
Full Program:
#include
#define uint unsigned int
#define uchar unsigned char
sbit Led = P1^0;
sbit Led1 = P1^1;
void delay(uint z);
/******** Main function ********/
void main()
{
uchar Key_Temp;
uchar Key; // Key value
while(1)
{
Key = 0; // clear to 0
P3 = 0xF0;
Key_Temp = P3;
Key_Temp &= 0xF0; // Only take the upper four bits This sentence is necessary because 51IO is only quasi-bidirectional. To make it have input function, it needs to be set to 1
if(0xF0!=Key_Temp) // Check if a key is pressed
{
delay(10); // Delay for a while to skip the jitter time
Key_Temp = P3 & 0xF0 ; // First take P3 and then AND it with 0xF0 to get the upper four bits
if(0xF0 != Key_Temp) // Check again whether the key is pressed
{
P3 = Key_Temp | 0x0F; // Keep the upper four bits Set all the lower four bits to 1 and output
Key = P3; // read in again
}
}
switch(Key)
{
case 0xEE: Led = 0; break; // S1
case 0xDE: Led1 = 0; break;
case 0xBE: break;
case 0x7E: break;
case 0xED: break;
case 0xDD: break;
case 0xBD: break;
case 0x7D: break;
case 0xEB: break;
case 0xDB: break;
case 0xBB: break;
case 0x7B: break;
case 0xE7: break;
case 0xD7: break;
case 0xB7: break;
case 0x77: break; // S16
default:
Led = Led1 = 1;
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
Previous article:4*4 keyboard scan code
Next article:51 MCU Learning 3- Stepper Motor
- Popular Resources
- Popular amplifiers
- Enter the Matrix MS-DOS screensaver.Sample of using bios functions (by int 10h) for text data out
- Multi-channel pressure measuring instrument based on C8051F020 single chip microcomputer
- Design of automatic player for work and rest signal based on 51 single chip microcomputer
- AVR243 Matrix Keyboard Decoder
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Questions about generating light painting with allegro16.6
- NUCLEO-G431RB Review -> OPAMP
- CCS8.0
- Advantages of Integrated Current Sensing
- High-speed acquisition module
- What does USB_OTG_FS and USB_OTG_HS mean?
- I2C Master Mode for TI BLE CC2541
- Where can I find the PHI EVM Controller information for the TI ADC acquisition board?
- [NXP Rapid IoT Review] W2-Sensor Data Collection Test
- Today at 10:00 AM, live broadcast with prizes: [Introduction to the Industrial Open Source Platform based on TI Sitara AM5708]