stm32-10ADC实验(DMA方式)

stm32-10ADC实验(DMA方式)



1、ADC相关

1. 介绍

12位ADC是一种逐次逼近型模拟数字转换器。它有18个通道,可测量16个外部和2个内部信号源。各通道的A/D转换能够单次、连续、扫描或间断模式执行。 ADC的结果能够左对齐或右对齐方式存储在16位数据寄存器中。git

2. 框图

ADC功能框图

3. 方法

ADC采集转换 -> ADC数据寄存器 -> 内存ADC_ConvertedValue -> 串口
- ADC1_GPIO_Config();//ADC端口初始化
- 打开ADC,DMA,GPIO相关时钟
- 配置GPIO引脚,模式等web

  • ADC1_Mode_Config();//ADC模式设置
  • DMA初始化
    • 将 DMA 的通道 x 寄存器重设为缺省值
    • 为DMA_Init结构体赋值
      u32 DMA_PeripheralBaseAddr;
      u32 DMA_MemoryBaseAddr;
      u32 DMA_DIR;
      u32 DMA_BufferSize;
      u32 DMA_PeripheralInc;
      u32 DMA_MemoryInc;
      u32 DMA_PeripheralDataSize;
      u32 DMA_MemoryDataSize;
      u32 DMA_Mode;
      u32 DMA_Priority;
      u32 DMA_M2M;
    • 初始化DMA相关寄存器
  • ADC初始化:
    • 为ADC_Init结构体赋值
      uint32_t ADC_Mode;
      FunctionalState ADC_ScanConvMode;
      FunctionalState ADC_ContinuousConvMode;
      uint32_t ADC_ExternalTrigConv;
      uint32_t ADC_DataAlign;
      uint8_t ADC_NbrOfChannel;
    • 初始化ADC相关寄存器
  • ADC转换时间配置
    • 配置ADC时钟
    • 配置采样周期
  • 使能ADC1的DMA
  • 使能ADC1
  • ADC自校准
    • 复位校准寄存器
    • 等待复位完成
    • 校准ADC
    • 等待
  • 设置启动触发方式:软件开启转化缓存

  • 读取转换的AD值svg

  • 打印到串口调试助手

2、程序分析

1. main.c
  1. 配置 USART1:将采集的模拟电压值显示到串口调试助手
  2. 配置ADC:采集电压值并用DMA传输到SRAM
  3. 打印
#include "stm32f10x.h"
#include "bsp_usart1.h"
#include "bsp_adc.h"

//time : pc1-1 0 1us
// pc1-2 0 0.5us 1.5us
// pc1-3 0 0.5us 0.8us

// ADC1转换的电压值经过MDA方式传送到SRAM:ADC_ConvertedValue
extern __IO uint16_t ADC_ConvertedValue;

// 局部变量,用于保存转化计算后的电压值
float ADC_ConvertedValueLocal;

// 软件简单延时函数
void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);
} 

/** * @brief 主函数 * @param 无 * @retval 无 */
int main(void)
{   
    /* USART1 config */
    USART1_Config();

    /* enable adc1 and config adc1 to dma mode */
    ADC1_Init();

    printf("\r\n ----这是一个ADC实验(采用DMA传输)----\r\n");

    while (1)
    {
        ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096 * 3.3; // 读取转换AD的值

        printf("\r\n The current AD value = 0x%04X \r\n", ADC_ConvertedValue); 
        printf("\r\n The current AD value = %f V \r\n",ADC_ConvertedValueLocal); 

        Delay(0xffffee);  
    }
}
2.ADC1_Init()
void ADC1_Init(void)
{
    ADC1_GPIO_Config();//ADC端口初始化
    ADC1_Mode_Config();//ADC模式设置
}
1.关于ADC1_GPIO_Config函数
/** * @brief 使能ADC1和MDA1的时钟,初始化PC.01 * @param * @retval */
static void ADC1_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    /* Enable DMA clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    /* Enable ADC1 and GPIOC clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

    /* Configure PC.01 as analog input */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;       // 模拟输入
    GPIO_Init(GPIOC, &GPIO_InitStructure);              // PC一、输入时不用设置速率
}

2.关于ADC1_Mode_Config函数

DMA初始化:
1. 使用DMA通道1
2. 数据:ADC数据寄存器 –> 内存
3. 使用循环模式
其中ADC_DR数据寄存器保存了ADC转换后的值,把它做为DMA传输的源地址函数

