Application of analog comparator inside microcontroller

Publisher:EnchantedMelodyLatest update time:2012-02-18 Source: 国外电子元器件Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Generally speaking, microcontrollers with internal A/D converters are relatively expensive, and generally only have 8 to 10-bit resolution, which is obviously not suitable for occasions requiring high resolution; and ordinary microcontrollers do not have A/D converters at all. With the development of modern electronic technology, some small-sized microcontrollers with built-in analog comparators have emerged, such as ATMAL's AT89C2051, ZILOG's Z86E04, MICROCHIP's PIC16C620, etc. When these microcontrollers are used, the ports connected to the comparators are generally used only as ordinary I/O, and the built-in analog comparators are rarely used. Taking AT89C2051 as an example, the following discusses a new method of using the built-in analog comparator of a microcontroller to form an A/D converter.

1 Hardware conversion circuit

AT89C2051 is one of the MCS51 microcontrollers. Although it has only 20 pins, it integrates the standard core of the 51 series microcontrollers, including 2k program memory, 128 bytes of data memory, 2 16-bit timer counters, a standard full-duplex UART and an accurate analog comparator, which is not available in previous products. Figure 1 is a circuit diagram of a dual-integral A/D converter using the analog comparator of AT89C2051. Among them: the structure of the built-in analog comparator is shown in the dotted line enclosed part in the figure. The positive and negative input terminals of the comparator are connected to P1.0 and P1.1 respectively. These are the output and input ports of two open-drain and pull-up resistors. When "1" is written to P1.0 and P1.1, M1 and M2 are cut off, which is equivalent to the digital part of P1.0 and P1.1 being suspended. At this time, the input of the comparator is not affected by the output of the microcontroller port; because P1.0 and P1.1 have strong current sinking capability, when "0" is written, P1.0 and P1.1 can absorb 20mA of current sinking, and the saturation voltage of M1 and M2 is very low. This feature can be used to completely discharge the integral capacitor. The comparator output terminal is connected to P3.6 inside the microcontroller, and the output result of the comparator can be obtained by reading P3.6. Therefore, the built-in comparator of AT89C2051 and a small number of peripheral devices can be used to form a dual-integral A/D converter. In Figure 1, I0 is a constant current source with a current of approximately 0.5 to 2 mA. Cf is an integrating capacitor. The selection of Cf and I0 depends on the number of bits of the A/D conversion. Vref is a reference voltage, which is generally half of the maximum value of the analog input voltage. U2 is an analog switch, in which channel 0 is connected to the reference voltage and channels 1 to 7 are connected to the analog input. That is, the A/D converter has 7 input channels.



2 Conversion process

When the constant current source integrates the capacitor, the voltage on the integral capacitor is linearly proportional to the time. In this way, the internal timing counter of the microcontroller can be used to measure the integration time of the reference voltage and the analog input voltage, and then the conversion result can be obtained through the CPU calculation. The ports P1.2 to P1.4 of the microcontroller can be used to output the analog switch channel selection address, and the timing counter T0 can be set to mode 1, 16-bit timing state, to determine the integration time. The conversion process can be divided into 5 steps:

The first step is to discharge the integrating capacitor, mainly by writing "0" to P1.1, using its large current sink to discharge Cf, and at the same time clearing the timing counter T0.

The second step is the reference voltage integration, that is, the analog switch selects channel 0, which is equivalent to connecting Vref to the positive input of the comparator, and writing "1" to P1.1, and starting the timing counter at the same time, so that I0 starts to integrate Cf; the program reads the P3.6 state in a loop to detect the output result of the comparator. When the integral voltage on the integral capacitor is slightly greater than (because the comparator has an extremely high gain, it can be approximately regarded as equal to) the reference voltage, the output of the comparator is reversed, and P3.6 jumps from high to low. After the program detects this jump, it stops the timing counter and saves the timing counter result Tref at this time. At this time, the relationship between the constant current source and the capacitor integration can be obtained: Vref = (I0 Tref) / Cf

