I. Overview
This article describes the powerful ADC module of STM32. ADC (Analog to Digital Converter) is the conversion of analog quantity into digital quantity, and the ADC module of STM32 has many functions. This article mainly describes "three-channel successive conversion (single, single-channel software trigger)".
According to my experience, the ADC module functions and configurations of all series of STM32 chips are similar. Therefore, although this article takes F1 as an example, it is actually applicable to other series (F0, F2, F4, etc.).
This article provides example code: three channels, configured for successive conversion (interval mode), suitable for software-triggered conversion (one channel is converted each time it is triggered), and one cycle requires three software triggers.
Example experimental results:
Channel 1 is connected to ground, channel 2 is connected to 1.5V power supply, and channel 3 is connected to VCC
This article describes relatively more knowledge points. If you are learning the ADC conversion function of STM32 for the first time, you can refer to another relatively simple article of mine:
STM32F10x_ADC1 single channel single acquisition
See below for more details about this article.
Ⅱ. Example Project Download
The examples provided by the author for beginners have removed many unnecessary functions and streamlined the official code, so that beginners can understand it at a glance and provide simple and clear projects for everyone to learn.
The example projects I provided were all uploaded to the 360 cloud disk after multiple tests on the board and no problems were found. You are welcome to download them for testing and reference learning.
The software project provided for download is based on Keil (MDK-ARM) V5 version and STM32F103ZE chip, but it is also applicable to other F1 models (applicable to other F1 models: follow WeChat and reply "change model").
STM32F10x_ADC three-channel successive conversion (single, single-channel software trigger) example source code project:
https://yunpan.cn/cBNcrax8UHhmEAccess password
STM32F1 information:
https://yunpan.cn/crBUdUGdYKam2 Access password ca90
III. About ADC
For the introduction and functions of ADC, please download the reference manual for reference. Here are some important points:
1.12-bit resolution
Among all the STM32 series chips, only a few are 16-bit, such as the F373 chip.
12-bit resolution means that the accuracy of our voltage acquisition can reach: Vref /4096.
Acquisition voltage = Vref * ADC_DR / 4096;
Vref: reference voltage
ADC_DR: Read the value of the ADC data register
Since the register is 32 bits, it is divided into left alignment and right alignment during configuration. Generally, we use right alignment, that is, the lower 12 bits of data are valid data.
2. Conversion Mode
A. Single and continuous conversion
Single: single-channel single conversion, multi-channel single (multiple times) conversion;
Continuous: single-channel continuous conversion, multi-channel continuous (cyclic) conversion;
B. Dual ADC Mode
That is, two ADCs are used, for example, ADC1 and ADC2 are used at the same time, which is the dual ADC mode. In this mode, the following modes can be configured: synchronous regular mode, synchronous injection mode, independent mode, etc.
3. Trigger source
The trigger source is the source that triggers the ADC conversion, which includes external interrupt lines, timers, software, etc. We beginners often use software triggers, that is, we need to convert once and start our software once (the example provided in this article is also software triggered).
IV. Description of Examples in this Article
The configuration and knowledge points about the ADC part in the examples in this article are relatively more for beginners and relatively difficult to understand.
According to the title "ADC three-channel successive conversion (single, single-channel software triggered)" it is not difficult for us to understand the conversion process, but how to implement it is a difficult point.
1. Three channels: We define three channels ADC1: ADC_Channel_1, ADC_Channel_2, ADC_Channel_3.
2. Successive conversion: We use the discontinuous mode (rule group), that is, the sequence of triggering conversions is defined in the rule group.
3. Single: We trigger the conversion once.
4. Single channel: Only one channel is converted each time it is triggered.
V. Source code analysis
The author built the project based on the F1 standard peripheral library (beginners are also recommended to use the official standard peripheral library), and mainly describes it in the form of a library (if your F1 chip is different from the provided project, you can reply to WeChat "modify the model").
Here are some important points about ADC:
1. Input pin configuration
This function is located under the adc.c file;
For the correspondence between pins and channels, please refer to the data sheet of the chip you are using.
Notice:
Why is it "ADC123_IN1"? Instead of ADC1_IN1, or ADC2_IN1?
The reason is that ADC1, ADC2 and ADC3 share these pins.
2. ADC Configuration
This function is located under the rtc.c file;
This function is the focus of this article, and is the focus of configuring the working mode, rule channel, and intermittent mode. The following will explain the meaning of the source code content in turn;
A. Initialize basic parameters:
Working mode: ADC_Mode = ADC_Mode_RegSimult;
There are 10 modes in total, mainly for dual ADC use. I won't describe them here for beginners, but those who are interested can study the use of each mode by themselves.
Scan mode: ADC_ScanConvMode = ENABLE;
It mainly refers to multiple channels, that is, whether you have multiple channels.
Multi-channel: ENABLE;
Single channel: DISABLE;
Conversion mode: ADC_ContinuousConvMode = DISABLE;
Here is the configuration whether continuous conversion is required.
Continuous conversion ENABLE: It means that you only need to start (trigger) the conversion once, and then you can work continuously without starting (triggering) again.
Single conversion DISABLE: It means that after one conversion is completed, it needs to be started (triggered) again to work.
Trigger mode: ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
The trigger mode is the method used to trigger the ADC conversion. There are timer, external trigger, software trigger, and software trigger is commonly used. There are many trigger modes here, and you can refer to their parameters for details.
Alignment method: ADC_DataAlign = ADC_DataAlign_Right;
Right align: the lower 12 bits of data are valid bits (commonly used);
Align left: the high 12 bits are valid bits of data;
Number of channels: ADC_NbrOfChannel = 3;
This parameter is relatively simple, we define the number of channels to work with.
B. Set the rule group channel:
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_SampleTime_55Cycles5);
We define the conversion order of channel 1 as 1st, the conversion order of channel 2 as 2nd, and the conversion order of channel 3 as 3rd;
ADC_DiscModeChannelCountConfig(ADC1, 1);
ADC_DiscModeCmd(ADC1, ENABLE);
Rule group discontinuous mode configuration. We configure the short sequence to 1, which means that one channel is converted each time it is triggered.
See the reference manual for discontinuous mode.
C. Verification:
ADC_ResetCalibration(ADC1); //Calibration reset
while(ADC_GetResetCalibrationStatus(ADC1)); //Wait for reset to complete
ADC_StartCalibration(ADC1); //Start ADC1 calibration
while(ADC_GetCalibrationStatus(ADC1)); //Wait for calibration to complete
It is recommended to calibrate once each time you power on.
The ADC has a built-in self-calibration mode. Calibration can significantly reduce the accuracy errors caused by variations in the internal capacitor bank. During calibration, an error correction code (digital value) is calculated on each capacitor and this code is used to eliminate the errors introduced on each capacitor in subsequent conversions.
3. ADC acquisition
This function is located under the adc.c file;
After the above configuration is completed, it is the actual data collection process. Since we configure the browsing (loop) mode, single acquisition, that is, we will loop and collect one channel every time we call the trigger function.
VI. Description
The ADC conversion function of STM32 is indeed powerful and relatively complicated. Perhaps the description in the article is not clear enough. If you have any questions, you can follow WeChat and leave a message on WeChat.
Regarding the software engineering examples provided by the author, you can follow me on WeChat and reply “About Engineering” in the conversation box to find descriptions of engineering structure, model modifications, etc.
The above summary is for reference only. Please forgive me if there are any inaccuracies.
Previous article:SPI_FLASH timing description and driver programming
Next article:STM32F10x_ADC three-channel DMA continuous conversion (3 channels, software single trigger)
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- Melexis uses coreless technology to reduce the size of current sensing devices
- NU514-RGBW four-way control constant current linear constant current IC
- Do you know the eight important knowledge points of FPGA design?
- Embedded system construction
- Common usage of MSP430 watchdog and writing method of interrupt function
- MQBoard - MicroPython microframework for managing MQTT
- Design of three-phase UPS for power system
- BT8896A Specification (Zhongke Bluexun)
- What is the amplification factor of this op amp?
- Multisim simulation of infinite gain multi-channel negative feedback (MFB) second-order Butterworth low-pass filter
- Help with "How to select capacitor voltage for VBAT pin of SIM900A?"