关于STM32中ADC多通道连续采样的配置

一、理论基础


我这次要做的是对六个通道进行循环采样,主要理论基础根据《STM32中文参考手册》查询。在这里写下我自己的一些理解。

1.1 通道选择

这里有规则通道和注入通道之分,规则通道类似常规转换,注入通道类似于中断。
这里我要进行的是六个通道的循环采样,所以配置规则通道。

“每个组包含一个转换序列,该序列可按任意顺序在任意通道上完成。例如,可按以下顺序对序列进行转换:ADC_IN3、ADC_IN8、ADC_IN2、ADC_IN2、ADC_IN0、ADC_IN2、ADC_IN2、ADC_IN15。“

讲在对规则通道进行配置时可以选择顺序。一个规则组最多可进行16个通道转换。

1.2 转换模式

在单次转换模式下,ADC 执行一次转换。
在连续转换模式下,ADC 结束一个转换后立即启动一个新的转换。
我这里进行的是循环扫描,所以选择连续转换模式。

1.3 扫描模式

此模式用于扫描一组模拟通道,这里有一个问题需要注意,我用黑体标出:

由于规则通道组只有一个数据寄存器ADC_DR ,因此,对于多个规则通道的转换,使用 DMA 非常有
帮助。这样可以避免丢失在下一次写入之前还未被读出的 ADC_DR 寄存器中的数据。

到这里理论基础已经足够了,证明了可以写出适用的程序

二、代码组织过程


2.1 硬件资源与配置过程

首先列出我的硬件资源

PA2---------ADC123 IN2          µçѹ
PA3---------ADC123 IN3          µçÁ÷
PA6---------ADC12  IN6          µç»ú1
PA7---------ADC12  IN7          µç»ú2
PC4---------ADC12  IN14         µç»ú3
PC5---------ADC12  IN15         µç»ú4

&keil上面的注释汉字编码格式与其他都不兼容,于是注释粘贴过来会乱码,只要不影响阅读我就懒得改了….
接下来根据STM32官方库文件中配置方法配置

===============================================================================
                     ##### How to use this driver #####
===============================================================================

这里介绍对adc库文件配置,配置过程根据介绍来

