MCU key scanning program state machine method

Publisher:EtherealGazeLatest update time:2015-02-10 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#define key_input PIND.7 // Key input port
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2

char read_key(void)
{
                static char key_state = 0;
                char key_press, key_return = 0;

                key_press = key_input; // Read key I/O level
                switch (key_state)
                {
                        case key_state_0: // Key initial state
                                if (!key_press) key_state = key_state_1; // Key is pressed, state changes to key confirmation state
                                break;
                        case key_state_1: // Key confirmation state
                                if (!key_press)
                                {
                                        key_return = 1; // Key is still pressed, key confirmation output is "1"
                                        key_state = key_state_2; // State changes to key release state
                                }
                                else
                                        key_state = key_state_0; // Key is released, change to key initial state
                                break;
                        case key_state_2:
                                if (key_press) key_state = key_state_0; // Key is released, change to key initial state
                                break;
                }       
            return key_return;
}

 

 

Another principle is that the execution time of the designed interrupt service function should be as short as possible. In this way, if multiple interrupts are used in the system, even if the interrupt nesting method is not adopted, it can ensure that the system can respond to the requests of each interrupt in time.

Therefore, I like to use only one flag variable in the timer interrupt, so that the interrupt service function has the fastest execution time and will not block the timely response of other interrupts. In the main program, the flag variable is judged and then the key is scanned. Although the scanning interval is not strictly 10ms at this time, there are more or less delays, and the delay is uncertain, but it will not affect the correctness of the key scanning (the process of one key is at least 300ms).

Set a timer interrupt to scan the key every 10MS. , read the value. . If there are multiple keys and extra timers, you can try this method, which is efficient. It is just right for STM32. cool.

Keywords:MCU Reference address:MCU key scanning program state machine method

Previous article:MCU receiving PT2272 wireless receiving module problem
Next article:MSP430G2553 control program and error handling with interrupt

Recommended ReadingLatest update time:2024-11-16 13:45

Hetai MCU timer digital tube display
;Content: 4-digit common cathode digital tube display 0000-9999 display ;Digital tube segment code connection method A-PD0, B-PD1....G-PD6, H-PD7 ;Digital tube bit code connection method OM0-PC0...COM4-PC3 ;Purpose: ;1. Look up the table ; 2. Indirect addressing ; 3. Use of timer ;Source program download address
[Microcontroller]
Techniques of driving current between microcontroller and LCD interface
  The bias voltage is generated by using an external resistor ladder network (see the circuit diagram below). Because the resistor ladder network is connected between VDD and Vss, there will be a current flowing through the resistor ladder network, and the current is inversely proportional to the resistance. In other
[Microcontroller]
Techniques of driving current between microcontroller and LCD interface
Nuvoton N76E003+GPRS MCU reset troubleshooting notes
Application Scenario The GPRS module uses AIR202/AIR208, and the power supply solution is JW5033; The single chip microcomputer uses Nuvoton N76E003, dual serial ports, 18K flash, and the power supply shares a 5V input power supply with the GPRS DCDC, namely JW5033. Problem Description When GPRS is connected to th
[Microcontroller]
Nuvoton N76E003+GPRS MCU reset troubleshooting notes
Design of MP3 player based on C51 single chip microcomputer
0 Introduction Portable MP3 players are a digital product that integrates audio playback and data storage. Their functional structure is highly discussed by electronic designers. MP3 is the abbreviation of MPEG-1 Audio III (1ayerIII). MPEG-1 Audio (ISO/IECll 172-3) is the most commonly used audio compression sta
[Microcontroller]
Design of MP3 player based on C51 single chip microcomputer
Doorbell program + circuit made by AVR microcontroller timer
  #define F_CPU  1000000UL  #include avr/io.h #include avr/interrupt.h #include util/delay.h #define INT8U   unsigned char #define INT16U  unsigned int #define DoorBell() (PORTD ^= 0x01)          / #define Key_DOWN() ((PINB & 0x80) == 0x00)  volatile INT16U soundDelay; //Two different values ​​correspond to
[Microcontroller]
Doorbell program + circuit made by AVR microcontroller timer
The single chip microcomputer controls the digital tube to move left
#include reg52.h sbit LATCH1=P2^2; //segment latch sbit LATCH2=P2^3; //bit latch unsigned char code DuanMa ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // Display segment code value 0~9 unsigned char code WeiMa ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; // respectively correspond to the corresponding dig
[Microcontroller]
Digital Control Constant Current Source Based on 51 Single Chip Microcomputer
The circuit diagram is as follows: The main purpose of this design is to design a digital control voltage-stabilized power supply. It uses the single-chip microcomputer STC89C51 as the main control chip to control the output voltage of the digital/analog converter (TLC5615), and forms a negative feedback system thro
[Microcontroller]
Digital Control Constant Current Source Based on 51 Single Chip Microcomputer
EZ-USB MCU "high precision, high frequency, high stability" is guaranteed
1. Introduction In recent years, with the development of communication systems, people have higher and higher requirements for signal sources. Direct Digital Frequency Synthesis (DDS or DDFS) has been further developed in this context. It has the advantages of relative bandwidth, high frequency resolution, short freq
[Power Management]
EZ-USB MCU
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号