It means providing some functions for tasks to call, get key information, or read key values.
Define a header file
Code
1. #ifndef _KEY_H_ //Prevent repeated references to the document. If the symbol _KEY_H_ has not been defined, compile the following statement
2. #define _KEY_H_ //As long as it is referenced once, that is, #include
3. unsigned char keyHit( void ); //If the key is pressed, it returns non-zero, otherwise it returns 0
4. unsigned char keyGet( void ); //Read the key value. If no key is pressed, wait until the key is pressed.
5. void keyPut( unsigned char ucKeyVal ); //Save the key value ucKeyVal to the end of the key buffer queue
6. void keyBack( unsigned char ucKeyVal ); //Return the key value ucKeyVal to the first key buffer queue
7. #endif
Define the function body document KEY.C as follows:
Code
1. #include "key.h"
2. #define KeyBufSize 16 //Define the number of bytes in the key buffer queue
3. unsigned char KeyBuf[ KeyBufSize ]; //Define an unsigned character array as the key buffer queue. This queue is advanced
4. //First-out, circular access, index from 0 to KeyBufSize-1
5. unsigned char KeyBufWp=0; //As array subscript variable, record the storage location
6. unsigned char KeyBufRp=0; //As array subscript variable, record the read position
7. //If the storage position is the same as the read position, it means there is no key data in the queue
8. unsigned char keyHit(void)
9. { if( KeyBufWp == KeyBufRp ) return( 0 ); else return( 1 ); }
10.
11. unsigned char keyGet(void)
12. { unsigned char retVal; //temporarily store the read key value
13. while( keyHit()==0 ); //Wait for a key to be pressed, because the return value of the keyHit() function is 0, indicating no key has been pressed
14. retVal = KeyBuf[ KeyBufRp ]; //Read the key value from the array
15. if( ++KeyBufRp >= KeyBufSize ) KeyBufRp=0; // read position plus 1, loop back to the initial position if out of queue
16. return( retVal );
17. }
18.
19. void keyPut( unsigned char ucKeyVal )
20. { KeyBuf[ KeyBufWp ] = ucKeyVal; //Store the key value in the array
21. if( ++KeyBufWp >= KeyBufSize ) KeyBufWp=0; //Add 1 to the storage position, and loop back to the initial position if it exceeds the queue
twenty two. }
twenty three. /*********************************************** ******************************************
24. For some reason, the read key is useless, but other tasks need to use the key, but it is inconvenient to transmit. In this case, you can return the key queue. Just like picking up the wrong letter, it is necessary to return it.
25. ************************************************ ******************************************/
26. void keyBack( unsigned char ucKeyVal )
27.
28. /*
29. If KeyBufRp=0; minus 1, it is FFH, which is greater than KeyBufSize, that is, it returns from the head of the array to the tail of the array. Or if KeyBufRp exceeds the queue position due to interference, it must also be adjusted back to the normal position.
30. */
31. if( --KeyBufRp >= KeyBufSize ) KeyBufRp=KeyBufSize-1;
32. KeyBuf[ KeyBufRp ] = ucKeyVal; //Restore key value
33. }
The following is a step-by-step explanation of the keyboard physical layer driver.
Circuit commonality: P2 port is connected to a common cathode digital tube, common cathode is connected to GND, P2.0 is connected to segment a, P2.1 is connected to segment b, ..., P2.7 is connected to segment h.
Software commonality: code unsigned char Seg7Code[10] is the seven-segment digital tube common cathode coding table.
Code unsigned char Seg7Code[16]=
// 0 1 2 3 4 5 6 7 8 9 A b C d E F
{0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
Previous article:51 MCU Programming Section 5: Keyboard Driver Example
Next article:51 MCU Programming Section 4: Digital Tube Driver
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
- (C-Wireless Charging Electric Car) Liaoning Province First Prize_C
- Benefits of Spread Spectrum
- Don’t sell your old phone to buy a new one, learn how to make your own server!
- Can anyone recommend an inexpensive isolation amplifier product, either a chip or a module, with an accuracy of 1% and DC signal sampling?
- Problems with STM32F767 driving Bluetooth module
- HFSS Wave Ports and Lumped Ports
- Using the MS-formula library of st 3.1, the scan results do not contain the device name
- Simplifying Equalization Design for USB 3.0 Systems
- Understand these! The subtle differences between RS-232, RS-422, and RS-485
- Teach you step by step to achieve the spotlight effect