Practical Eight-Channel Level Measuring Instrument Based on 89C52 Microcontroller
2026-04-06 06:40:40··#1
1. Overall Composition The signal sampling of this measuring instrument can accept eight channels of shaped standard TTL level signals through the eight input lines of port P0. Therefore, it can measure pulse-type level sampling signals from different sensors. Only the original signal needs to be shaped. Each incoming pulse is captured by port P0, and the pulse count can be incremented by one through software programming. The count can be read periodically to convert the corresponding level information. The connection of lines P3.3 to P3.5 to the serial display driver Max7219 and software programming can drive an eight-digit LED display. Four buttons are connected through lines P1.3 to P1.6 to complete button operation. Port P2 is used as an eight-channel output control, which can drive eight solid-state relays. See Figure 1 for the specific composition. Figure 1 2. Hardware Circuit Design 2.1 Resource Allocation The AT89C52 chip is a highly integrated 8-bit microcontroller based on the MCS-51 architecture. Its main features are an enhanced serial port for multiprocessor communication and an increment/decrement timer/counter, specifically designed for control applications. Another important reason for choosing the 89C52 is that it implements 256 bytes of RAM on the chip. The high-end 128 bytes of address are the same as the address of the Special Function Register (SFR), but they can be distinguished by different addressing modes. Therefore, it solves the RAM space problem for the software programming of the measuring instrument, eliminating the need for additional random access memory expansion and saving hardware resources. 2.2 Serial Display Design When designing multi-digit LED displays, all segment selection lines are often connected in parallel and controlled by an 8-bit I/O port. Parallel expansion ports can also be used to construct the display circuit. Usually, this requires more expansion device pins and is more expensive. In this measuring instrument, to save I/O port resources, a serial display method using the Max7219 chip is adopted. The Max7219 is a serial input, output common cathode display driver circuit that can directly interface with the three I/O lines of the microcontroller. It is particularly suitable for systems with few I/O lines and can programmatically control the brightness of the digital tube. In this design, lines P3.3 to P3.4 are latched by a 74LS273 and connected to the Max7219. P3.3 and P3.4, after latching, serve as the 7219's serial data input signal DIN and clock signal CLK, respectively. The output port P3.5 is latched as the LOAD signal. The specific hardware circuit diagram is shown in Figure 2. Figure 2: Main Hardware Connection Diagram. 2.3 Software Counter Design In level measuring instruments, when the signal sent by the sensor is a pulse quantity, a dedicated counter chip such as the 8254 is usually needed to count the pulses. The CPU processes this changing count signal to make the number of pulses sent by the sensor correspond to the level signal. In this design, the P0 port resource of the 89C52 is cleverly utilized. The pulse quantity is shaped and directly sent to the eight data lines of the P0 port, which is then used as a data input port. Through software design, each channel allocates a storage unit (utilizing on-chip RAM). When there is a level transition on the P0 port, the corresponding counting unit increments by 1. A timer is set in software; when the timer expires, the value in the storage unit is read and cleared. This completes the function of the software counter, saving hardware resources and reducing hardware costs. 3. Software Design 3.1 128-Byte Memory Allocation Another major feature of this measuring instrument design is the full utilization of 128 bytes of on-chip RAM, eliminating the need for external storage chips. The CPU's internal address ranges from 15H to 0C5H, all of which are allocated as flags or storage units. Specifically, 15H to 1CH are for each channel's sampling time, 09H to 0FH are for various flag bits, 42H to 5BH are for counting storage and processing units related to each channel's counting, 5CH to 63H are for counting storage units corresponding to the level alarm threshold, and 64H to 6FH and 3CH to 3FH are for alarm category storage units, etc. Through reasonable allocation, although there are only 128 bytes of RAM, the data processing requirements for 8-channel level measurement can be met. 3.2 The display part of the Max7219 driver is placed in the DISP display subroutine. After the Max7219 is initialized at the beginning of the program, the data to be displayed is placed in the display buffer address starting from 20H to 27H. When each channel needs to be displayed, the display subroutine can be called directly. The display part of the program is as follows: DISP: MOV R0,#20H ; R0 points to the first address of the display buffer MOV R1,#01H MOV R3,#08H LOOP3: MOV a,@R0 MOV R4,A ; Data in the display buffer is sent to R4 MOV A,R1 lCALL WRITE ; Data is latched to 273 INC R1 INC R0 DJNZ R3,LOOP3 3.3 The debugged source code of the sampling processing subroutine is as follows: LOOP: MOV A,#0FH ; The main program loop begins MOV P0,A ; First, query port P0 and read the ray count MOV A,P0 XCH A,PQ XRL A,PQ MOV B,A JB B.0,JIA1 ; If there is a count on the first channel, jump to the increment program JIA10:JB B.1,JIA2 ; If there is a count on the second channel, jump to the increment program JIA20:JB B.2,JIA3 ;….. JIA30:JB B.3,JIA4 JIA40:JB B.4,JIA5 JIA50:JB B.5,JIA6 JIA60:JB B.6,JIA7 JIA70:JB B.7,JIA8 ; The 8th channel has a counter, jump to the increment program LJMP LOOP01 ; then process the keyboard query 3.4 Main Program Flowchart The main program flowchart is shown in Figure 3 Figure 3 Main Program Flowchart 4. Conclusion The innovation of this design lies in making full use of the hardware resources of AT89C52 to realize a software counter, which can display the level signal reflected by the pulse signal through a digital tube. The display part uses the Max7219 display driver, combined with software programming, which greatly saves hardware resources, reduces the number of general single-chip microcontroller expansion chips, and reduces costs. After this instrument is put into production, the direct hardware cost is reduced by approximately 300 yuan per unit compared to systems composed of other methods. The annual economic benefit after production is approximately 600,000 yuan (based on a profit of 8,000 yuan per instrument). References: 1. Wu Yuping, trans., MCS51 Microcontroller Series User Guide, Electronic Industry Press, 1995. 2. Wang Jianping, Max7219 Serial LED Display Driver and Application, Electronic Technology Application, 1996, 6. 3. Liu Zhizhuang, Hong Tiansheng, et al., Design of an Intelligent Liquid Level Detector, Microcomputer Information, 2007, No. 2-1, pp. 156-157. 4. Shi Yuanping, Yu Jingsheng, et al., Multi-channel Nuclear Scale Based on Industrial Control Computer, Microcomputer Information, 2008, No. 2-1.