The third step is to discharge the integrating capacitor, that is, repeat the first step to discharge Cf and clear the timing counter T0.

The fourth step is input voltage integration. At this time, the analog switch can select one of channels 1 to 7, which is equivalent to connecting the analog input voltage Vx to the positive input of the comparator. Repeat the second step to integrate the input voltage, and the integration time Tx can be obtained. Therefore, Vx = (I0Tx) / Cf

The integrated voltage waveform on the integrating capacitor Cf in the above four steps is shown in FIG2 .


The fifth step is to obtain the result of A/D conversion through CPU calculation. By dividing the result of the second step by the result of the fourth step, we can get:
Vref/Vx=Tref/Tx

After transformation:
Vx=(VrefTx)/Tref

The above formula is the result of A/D conversion.

From the above formula, we can see that the A/D conversion result Vx is only related to Vref, Tx, Tref, but not to I0 and Cf. This is very important. Because it means that the error caused by the temperature drift of the constant current source and the integral capacitor is suppressed during the conversion process, thereby ensuring the stability of the A/D converter, which is also the advantage of the integral A/D converter. In theory, the accuracy of the A/D converter depends only on the stability of the reference voltage and the accuracy of the microcontroller timing counter, and both of these points are relatively easy to guarantee. Of course, this refers to the case where the constant current source is an ideal constant current source. In fact, the characteristics of the constant current source determine the nonlinear error of the A/D converter. Therefore, in situations with higher requirements, a constant current source integrated circuit with good linearity, such as LM334, should be selected. In the case of low requirements, the constant current source circuit composed of discrete components shown in Figure 3 can be used to realize A/D conversion. At this time, the integral capacitor can choose a polyester capacitor with a small temperature coefficient. [page]


It should be noted that since the microcontroller uses the port query method to detect the output result of the comparator, the MCS51 series microcontroller port query command takes 2 machine cycles, and the timer counter needs to add 1 in each machine cycle. Therefore, when using it, it is possible that when the output of the comparator is reversed, the program cannot stop the timer counter immediately, but waits until the next machine cycle runs to the CLRTR0 statement before stopping. In this way, the value of the timer counter is always an even number. If this problem is not processed in the program, it will cause errors. The solution is to increase the integration time of I0 and Cf, that is, double the integration time, and then divide the reading of the timer counter, that is, Tref and Tx, by 2, so that the error of the timer counter adding 1 can be removed and the correct result can be obtained. In other words, if you want to get a 12-bit resolution, you need a 13-bit timer counter reading. Although this algorithm sacrifices the 1-bit resolution of the timer counter and lengthens the conversion cycle, it ensures the accuracy of the measurement result. For microcontrollers with analog comparator output jump interrupts, such as Z86E08, the above process is simpler to handle. The program can be used to set the interrupt when the comparator output reverses, and the timing counter can be stopped at the beginning of the interrupt subroutine. The reading minus the number of machine cycles from the comparator interrupt to the execution of the interrupt subroutine is the actual integration time.

Since the timing counter of AT89C2051 is 16 bits, the resolution of the A/D converter can actually reach 15 bits. The resolution of the A/D converter can be changed by adjusting the values ​​of I0 and Cf. The relationship between the values ​​of I0 and Cf and the resolution and the clock frequency of the microcontroller should satisfy the following formula:

12×2N+1/(f0 Vmax)≤Cf/IO≤12×216/(f0Vmax)

Where Vmax is the maximum analog input voltage, N is the number of bits of resolution, and f0 is the clock frequency of the microcontroller. According to the above formula, a sufficiently long integration time should be ensured during design to ensure the resolution requirement. At the same time, it should also be ensured that the integration time does not exceed the maximum timing value of the microcontroller to avoid causing the timing counter to overflow.

3 Software Programming

The main procedures of the whole working process are as follows (the program is written in MCS51 assembly language). The division subroutine and the 20μs delay subroutine are:

(1) Initialize the main program:

4 Conclusion

