STM32ADC Use method Analysis

Source: Internet
Author: User

Because the project needs to use to STM32 ADC function, although the use of ADC is not unfamiliar, but the first Contact Stm32 ADC function is still a kind of feel, mainly because the STM32ADC design is more complex, for a while the relevant terminology is unfamiliar, In addition, the functions involved in the firmware library are categorized but still feel function more, difficult to grasp quickly, now will be a personal understanding to write, so that everyone progress together.

Features of Stm32 ADC

The first thing to know is to grasp the whole, ADC is the same truth, STM32 ADC Peripherals provide a very good function, although a large part of it may not be used in the project, but to deepen their understanding of the rational use of its resources is also very necessary.

The following is an example of the stm32l151 used in the project, with the other types being similar. Reference RM0038 Reference Manual (Cd00240193.pdf)

First look at the original:

12-bit, 10-bit, 8-bit or 6-bit configurable resolution
interrupt generation at the end of regular conversions, end of injected conversions, and in case of analog watchdog or O Verrun events (for regular conversions)

Interrupts when rule conversion ends, injection conversion ends, and simulated watchdog overflow and overrun events (rule conversion mode) occur

single and continuous conversion modes//single and continuous conversion mode
scan mode for automatic conversions in a fully programmable order//support for the completion of programmable scanning mode in automatic conversion
programmable data alignment with IN-BUILT data coherency//Alignment programmable
programmable and individual sampling time for each ADC channel//can set up separate sampling times for each channel
external trigger option with configurable edge detection for both regular and injected
Conversions//external trigger mode supports configurable edge triggering for both rule and injection conversion modes
discontinuous mode//discontinuous modes
ADC conversion time:1μs at full speed (ADC clocked in) down to 4μs at low speed (ADC clocked at 4 MHz), Indepe Ndent of the APB clock//minimum sampling time
automatic Power-up/power-down to reduce the power consumption//automatic up and down to save energy
ADC Supply Requirements:
–2.4 V to 3.6 V in full speed or with reference zooming (vref+ < Vdda)
–down to 1.8 V at slower speeds//operating voltage range
ADC Input range:vref–≤vin≤vref+
Automatic programmable hardware delay insertion between
DMA request generation during regular channel//rule conversion supports DMA

From an official description of the powerful features that STM32ADC provides, and some of the features of the work, how can they be used?

First, we need to define two concepts

1. Rule Group and injection group

