Stm32 ADC Voltage Test

Source: Internet
Author: User

1. stm32f103 ADC

In this example, the pa1 pin of the stm32f103 chip is used to test the analog input voltage value.

Article 31st, pin definition:

We know that pa1 uses adc1 Channel 1.

The Clock Tree is shown in the following figure:

Adc1 can be divided into 2, 4, 6, and 8, and the ADC input clock cannot exceed 14 MHz (see the stm32 reference manual rm0008 chapter 11th ADC ).

// Initialize the ADC // Here we only use the rule Channel as an example // We will enable the channel 0 ~ by default ~ 3 void adc_init (void) {adc_inittypedef adc_initstructure; gpio_inittypedef gpio_initstructure; enabled (enabled | enabled, enable); // enable adc1 channel clock rcc_adcclkconfig (Enabled ); // set the ADC frequency factor 6 72 m/6 = 12. The maximum ADC time cannot exceed 14 m // pa1 is used as the input pin of the analog channel gpio_initstructure.gpio_pin = gpio_pin_1; gpio_initstructure.gpio_mode = gpio; // simulate the Input Pin gpio_init (gpioa, & gpio_initstructure); adc_deinit (adc1); // reset adc1 and reset all registers of the peripheral adc1 to the default value adc_initstructure.adc_mode = adc_mode_independent; // ADC working mode: adc1 and adc2 work in standalone mode. adc_initstructure.adc_scanconvmode = Disable; // The module conversion works in single channel mode. adc_initstructure.adc_continuousconvmode = Disable; // modulus conversion works in a single conversion mode. Convert = adc_externaltrigconv_none; // The conversion is triggered by the software rather than the external start adc_initstructure.adc_dataalign = adc_dataalign_right; // The number of adc_init (adc1, & adc_initstructure) of the ADC channel for rule conversion in sequence; // initialize the adc_cmd (adc1, enable) Register of the peripheral adcx according to the parameter specified in adc_initstruct ); // enable the specified adc1 adc_resetcalibration (adc1); // enable the reset calibration while (adc_getresetcalibrationstatus (adc1); // wait until the reset calibration ends adc_startcalibration (adc1 ); // enable the ad calibration while (adc_getcalibrationstatus (adc1); // wait until the calibration ends adc_softwarestartconvcmd (adc1, enable); // enable the software conversion startup function of the specified adc1}
// Obtain the ADC value // CH: the channel value is 0 ~ 32010get_adc (u8 ch) {// set the channel of the specified ADC rule group, a sequence, the sampling time adc_regularchannelconfig (adc1, CH, 1, adc_sampletime_239cycles5); // adc1, ADC channel, the sampling time is 239.5 period adc_softwarestartconvcmd (adc1, enable); // enable the software conversion startup function of the specified adc1 while (! Adc_getflagstatus (adc1, adc_flag_eoc); // return adc_getconversionvalue (adc1) after the end of the conversion; // return the Conversion Result of the last adc1 rule group} 2010get_adc_average (u8 CH, u8 times) {u32 temp_val = 0; u8 t; for (t = 0; t <times; t ++) {temp_val + = get_adc (CH); delay_ms (5 );} return temp_val/times;} void adctask (void) {float vol, adcx; while (1) {adcx = get_adc_average (adc_channel_1, 10 ); // ADC value Vol = (float) adcx * (3300/4096); // voltage value (MV) delay_ms (200 );}}

2. stm32f407 ADC

In this example, the input voltage value is simulated using the pf9 pin of the stm32f407 chip.

Source Document: Page 43rd of stm32f4x7-datasheet.pdf, pin definition:

View STM32F4x7-Reference manual.pdf page 51st, memory ing:

Click the link to view the ADC register ing:

Also view the data register offset:

Know the data register address: # define adc3_dr_address (uint32_t) 0x4001224c)

View STM32F4x7-Reference manualtracing page 165th dma2 request mapping:

We know that dma2 stream0 Channel 2 is used to map to adc3.

#define ADC3_DR_ADDRESS    ((uint32_t)0x4001224C)__IO uint16_t ADC3ConvertedValue = 0;__IO uint32_t ADC3ConvertedVoltage = 0;//ADC3 channel07 with DMA configurationvoid ADC3_CH7_DMA_Config(void){  ADC_InitTypeDef       ADC_InitStructure;  ADC_CommonInitTypeDef ADC_CommonInitStructure;  DMA_InitTypeDef       DMA_InitStructure;  GPIO_InitTypeDef      GPIO_InitStructure;  /* Enable ADC3, DMA2 and GPIO clocks ****************************************/  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOF, ENABLE);  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);  /* DMA2 Stream0 channel2 configuration **************************************/  DMA_InitStructure.DMA_Channel = DMA_Channel_2;    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;  DMA_InitStructure.DMA_BufferSize = 1;  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;  DMA_InitStructure.DMA_Priority = DMA_Priority_High;  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;           DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;  DMA_Init(DMA2_Stream0, &DMA_InitStructure);  DMA_Cmd(DMA2_Stream0, ENABLE);  /* Configure ADC3 Channel7 pin as analog input ******************************/  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;  GPIO_Init(GPIOF, &GPIO_InitStructure);  /* ADC Common Init **********************************************************/  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;  ADC_CommonInit(&ADC_CommonInitStructure);  /* ADC3 Init ****************************************************************/  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;  ADC_InitStructure.ADC_ScanConvMode = DISABLE;  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  ADC_InitStructure.ADC_NbrOfConversion = 1;  ADC_Init(ADC3, &ADC_InitStructure);  /* ADC3 regular channel7 configuration *************************************/  ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);  /* Enable ADC3 DMA */  ADC_DMACmd(ADC3, ENABLE);  /* Enable ADC3 */  ADC_Cmd(ADC3, ENABLE);}int main(void){  /* ADC3 configuration *******************************************************/  /*  - Enable peripheral clocks                                              */  /*  - DMA2_Stream0 channel2 configuration                                   */  /*  - Configure ADC Channel7 pin as analog input                             */  /*  - Configure ADC3 Channel7                                               */  ADC3_CH7_DMA_Config();  /* Start ADC3 Software Conversion */   ADC_SoftwareStartConv(ADC3);  while (1)  {    ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
    delay_ms(200);  }}

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.