A detailed analysis of a classic STM32 ADC multi-channel conversion

    STM32ADC multi-channel conversion

    Description: The ADC continuously acquires 11 analog signals and transmits them to the memory by DMA. The ADC is configured for sweep and continuous conversion mode with an ADC clock configuration of 12 MHz. The converted data is transferred to the memory by the DMA loop after each conversion. The ADC can continuously acquire N times for averaging. Finally, the result of the final conversion is transmitted through the serial port.

    A detailed analysis of a classic STM32 ADC multi-channel conversion

    The procedure is as follows:

    #include“stm32f10x.h”//This header file includes all the peripheral registers, bits, and memory map definitions of STM32F10x.

    #include"eval.h"//Head file (including serial port, button, LED function declaration)

    #include"SysTIckDelay.h"

    #include"UART_INTERFACE.h"

    #include

    #defineN50//50 times per channel

    #defineM12// is 12 channels

    vu16AD_Value[N][M];// is used to store the ADC conversion result, which is also the target address of the DMA

    vu16After_filter[M];// is used to store the results after averaging

    inTI;

    voidGPIO_ConfiguraTIon(void)

    {

    GPIO_InitTypeDefGPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//The multiplexed push-pull output is used because the USART1 pin is multiplexed to the GPIO port.

    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_Init(GPIOA,&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOA,&GPIO_InitStructure);

    //PA0/1/2 as an analog channel input pin

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//analog input pin

    GPIO_Init(GPIOA,&GPIO_InitStructure);

    //PB0/1 as an analog channel input pin

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//analog input pin

    GPIO_Init(GPIOB,&GPIO_InitStructure);

    //PC0/1/2/3/4/5 as analog channel input pin

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//analog input pin

    GPIO_Init(GPIOC,&GPIO_InitStructure);

    }

    }

    voidRCC_Configuration(void)

    {

    ErrorStatusHSEStartUpStatus;

    RCC_DeInit();//RCC system reset

    RCC_HSEConfig(RCC_HSE_ON);//Open HSE

    HSEStartUpStatus=RCC_WaitForHSEStartUp();//Wait for HSE ready

    If(HSEStartUpStatus==SUCCESS)

    {

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//EnablePrefetchBuffer

    FLASH_SetLatency(FLASH_Latency_2);//Set2Latencycycles

    RCC_HCLKConfig(RCC_SYSCLK_Div1);//AHBclock=SYSCLK

    RCC_PCLK2Config(RCC_HCLK_Div1);//APB2clock=HCLK

    RCC_PCLK1Config(RCC_HCLK_Div2);//APB1clock=HCLK/2

    RCC_PLLConfig (RCC_PLLSource_HSE_Div1, RCC_PLLMul_6); / / PLLCLK = 12MHz * 6 = 72MHz

    RCC_PLLCmd(ENABLE);//EnablePLL

    While(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//WaittillPLLisready

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //SelectPLLassystemclocksource

    While(RCC_GetSYSCLKSource()!=0x08);//WaittillPLLisusedassystemclocksource

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB

    |RCC_APB2Periph_GPIOC|RCC_APB2Periph_ADC1|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);//Enable ADC1 channel clock, each pin clock

    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12, the maximum ADC time cannot exceed 14M

    RCC_AHBPeriphClockCmd (RCC_AHBPeriph_DMA1, ENABLE); / / enable DMA transfer

    }

    }

    voidADC1_Configuration(void)

    {

    ADC_InitTypeDefADC_InitStructure;

    ADC_DeInit(ADC1);//Reset all registers of peripheral ADC1 to their default values

    ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//ADC working mode: ADC1 and ADC2 work in standalone mode

    ADC_InitStructure.ADC_ScanConvMode=ENABLE;//Analog conversion works in scan mode

    ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;//Analog conversion works in continuous conversion mode

    ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//External Trigger Conversion Off

    ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//ADC data right aligned

    ADC_InitStructure.ADC_NbrOfChannel=M;//Number of ADC channels that perform regular rule conversion

    ADC_Init(ADC1,&ADC_InitStructure);//Register the peripheral ADCx registers according to the parameters specified in ADC_InitStruct

    / / Set the rule group channel of the specified ADC, set their conversion order and sampling time

    //ADC1, ADC channel x, the regular sampling order value is y, and the sampling time is 239.5 cycles.

    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 5, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 6, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 7, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 8, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 9, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 10, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 11, ADC_SampleTime_239Cycles5);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 12, ADC_SampleTime_239Cycles5);

    / / Turn on the DMA support of the ADC (to achieve the DMA function, you also need to independently configure the DMA channel and other parameters)

    ADC_DMACmd (ADC1, ENABLE);

    ADC_Cmd(ADC1,ENABLE);//Enable the specified ADC1

    ADC_ResetCalibration(ADC1);//Reset the specified ADC1 calibration register

    While(ADC_GetResetCalibrationStatus(ADC1));//Get the status of the ADC1 reset calibration register, set the status and wait

    ADC_StartCalibration(ADC1);//Start specifying the calibration status of ADC1

    While(ADC_GetCalibrationStatus(ADC1));//Get the calibration program of the specified ADC1, wait for the setting state

    }

    voidDMA_Configuration(void)

    {

    DMA_InitTypeDefDMA_InitStructure;

    DMA_DeInit(DMA1_Channel1);//Reset the DMA channel 1 register to the default value

    DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&ADC1-"DR;//DMA peripheral ADC base address

    DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&AD_Value;//DMA memory base address

    DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC; / / memory as a destination for data transfer

    DMA_InitStructure.DMA_BufferSize=N*M;//The size of the DMA buffer of the DMA channel

    DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//The peripheral address register is unchanged

    DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;//Memory address register increment

    DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;//Data width is 16 bits

    DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;//Data width is 16 bits

    DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;//Works in loop cache mode

    DMA_InitStructure.DMA_Priority=DMA_Priority_High;//DMA channel x has high priority

    DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//DMA channel x is not set to memory to memory transfer

    DMA_Init(DMA1_Channel1, & DMA_InitStructure); // Initialize the DMA channel according to the parameters specified in DMA_InitStruct

    }

    Marine Battery System

    Feature:
    1.Superior uniformity and EV grade safefty LFP battery ;
    2.Customized modular and large-scale ESS solution;
    3.Reliable safety design and remote real-time monitoring;
    4.High cost effective and short delivery duration.

    mwh ,ess container, bess, commercial battery,battery container,mega,megawatt,Marine battery

    Shenzhen Enershare Technology Co.,Ltd , https://www.enersharepower.com

    Previous Post: China and South Korea will carry out joint research on connecting China and South Korea's power network
    Next Post: When traditional industries meet smart manufacturing: Can the textile industry come back to life?
    Home
    Recent Posts
    • Is the system time wrong? Easy way to make Windo…
    • The state issued a warning to the "new nati…
    • Simple household electronic thermometer
    • The application structure change of the expanded…
    • How smart home terminals are safer, more comfort…
    • With Unreal Engine and AR technology, anyone can…
    • Intelligent sweeping robot working principle Swe…
    • Cordless telephone microcomputer control circuit…
    • Qualcomm and several Chinese technology vendors …
    • How to prevent high-incidence seasons? These are…
    • What problems are companies facing in their digi…
    • The program starts to find that the port is occu…
    • Science and technology early news: Another year …
    • Xilinx Power Estimator (XPE) Demo
    • How does Defiance Technology do FaceID?
    • BT EE plans to launch its first 5G trial network…
    • Seize the smart street light market Lite-On Tech…
    • Photovoltaic industry, "the most stringent …
    • Ningde times won the IPO approval which partners…
    • Detailed pictures introduce common materials for…