[..]
    (#) Enable the ADC interface clock using 
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADCx, ENABLE); 

    (#) ADC pins configuration
         (++) Enable the clock for the ADC GPIOs using the following function:
             RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);   
         (++) Configure these ADC pins in analog mode using GPIO_Init();  

     (#) Configure the ADC Prescaler, conversion resolution and data 
         alignment using the ADC_Init() function.
     (#) Activate the ADC peripheral using ADC_Cmd() function.

     *** Regular channels group configuration ***

常规配置过程:首先使能ADC时钟,然后使能引脚时钟,然后使能引脚,再配置ADC参数,最后执行ADC。

接下来配置多通道ADC方法

[..]    
       (+) To configure the ADC regular channels group features, use 
           ADC_Init() and ADC_RegularChannelConfig() functions.
       (+) To activate the continuous mode, use the ADC_continuousModeCmd()
           function.
       (+) To configurate and activate the Discontinuous mode, use the 
           ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.
       (+) To read the ADC converted values, use the ADC_GetConversionValue()
           function.

     *** Multi mode ADCs Regular channels configuration ***

这里涉及多通道配置的函数有两个函数ADC_Init()和ADC_RegularChannelConfig(),

To activate the continuous mode, use the ADC_continuousModeCmd() function.

使能连续采样模式,需要用到ADC_continuousModeCmd()配置,具体这个函数用法后面再讲。再后面的介绍是关于注入组的,与这里无关,就不贴了。

总结一下,配置ADC需要配置以下几个结构体

    GPIO_InitTypeDef GPIO_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    ADC_InitTypeDef ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

GPIO引脚配置,ADC公共配置,ADC单独配置,DMA配置(多通道连续采样用的是同一个寄存器,用DMA把数据及时传出),NVIC中断配置,这里配置DMA数据传输中断,用于对取值进行处理。

2.2 ADC_CommonInitTypeDef配置:

GPIO配置比较简单,不作解释,ADC_CommonInitTypeDef 配置首先看其结构体定义:

typedef struct 
{
  uint32_t ADC_Mode;                      /*!< Configures the ADC to operate in 
                                               independent or multi mode. 
                                               This parameter can be a value of @ref ADC_Common_mode */                                              
  uint32_t ADC_Prescaler;                 /*!< Select the frequency of the clock 
                                               to the ADC. The clock is common for all the ADCs.
                                               This parameter can be a value of @ref ADC_Prescaler */
  uint32_t ADC_DMAAccessMode;             /*!< Configures the Direct memory access 
                                              mode for multi ADC mode.
                                               This parameter can be a value of 
                                               @ref ADC_Direct_memory_access_mode_for_multi_mode */
  uint32_t ADC_TwoSamplingDelay;          /*!< Configures the Delay between 2 sampling phases.
                                               This parameter can be a value of 
                                               @ref ADC_delay_between_2_sampling_phases */

}ADC_CommonInitTypeDef;

ADC_Mode 配置ADC工作模式,是单独工作还是协同工作,协同工作时两个或三个ADC同时转换,可以加速转换。这里我只需要一个ADC独立工作即可。
ADC_Prescaler配置分频参数,ADC在APB2上,时钟是84MHz。在工作中,ADC时钟最好不要超过36MHz,太快会影响精度。
有以下几个选项

#define ADC_Prescaler_Div2                         ((uint32_t)0x00000000)
#define ADC_Prescaler_Div4                         ((uint32_t)0x00010000)
#define ADC_Prescaler_Div6                         ((uint32_t)0x00020000)
#define ADC_Prescaler_Div8                         ((uint32_t)0x00030000)

ADC_DMAAccessMode用于DMA传输模式配置,有下面几个选项

#define ADC_DMAAccessMode_Disabled      ((uint32_t)0x00000000)     /* DMA mode disabled */
#define ADC_DMAAccessMode_1             ((uint32_t)0x00004000)     /* DMA mode 1 enabled (2 / 3 half-words one by one - 1 then 2 then 3)*/
#define ADC_DMAAccessMode_2             ((uint32_t)0x00008000)     /* DMA mode 2 enabled (2 / 3 half-words by pairs - 2&1 then 1&3 then 3&2)*/
#define ADC_DMAAccessMode_3             ((uint32_t)0x0000C000)     /* DMA mode 3 enabled (2 / 3 bytes by pairs - 2&1 then 1&3 then 3&2) */

看注释,ADC_DMAAccessMode_Disabled为不使用ADC,ADC_DMAAccessMode_1用于DMA12位精度采样按顺序传输,ADC_DMAAccessMode_2用于DMA12位精度下,两个ADC协同工作时的传输,这样做加快了转换速度。ADC_DMAAccessMode_3与2差不多,但是精度变为6位了,转换速度更快但精度变低。

ADC_TwoSamplingDelay用于配置两次采样间时间间隔。

#define ADC_TwoSamplingDelay_5Cycles               ((uint32_t)0x00000000)
#define ADC_TwoSamplingDelay_6Cycles               ((uint32_t)0x00000100)
#define ADC_TwoSamplingDelay_7Cycles               ((uint32_t)0x00000200)
#define ADC_TwoSamplingDelay_8Cycles               ((uint32_t)0x00000300)
#define ADC_TwoSamplingDelay_9Cycles               ((uint32_t)0x00000400)
#define ADC_TwoSamplingDelay_10Cycles              ((uint32_t)0x00000500)
#define ADC_TwoSamplingDelay_11Cycles              ((uint32_t)0x00000600)
#define ADC_TwoSamplingDelay_12Cycles              ((uint32_t)0x00000700)
#define ADC_TwoSamplingDelay_13Cycles              ((uint32_t)0x00000800)
#define ADC_TwoSamplingDelay_14Cycles              ((uint32_t)0x00000900)
#define ADC_TwoSamplingDelay_15Cycles              ((uint32_t)0x00000A00)
#define ADC_TwoSamplingDelay_16Cycles              ((uint32_t)0x00000B00)
#define ADC_TwoSamplingDelay_17Cycles              ((uint32_t)0x00000C00)
#define ADC_TwoSamplingDelay_18Cycles              ((uint32_t)0x00000D00)
#define ADC_TwoSamplingDelay_19Cycles              ((uint32_t)0x00000E00)
#define ADC_TwoSamplingDelay_20Cycles              ((uint32_t)0x00000F00)

这里的单位cycle根据时钟分频后数值计算。

2.3 ADC_InitTypeDef 结构体配置

首先看结构体定义

typedef struct
{
  uint32_t ADC_Resolution;                /*!< Configures the ADC resolution dual mode. 
                                               This parameter can be a value of @ref ADC_resolution */                                   
  FunctionalState ADC_ScanConvMode;       /*!< Specifies whether the conversion 
                                               is performed in Scan (multichannels) 
                                               or Single (one channel) mode.
                                               This parameter can be set to ENABLE or DISABLE */ 
  FunctionalState ADC_ContinuousConvMode; /*!< Specifies whether the conversion 
                                               is performed in Continuous or Single mode.
                                               This parameter can be set to ENABLE or DISABLE. */
  uint32_t ADC_ExternalTrigConvEdge;      /*!< Select the external trigger edge and
                                               enable the trigger of a regular group. 
                                               This parameter can be a value of 
                                               @ref ADC_external_trigger_edge_for_regular_channels_conversion */
  uint32_t ADC_ExternalTrigConv;          /*!< Select the external event used to trigger 
                                               the start of conversion of a regular group.
                                               This parameter can be a value of 
                                               @ref ADC_extrenal_trigger_sources_for_regular_channels_conversion */
  uint32_t ADC_DataAlign;                 /*!< Specifies whether the ADC data  alignment
                                               is left or right. This parameter can be 
                                               a value of @ref ADC_data_align */
  uint8_t  ADC_NbrOfConversion;           /*!< Specifies the number of ADC conversions
                                               that will be done using the sequencer for
                                               regular channel group.
                                               This parameter must range from 1 to 16. */
}ADC_InitTypeDef;

ADC_Resolution配置采样精度,如下选项:

#define ADC_Resolution_12b                         ((uint32_t)0x00000000)
#define ADC_Resolution_10b                         ((uint32_t)0x01000000)
#define ADC_Resolution_8b                          ((uint32_t)0x02000000)
#define ADC_Resolution_6b                          ((uint32_t)0x03000000)

分别是12bit、10bit、8bit、6bit

ADC_ScanConvMode配置是否为扫描模式,我需要对多通道转换,自然要开启扫描模式,填写ENABLE

ADC_ExternalTrigConvEdge用于配置外部触发的上升沿还是下降沿使能选项如下:

#define ADC_ExternalTrigConvEdge_None          ((uint32_t)0x00000000)
#define ADC_ExternalTrigConvEdge_Rising        ((uint32_t)0x10000000)
#define ADC_ExternalTrigConvEdge_Falling       ((uint32_t)0x20000000)
#define ADC_ExternalTrigConvEdge_RisingFalling ((uint32_t)0x30000000)

上升沿,下降沿或者双沿。

ADC_ExternalTrigConv配置外部触发连续转换。

ADC_DataAlign配置数据对齐是左对齐还是右对齐

ADC_NbrOfConversion配置规则采样中的通道个数

这里完成了对ADC的配置,接下来还要对采样通道顺序配置

void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)

用这个函数,Rank是优先级,根据优先级排序得到采样顺序。

2.4 DMA配置

ADC1映射的是DMA2_Stream0,具体配置方法不多讲,需要注意的是ADC传送数据之后是六个通道轮番传,所以后面数据处理的时候要区分开。

三、代码示例

void    adc_configure(void)
{    
    GPIO_InitTypeDef GPIO_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    ADC_InitTypeDef ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;    //PA2/3/6/7 ͨµÀ5
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//Ä£ÄâÊäÈë
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//²»´øÉÏÏÂÀ­
    GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯  

    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//¶ÀÁ¢Ä£Ê½
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; 
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
    ADC_CommonInit(&ADC_CommonInitStructure);

    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;    
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 6;
    ADC_Init(ADC1, &ADC_InitStructure);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_480Cycles );    
    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_480Cycles );
    ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 3, ADC_SampleTime_480Cycles );
    ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_480Cycles );
    ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 5, ADC_SampleTime_480Cycles );
    ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 6, ADC_SampleTime_480Cycles );

    ADC_Cmd(ADC1, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    DMA_DeInit(DMA2_Stream0);

    DMA_InitStructure.DMA_Channel = DMA_Channel_0; 
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR;
    DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&Adc_Value;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = 0X1E;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_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_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_Init(DMA2_Stream0, &DMA_InitStructure);

    DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);

    DMA_Cmd(DMA2_Stream0, ENABLE); 

    ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE); 
    ADC_DMACmd(ADC1, ENABLE);
    ADC_SoftwareStartConv(ADC1);
}

实际进行数据处理的函数如下:

void Getcurrent(void)
{
    u8 i,j;
    u32 Adc_Value_RAW[6];

    for(i=0;i<6;i++)
    {
        Adc_Value_RAW[i]=0;
    }

    for(i=0;i<6;i++)
    {
        for(j=0;j<5;j++)
            Adc_Value_RAW[i] += Adc_Value[i+6*j];
    }

    for(i=0;i<6;i++)
    {
        Adc_Value_process[i]=Adc_Value_RAW[i]*3.3/4096/5;
    }

    Adc_Value_process[0] = Adc_Value_process[0]*10.0f;
    Adc_Value_process[1] = Adc_Value_process[1]*100.0f/76.0f;

    for(i=2;i<6;i++)
    {
        Adc_Value_process[i] = (Adc_Value_process[i]-0.5f)/0.185f;
    }
    power=Adc_Value_process[0]*Adc_Value_process[1];
}

第一次写,排版及说明都不大美~大家见谅,有问题可以交流~~

猜你喜欢

转载自blog.csdn.net/u013721521/article/details/63711572