1. Insufficient traditional MCU program development
In traditional MCU programs, the structure is usually centered on "process" and "operation". The program is executed in sequence according to the specified process. The connection with peripherals is generally interrupted. All peripheral processing work is completed in the interrupt service program. The main program usually initializes the system and waits for the interrupt to occur. This structure is mature and easy to understand, but it has the following shortcomings:
(1) Due to the limitation of MCU performance, the system may be slow to respond to other interrupts, especially for systems with many interrupt sources and long interrupt processing time (such as LED display, keyboard scanning, etc.);
(2) The interrupt service routine is too long, and the system cannot respond to interrupts of the same level during the interrupt service;
(3) It may cause code reentry, increase stack overhead, and cause unpredictable results;
(4) When debugging a program, the time and energy spent on coordinating the timing of each module increases significantly as the system becomes more complex.
If you introduce the event-driven mechanism in Windows programs when writing microcontroller programs, separate the interrupt response from the event handler, the task of the interrupt service program is only to generate a flag that an interrupt has occurred, while the event processing is completed by the handler, and the main program is responsible for judging the flag and scheduling the handler. In this way, the length of the interrupt service program can be greatly shortened, the time consumption of the interrupt service program can be reduced, and the system's response ability to multiple interrupts can be improved, thereby better solving the above contradictions.
2 Windows event-driven mechanism
In the Windosw system, the design of the program is based on event-driven. When an object has a related event (such as pressing a mouse button), the object generates a specific message to identify the event. The message is sent to the message queue, or sent directly to the processing object without entering the queue. The main program is responsible for organizing the message queue, sending the message to the corresponding processing program, so that the corresponding processing program performs the corresponding action, and returns the control to the main program after completing the corresponding processing.
In this mechanism, the object request is just to add the corresponding message to the queue, and the time-consuming processing is separated to the processing function. In this structured program, each functional module has clear boundaries, which is easy to expand and can make full use of the CPU's processing power, so that the system responds to the outside world accurately and promptly.
3 Event-driven MCU programming
Compared with Windows system, the resources of single-chip microcomputer are very limited. Therefore, the event-driven mechanism in single-chip microcomputer program can only adopt a simplified method. When an interrupt occurs, the interrupt service program sets the corresponding flag. Different flags represent different interrupt messages, and the main program constantly judges these flags to decide which processing function to start. After the corresponding processing function is started and handles the related tasks, it clears this flag and then returns the control to the main program. With this mechanism, limited resources can be reasonably utilized, which greatly reduces the workload of program debugging. For delay and timing processing (such as LED display, keyboard scanning, etc.), it is more convenient to use a timer to complete the delay and timing tasks, thereby freeing the CPU from such time-consuming tasks and ensuring that the system has sufficient response capabilities to multiple interrupts.
This article takes an IC card reader as an example to illustrate the specific application of event-driven mechanism in single-chip microcomputer programming.
3.1 Hardware Structure
This system is based on ATMEL's 89C51 (as shown in Figure 1). 89C51 is inexpensive, has good performance, and has a 4KB rewritable program memory on the chip, which can meet the requirements of this system. In order to simplify the hardware structure and system energy consumption, the keyboard uses a matrix keyboard scanned by software. The LED display uses segment dynamic scanning, and at any time, only one segment of the LED is lit. Specifically, when the bit selection signal is sent to the common pole of a certain LED, the segment code (including decimal point) of the LED is output in turn every other time slice. After one bit is output, the next bit is output one by one. From the first to the Nth LED, it is divided into 8×N time slices for cyclic scanning and display. The serial port UART is used as the channel for the system to communicate with the external data, and the reading and writing of the IC card is realized by the MCU simulating the I2C protocol.
3.2 MCU Programming Based on Event-Driven Mechanism
Interrupt request flag
A bit-addressable unit is defined in the system, which is named Message_Flag, to record and describe the interrupt event. The definition of each bit is as follows:
*A bit in Message_Flag is 1, which means a corresponding event is currently occurring; a bit in Message_Flag is 0, which means no corresponding event is occurring. [page]
Implementation of LED display
The structure of the display module is shown in Figure 2. Timer T0 is used as the timing reference for the dynamic scanning of the LED. The maximum timing value of T0 is Tseg=20ms/(8×N) (where N is the number of LED bits). Changing the value of Tseg can change the brightness of the display. T0 requests an interrupt from the MCU every Tseg time, and sets the corresponding flag bit (D0 bit in Message_Flag) in the interrupt service program of T0. After the main program detects that this flag bit is set, it starts the display module to realize the display output of the bit segment.
Keyboard input implementation
The structure of the keyboard module is shown in Figure 3. During the LED dynamic scanning period, only the bit selection line corresponding to the lit LED maintains a low level for about 3ms, while the bit selection line of the LED (i.e. the column line of the keyboard) maintains a high level during most of the system operation. When a key is pressed, one of the row lines of the keyboard will be pulled to a high level, and after passing through the NOR gate, the INT1 interrupt will be applied to the MCU, and the timer T1 with a timing time of 20ms will be started in the INT1 interrupt service program. After the T1 timing time is up, the T1 interrupt will be applied to the MCU, and the corresponding interrupt application flag (D1 bit in Message_Flag) will be set in the T1 interrupt server program. After the main program detects that this flag is set, it starts the keyboard scanning module to realize keyboard input. When the keyboard input is completed (the user presses the "Confirm" key), the keyboard input confirmation flag (D7 bit in Message_Flag) is set.
IC card reading and writing
The SDA and SCL of the IC card are connected to the MCU through the card holder through P1.0 and P1.1 respectively. When the IC card is inserted into the card holder, the micro switch on the holder changes INT0 to a low level and requests the INT0 interrupt to the MCU. In the INT0 interrupt service program, the corresponding interrupt request flag (D2 bit in Message_Flag) is set. After the main program detects that this flag is set, it starts the IC card reading module and implements the card reading operation with the software module I2C protocol. After the data processing is completed, the card writing operation is also completed through the software module I2C protocol.
Serial communication
In practical applications, UART can be converted into RS232C to connect to a PC or converted into other protocols such as RS485 to form a single-chip network. The MCU communicates with the outside in an interrupt mode, and the corresponding interrupt request flag (D4 in Message_Flag) is set in the interrupt service program of the serial port. After the main program detects that this flag is set, it starts the serial communication module to realize data communication with the outside.
Main program design
In summary, the main program first completes the system initialization, and then cyclically detects the interrupt request flags of each interrupt. If a flag is set, the corresponding processing module is started to complete the corresponding task. The program structure is as follows (written in C51):
vnsigned bdata message_flag;
sbit t0_int=message_flag^0;
sbit t1_int=message_flag^1;
sbit int0_int=message_flag^2;
sbit uart_int = message_flag^4;
sbit kb_enter=message_flag^7;
unsigned char kb_buf[8];
unsigned char led_buf[8];
unsigned char ic_buf[8];[page]
unsigned char num_buf[8];
void uum_proc(void); /*Data processing module*/
void ledbuf_write(unsigned,unsigned int); /*data processing*/
void system_init(void); /*System initialization*/
void uart_commune(void); /*Serial communication module*/
void led_display(void); /*LED display*/
void kb_scan(void); /*keyboard scan*/
void ic_reader(void); /*Read IC card*/
void ic_writer(void); /*Write IC card*/
void set_timer(unsigned int time_len,unsigned char type,unsigned char id); /*Set the timer*/
void t0_int_sever(void); /*Timer T0 interrupt service*/
void t1_int_sever(void); /*Timer T1 interrupt service*/
void int0_int_sever(void); /*INT0 interrupt service*/
void int1_int_sever(void); /*INT1 interrupt service*/
void uart_int_sever(void); /*Serial port interrupt service*/
void main(void)
{
system_init();
while(1) {
if (t0_int) led_display();
if (int0_int) ic_reader();
if (t1_int) kb_scan();
if (uart_int) uart_commune();
if (kb_enter){
num_proc();
ic_writer();
ledbuf_write(num_buf,8);
}
}
}
The event-driven MCU programming is to separate the time-consuming processing part of the interrupt service by setting the phase flag in the interrupt service program. After the interrupt returns, the main program starts the corresponding processing module according to the flag. After the task processing is completed, the corresponding flag is cleared. Since the interrupt service program is short, it can generally respond to various interrupts in real time; and the processing programs will not be called by each other, so there will be no code reentry; the boundaries of each module are clear, which greatly facilitates the coordination of each module in the program.
Practice has proved that the use of event-driven mechanism to weave single-chip microcomputer programs can be easily completed even for multi-interrupt and multi-module systems that require precise timing and consume a lot of time.
Previous article:The Making and Using of Keil Monitor-51 Simulation Board
Next article:Design of Serial Communication between Linux PC and 51 Series 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
- How about this B-G474E-DPOW1 digital power development board?
- Company team building
- PCB modular layout - capacitor design
- Using zstack to start a timer with a timer of 1 millisecond cannot be achieved
- Fujitsu White Paper Award Download | FRAM High-Performance Memory Optimizes In-Vehicle Electronic Systems
- Review summary: Erha image recognition artificial intelligence vision sensor
- MCU interrupt
- UTC time zone conversion of single chip microcomputer
- Find books for systematic study
- LLAKG: Arduino automatic watering system (Episode 2: C language program and function implementation)