Stm32--adc

Source: Internet
Author: User

Stm32--adc


Tenet: The learning of technology is limited and the spirit of sharing is limitless.


First, ADC Indicators

There are 18 channels to measure 16 external and 2 internal signal sources. The A/D conversion of each channel can be performed in a single, continuous, scan or discontinuous mode; the results of the ADC can be left-aligned or right-aligned in a 16-bit data register, and the analog watchdog feature allows the application to detect whether the input voltage exceeds the user-defined high/low threshold value.

For ADC, the most important thing we are interested in is its resolution, conversion time, ADC type, reference voltage range:

(1) Resolution:12-bit resolution. The negative voltage cannot be measured directly, so there is no sign bit, i.e. its minimum quantization unit lsb= vref+/212.

(2) conversion time: The conversion time is programmable. Sampling takes at least 14 ADC clock cycles at a time, while the ADC has a clock frequency of up to 14MHz, which means that it has a minimum sampling time of 1us. Sufficient for the sampling work of the medium and low frequency digital oscilloscope.

(3)ADC type: The STM32 is a successive-comparison ADC.

( 4 ) Reference voltage

The STM32 ADC is not able to measure the negative voltage directly, and its input voltage signal range is: V ref-≤v in≤v ref+. When you need to measure the negative voltage or the voltage signal is out of range, we must first go through the arithmetic circuit to translate or use the resistance divider.

Second, ADC Work Process

The input signal is sent through these channels to the ADC assembly, and the ADC component needs to be triggered to start the conversion, such as Exti external trigger, timer trigger, or software trigger. After the ADC component receives the trigger signal, the signal of the input channel is sampled and the analog-to-digital conversion is performed at the drive of the ADCCLK clock, where the ADCCLK is from the ADC prescaler.

The converted values of the ADC parts are saved to a 16-bit regular channel data register (or injected into the channel data register), which we can read to memory (variable) via CPU instructions or DMA. After A/D conversion, the DMA request can be triggered or the conversion end event of the ADC is triggered. If the analog watchdog is configured and the collected voltage is greater than the threshold value, the watchdog interrupt is triggered.

Third, ADC Collect Data " DMA Mode "

In the STM32, the use of the ADC is often used in the DMA transmission, the ADC peripheral data transferred by DMA to the SRAM, and then processed, or even directly transfer the ADC data to the serial port to send to the host computer. "Interruption efficiency is not enough"

1 , Configuration GPIO Port

After the ADC and DMA are configured, the ADC collects data continuously, and the DMA automatically transfers the data collected by the ADC to the in-memory variable adc_convertedvalue, so the adc_convertedval used in the while loop of the main function UE are real-time values.

/* Enable DMA clock, GPIO clock, and ADC1 clock. The GPIO pin PC1 used by the ADC1 Channel 11 is then configured as an analog input mode, and the analog input must be used as input to the ADC. Each ADC channel corresponds to a gpio pin port, and the GPIO pin can be used to simulate voltage input after it is set to analog input mode. */static void Adc1_gpio_config (void) {  gpio_inittypedef gpio_initstructure;  Rcc_ahbperiphclockcmd (RCC_AHBPERIPH_DMA1, ENABLE); /* Enable DMA clock *  /rcc_apb2periphclockcmd (RCC_APB2PERIPH_ADC1 | RCC_APB2PERIPH_GPIOC, ENABLE); /* Enable ADC and GPIOC clock */  * To configure PC1 bit analog input, input mode without setting rate */  Gpio_initstructure.gpio_pin = gpio_pin_1;  Gpio_initstructure.gpio_mode = Gpio_mode_ain;  Gpio_init (GPIOC, &gpio_initstructure);}


2 , Configuration DMA and the ADC Mode

typedef enum {DISABLE = 0, ENABLE =! DISABLE} functionalstate;typedef struct{  uint32_t adc_mode;  Functionalstate Adc_scanconvmode;  Functionalstate Adc_continuousconvmode;  uint32_t Adc_externaltrigconv;  uint32_t adc_dataalign;  uint8_t Adc_nbrofchannel;} Adc_inittypedef;

(1)adc_mode: for measuring the voltage value after the resistor is divided, the requirement is not high, only one ADC is used Can meet the requirements, so this member is assigned to Adc_mode_independent (standalone mode).

(2)adc_scanconvmode: When there are multiple channels need to collect signals, the ADC can be Configured to scan each channel in a certain order, that is, the values of each channel are collected in turn.

(3)adc_continuousconvmode: Continuous Conversion mode, this mode is opposite to single conversion mode, single conversion mode The ADC stops the conversion only once the data is collected. The continuous conversion mode turns on the next conversion immediately after the last ADC conversion is complete.

(4)adc_externaltrigconv: The ADC needs to start a analog-to-digital conversion after receiving a trigger signal, such as an external interrupt trigger (Exti line), a timer trigger, two external trigger signals that can be triggered using a software control if no external trigger signal is used.

