STM32 is mainly a digital circuit. The digital circuit has only high and low levels and does not have the concept of a few volts. Therefore, if you want to read the voltage value, you need to use ADC analog-to-digital converter. ADC Read the analog voltage of the pin, convert it into a data, store it in the register, and then read the data into the variable, then we can perform display, judgment, recording and other operations. ADC can convert analog signals into digital signals, which is a bridge between analog circuits and digital circuits It is a bridge from analog to digital. On the other hand, if there is a bridge from analog to digital, there must be a bridge from digital to analog. This is DAC, digital to analog converter. Using DAC, digital variables can be converted into analog voltage. However, in the last section, we also learned a bridge from digital to analog, PWM, In the previous section, we used PWM to control the brightness of LED and the speed of motor, which is the function of DAC. At the same time, PWM only has two states: fully on and fully off. There is no power loss in both states. Therefore, in the high-power application scenario of DC motor speed regulation, using PWM to equivalent analog quantity is a better choice than DAC, In addition, PWM circuit is simpler and more commonly used, so it can be seen that PWM still occupies a lot of application space of DAC. Since the application of DAC is mainly in the fields of waveform generation, such as signal generator, audio decoding chip, etc., PWM is not easy to replace in these fields. This is a brief introduction of ADC and DAC. We learn about ADC and DAC in this section As you know, our STM32 has no peripherals of DAC
The ADC of STM32 is 12 bit (resolution, generally expressed in how many bits, 12 bit AD value, its expression range is 0 – 2 to the 12th power minus 1, that is, the quantization result range is 0 – 4095, the higher the number of bits, the finer the quantization result, the higher the corresponding resolution) successive approximation type (ADC operating mode) ADC, 1us conversion time (conversion frequency, AD The conversion takes a short period of time. Here, 1us means that it takes 1us from AD conversion to the result. The corresponding AD conversion frequency is 1MHz, which is the fastest conversion frequency of STM32 ADC. If you need to convert a signal with a very high frequency, you should consider whether the conversion frequency is enough, If your signal frequency is relatively low, the maximum 1MHz conversion frequency is enough)
Input voltage (generally required to change between the negative and positive pole of the chip power supply, 0V corresponds to 0, 3.3V corresponds to 4095, and the middle is a one-to-one linear relationship) Range: 0-3.3V, conversion result range: 0-4095
18 input channels, can measure 16 external (16 GPIO ports, connect analog signals directly on the pins, and the pins can measure voltage directly without any additional circuit) And 2 internal signal sources (Internal temperature sensor and internal reference voltage. The temperature sensor can measure the CPU temperature. For example, if your computer can display a CPU temperature, you can use ADC to read the temperature sensor for measurement. The internal reference voltage is a reference voltage of about 1.2V, which does not change with the external power supply voltage, so if you supply power to your chip If it is not the standard 3.3V, it may be wrong to measure the voltage of the external pin. Then you can read the reference voltage for calibration, so that you can get the correct voltage.)
Rule group and injection group are two conversion units. This is the enhanced function of STM32 ADC. The common AD conversion process is to start a conversion, read a value, then start again, read a value. However, STM32 ADC is more advanced. You can list one group, start one group at a time, convert multiple values consecutively, and have two groups, One is the rule group for general use, and the other is the injection group for emergencies
The analog watchdog automatically monitors the input voltage range. This ADC can generally be used to measure the light intensity, temperature and other values, and often has a demand that if the light is higher than a certain threshold value, lower than a certain threshold value, or the temperature is higher than a certain threshold value, lower than a certain threshold value, perform some operations. This judgment is higher than a certain value, lower than a certain threshold value, You can use the analog watchdog to automatically execute. The analog watchdog can monitor certain specified channels. When the AD value is higher than the upper threshold value or lower than the lower threshold value set by it, it will apply for an interrupt. You can perform corresponding operations in the interrupt function, so that you don’t have to read the value manually constantly and use if to judge
STM32F103C8T6 ADC resources: ADC1, ADC2, 10 external input channels (that is, it can only measure analog signals of 10 external pins at most. As mentioned above, there are 16 external signal sources at most in this series, but our chip has fewer pins and many pins are not led out)
Next, let’s understand how this successive approximation ADC measures voltage
This is the internal structure of successive approximation ADC. Understanding this structure is very helpful for you to learn the ADC of STM32, because the ADC principle of STM32 is the same as this one, but STM32 only draws a box to represent ADC, and does not describe the internal structure
This figure is the internal structure of ADC0809. It is an independent 8-bit successive approximation ADC chip. In the past, the performance of the single chip microcomputer was not very strong, so an external ADC chip was needed to perform AD conversion. ADC0809 is a classic ADC chip. Now, the performance and integration of the single chip microcomputer have been greatly improved, Many single chip computers have already integrated ADC peripherals inside, so there is no need for external chips. The pins can measure the voltage directly, which is very convenient to use
First of all, IN0~IN7 on the left is an 8-channel input channel. Select one channel through the channel selection switch and input to this point for conversion. Here is address locking and decoding, which is the channel you want to select. Put the channel number on these three legs (ADDA, ADDB, ADDC) Then give a latch signal (ALE), and the corresponding path switch here can be automatically dialed. This part is equivalent to a data selector that can pass the analog signal. Because ADC conversion is a very fast process, you give a start signal, and the conversion will be completed after several us. So if you want to convert multiple signals, There is no need to design multiple AD converters, just one AD converter, and then add a multiplexer switch. If you want to convert which channel, pull the switch, select the corresponding channel, and then start the conversion.
This is the part of the input channel selection. The ADC0809 has only eight input channels. The ADC in STM32 has 18 input channels, so here is an 18 input multiplexer. Then the input signal is selected. How can we know the code data corresponding to this voltage? This requires us to use the successive approximation method to compare one by one. First, this is a voltage comparator, which can judge the size relationship between the two input signal voltages. The output of a high and low level indicates who is larger and who is smaller. Its two input terminals, one is the voltage to be measured, and the other is the voltage output terminal of the DAC here, DAC It is a digital to analog converter. As we said before, if you give it a data, it can output the voltage corresponding to the data, DAC The internal conversion is realized by using the weighted resistance network. See the AD/DA section in the 51 MCU tutorial for details. Now, we have an external channel input voltage with unknown coding and a DAC output voltage with known coding. They are both input to the voltage comparator at the same time for size judgment. If the DAC output voltage is relatively large, I will turn down the DAC data. If the voltage output by the DAC is relatively small, I will increase the DAC data until the voltage output by the DAC is nearly equal to the voltage input by the external channel, so the data input by the DAC is the encoded data of the external voltage.
This process of voltage regulation is completed by the successive approximation of SAR. In order to find the unknown voltage code as quickly as possible, we usually use the dichotomy to find it. For example, here is an 8-bit ADC. The code is from 0 to 255. When we compare for the first time, we will input half of 255 to the DAC. That is 128. Then we will see who is larger and who is smaller. If the voltage of the DAC is larger, we will give half of 128 to 64 when we compare for the second time.
In this way, you can quickly find the code of unknown voltage. If you use binary to represent this process, you will find that 128, 64 and 32 are exactly the bit weight of each bit of binary. This judgment process is equivalent to the process of judging whether binary is 1 or 0 from high to low, This is the source of the successive approximation type name. For 8-bit ADC, the unknown voltage code can be found by judging 8 times from the high bit to the low bit. For 12 bit ADC, the unknown voltage code needs to be judged 12 times. This is the successive approximation process. Then, after AD conversion, DAC The input data of is the code of unknown voltage, which is output here. There are 8 lines in 8 bits and 12 lines in 12 bits. At last, here EOC is End Of Convert, conversion end signal, START is to start conversion, give an input pulse, start conversion, CLOCK It is the ADC clock. Because ADC judges step by step, it needs the clock to promote this process. Next, VREF+ VREF and VREF are the reference voltage of the DAC. For example, if you give a data of 255, which corresponds to 5V or 3.3V, it depends on the reference voltage. The reference voltage of the DAC also determines the input range of the ADC, so it is also the reference voltage of the ADC. At the last left is the power supply of the entire chip circuit, VCC With GND, the positive pole of the reference voltage is usually the same as that of VCC, which is connected together, and the negative pole of the reference voltage is also the same as that of GND, which is also connected together. Therefore, in general, the range of the input voltage of ADC is the same as that of the power supply of ADC.