ADC初始化:
1. ADC_InitStructure.ADC_Mode:ADC有多种工做模式,详见数据手册
2. ADC_InitStructure.ADC_ScanConvMode:扫描转换模式用于多通道采集,轮流采集各通道的值
3. ADC_InitStructure.ADC_ContinuousConvMode:连续转换模式,ADC不断地采集转换
4. ADC_InitStructure.ADC_ExternalTrigConv:ADC收到触发信号才开始模数转换,中断触发/定时器出发/软件触发
5. ADC_InitStructure.ADC_DataAlign:数据对齐方式
6. ADC_InitStructure.ADC_NbrOfChannel:要进行ADC数据转换的通道数ui

ADC转换时间配置:
1. 配置ADC时钟频率,即设置ADC预分频器的分频值,上限不超过14MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
2. 每一个不一样的ADC通道能够设置为不一样的采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5);//规则通道配置函数es5

使能ADC1的DMAspa

使能ADC13d

ADC自校准
- ADC有一个内置自校准模式。校准可大幅减少因内部电容器组的变化而形成的准精度偏差。在校准期间,每一个电容器上都会计算出一个偏差修正码(数字值),这个码用于消除在随后的转换中每一个电容器上产生的偏差。
- 经过设置ADC_CR2寄存器的CAL位启动校准。一旦校准结束, CAL位被硬件复位,能够开始正常转换。建议在上电时执行一次ADC校准。校准阶段结束后,校准码储存在ADC_DR中。
ADC_ResetCalibration(ADC1);//复位校准寄存器
ADC_StartCalibration(ADC1);//校准函数
- 注意:
1. 建议在每次上电后执行校准。
2. 启动校准前, ADC必须处于关电状态(ADON=’0’)超过至少两个ADC时钟周期。调试

设置启动触发方式:软件开启转化
ADC_SoftwareStartConvCmd(ADC1, ENABLE);

/** * @brief 配置ADC1工做模式为DMA模式 * @param * @retval */
static void ADC1_Mode_Config(void)
{
    DMA_InitTypeDef DMA_InitStructure;
    ADC_InitTypeDef ADC_InitStructure;

    /* DMA channel1 configuration */
    DMA_DeInit(DMA1_Channel1);

    DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;         //外设基地址:ADC数据寄存器地址
    DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;    //内存基地址:SRAM地址
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;                  //外设:传输目的
    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;//外设数据宽度16位
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;        //内存数据宽度16位
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;                            //工做在循环缓存模式
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;                        //高优先级
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                               //失能内存到内存传输
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);

    /* Enable DMA channel1 */
    DMA_Cmd(DMA1_Channel1, ENABLE);

    /* ADC1 configuration */    
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;      //独立ADC模式
    ADC_InitStructure.ADC_ScanConvMode = DISABLE ;          //禁止扫描模式
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;      //开启连续转换模式
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;     //不使用外部触发转换
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;                  //采集数据右对齐
    ADC_InitStructure.ADC_NbrOfChannel = 1;                                 //要转换的通道数目1
    ADC_Init(ADC1, &ADC_InitStructure);

    /*配置ADC时钟,为PCLK2的8分频,9MHz*/
    RCC_ADCCLKConfig(RCC_PCLK2_Div8); 
    /*配置ADC1的通道11为55.5个采样周期,序列为1 */ 
    ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5);

    /* Enable ADC1 DMA */
    ADC_DMACmd(ADC1, ENABLE);

    /* Enable ADC1 */
    ADC_Cmd(ADC1, ENABLE);

    /* 复位校准寄存器 */   
    ADC_ResetCalibration(ADC1);
    /* 等待校准寄存器复位完成 */
    while(ADC_GetResetCalibrationStatus(ADC1));

    /* ADC校准 */
    ADC_StartCalibration(ADC1);
    /* 等待校准完成 */
    while(ADC_GetCalibrationStatus(ADC1));

    /* 采用软件触发ADC */ 
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}

3.关于控制ADC初始化的结构体的定义
/** * @brief ADC Init structure definition */

typedef struct
{
  uint32_t ADC_Mode;                      /*!< Configures the ADC to operate in independent or dual mode. This parameter can be a value of @ref ADC_mode */

  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_ExternalTrigConv;          /*!< Defines the external trigger used to start the analog to digital conversion of regular channels. This parameter can be a value of @ref ADC_external_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_NbrOfChannel;               /*!< Specifies the number of ADC channels that will be converted using the sequencer for regular channel group. This parameter must range from 1 to 16. */
}ADC_InitTypeDef;

4. 规则通道配置函数:ADC_RegularChannelConfig
  • 配置ADCx的通道x的采样次序以及采样周期