(5)adc_dataalign: data alignment.

(6)adc_nbrofchannel: This member saves the number of channels for ADC data conversion, which can be 1 to 16.

After populating the struct, you can call the peripheral initialization function to initialize it, initialize the ADC using the Adc_init () function, and do not forget to call the Adc_cmd () function to enable the ADC peripheral, using the Adc_dmacmd () function to enable the DMA interface of the ADC.

The DMA configuration section of the/*ADC is similar to the Serial DMA configuration section, and its DMA is configured as a whole: using DMA1 Channel 1, data from the ADC peripheral data registers (Adc1_dr_add RESS) transfer to memory (adc_convertedvalue variable), memory, peripheral address are fixed, each transmission of data size is half word (16 bits), using the DMA loop transfer mode.  */static void Adc1_mode_config (void) {dma_inittypedef dma_initstructure;  Adc_inittypedef adc_initstructure; Dma_deinit (DMA1_CHANNEL1); /* DMA Channel 1 */dma_initstructure.dma_peripheralbaseaddr = adc1_dr_address; ADC Address dma_initstructure.dma_memorybaseaddr = (u32) &ADC_ConvertedValue;  Memory address Dma_initstructure.dma_dir = DMA_DIR_PERIPHERALSRC;  Dma_initstructure.dma_buffersize = 1; Dma_initstructure.dma_peripheralinc = dma_peripheralinc_disable; Peripheral address Fixed dma_initstructure.dma_memoryinc = dma_memoryinc_disable; memory address fixed dma_initstructure.dma_peripheraldatasize = Dma_peripheraldatasize_halfword;  Half word dma_initstructure.dma_memorydatasize = Dma_memorydatasize_halfword; Dma_initstructure.dma_mode = Dma_mode_circular;  Cyclic transmission dma_initstructure.dma_priority = Dma_priority_high; Dma_initstructure.dma_m2m = dma_m2m_disable;  Dma_init (Dma1_channel1, &dma_initstructure); Dma_cmd (Dma1_channel1, ENABLE); /* Enable DMA Channel 1 */adc_initstructure.adc_mode = adc_mode_independent; Standalone ADC mode adc_initstructure.adc_scanconvmode = DISABLE; No scanning mode, scanning mode for multichannel acquisition Adc_initstructure.adc_continuousconvmode = ENABLE; Turn on continuous conversion mode, that is, ADC conversion adc_initstructure.adc_externaltrigconv = Adc_externaltrigconv_none; Do not use external trigger conversion adc_initstructure.adc_dataalign = Adc_dataalign_right; Capture data right-aligned adc_initstructure.adc_nbrofchannel = 1;  Number of channels to convert 1 adc_init (ADC1, &adc_initstructure);  Rcc_adcclkconfig (RCC_PCLK2_DIV8);  /* Configure ADC Clock for PCLK2 of 8, i.e. 9mhz*/adc_regularchannelconfig (ADC1, Adc_channel_11, 1, adc_sampletime_55cycles5); /* Channel 11 for configuration ADC1 is 55. 5 sampling cycles, sequence 1 */Adc_dmacmd (ADC1, ENABLE);  /* Enable ADC1 */Adc_cmd (ADC1, enable);  Adc_resetcalibration (ADC1);  /* Reset Calibration Register */while (Adc_getresetcalibrationstatus (ADC1));  /* Wait for calibration Register reset Complete */adc_startcalibration (ADC1); /* ADC Calibration */WHIle (Adc_getcalibrationstatus (ADC1));  /* Wait for calibration to complete */Adc_softwarestartconvcmd (ADC1, ENABLE); /* The ADC conversion is triggered by using the software because there is no external trigger */}

3 , ADC Conversion Time


The common clock frequency for the PCLK2 is adcclk MHz, and the ADCCLK must be less than 8 MHz, so in this case, the maximum frequency of the PCLK2 will be at the rate of adcclk=9 MHz, which is at the time of one-to-zero. If you want to make the ADC run at the highest frequency of 4 MHz, you can configure the PCLK2 to be in the range of-

The ADC's conversion time is not only related to the ADC's clock, but also to the sampling period. Each of the different ADC channels can be set to a different sampling period.

4 , ADC Self-calibrating

You need to start the ADC's self-calibration before starting the ADC conversion. The ADC has a built-in self-calibrating mode, which significantly reduces the accuracy error due to changes in the internal capacitor banks. During calibration, an error correction code (numeric value) is calculated on each capacitor, which is used to eliminate the error generated on each capacitor in subsequent conversions.

5 , calculate the voltage value

The actual voltage value = ADC Conversion value xlsb;stm32 The accuracy of the ADC is 12 bits, while the ref+ reference voltage value is 3.3V, so the LSB =3.3/212.

Stm32--adc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.