Using AT89C51 to realize analog-to-digital conversion

Publisher:悠闲时光Latest update time:2011-02-21 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This tutorial uses a simple voltage measurement circuit to explain the use of the AT89C2051 on-chip comparator and the design of the analog-to-digital conversion main program, and explains the principle of using the RC charging law to achieve analog-to-digital conversion and obtain the approximate value of the measured voltage.

1. Circuit composition and function

As shown in Figure 1, this circuit is mainly composed of AT89C2051 (IC1), R10, C4, etc. The measured voltage is input from P1.1. The circuit obtains the voltage value by comparing the charging voltage of C4 with the measured voltage, and then converts the measured voltage value into the corresponding display code and sends it to LED1 and LED2 for display.

2. Introduction to AT89C2051

Compared with AT89C51, AT89C2051 removes P0 and P2 ports, and embeds an accurate analog comparator between P1.0, P1.1 of P1 port and P3.6 of P3 port. Other hardware resources are exactly the same. AT89C2051 has the following standard features: 2K bytes of flash memory, 128 bytes of RAM, 15 I/O lines, 2 16-bit timers/counters, 5 two-level interrupt source structures, a full-duplex serial port, an accurate analog comparator, on-chip oscillator and clock circuit. In addition, AT89C2051 has static logic, which can work at low to zero frequency, and supports two software-selectable power saving modes: in idle mode, the CPU stops working, but the RAM, timer/counter, serial port and interrupt system are still working; in power-down mode, the contents of RAM are saved and the oscillator is frozen, and all other on-chip control unit functions are disabled until the next hardware reset.

AT89C2051 has only 20 pins. The pin arrangement and the connection diagram of the on-chip comparator are shown in Figure 2. As can be seen from the figure, P1.0 is connected to the non-inverting input terminal of the comparator.

P1.1 is connected to the inverting input of the comparator, and the output of the comparator is connected to P3.6 inside the chip, while P3.6 has no external pins. Therefore, the P1 port of AT89C2051 has a complete 8 I/O port lines of external pins, while the P3 port has only 7 I/O lines of external pins, making the number of I/O lines available for external use to be 15. The performance of AT89C2051 is as excellent as that of AT89C51, and the structure is very simple, thus providing a flexible and inexpensive solution for many embedded control systems. Therefore, it is widely used in various intelligent and automated instruments, meters and home appliances.

3. Principle of Digital-to-Analog Conversion

Figure 3 is a simplified diagram of the digital-to-analog conversion circuit. As can be seen from the figure, a resistor R10 is connected between the in-phase input terminals P1.0 and P1.4 of the comparator IC1, and the measured voltage is input to the inverting terminal of the comparator IC1 through P1.1. In this way, when the measured voltage Ui is input from P1.1 and measurement begins, P1.4 is first set to a low potential, C4 will discharge to the ground, and the potential of P1.0 will be zero. Since the potential of P1.1 is higher than the potential of P1.0 (Ui>0) at this time, the output terminal P3.6 of IC1 is low potential. Entering the measurement program, P1.4 is first set to a high potential, VCC will charge C4 through R9 and R10, and the potential of P1.0 will rise exponentially over time. When the potential of P1.0 rises to the same level as or slightly higher than that of P1.1, the output terminal P3.6 of IC1 will jump from a low potential to a high potential. Therefore, we can use the program to control the CPU to continuously detect the potential of P3.6 while C4 is charging. When the potential of P3.6 jumps high, we think that the voltage of P1.0 is equal to the voltage of P1.1. Since the charging voltage of C4 can be accurately calculated by the length of the charging time, the value of the measured voltage Ui is indirectly measured by calculating the charging voltage of C4. VCC charges C4 through R10 (the influence of R9 and the pull-up resistor of P1.4 is ignored). The voltage across C4 rises exponentially with time. The charging voltage value at any time can be calculated according to UC=VCC (1-et/RC). For ease of understanding, we only select a small section of the charging curve shown in Figure 4, straighten it, and then explain the principle of analog-to-digital conversion according to the law of linear change. As can be seen from Figure 4, the initial part of the C4 charging curve from 0 to 500mV is steeper and close to a straight line. In order to simplify the program design, we regard the OA section as a straight line. In this way, when C4 is charged from zero potential to the charging voltage reaching 500mV, the charging time is proportional to the voltage at both ends. We appropriately set the values ​​of R10 and C4 so that the charging voltage of C4 rises by 10mV every 20μs, and then compile a matching program. Since C4 starts charging, the potential of P3.6 is detected every 20μs, and the number of detections is accumulated in the register until the potential of P3.6 jumps. In this way, the accumulated number in the register has a corresponding relationship with the charging voltage at both ends of C4. If the potential of P3.6 jumps from low to high during the fifth detection, it means that the measured voltage is 50mV. In this way, after we measure that the potential of P3.6 jumps high, we stop detecting and multiply the value in the register by 10mV to obtain the voltage value of the measured voltage Ui, thus realizing the conversion between modulus and quantity.