It's important to differentiate between the two because it relates to the actual ADC capabilities, and here's an introduction that feels good, recommended to everyone, (http://wenku.baidu.com/link?url= BLWVQAXELZQJCBVMUQ3QSH9N-J3A8LNLHTTWO01YVST2Z8UCN4TJPYHGERVR5G925GQJJRVSHYHRXDCSTUDVNJZWSDYCFTMRXLQ6Z2OQWS7) My understanding is that the rules group is to follow a Fixed rule execution (mostly for large-scale continuous conversion) and injection group performs a single conversion, only when we need it. In our project, we will read some of the temperature sensor on the ADC, so we use the injection group for its reading, and we also need to read the ADC data of several channels periodically for several times, so we choose the method of the rule group for its operation.

Single/continuous and scan mode

The single time corresponds to the continuous, this is easy to understand, that is, only once a single conversion, continuous mode after triggering the conversion will continue to be converted.

Scanning mode, however, indicates that the next channel conversion (which needs to be specified in the order of the channel conversion sequence) immediately after the end of a channel transition is meaningful in either a single or continuous mode.

Understanding these can be a preliminary programming, taking into account the operation of the register to be more familiar with the register, and the project duration is more tense, so the firmware library method.

2.stm32 Firmware Library Brief Introduction

The V1.2 version of the firmware Library (stsw-stm32077) is used in the project, and a large number of sample programs are integrated into the firmware library, which is very helpful for quick start.


In view of the functions provided in stm32l1xx_adc.h:

/* Function used to set the ADC configuration to the default reset state *****/
void Adc_deinit (adc_typedef* adcx);


/* Initialization and Configuration functions *********************************/
void Adc_init (adc_typedef* adcx, adc_inittypedef* adc_initstruct);
void Adc_structinit (adc_inittypedef* adc_initstruct);
void Adc_commoninit (adc_commoninittypedef* adc_commoninitstruct);
void Adc_commonstructinit (adc_commoninittypedef* adc_commoninitstruct);
void Adc_cmd (adc_typedef* adcx, functionalstate newstate);
void Adc_bankselection (adc_typedef* adcx, uint8_t Adc_bank);


/* Power Saving functions *****************************************************/
void Adc_powerdowncmd (adc_typedef* adcx, uint32_t adc_powerdown, functionalstate newstate);
void Adc_delayselectionconfig (adc_typedef* adcx, uint8_t adc_delaylength);


/* Analog watchdog configuration functions ************************************/
void Adc_analogwatchdogcmd (adc_typedef* adcx, uint32_t adc_analogwatchdog);
void Adc_analogwatchdogthresholdsconfig (adc_typedef* adcx, uint16_t highthreshold,uint16_t LowThreshold);
void Adc_analogwatchdogsinglechannelconfig (adc_typedef* adcx, uint8_t adc_channel);


/* Temperature Sensor & Vrefint (voltage Reference Internal) management function * *
void Adc_tempsensorvrefintcmd (Functionalstate newstate);


/* Regular channels Configuration Functions ***********************************/
void Adc_regularchannelconfig (adc_typedef* adcx, uint8_t adc_channel, uint8_t Rank, uint8_t adc_sampletime);
void Adc_softwarestartconv (adc_typedef* adcx);
FlagStatus adc_getsoftwarestartconvstatus (adc_typedef* adcx);
void Adc_eoconeachregularchannelcmd (adc_typedef* adcx, functionalstate newstate);
void Adc_continuousmodecmd (adc_typedef* adcx, functionalstate newstate);
void Adc_discmodechannelcountconfig (adc_typedef* adcx, uint8_t number);
void Adc_discmodecmd (adc_typedef* adcx, functionalstate newstate);
uint16_t Adc_getconversionvalue (adc_typedef* adcx);


/* Regular channels DMA Configuration functions *******************************/
void Adc_dmacmd (adc_typedef* adcx, functionalstate newstate);
void Adc_dmarequestafterlasttransfercmd (adc_typedef* adcx, functionalstate newstate);


/* Injected channels Configuration functions **********************************/
void Adc_injectedchannelconfig (adc_typedef* adcx, uint8_t adc_channel, uint8_t Rank, uint8_t adc_sampletime);
void Adc_injectedsequencerlengthconfig (adc_typedef* adcx, uint8_t Length);
void Adc_setinjectedoffset (adc_typedef* adcx, uint8_t Adc_injectedchannel, uint16_t Offset);
void Adc_externaltriginjectedconvconfig (adc_typedef* adcx, uint32_t adc_externaltriginjecconv);
void Adc_externaltriginjectedconvedgeconfig (adc_typedef* adcx, uint32_t Adc_externaltriginjecconvedge);
void Adc_softwarestartinjectedconv (adc_typedef* adcx);
FlagStatus adc_getsoftwarestartinjectedconvcmdstatus (adc_typedef* adcx);
void Adc_autoinjectedconvcmd (adc_typedef* adcx, functionalstate newstate);
void Adc_injecteddiscmodecmd (adc_typedef* adcx, functionalstate newstate);
uint16_t Adc_getinjectedconversionvalue (adc_typedef* adcx, uint8_t adc_injectedchannel);


/* Interrupts and flags management functions **********************************/
void Adc_itconfig (adc_typedef* adcx, uint16_t adc_it, functionalstate newstate);
FlagStatus Adc_getflagstatus (adc_typedef* adcx, uint16_t adc_flag);
void Adc_clearflag (adc_typedef* adcx, uint16_t adc_flag);
Itstatus Adc_getitstatus (adc_typedef* adcx, uint16_t adc_it);
void Adc_clearitpendingbit (adc_typedef* adcx, uint16_t adc_it);

/* ******************* Flags management functions **********************************/

FlagStatus Adc_getflagstatus (adc_typedef* adcx, uint16_t adc_flag);

void Adc_clearflag (adc_typedef* adcx, uint16_t adc_flag);

From the above different colors and annotations can be seen function by function into several parts of each other independent of each other (except flags management) Understanding these pairs is very helpful to us in using library function programming.


Ii. Initialization of Stm32 ADC

The initialization of external peripherals includes clock, gpio function (reuse type), peripheral function setting, interrupt configuration and other steps, the project refers to the firmware Library sample program initialization section, the following example program (Adc1_freeze) initialization as an example to explain.

1. Find Adc_config

void Adc_config (void)
{
* Enable the HSI (16Mhz) * *
Rcc_hsicmd (ENABLE);//clock source selection and configuration, see clock tree

/* Check that HSI-oscillator is ready * *
while (Rcc_getflagstatus (rcc_flag_hsirdy) = = RESET);

/* Configure RV3 Input voltage * *


* Enable the GPIOF Clock * *
Rcc_ahbperiphclockcmd (IDD_MEASUREMENT_GPIO_CLK, ENABLE);

/* Configure pf.10 (ADC Channel31) or pa.12 (ADC channel) in analog mode * *

Gpio part, the more messy, to the practical Gpio are configured to simulate the input mode
Gpio_initstructure.gpio_pin = gpio_pin_x;
Gpio_init (Idd_measurement_gpio, &gpio_initstructure);

/* Configure the IDD input * *

/* Configure pf.11 (ADC Channel 1b) or pa.05 (ADC Channel 5) in analog mode * *
Gpio_initstructure.gpio_pin = gpio_pin_y;
Gpio_initstructure.gpio_mode = Gpio_mode_an;
GPIO_INITSTRUCTURE.GPIO_PUPD = Gpio_pupd_nopull;
Gpio_init (Idd_measurement_gpio, &gpio_initstructure);


/* ADC1 Configuration------------------------------------------------------* *
Rcc_apb2periphclockcmd (RCC_APB2PERIPH_ADC1, ENABLE);//adc1 clock, suggest to put together with the above clock


/* ADC1 Deinit * *
Adc_deinit (ADC1);

#ifdef Use_stm32l152d_eval
/* Select ADC Bank Channel * *
Adc_bankselection (ADC1, Adc_bank_b);
#endif

/* ADC1 Configuration of CHANNEL18/31 and channel5/1b:continuous mode, external trigger (TIM2) *///ADC initialization section, select Configuration as needed
Adc_initstructure.adc_resolution = adc_resolution_12b;//12bit Resolution
Adc_initstructure.adc_scanconvmode = enable;//Scanning mode Open
Adc_initstructure.adc_continuousconvmode = enable;//Continuous Conversion
Adc_initstructure.adc_externaltrigconvedge = adc_externaltrigconvedge_falling;//rise along the trigger
Adc_initstructure.adc_externaltrigconv = adc_externaltrigconv_t2_trgo;//external trigger, matching top configuration

The project uses a software trigger, triggering method: No different from the example
Adc_initstructure.adc_dataalign = adc_dataalign_right;//right-aligned
adc_initstructure.adc_nbrofconversion = number of 2;//conversions (scan-specific) item set to 6 (maximum 16), continuous 6-channel data
Adc_init (ADC1, &adc_initstructure);




/* ADC1 Regular Channel Config *///rule Channel sequential configuration, the four channels used in the project are set to 1-4 in order
Adc_regularchannelconfig (ADC1, Idd_measurement_adc_channel, 1, adc_sampletime_192cycles);
Adc_regularchannelconfig (ADC1, Rv3_measurement_adc_channel, 2, adc_sampletime_192cycles);

There is no injection channel, the injection channel needs to be configured to use the channel, so that each channel corresponds to an injection channel data register (number 4), so the maximum number of channels can be selected as 4, configured in the same way, simply replace the configuration function for Adc_ Injectedchannelconfig can be. Project set up 4 injection channels


In addition, the sample does not use interrupts and DMA, so the sampled data is easily lost because the rule channel is common to a data register, so you need to read the data in one conversion cycle, and then read the data from the next channel.

* Enables the ADC1 power down during Delay///energy saving mode, optional
Adc_powerdowncmd (ADC1, Adc_powerdown_idle_delay, ENABLE);

/* Delay until the converted data has been read * * *
Adc_delayselectionconfig (ADC1, Adc_delaylength_freeze);


/* Enable ADC1 *///to enable peripheral power and peripherals to begin work
Adc_cmd (ADC1, ENABLE);

/* Wait until the ADC1 is ready *///waits for peripheral stabilization work Adc_getflagstatus
while (Adc_getflagstatus (ADC1, adc_flag_adons) = RESET)
{
}
}


Initialization partially completed

Third, the use of external set

The use of peripherals does not depend on the choice of a reasonable way to read the channel data.

The first is to read the injected channel data, the injection channel needs to start the conversion, the function is injected into the library function group in the group of functions (except flag)

Take injection group 1 as an example

int Adc_injected_read (void)

{

int ad_val;

Adc_softwarestartinjectedconv (ADC1);//software triggering, initiating conversion

while (RESET ==adc_getflagstatus (ADC1,ADC_FLAG_JEOC))//Waiting for injection group conversion complete

Ad_val = Adc_getinjectedconversionvalue (ADC1, Adc_injectedchannel_1); Gets the injected channel 1 data corresponding to the ADC channel in the initialization configuration

return ad_val; Data not being processed further

}


The rule group uses DMA transfers and is set up in initialization.

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.