8051rtx programming example

Source: Internet
Author: User

Software Design of Single-chip Microcomputer Based on rtx51

With the increasingly wide application of single-chip microcomputer, the demand for software development efficiency is getting higher and higher, from assembly to C language, and then to the operating system. MCS51, as the main force of the ever-increasing single-chip microcomputer world, has been greatly developed for its rtx51, and has low hardware requirements, it is easy to use and flexible, so it is more and more widely used in the software development of single chip microcomputer.
Keywords: Single Chip Microcomputer C51 rtx51
1 Overview
Many single-chip microcomputer applications need to execute many tasks at the same time. For such applications, we can use real-time operating systems to flexibly arrange system resources. Rtx51 is a small real-time multi-task operating system developed by Keil in the United States for MCS51 series single-chip microcomputer. It can work in all 8051 Single-Chip Microcomputer and derivative families, simplifying the complex software design, this shortens the project cycle. We use rtx51 in practice to develop single-chip microcomputer software. The GPS receiving board software controlled by single chip microcomputer has been designed to achieve good results.
2 rtx51 Introduction
Rtx51 has two modes: rtx51 full mode and minimum mode. The rtx51 minimal mode version is a subset of the rtx51 full version and can easily run on the 8051 system without external RAM (dxata ). The rtx51 full mode has four task priorities, which can be processed in parallel with the interrupt function. Each task uses the "Mailbox" system to transmit signals and messages, you can apply for and release the memory from the memory pool. At the same time, you can force a task to stop running, waiting for an interruption, or other semaphores or messages from the interruption. The requirements of rtx51 for system hardware are listed in Table 1.
2.1 rtx51 task
Rtx51 is divided into two types of tasks: Quick task and standard task. Fast tasks have a fast response speed. Each fast task uses 8051 independent register groups and has its own stack area. Rtx51 supports up to three quick tasks at the same time. Standard tasks require a little more time for Task Switching. Therefore, less internal RAM is used than quick tasks, and all standard tasks share one register group and stack. When the task is switched, the register status and stack content of the current task are transferred to the external memory. Rtx51 supports up to 16 standard tasks.
Rtx51 task status:
① Run (Runnign) -- the currently running task is in the running state, and only one task can be run at a time.
② Ready: The waiting task is in ready state. After the currently running task exits, the task with the highest priority in the ready queue enters the running state.
③ Blocked: The task waiting for an event is in the blocked State. If the event takes place with a higher priority than the task in progress, the task enters the running state; if the priority is lower than that of a running task, the task enters the ready state.
④ Deleted -- a task not started is deleted.
⑤ Task Switching-rtx51 includes an event-driven task switching mechanism, which can be switched according to the task priority, that is, a preemptible multitasking system; there is also an optional time slice rotation switching Task Mode. In the time slice rotation mode, tasks of the same level occupy the CPU by time slice. Rtx51 tasks have four priorities: 0, 1, and 2, which can be assigned to standard tasks. Priority 3 is reserved for quick tasks. Each task can wait for an event, without increasing the burden on the system. A task can wait for a message, signal, interruption, timeout event, or a combination of these events. Task switching is performed according to certain rules, including: the tasks with the highest priority in the "ready" status are executed first; if the tasks in the "ready" status are of the same priority, then, execute the command before entering the "ready" state.
Figure 1 of rtx51 task switching.

