Using event-driven mechanism in single-chip computer programming

Publisher:温馨阳光Latest update time:2012-03-08 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Using event-driven mechanism in single-chip computer programming

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

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号