Design and implementation of digital instrument system based on RTOS and ISP functions
[Copy link]
Abstract: Taking Atmel's AT89S52 single-chip microcontroller as the object, this paper introduces the hardware and software design of the digital instrument system based on RTOS (multi-task real-time operating system) and ISP (in-system programmable) functions, and also gives the composition of the hardware system and the implementation code of the relevant software programming. The debugging test of a weapon system shows that the instrument system has the advantages of intuitive display, accuracy and reliability. Keywords: RTOS; ISP; single-chip microcontroller; LCD module introduction Atmel's controllers are widely used in engineering practice due to their high integration and convenient online debugging. This paper discusses the software and hardware implementation process of the digital instrument system based on the preliminary research of a certain weapon system. Its main functions include: real-time acquisition of the 6-way positive and negative analog voltage values and combat environment temperature of the distribution box system, and dynamic display through the MSl2232B LCD module. 1 Software Features With the widespread use of C language in embedded systems, the real-time and complexity design requirements of software systems are becoming higher and higher. It is difficult to complete the relevant software design or implementation using the usual single-task infinite loop structure and interrupt service subroutine programming ideas. RTOS (multi-task real-time operating system) is a more popular processing method at this stage. The basic design idea of RTOS is to make different task function bodies according to the functions of various system inputs and processing requirements, and assign appropriate priorities. Referring to the multi-threaded processing mechanism of the Windows system, the CPU execution time is divided into several segments, and each task function body corresponds to a time segment. After execution within the specified time, it is allowed to switch to another task. Because each time period is very short, it shows the multi-task real-time processing characteristics. Because the tasks are not executed simultaneously and have jump characteristics, there can be multiple infinite loop structures in the entire project management file, and the main entry function is omitted, which is the biggest feature of RTOS. In this paper, Keil integrated development environment is used as the platform, and the RTX5l Tiny version is used to complete the entire software design process.
ISP (In-System Programmable) is a Flash technology suitable for Atmel series devices. Through the PC's LPT standard parallel interface, it can download the source code in Intel format to the corresponding storage area of the microcontroller in real time. The biggest feature of ISP is that it simplifies the debugging process, improves the maintainability of the software system, and enables it to have online upgrade features. In addition, the entire development process eliminates the operation of repeatedly plugging and unplugging circuits. 2 System Hardware Composition The hardware of this system mainly includes the following modules: AT89S52 main controller, ADC0809 analog/digital converter, MSl2232B LCD, temperature sensor and voice alarm, etc. Among them, the AT89S52 microprocessor mainly completes the hardware control of peripheral devices and related data calculation, ADC0809 collects 6 analog signals and inputs them into the main controller, MSl2232B completes the display of numbers and related identification characters, and the voice module gives corresponding alarm information when the voltage fluctuation of the distribution system exceeds the predetermined index. 2.1 Main controller module
The main controller is the AT89S52 single-chip microcontroller produced by Atmel. Compared with the conventional C51 series, its outstanding features are 8KB flash memory (reducing the requirements for external expansion memory), 256B RAM memory (can accommodate more local variables), 8 interrupt sources and ISP online programmable features. 2.2 LCD display module MSl2232B belongs to the graphic dot matrix series display device, with double-line display function, its dot matrix structure is 122×32, through AT89s52 to give the necessary data and instructions to operate the MSl2232B main and auxiliary controllers, so as to complete the display of graphics, characters and other related information. This LCD display module also has strong versatility. Users can form a complete LCD system with a small number of components. The circuit connection is shown in Figure 1. 2.3 A/D data acquisition module Since this system involves multiple analog signals, the AD20809 general-purpose analog/digital converter is selected. It uses successive approximation to complete the A/D conversion process. It has 8 analog switches on the chip and can automatically latch the signal. The input voltage range is 0V~+5V. Because the output of the device has a 1TrL three-state latch buffer structure, it can be directly connected to the I/O port of the single-chip microcontroller. The relevant interface connection is shown in Figure 2. It should be noted that among the 6 power supply signals in this system, only one negative power supply signal meets the input characteristics of ADC0809 0V~5v, so it must be equipped with a corresponding inverting circuit, which is completed by the LM358 operational amplifier. The LM358 can also be used to expand the monitoring range (with the premise of reducing the measurement accuracy), which is feasible for occasions where the accuracy requirements are not too high. The PC817 optocoupler in Figure 2 is used to detect negative voltage signals. When IN4 is a low-level signal, it corresponds to a negative voltage signal. At this time, a "-" sign is given in the corresponding row position of MSl2232B, which not only solves the compatibility problem of positive and negative voltages displayed in the same line, but also reduces the amount of software design (from 6 to 3), while improving the real-time performance of the system accordingly. 2.4 Temperature sensor module DS18820 is a 1-Wire digital temperature sensor launched by Dallas Corporation. It can collect temperature data of the field environment in real time and convert the corresponding value into digital output. The connection between the temperature sensor and AT89S52 is shown in Figure 3. 2.5 Voice Alarm Module This module mainly realizes the output of alarm information under abnormal conditions. As long as the fluctuation value of the voltage signal of the distribution box exceeds a certain range, the audio alarm device will give different frequency signals to make the buzzer sound to remind the relevant operators to pay attention. 3 System software design
According to the program structure requirements of the RTOS system, the software engineering mainly consists of the following four parts: initialization, data acquisition and A/D conversion, display subroutine, temperature monitoring and alarm module. If the initialization is set to Task 0, then in addition to completing the corresponding hardware configuration and variable initialization, all other tasks must be started. In addition, because the initialization process only needs to be performed once, Task 0 should also delete itself. The specific program code is as follows: void Init(void)_task_Intial { variable=0; LcdInitialO; ClrScreenO; 0s_create_task(AI)C0809I)ata); os_create_task(DSl8820Temperate); 0s_create_task(LCMI)isplay); 0s_delete_task(Intial); } The relevant macro definition variable values are: #define Initial O #define ADC0809Data 1 #define DSl 8820Temperate 2 #define LCMDisplay 3 When debugging this program in the Keil integrated development environment, in addition to including the corresponding header file (rtx51tny.h), you must also set the corresponding environment parameter values. 3.1 Digital-to-Analog Conversion Subroutine The data acquisition and A/D conversion subroutine mainly samples, quantizes and processes the return value of the analog signal transmitted from the distribution box. The electrical timing and related technical requirements during software programming are shown in Figure 4. In the timing diagram, the rising edge clears the value in the quantization result register, the falling edge starts, and the A/D conversion; the high level state of the sampling start pulse should be maintained for at least 100μs; EOC becomes low level to indicate that the conversion process is in progress, and the hardware hysteresis effect time of the start signal should be at least 10μs; MSB corresponds to P0.7 of the AT89S52 parallel port, and LSB corresponds to P0.0.
The key parts of the program source code and comments are as follows: unsigned int AI)C0809(void) { unsigned int wait = 3; ChannelChooseA=1; ChannelChooseB=0; ChannelChooseC=0; ADStartPulse=0; ADStartPulse=1; //Generate the start pulse for A/D conversion (Rising edge part) delay(); // Ensure the high level width of the start pulse At least greater than 100μs ADStartPulse=0; //Generates the start pulse for A/D conversion (Falling edge part) wait--; wait--; //Delay the command to ensure that EOC is reached The hardware hysteresis requirement required to go low. 'while fADConvertEOC==0): return(Result); / Return the converted quantized value of A/[) Return to the main function. } 3.2 LCD display module The MSl2232B graphic dot matrix liquid crystal display (LCM) module is divided into Master and Slave controllers on the left and right, with 4 pages in total. The left column address is 0-61, and the right column address is 0-61. The key to operating this device is to correctly configure the working status of the master and slave controllers according to the timing diagram. There are two common methods for accessing LCM using AT89S52, namely memory mapping register addressing and analog interface timing. This system uses the latter method. Analysis of the main function module source code: void MasterWriteD(unsigned char Ramdata) //Write data to the Master controller { ReadOrWrite = 0; InstructionOrData=1; MasterEl=1; P0=Ramdata; MasterEl: 0; } void MasterWriteI(unsigned char Ramdata) //Write instructions to the Master controller { ReadOrWrite = 0; InstructionOrData=0; MasterEl=1; P0=Ramdata; MasterE1=O; } void ReadState(void) //Detect the working status of the main and auxiliary controllers { ReadOrWrite=1; InstructionOrData=0; MasterEl=1: SlaveE2=1; PO = 0xff; while(BusStateBusy==1); } void SetPage(unsigned char page0, unsigned char pagel) { MasterWriteI(0xb8 | pagel); SlaveWriteI(0xb8 | page0); } void SetColumn(unsigned char address0, unsigned char addressl) { MasterWriteI(0x3f & addressl); SlaveWfiteI(0x3f & address0); } //Display a column of information (consisting of 8 pixels) on the left LCM screen of the main controller operation void MasterPutChar(unsigned char masterchar) { ReadState(); MasterWriteD(masterchar); } Generally speaking, the RAM storage space of AT89S52 is limited. If the dot matrix code of graphics or characters (you can download the relevant application plug-in, automatically generated by .bmp file or characters) is too large, there will be an insufficient memory warning prompt when it is placed in the RAM area. To solve this problem, you can do the following: unsigned char code Dotn[]=lOxOO,Ox00,0x30, 0x00, 0x30, Ox00, Ox00, 0x00}; The dot matrix code is defined by the code keyword and placed in the ROM area. 3.3 1-Wire subroutine of DS18820
The outstanding advantage of DSl8820 is that it can directly output the ambient temperature collected on site in digital form, which can save the subsequent signal amplification and analog/digital conversion parts, and the components used to build the peripheral circuit are few and relatively simple, and the system cost is low; the 1-Wire interface communicates with AT89S52, and its structure is convenient for multi-point measurement and easy to expand; the temperature measurement range is wide (-550℃~1250℃) and the accuracy is high. The key to the operation of l-Wire is the processing of software timing. The delay parameters are different for different crystal oscillators. This system takes 12MHz as an example and gives the following main program code: bit DallassReset(void) { unsigned char presence = 0; DallassDataout = 1; //Release the reset pulse before sending it out Put bus delay(10); DallassDataout = 0; delay(240); // Ensure the delay is between 4801μs and 9601μs DAUassDataout = 1; delay(35); Dresence = DallassDataout; delay(60); return (presence); } //The write sequence starts when the master controller pulls the bus low void writeCommand(unsigned char command) { unsigned char count; for(count=O; count<8; count++) { DallassDataout = 0; if(command&0x01==1) { DallassDataout = 1; //Write "1" timing After pulling the bus low, the AT89S52 master controller must release the bus within 15μs, so there cannot be a long delay statement before this statement } delay(35); //Whether writing "1" or "0", there must be a low level of at least 601as DallassDataout=1; //Bus recovery process command=command>>1; } } //The read timing starts from the moment the master controller releases the bus after pulling the bus down for at least 1μs unsigned char readdata(void) { unsigned char value; unsigned char midtemp = 0; for(value=0; value<8; Value++) { I DallassDataout = 0; midtemp=midtemp>>1; -nop_O; _nop_O; DallassDataout = 1; //When reading the timing, the controller must release the bus within 15μs and then sample the bus status, so the delay statement before this statement cannot be too long if(DallassDataout==1) { midtemp=midtemp | 0x80; //Because midtemp is initially assigned a value of 0, if the sampling bus state is 0, no processing is required and 0 will be automatically returned. } delay(35); // Delay 70μs to complete this Bit The bit read timing process prepares for the next read } return (midtemp); } void delay(unsigned char out) { unsigned char in; for(in=out; in>O; in--); } 4 ISP Structure Principle Integrating the ISP function into the formed PCB can realize the real-time update and upgrade of the program. Its structural principle is shown in Figure 5. This is obviously very convenient and feasible for the design of complex engineering systems. 5 Conclusion With the development of intelligent instrumentation technology, the liquid crystal display module has become a popular trend. The software and hardware design scheme given in this article has been correctly, reliably and stably operated during the debugging of a weapon system. It not only overcomes the defects of large size and high failure rate of NT analog instruments, but also greatly improves the agility of data detection due to the use of RTOS technology. In addition, the online integration of ISP functions also reserves space for the expansion of system functions.
|