Recently I have been developing Lingyang's GPL32001 microcontroller, and the main product is a piano.
On this piano, we can see many keys, including piano keys and function selection buttons. Facing so many keys, it must be stressful for a young person who has just started working. The characteristics of piano keys are different from ordinary keys. One piano key is composed of two keys, and one key stores two kinds of information, strength and key value.
The project requirements of the program I wrote are as follows: each key can only be triggered once, and when triggered, a different key code must be issued, and the key code value is read out through the audio decoding box. For example, the first white key is key01---> the corresponding key value is 0000 0001, which is 0x01. The arrangement of the function keys is different from the keys. The arrangement of the function keys starts from the serial number key55, and the key value is also different from the keys. In view of this feature, it can be identified whether the machine has a short circuit, a circuit breaker, or other hardware damage.
Well, the question I raised today is also common in single-chip microcomputer development, that is, buttons. Students who have learned single-chip microcomputers have played with buttons, and the code at the beginning is like this:
if(key == 0)
bell = 0 ;
else
bell = 1 ;
But if this is the case, assuming it is in an infinite loop, if the button detects a low level as being pressed, the button will continue to be triggered, and the branch with bell=0 will be continuously executed.
So I thought of a good way, and I wrote it like this in my project.
Define a static int lock; then do the following operations, of course this operation is performed in an infinite loop:
//Get button status data = *P_IOE_Data; if((data&0x0080)) { IOE_lock = 0 ; } if((data&0x0080) == 0) { if(IOE_lock == 0) { play_sound_hightolow(0x33,Vol_value); } IOE_lock = 1 ; }
if((data & 0x0080)) means the key is not pressed, the key lock flag is 0, the staic type will record the value of this flag variable, when if((data & 0x0080) == 0), the key is pressed, I need to determine whether the key lock flag is 0, if it is 1, then the program will definitely not run the play_sound_hightolow(); function, so when the key is pressed, the lock initialization value is 0, the speaker emits a sound code, and the audio decoder reads the corresponding key value as 0x33. After reading, the lock flag is immediately set to 1. If you keep pressing the key at this time, because the lock flag is equal to 1, it is invalid, and the program does not enter the code state. When released, the state of the key changes from 1 to 0. At this time, if you press the key again, it is valid again, and then locked.
The advantage of this is that when the key is pressed, the code state is triggered only once, and the sound code 0x33 will not be continuously sent. It is only sent once. In the appropriate development and utilization of the logo lock, many problems can be solved conveniently and efficiently.
Previous article:Application of NOR_FLASH in MCU Development
Next article:Talk about the program framework of single-chip microcomputer
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
- Hardware Circuit Design of PCB Tester Based on FPGA
- HT16K33 Driver
- Director Tony Testa teaches you how to design products for Wi-Fi 6
- Work submission: Fan modification based on RSL10
- What basics are needed to learn programming?
- Notes on the initialization process of Qinheng CH395 program
- [Repost] 5 ways to reduce the total failure rate of UPS power supply
- Award-winning live broadcast: TI DLP products empower industrial light control and innovation registration is open~
- TI C6678 Multi-core DSP Architecture
- EEWORLD University Hall----Live Replay: CAN SIC signal improvement function