/** * @brief Configures for the selected ADC regular channel its corresponding * rank in the sequencer and its sample time. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral. * @param ADC_Channel: the ADC channel to configure. * This parameter can be one of the following values: * @arg ADC_Channel_0: ADC Channel0 selected * @arg ADC_Channel_1: ADC Channel1 selected * @arg ADC_Channel_2: ADC Channel2 selected * @arg ADC_Channel_3: ADC Channel3 selected * @arg ADC_Channel_4: ADC Channel4 selected * @arg ADC_Channel_5: ADC Channel5 selected * @arg ADC_Channel_6: ADC Channel6 selected * @arg ADC_Channel_7: ADC Channel7 selected * @arg ADC_Channel_8: ADC Channel8 selected * @arg ADC_Channel_9: ADC Channel9 selected * @arg ADC_Channel_10: ADC Channel10 selected * @arg ADC_Channel_11: ADC Channel11 selected * @arg ADC_Channel_12: ADC Channel12 selected * @arg ADC_Channel_13: ADC Channel13 selected * @arg ADC_Channel_14: ADC Channel14 selected * @arg ADC_Channel_15: ADC Channel15 selected * @arg ADC_Channel_16: ADC Channel16 selected * @arg ADC_Channel_17: ADC Channel17 selected * @param Rank: The rank in the regular group sequencer. This parameter must be between 1 to 16. * @param ADC_SampleTime: The sample time value to be set for the selected channel. * This parameter can be one of the following values: * @arg ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles * @arg ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles * @arg ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles * @arg ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles * @arg ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles * @arg ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles * @arg ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles * @arg ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles * @retval None */
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
{
  uint32_t tmpreg1 = 0, tmpreg2 = 0;
  /* Check the parameters */
  assert_param(IS_ADC_ALL_PERIPH(ADCx));
  assert_param(IS_ADC_CHANNEL(ADC_Channel));
  assert_param(IS_ADC_REGULAR_RANK(Rank));
  assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
  /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
  if (ADC_Channel > ADC_Channel_9)
  {
    /* Get the old register value */
    tmpreg1 = ADCx->SMPR1;
    /* Calculate the mask to clear */
    tmpreg2 = SMPR1_SMP_Set << (3 * (ADC_Channel - 10));
    /* Clear the old channel sample time */
    tmpreg1 &= ~tmpreg2;
    /* Calculate the mask to set */
    tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
    /* Set the new channel sample time */
    tmpreg1 |= tmpreg2;
    /* Store the new register value */
    ADCx->SMPR1 = tmpreg1;
  }
  else /* ADC_Channel include in ADC_Channel_[0..9] */
  {
    /* Get the old register value */
    tmpreg1 = ADCx->SMPR2;
    /* Calculate the mask to clear */
    tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel);
    /* Clear the old channel sample time */
    tmpreg1 &= ~tmpreg2;
    /* Calculate the mask to set */
    tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
    /* Set the new channel sample time */
    tmpreg1 |= tmpreg2;
    /* Store the new register value */
    ADCx->SMPR2 = tmpreg1;
  }
  /* For Rank 1 to 6 */
  if (Rank < 7)
  {
    /* Get the old register value */
    tmpreg1 = ADCx->SQR3;
    /* Calculate the mask to clear */
    tmpreg2 = SQR3_SQ_Set << (5 * (Rank - 1));
    /* Clear the old SQx bits for the selected rank */
    tmpreg1 &= ~tmpreg2;
    /* Calculate the mask to set */
    tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
    /* Set the SQx bits for the selected rank */
    tmpreg1 |= tmpreg2;
    /* Store the new register value */
    ADCx->SQR3 = tmpreg1;
  }
  /* For Rank 7 to 12 */
  else if (Rank < 13)
  {
    /* Get the old register value */
    tmpreg1 = ADCx->SQR2;
    /* Calculate the mask to clear */
    tmpreg2 = SQR2_SQ_Set << (5 * (Rank - 7));
    /* Clear the old SQx bits for the selected rank */
    tmpreg1 &= ~tmpreg2;
    /* Calculate the mask to set */
    tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
    /* Set the SQx bits for the selected rank */
    tmpreg1 |= tmpreg2;
    /* Store the new register value */
    ADCx->SQR2 = tmpreg1;
  }
  /* For Rank 13 to 16 */
  else
  {
    /* Get the old register value */
    tmpreg1 = ADCx->SQR1;
    /* Calculate the mask to clear */
    tmpreg2 = SQR1_SQ_Set << (5 * (Rank - 13));
    /* Clear the old SQx bits for the selected rank */
    tmpreg1 &= ~tmpreg2;
    /* Calculate the mask to set */
    tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
    /* Set the SQx bits for the selected rank */
    tmpreg1 |= tmpreg2;
    /* Store the new register value */
    ADCx->SQR1 = tmpreg1;
  }
}