4. Display Principle

In Figure 1, the a, b, c, d, e, f, and g terminals of LED1 and LED2 are connected to P3.0 to P3.7 (except P3.6) of IC1P3 port, and the GND terminals are connected to P1.2 and P1.3 of IC1. When displaying, the segment selection code is sent to P3 port and the bit selection code is sent to P1 port, and the measured value will be displayed in LED1 and LED2. The relevant principles and transformation process have been described in detail in the previous lectures.

5. Programming Ideas

After the system is powered on, it is initialized, the digital tubes LED1 and LED2 are turned off, and then the measurement subroutine is entered: CLR P1.4 is executed to make P1.4 at a low potential, and C4 is discharged to the ground through R10. After calling the 40ms display subroutine, the charge of C4 is completely discharged, and the potential of P1.0 is zero. MOV R0, #00H is executed to clear the detection times accumulation register R0, and then SETB P1.4 is executed to make P1.4 at a high potential, and VCC charges C4 through R10. After several no operations, the INC R0 instruction is executed to add 1 to the content in R0, and then JNB P3.6, 000FH is executed to determine whether the charging voltage is equal to the measured voltage. If the potential of P3.6 is low, it means that the charging voltage of C4 is lower than the measured voltage. The program jumps to 000F, and then detects the potential of P3.6 after a no-operation charging delay. If the potential of P3.6 is high, it means that the voltage of P1.0 is equal to the voltage of P1.1. SJMP 0006H is executed, and the program immediately returns to 0006 to discharge C4, and enters the display subroutine to process the measured data, send the processed data to the digital tube display, and then perform the next measurement. From the space from 000FH to 0018H in the program list, it can be seen that the 6 no-operations and INC R0 are single machine cycle instructions, and the execution time is 2μs, while JNB P3.6, 000FH is a double machine cycle instruction, and the execution time is 4μs. In this way, the detection time interval of P3.6 is 18μs each time. According to the values ​​of R10 and C4 in the circuit of Figure 1, the above program can make the charging voltage of C4 rise by about 10mV each time the potential of P3.6 is detected. Then enter the display subroutine, send the data in R0 to the LED for display after transformation, and we can read the value of the measured voltage Ui. The main program of analog-to-digital conversion is placed in the space from 0000H to 001AH, and the data display subroutine should be placed at 001BH. The main function of the display subroutine is to obtain the display code after decoding the data, and its principle will not be repeated. If there is a high precision requirement for analog-to-digital conversion, it is necessary to calculate the corresponding values ​​of multiple moments in advance according to UC=VCC (1-et/RC), so as to form a corresponding data table of charging time and charging voltage value, and then compile a program to take out the charging time, that is, the data in register R0, and obtain a more accurate charging voltage value by table comparison. This article aims to explain the basic principle of analog-to-digital conversion, which will not be described in detail.

6. Main program list

Address Machine Code Instruction Explanation

0000 75B000 MOV P3, #00H; Initialization

0003 7590EF MOV P1, #EFH ; Turn off the display

0006 C294 CLR P1.4; Discharge

0008 12001B LCALL 001B ; Call 40ms display subroutine

000B 7800 MOV R0, #00H; Clear the cumulative register

000D D294 SETB P1.4 ; Start charging

000F 00 NOP; No operation delay

0010 00 NOP ;

0011 00 NOP ;

0012 00 NOP ;

0013 00 NOP ;

0014 00 NOP ;

0015 08 INC R0; Cumulative number of detections

0016 30B6F6 JNB P3.6,000FH; Lower than Ui, recharge comparison

0019 80EB SJMP 0006H ; Return to next measurement

Reference address:Using AT89C51 to realize analog-to-digital conversion

Previous article:The use of single chip microcomputer serial port and "Bluetooth" electrical measurement system
Next article:Using AT89C51 to realize self-setting time control

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号