2.2 rtx51 event
◇ Timeout (timeout): the specified number of time cycles of pending tasks.
◇ Interval: similar to timeout, but the software timer does not reset, a typical application is to generate a clock.
◇ Signal: used for internal synchronization and coordination of tasks.
◇ Message: Applicable to rtx51 full, used for information exchange. We can send a message to a specific mailbox. A Message consists of 2 bytes. It can be the data that the user determines based on their own needs, or it can be a pointer to the data. If the message list in the mailbox is full and the message is interrupted, the message will be lost. if the message is sent by the task, the task enters the waiting state, until the mailbox has a new location to receive this message. The mailbox manages messages according to the FIFO principle. If several tasks are waiting to receive messages, the first to enter the waiting queue will receive messages. A single mailbox can store up to 8 messages. When the mailbox is full, there can be up to 16 waiting tasks.
◇ Interrup: Applicable to rtx51 full. semaphores are used to manage shared system resources. By using a "token", only one task can use certain resources at a time. If you apply for access to the same resource for several tasks, the first request will allow access, and other tasks will enter the waiting queue until 1st tasks are completed and the next task can continue.
The OS _wait () function suspends a task to wait for an event to occur. In this way, two or more tasks can be synchronized. The process is as follows: when the task wait event does not occur, the system suspends the task. when the event occurs, the system switches the task according to the task switching rule.
2.3 rtx51 interrupt handling
The rtx51 full mode provides two methods to deal with interruptions: one is the interrupt function of C51, and the other is the disconnection of rtx51. It can also be divided into fast task interruption and standard task interruption. For an interrupt function, it can also be used without rtx51. When an interrupt occurs,Program It jumps to the corresponding interrupt function, which is independent from the running task. The interrupt is handled outside the rtx51 system and is not associated with the task switching rule. For the method of task interruption, no matter whether it is a standard task to process the interruption, if the interruption occurs, the task waiting for the interruption enters the ready state from the "Waiting" status, switch according to the task switching rules. This type of Interrupt Processing is fully integrated into the rtx51, and the processing of hardware interrupt events is exactly the same as that of signal and information processing. In this way, the system responds to the interrupt enable register to comply with the task switching rules and ensure that the interrupt program is correct. Note that the interrupt enable register is fully controlled by rtx51 and manual modification is prohibited.

3. Application Instances
The following describes the application of rtx51 on the GPS receiving board controlled by a single-chip microcomputer.
(1) system hardware composition
MCU W77E58, fast 8051 kernel, 32 KB Rom, 1 kb xdata Ram, meets the hardware requirements of rtx51; keyboard, GPS positioning module, LCD module.
(2) system software composition
Software running environment Keil uvision2 6.20 integrated development environment with rtx51 complete version. The task key-board is used to monitor the keyboard. If a key is pressed, update the key code to mailbox 1. If an external interrupt occurs, wait for the GPS data to be received, and store the data, sends a signal to the display task. Task display processes different signals and messages received. Send-out: process the received data and send it out. Task voice for voice output.
System hardware and software structure 2 are shown.
The abbreviated source code is as follows:
# Include <rtx51.h> // contains the rtx51 header file

# Define display 0
# Define send_out1
# Define key_board2
# Define voice3
Void main (void)
{
Init system (); // system initialization
OS _start_system (Display); // start rtx51
}
Void task0 (void) _ task_display
{
OS _set_slice (1000); // you can specify the time slice size.
OS _enable_isr (0); // allows external interruption 0
OS _creat_task (send_out); // start the send_out task
OS _creat_task (VOICE); // start the voice task
For (;;){
Switch (OS _wait (k_sig + k_mbx + 1,255, & keyboard) // wait for receiving signals and keyboard messages for classification
{
Display1 ();
Break;
}
Case event_mbox; // switch (keyboard) when receiving data from the mailbox)
{
Case '1 ';
...
OS _send_signal (send_out); // sends a signal to the task send_out
...
OS _send_signal (VOICE);} // sends a signal to the task voice
...;
}
...;}
}
Void task1 (void) _ task_send_out // process the data sending task
{
While (1)
{
OS _wait (k_sig, 255, 0) // wait for the signal
Operation_send ();
}
}
Void task3 (void) _ task_voice
{
While (1 ){
OS _wait_signal (k_sig, 255, 0); // wait for the voice processing signal
Voice ();
}
}
Void interrupt (void) interrupt 2 using 1
{
Read_gps_data (p_gps_data); // receives data
Isr_send_signal (Display); // sends a signal to the display task
}
# Pragma registerbank (2) // use register Group 2

void task2 (void) _ task_keyboarsd_priority_3 // set it to a quick task
{< br> OS _attach_interrupt (0 ); // bind the task and External Interrupt 0
while (1) {
OS _wait (k_int, 255, 0 ); // waiting for interruption
key = iic_read_keyboard ();
OS _send_message (1, key, 0 ); // send the keyboard code to the mailbox 1
}< BR >}< br> 4 Conclusion
through practice, we can find that it is more convenient to use rtx51 to develop a single-chip program. Especially for large programs, this avoids tedious work such as writing a message loop by yourself, and significantly increases the efficiency. When the hardware resources are sufficient, the effect is more obvious.
(integrated e-forum)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.