How to add a lock to the microcontroller's buttons to prevent multiple triggers

Publisher:huanxinLatest update time:2018-11-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 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.


Reference address:How to add a lock to the microcontroller's buttons to prevent multiple triggers

Previous article:Application of NOR_FLASH in MCU Development
Next article:Talk about the program framework of single-chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号