您的位置:首页 > 其它

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

2017-03-18 17:41 435 查看

理论基础

=========================

STM32F4XX中文手册中关于ADC的介绍:

有 16 条复用通道。可以将转换分为两组:规则转换和注入转换。每个组包含一个转换序列,该序列可按任意顺序在任意通道上完成。例如,可按以下顺序对序列进行转换:ADC_IN3、ADC_IN8、ADC_IN2、ADC_IN2、ADC_IN0、ADC_IN2、ADC_IN2、ADC_IN15。

一个规则转换组最多由 16 个转换构成。必须在 ADC_SQRx 寄存器中选择转换序列的规则通道及其顺序。规则转换组中的转换总数必须写入 ADC_SQR1 寄存器中的 L[3:0] 位。

STM32F4可以实现6个通道ADC的连续转换。而问题在于采用扫描模式时,每个ADC只有一个存储寄存器ADC_DR,所以如果在下个数据来之前没有把这个数据读走就会丢失,好在有DMA传送数据。

程序配置方法

====

我使用的六路ADC如下:

PA2---------ADC123 IN2
PA3---------ADC123 IN3
PA6---------ADC12  IN6
PA7---------ADC12  IN7
PC4---------ADC12  IN14
PC5---------ADC12  IN15


在STM32F4xx_adc.c文件中有如下介绍

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


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

//使能ADC时钟

(#) 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();

//使能ADC使用的引脚

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

//配置ADC分频等~~

(#) Activate the ADC peripheral using ADC_Cmd() function.


根据我需要的硬件资源,选用ADC1配置6路规则通道,使用按照过程配置函数如下:

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

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2, ENABLE);//使能引脚及DMA时钟

//引脚初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_7;  //PA2/3/6/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;
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;//预分频为4分频;ADCCLK=PCLK2/4=84/4=21Mhz,最好不要超过36Mhz
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 );

DMA_DeInit(DMA2_Stream0);

DMA_InitStructure.DMA_Channel = DMA_Channel_0;  //ͨµÀÑ¡Ôñ
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR;//DMAÍâÉèµØÖ·
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&Adc_Value;//DMA ´æ´¢Æ÷0µØÖ·
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;//ÍâÉèÊý¾Ý³¤¶È:8λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;//´æ´¢Æ÷Êý¾Ý³¤¶È:8λ
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 Stream

//DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA2_Stream0, ENABLE);

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


这里关于ADC_CommonInitStructure的配置有如下说明

*** Multi mode ADCs Regular channels configuration ***
======================================================
[..]
(+) Refer to "Regular channels group configuration" description to
configure the ADC1, ADC2 and ADC3 regular channels.
(+) Select the Multi mode ADC regular channels features (dual or
triple mode) using ADC_CommonInit() function and configure
the DMA mode using ADC_MultiModeDMARequestAfterLastTransferCmd()
functions.
(+) Read the ADCs converted values using the
ADC_GetMultiModeConversionValue() function.

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_CommonInitTypeDef初始化结构体中第一个参数ADC_Mode有如下选项:

#define ADC_Mode_Independent                       ((uint32_t)0x00000000)
#define ADC_DualMode_RegSimult_InjecSimult         ((uint32_t)0x00000001)
#define ADC_DualMode_RegSimult_AlterTrig           ((uint32_t)0x00000002)
#define ADC_DualMode_InjecSimult                   ((uint32_t)0x00000005)
#define ADC_DualMode_RegSimult                     ((uint32_t)0x00000006)
#define ADC_DualMode_Interl                        ((uint32_t)0x00000007)
#define ADC_DualMode_AlterTrig                     ((uint32_t)0x00000009)
#define ADC_TripleMode_RegSimult_InjecSimult       ((uint32_t)0x00000011)
#define ADC_TripleMode_RegSimult_AlterTrig         ((uint32_t)0x00000012)
#define ADC_TripleMode_InjecSimult                 ((uint32_t)0x00000015)
#define ADC_TripleMode_RegSimult                   ((uint32_t)0x00000016)
#define ADC_TripleMode_Interl                      ((uint32_t)0x00000017)
#define ADC_TripleMode_AlterTrig                   ((uint32_t)0x00000019)


因为只使用了一个ADC,所以选择ADC_Mode_Independent;

第二个参数ADC_Prescaler时钟分频,APB时钟为84MHz,选择4分频,则实际ADC时钟为21MHz。

第三个参数ADC_DMAAccessMode有如下定义

#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_1用于12位精度,一个接一个获取;ADC_DMAAccessMode_2用于两个adc同时工作以提高ADC转换效率,ADC_DMAAccessMode_3与2大抵相同,但是精度取6位,这样速度更快。

这里我选用ADC_DMAAccessMode_1模式。

第四个参数ADC_TwoSamplingDelay用于配置两次采样间隔时间。

接下来ADC_InitStructure配置

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选择ADC采样精度,有如下选项:

#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)


采样精度取最高为12位。

第二个参数ADC_ScanConvMode配置扫描模式是否开启,此处多通道当然开启

第三个参数ADC_ContinuousConvMode配置连续转换模式,为使转换完可以进行下一次转换,采用连续转换模式。

第四个参数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_ExternalTrigConvEdge_None采用软件源触发而非外部触发。同样下一个参数ADC_ExternalTrigConv也无需配置

第六个参数ADC_DataAlign配置数据格式,有左对齐与右对齐两种模式,此处选用ADC_DataAlign_Right右对齐。

第七个参数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. */


故其数据从1开始而非0,我配置六个通道填6

接下来用ADC_RegularChannelConfig()函数配置六个通道优先级。

再后面DMA配置不讲了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stm32 编辑器