STM32 —— 多路ADC采集

Posted a1314521531

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32 —— 多路ADC采集相关的知识,希望对你有一定的参考价值。

#ifndef _ADC_H_
#define _ADC_H_

#include "type.h"
#include "debug.h"

void ADCInit(void);
uint16_t ADCGetData(uint8_t channel);
uint16_t ADCGetAverage(uint8_t channel, uint8_t times);

#ifndef CONFIG_CAN
void adc_test(void);
#endif

#endif /* _ADC_H_ */
// input1~5 -- 模拟量IO口; input6~10 -- 普通IO口用
#define ADC_CHANNEL        ADC1

#define AD_INPUT1_GRP      GPIOC
#define AD_INPUT1_INDEX    GPIO_Pin_0
#define AD_INPUT2_GRP      GPIOC
#define AD_INPUT2_INDEX    GPIO_Pin_1
#define AD_INPUT3_GRP      GPIOC
#define AD_INPUT3_INDEX    GPIO_Pin_2
#define AD_INPUT4_GRP      GPIOC
#define AD_INPUT4_INDEX    GPIO_Pin_3
#define AD_INPUT5_GRP      GPIOA
#define AD_INPUT5_INDEX    GPIO_Pin_2

#if 0
#define AD_INPUT6_GRP      GPIOA
#define AD_INPUT6_INDEX    GPIO_Pin_3
#define AD_INPUT7_GRP      GPIOC
#define AD_INPUT7_INDEX    GPIO_Pin_4
#define AD_INPUT8_GRP      GPIOC
#define AD_INPUT8_INDEX    GPIO_Pin_5
#define AD_INPUT9_GRP      GPIOB
#define AD_INPUT9_INDEX    GPIO_Pin_0
#define AD_INPUT10_GRP     GPIOB
#define AD_INPUT10_INDEX   GPIO_Pin_1
#endif
#define AD_INPUT_CONFIG(gpio, pos)  GPIOConfig(gpio, pos, GPIO_Mode_AIN)

#include "adc.h"
#include "stm32f10x.h"
#include "delay.h"
#include "target.h"

#define ADC_CHANNEL_NUM  5

static uint16_t ad_value[ADC_CHANNEL_NUM] = 0;

static void adc_gpio_clk_init(void)

  RCC_ADCCLKConfig(RCC_PCLK2_Div6);


static void adc_gpio_init(void)

  adc_gpio_clk_init();

  AD_INPUT_CONFIG(AD_INPUT1_GRP, AD_INPUT1_INDEX | AD_INPUT2_INDEX | AD_INPUT3_INDEX | AD_INPUT4_INDEX);
  AD_INPUT_CONFIG(AD_INPUT5_GRP, AD_INPUT5_INDEX);
  ADC_DeInit(ADC_CHANNEL);


static void adc_dma_init(void)

  DMA_InitTypeDef DMA_InitStructure;
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  DMA_DeInit(DMA1_Channel1);

  DMA_InitStructure.DMA_PeripheralBaseAddr  = (u32) & (ADC1->DR);
  DMA_InitStructure.DMA_MemoryBaseAddr      = (u32)&ad_value;
  DMA_InitStructure.DMA_DIR                 = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_M2M                 = DMA_M2M_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize  = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize      = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_BufferSize          = ADC_CHANNEL_NUM;
  DMA_InitStructure.DMA_MemoryInc           = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralInc       = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_Mode                = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority            = DMA_Priority_High;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);


static void adc_init()

  ADC_InitTypeDef ADC_InitStructure;

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = ADC_CHANNEL_NUM;
  ADC_Init(ADC_CHANNEL, &ADC_InitStructure);

  ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
  ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
  ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
  ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
  ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_4, 5, ADC_SampleTime_55Cycles5);

  ADC_DMACmd(ADC_CHANNEL, ENABLE);

  ADC_Cmd(ADC_CHANNEL, ENABLE);

  ADC_ResetCalibration(ADC_CHANNEL);

  while(ADC_GetResetCalibrationStatus(ADC_CHANNEL));

  ADC_StartCalibration(ADC_CHANNEL);

  while(ADC_GetCalibrationStatus(ADC_CHANNEL));


static void adc_start(void)

  ADC_SoftwareStartConvCmd(ADC_CHANNEL, ENABLE); // start convert
  DMA_Cmd(DMA1_Channel1, ENABLE);


void  ADCInit(void)

  adc_gpio_init();
  adc_dma_init();
  adc_init();
  adc_start();


uint16_t ADCGetData(uint8_t channel)

  uint16_t ret = 0;
  switch(channel)
  
  case ADC_Channel_0:
    ret = ad_value[0];
    break;
  case ADC_Channel_1:
    ret = ad_value[1];
    break;
  case ADC_Channel_2:
    ret = ad_value[2];
    break;
  case ADC_Channel_3:
    ret = ad_value[3];
    break;
  case ADC_Channel_4:
    ret = ad_value[4];
    break;
  

  return ret;


uint16_t ADCGetAverage(uint8_t channel, uint8_t times)

  uint16_t value;
  int i;

  for(i = 0; i < times; ++i)
  
    value += ADCGetData(channel);
  

  return (value / times);


#ifndef CONFIG_ADC
void adc_test(void)

  uint16_t buffer[ADC_CHANNEL_NUM];
  float temp[ADC_CHANNEL_NUM];

  buffer[0] = ADCGetData(ADC_Channel_0);
  temp[0] = (float)buffer[0] * 3.3 / 4096;
  printf("temp[0]: %f\\r\\n", temp[0]);

  buffer[1] = ADCGetData(ADC_Channel_1);
  temp[1] = (float)buffer[1] * 3.3 / 4096;
  printf("temp[1]: %f\\r\\n", temp[1]);

  buffer[2] = ADCGetData(ADC_Channel_2);
  temp[2] = (float)buffer[2] * 3.3 / 4096;
  printf("temp[2]: %f\\r\\n", temp[2]);

  buffer[3] = ADCGetData(ADC_Channel_1);
  temp[3] = (float)buffer[3] * 3.3 / 4096;
  printf("temp[1]: %f\\r\\n", temp[3]);

  buffer[4] = ADCGetData(ADC_Channel_4);
  temp[4] = (float)buffer[4] * 3.3 / 4096;
  printf("temp[2]: %f\\r\\n", temp[4]);

#endif

以上是关于STM32 —— 多路ADC采集的主要内容,如果未能解决你的问题,请参考以下文章

如何让stm32产生多路输出 PWM 最好有代码

STM32F103+CubeMX+ADC采集直流

STM32F0的多路ADC 无DMA

STM32CubeMX之ADC采集烟雾值

STM32实现ADC采集(待优化)

STM32实现ADC采集(待优化)