Introduction
In the design of single-chip microcomputers, it is often necessary to display a multi-level operation menu on the display screen. Each menu level has some menu items and corresponding key operations, as well as subsequent processing programs executed in response to various operations. The reference uses function pointers as structural elements to stretch the entire menu structure into a structure array, which contains all menu items. This method does not separate the menu item data from the function function, but writes the menu item data in the function function. When the menu item data needs to be changed, the function function must be modified, which greatly limits the versatility of the program and is not conducive to program maintenance. The reference slightly improves on the former, adding menu item display text and key reuse identifiers to the original structure, which makes the structure larger, and the text content of the menu item is very fixed and stored in the EEPROM, and the display content and position cannot be flexibly changed. The reference introduces the window and message mechanism under the Windows system to realize the multi-level menu design. This method requires the maintenance of stacks, message queues, window timers, etc., and the design is too complicated.
This paper aims to provide a lightweight single-chip multi-level menu implementation method to complete the menu design with less system resource consumption and a simple and convenient method. Considering that the menu program needs to have three basic elements: one is the content to be displayed in each menu window; the second is the key definition and response corresponding to each window; the third is the switching mechanism between menu items in the window and between windows. Therefore, the menu is divided into two parts: the menu window module and the keyboard processing module, which are designed independently.
1 Menu window module design
The main function of the menu window module is to complete the display control of the window in real time according to the menu window switching mechanism.
1.1 Menu window switching mechanism
The menu window switching mechanism includes two points: one is the realization of the switching effect between different windows; the other is the realization of the scrolling switching effect between the same-level menu items in the window. To achieve the above functions, two structures MenuState and MenuItems are defined. MenuState is defined as follows:
MenuState is a structure array related to window jump and window display, which is used to globally schedule the switching between windows. Among them, CurIndex is the index value of the window, which is used to identify the current window. UpIndex, DnIndex, and BackIndex are used to identify the window to which the program should turn when there is a "previous page", "next page", or "back" button operation. When its value is 255, it means there is no window to turn to, and when it is between 0 and 254, it indicates the window index to turn to. Therefore, this design can support 255 menu windows, which is enough to meet the needs of engineering applications. CurOperate is a function pointer used to point to the window display control program that should be executed when the current window is displayed to realize the display of the window.
MenuItems is a menu item structure that is used to save the display position and content of all menu items in the current window. The content of this structure is updated during the initialization process of the new window as the window is switched. This meets the needs of window switching and reverse scrolling between menu items. Since only menu items related to the current window are saved, the system resource consumption of this structure is very small.
Items contains all the menu items in the current window. Considering that some menu items do not have a redirect window, the value is set to 255 for such menu items. When a window is displayed, Items will be filled with the menu item information of the current window, and the filling is constructed in the order of the menu items. In this way, when there is a "move up" or "move down" key operation, the target line can be quickly found and the line can be reversed.
1.2 Menu window display control
The window display part encapsulates each menu window in the form of an independently named function. The function includes assigning values to Items, displaying the menu items of the current window at the specified position of the display screen, and completing drawing operations such as drawing points, drawing lines, and drawing buttons as needed. These functions are independent of each other and correspond to different menu windows. Only CurOperate is assigned when the window is switched, so it is very suitable for multi-person collaborative development.
It should be noted that the display control of the menu window is based on the display screen. TFT screens or dot matrix LCD screens are commonly used in engineering. When using it, it is necessary to implement the driver of the display screen and perform operations such as initialization, reading, and writing on the screen. Since it is not the focus of this article, this part of the content is omitted.
[page]
The menu operation of the microcontroller is mostly completed in the form of keys. Generally, there are multiple keys corresponding to different functions. In addition, the key reuse problem should also be considered. That is, the keys in the same position may have different function definitions in different windows. Therefore, the key values should be collected and analyzed in the program to respond to the operation correctly. The key processing process is shown in Figure 1.
For example, when the "Previous Page" button is pressed, the program will receive the button, parse the key definition according to the current window, and judge it as "Previous Page"; then obtain the index value of the window to be redirected in the window structure array Menu Index, obtain the pointer of the window display control program according to the index value, and release the menu item structure Items to execute the window display control program. The code is as follows:
[page]
3 Application Examples
In a certain equipment simulation project, the AT89C52 chip is used as the core device, the display part uses the graphic dot matrix LCD display module HG3202405V2-B-LWH-LV, and there are 6 external composite function keys to realize the menu design. Part of the code is as follows:
The main menu window is shown in Figure 2.
Conclusion
The multi-level menu design of single-chip microcomputer is a common problem in project development. This article provides a solution and has been applied in the project. Its characteristics are manifested in two aspects: first, the system overhead is small, and only two structures, MenuState and MenuItems, and several variables are needed to maintain each window and the same-level menu items in the window; second, the functional independence of each part of the module is strong, the display control programs of each menu window are independent of each other and do not affect each other, and the keyboard processing program is also self-contained. Therefore, the solution has a high degree of modularization, a simple and convenient development process, good scalability, strong portability, and is suitable for teamwork development and maintenance.
Previous article:Working principle of PIC microcontroller human-machine interface 4×4 row keyboard
Next article:Microchip's compact and flexible 8-bit Flash microcontroller solution
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
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- 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!
- Application of power amplifier in electrostatic suspension system
- Inexpensive fiber optic transceiver
- Design of wireless network sensor based on Bluetooth protocol
- Design and implementation of a fast and effective broadband power amplifier matching circuit
- Program Reading
- EMC Basics---Huawei
- SparkRoad-V thermal imaging data acquisition and display
- GD32VF103 modified firmware library for Gigabit RISC-V core
- [Raspberry Pi Pico Review] CoreMark program porting, test running points
- [TI mmWave Radar Evaluation]_4_AWR1843BOOST Corridor Ranging Test