As mentioned above, by using a single-chip microcomputer with an internal analog comparator and a small amount of foreign components, and slightly processing the program, a new A/D converter can be constructed. This method can save a large amount of I/O occupied by expanding the A/D, and this A/D converter can achieve very high resolution and accuracy, and has strong anti-interference ability and the resolution can be adjusted by the program, thereby increasing the application range and flexibility of the single-chip microcomputer, and at the same time the design of the application system is also concise and efficient.

References

1.AT89 series microcontroller. Ideal Electronics Co., Ltd.

2.Discrete Z8 Microcontrollers Databook.Zilong,Inc

Keywords:MCU Reference address:Application of analog comparator inside microcontroller

Previous article:SM2965 microcontroller and its ISP application technology
Next article:Interface between DTMF circuit HT9200A and microcontroller

Recommended ReadingLatest update time:2024-11-16 19:38

STM8 MCU Learning Summary 04
The "power LED board" made by using STM8 to complete the effective action of mobile phones and computers controlling power devices through Bluetooth has been fully debugged by writing "code" by myself, and many problems have been encountered, which are listed as follows: --------------------------Production-----------
[Microcontroller]
Design of intelligent vehicle control system based on MC9S12DG128 single chip microcomputer
In the smart car competition, the participating teams should make a smart car that can autonomously identify the route based on the car model platform, and then automatically identify the road and drive on the specially designed track. The smart car designed in this paper uses the 16-bit MC9S12DG128 microcontroller
[Microcontroller]
Design of intelligent vehicle control system based on MC9S12DG128 single chip microcomputer
Brief Introduction to the Design of Single Chip Microcomputer Power-off Protection Circuit
In single-chip computer systems such as digital clocks, some timers and calendar clocks, when the main power supply DC5V is lost, it is called power failure. After power failure, the single-chip computer stops working and the clock also stops. This result is undesirable in many occasions. In order to ensure that t
[Microcontroller]
Brief Introduction to the Design of Single Chip Microcomputer Power-off Protection Circuit
C51 Programming Summary - Strange Knowledge of MCU 2 (Keil Compiler Error)
1: The following error occurs when compiling with C51 Keil: error C249: 'DATA': SEGMENT TOO LARGE,  error C249: 'DATA': SEGMENT TOO LARGE Solution: Set as follows  2: Warning appears in C51 Keil compilation: UNRESOLVED EXTERNAL SYMBOL *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL     SYMBOL:  _DS1302_W
[Microcontroller]
C51 Programming Summary - Strange Knowledge of MCU 2 (Keil Compiler Error)
PIC microcontroller SPI slave framework
#include pic.h #include string.h #include STDIO.H   __CONFIG(0x3F32); //Chip configuration word Select HS mode oscillator, turn off WDT typedef unsigned char uchar; typedef unsigned int uint; uchar resive=0; uchar resive1=0; uchar send_buf ={'S',0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xA,0xB,0xc,0xD,0xe,0xf}; uchar sen
[Microcontroller]
PIC microcontroller: issues regarding very large arrays
Regarding the problem of very large arrays: The compiler used in this experiment is xc16-gcc.exe, the integrated development environment is MPLAB IDE v8.92, and the current compiler configuration is as follows: -g -Wall -mlarge-code -mlarge-data -Os -fno-ivopts As shown below: Here is a very large array: At
[Microcontroller]
PIC microcontroller: issues regarding very large arrays
NOP instruction in microcontroller
The NOP instruction can be seen in many programs in the microcontroller. Some people wonder what this empty instruction is used for. After many experiments, I finally confirmed that (who threw the tomato?) it has two main functions: 1. Accurate timing: NOP instruction time is 1 cycle, and the activity of 12M crystal
[Microcontroller]
Introduction to RAM testing methods based on several commonly used single-chip microcomputer systems
In various single-chip microcomputer application systems, the normality of the memory is directly related to the normal operation of the system. In order to improve the reliability of the system, it is necessary to test the reliability of the system . Through testing, the damage caused by memory failure to the system
[Microcontroller]
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号