Research and Development of RTOS51 Based on CygnalF040
| [Date:] |
Source: carefree electronic development network Author: Xin Liang, Wang Dongxing |
[Font:Large Medium Small] |
0. Introduction
At present, China's Monitoring and Management of most of the power supply and power equipment is basically still in the original management mode of manual inspection (such as the signal power supply of railway crossings), not only is the management efficiency low, in addition, the quality and level of power supply cannot be effectively guaranteed. Based on the real-time operating system small RTOS51, this paper mainly discusses the design process of a terminal unit for on-site monitoring of the power system, hoping to provide some ideas for improving the power distribution automation level, do your best.
1. Small rtos51Multi-taskReal-time Operating System
Small RTOS51 is a real-time multi-task operating system with open source code. It simplifies complex engineering software design that requires strict time. It has the following features: open source code, portable, solidified, preemptive, interrupt management, and low RAM requirements.
2. System Function Description
This fault diagnosis terminal is mainly used in the monitoring and management of railway channel traffic signal power supply. In view of the specific application scenarios of this fault diagnosis terminal, the following aspects are taken into account in the design: practicality, the fault diagnosis terminal is mainly used to monitor the signal power supply of railway crossings. Therefore, the following functions can be used to meet the requirements in the principle of Practicality: remote Detection, integration control, remote switch control, over-limits alarm, and fault recording; real-time performance, the fault diagnosis terminal must be within each power frequency cycle of the signal power supply (20 ms) uninterrupted and even collection of 20 discrete data points for timely calculation. In the event of a fault, an alarm can be triggered in time for timely processing. Therefore, real-time performance is required. Reliability, this fault diagnosis terminal is mainly used in railway crossings. Its work location is mostly in the wild, and the working environment is very bad. It must have sufficient reliability to ensure the stable operation of the system.
3. System hardware module
The system is divided into the main control module, AC signal input and conditioning module, human-machine interface module, CAN bus communication module, and switch input and output channel. The system's main control CPU is Cygnal's C8051F040 SoC Type Single Chip Microcomputer (hereinafter referred to as 040 single chip microcomputer ). The system LCD uses the sun man company in Changsha to produce a 20 × 04 serial LCD display module. Considering that NVRAM and real-time clock need to be used simultaneously in the design, a non-volatile time management RAM-DS1743 is selected in the design to improve the system integration, increase system reliability. In this design, the system CAN bus driver selects the TJA1050T bus driver manufactured by PHILIP.
4.Real-time Operating System Software Design
Based on the functions implemented by the system and the features of the cygnalf040 microprocessor, the system is divided into tasks, the priority of each task is determined, and part of the hardware-related kernel code of smallrtos51 is modified.
4.1Task division
Based on the functions required by the above fault diagnosis terminal, the system is divided into six tasks: fault detection, sampling calculation, CAN bus communication, man-machine interaction interface, and fault tolerance design. The real-time kernel smallrtos51 provides functions such as task scheduling, task management, inter-task communication, interrupt management, and memory management.
4.2Task creation and Scheduling
Smallrtos51 uses the static method to create a task, saving the ram overhead, so that smallrtos51 can be run in the CPU of the small ram, and each task must be an infinite loop. When the system starts a multi-task environment, it calls osstart () to initialize the stacks of all tasks and points the stack pointer to the task stack space with ID 0. The stack structure 1 of each task is shown in. In terms of task scheduling, smallrtos51 adopts a fixed priority (also known as static priority) allocation policy. The priority of any two tasks cannot be the same, and users can change the priority of the task.
4.3System software requirements
According to system function requirements, the following API functions must be implemented on the smallrtos51 platform, and the system services provided by smallrtos51, such as time delay and semaphores, must be used.
4.3. 1Man-Machine Interface
(1) keyboard driver.
The system uses a stand-alone keyboard design. The essence of the menu driver design is to periodically process the keyboard. The message queue is used to transmit the Keyboard Message to handle the key issues. The key value is parsed, execute the function module to refresh the screen at the same time. After the interruption occurs, obtain the void getkey (void); apply the system function to complete the deshake delay; send the DOWNKEY message when the key is pressed; send the UPKEY message when the key is released, resolution Based on different key values: No buttons: Skip the keyboard parsing module without keys. The key value is 0x79. direction keys: the operation status of the current system is divided into two situations. If the current screen is selected as the menu, the up and down arrow keys are used to move the cursor to change the options. If the screen is set, the left and right direction keys are used to move the cursor and change the setting options. The up and down keys are used to change the corresponding options. Their key values are 0xe9, 0xd9, 0xb9, and 0xf1; validation key: This is also divided into two situations. If the current display screen is in the menu selection status, the validation key is used to enter the next level sub-menu or function module. When the current screen is a set screen, the confirmation key is used to confirm the storage of the changed set value, its key value is 0xf1; the Return key: The Return key is used to return to the upper menu, and the Return key value is 0xf8. The keyboard flowchart 2 is shown, and the man-machine interface software diagram 3 is shown.
(2) LCD Display Section. The LCD api function implements the following functions: LCD Initialization Configuration and ASCII string display. Figure 6 shows the interface diagram of the LCD module. The application uses the following eight functions to perform operations on the LCD interface: LcdPos (), LcdRead (), LcdWDat (), LcdInit (), DisANum (), LcdWrite (), and DisAStr (), LCDWFont (), BcdToDec (), DecToBcd (). LcdInit () is the initialization code of the module. Because SmallRTOS51 provides services, LcdInit () must be activated before other functions are used and called after multi-task startup. LcdInit () initialize the hardware port configuration, create a semaphore, and set the LCD working mode. In addition, the LCDWFont function can be used to customize 8 5x8 pixel characters or symbols. For example, define a downward arrow "lower". The program list is:
Void XLCDWFont (void)
{Uint8 I;
Uint8 font [] = {0x04,0x04,0x04,0x04,0x15, 0x0e, 0x04,0x00 };
For (I = 0; I <8; I ++)
{
LcdWCmd (0x40 + I );
LcdWDat (font [I]);
}
}
Figure 2 Keyboard Flowchart
Figure 3 man-machine interface software diagram
4.3. 2Real-time clock
The real-time clock-related registers integrated in DS1743 are located at the maximum eight addresses in RAM, that is, the control registers are located at the address 1FF8, year, month, day, week, hour, minute, the second register is located at 1FF9-1FFF. The control register has two digits: Read bit and write bit. By writing 1 to these two digits, the update action of the external clock register is aborted to prevent reading of incorrect clock data during the update transition. There are two main function bodies: Ds1743GetTime () and Ds1743SetTime (). The conversion functions of uint8 DecToBcd (uint8 number) and uint8 BcdTDeco (uint8 number) are decimal and BCD codes.
4.3. 3 canBus driver
Generally, the Communication Software of the most basic CAN Bus node includes three parts: initialization of the CAN node, transmission of messages, and receipt of messages. First, create a buffer and initialize the CAN node. It mainly includes the following parts: the setting of the CAN pin output mode, the setting of the baud rate parameter, and the initialization of the message object, and the settings for the corresponding interrupt allowed bits when receiving or sending an interruption is allowed. To enter the initialization status, you must set the Init bit in the CAN control register to 1. After the setting is complete, reset the Init bit to exit the initialization status. The initialization program initializes all the packet objects (generally sets all values to zero), and sets the CAN control register (CAN0CN) and BITREG, the sender and receiver are initialized respectively. Initialize, send, and receive objects in the main program, and start the CAN Processing Mechanism (initialize BITREG and CAN0CN). The following is the CAN Startup Program:
Void StartCAN (uint8 count)
{
WORD bps;
Switch (count) // baud rate parameter settings
{.............................................}
EIE2 | = 0x20; // open interrupt
}
The CAN packet is automatically sent by the CAN controller. You only need to transfer the corresponding data to the sending buffer register based on the identifier of the received remote frame, then write the encoding of the packet object into the command request register to start sending, And the sending is done by hardware. Here, we use timed update to update the data in the sent packet object. The data is automatically sent by the Controller. when it receives a remote frame, it sends the data frame with the same identifier.
The CAN controller automatically receives and sends messages. The receiving program only needs to read and process the received data from the receiving cache. The basic method is the same as that of the sender, but the receiver uses the interrupt method. In this application, the receiving program mainly receives the parameter data set by the upper computer to the lower computer, and receives data only when the data is modified. Therefore, it is appropriate to use the interrupt method for processing. During the read and write operations on the buffer zone, two semaphores, read and write, are introduced to synchronize the operations on both ends of the buffer zone. The advantage of this design is that when a user's task wants to write but the buffer is full, he sleeps on the semaphore so that the CPU can run other tasks, the task that wakes up this sleep after ISR reads data from the buffer zone. Similarly, when a user's task wants to read data but the buffer zone is empty, it can also sleep on the semaphore, wait until the external device has data to wake up. Because the SmallRTOS51 semaphore provides a timeout Wait Mechanism, the CAN port also has the read/write capability of timeout.
4.3. 4Switch Input/output module
The switch quantity input module provides an input channel for the fault diagnosis terminal to correctly monitor the switch Quantity Status. The underlying driver functions of the switch input/output module mainly include the following two functions:
Uchar SwitchStausGet (void) // gets the Switch Status
Void SwitchStausSet (uchar dat) // sets the Switch Status
5.Conclusion
The small RTOS real-time operating system is used in this design. For real-time operating systems, real-time scheduling, deterministic time response, and high system reliability are required. The most important indicator is the memory overhead. In this design, the single-chip microcomputer uses cygnal f040 and the internal RAM is 4352 bytes (4 K + 256). After the program is optimized and debugged, the RAM occupies a total of 402 bytes, meeting the system memory requirements, ram does not need to be expanded to implement system functions. The power system fault diagnosis terminal system developed based on the small rtos51 embedded operating system features stable performance, good real-time performance, and strong scalability. System Operation tests show that the system is stable and reliable, and can meet various real-time performance requirements. It has good application prospects in the terminal technology of fault diagnosis.
References
[1] transplantation of Wang Dongxing uC/OS-II on MCU 2006.5 [J], Microcomputer Information,: 95-97
Jean J. labrosse. embedded real-time operating system uC/OS-II. 2nd. Translated by Shao Beibei, Beijing: Beijing University of Aeronautics and Astronautics Press, 2003.5
[3] Chen Mingji, Zhou licong. Principle and Application of the embedded real-time operating system small rtos51. Beijing: Beijing University of Aeronautics and Astronautics Press, 2004.3
[4] Jin yongsheng, Fan Mingfeng. Design of Data Collector Based on small rtos51, application of single chip microcomputer and embedded system 2004.5