STM32
直播中

湛蓝

12年用户 448经验值
擅长:控制/MCU
私信 关注
[问答]

如何对单缓冲模式的ADC+DMA如何进行配置

怎样去操作单缓冲模式的ADC+DMA呢?
如何对单缓冲模式的ADC+DMA如何进行配置?



回帖(1)

石飞鹏

2021-10-18 15:18:01
  STM32F407+ADC多通道+DMA
  单缓冲模式的ADC+DMA操作通过
  407主频168MHZ该监听采集了8路AD数据,每次路采集100组数据,然后进行平均处理,当然也可以加入卡尔曼滤波,采集会更准确,ADC采集的卡尔曼滤波有很多。
  基础配置
  GPIO对应表
  PA0——》 ADC123_IN0
  PA1 ——》 ADC123_IN1
  PB0 ——》 ADC12_IN8
  PB1 ——》 ADC12_IN9
  PC1 ——》 ADC12_IN11
  PC2 ——》 ADC123_IN12
  PC4 ——》 ADC12_IN14
  PC5 ——》 ADC12_IN15
  void RCC_Configuration()
  {
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
  |RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF
  |RCC_AHB1Periph_CRC | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_DMA1
  , 使能够);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG | RCC_APB1Periph_SPI2 | RCC_APB1Periph_SPI3
  |RCC_APB1Periph_USART2 |RCC_APB1Periph_USART3| RCC_APB1Periph_TIM2 ,启用);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_ADC1| RCC_APB2Periph_TIM9
  | RCC_APB2Periph_SPI1 | RCC_APB2Periph_SYSCFG| RCC_APB1Periph_USART2 | RCC_APB2Periph_USART6 ,ENABLE);
  }
  void GPIO_Config(void)
  {
  //AD 样本---------------------------------------------- ——
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1| GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  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_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  }
  ADC配置
  #define ADC_SAMPLE_PNUM 100//AD采样点数100
  #define ADC_SAMPLE_CNUM 8//AD采样数
  volatile unsigned short m_ADCValue[ADC_SAMPLE_PNUM][ADC_SAMPLE_CNUM] = {0};
  void ADC_Config_Conver(无效)
  {
  DMA_InitTypeDef DMA_InitStructure;
  ADC_InitTypeDef ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_Cmd(DMA2_Stream0,禁用);
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (unsigned int)&(ADC1-》DR);
  DMA_InitStructure.DMA_Memory0BaseAddr = (unsigned int)&m_ADCValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = ADC_SAMPLE_PNUM*ADC_SAMPLE_CNUM;
  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_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  // //双缓冲模式设置
  // DMA_DoubleBufferModeConfig(DMA2_Stream0,(uint32_t)(u16 *)(m_ADCValue1),DMA_Memory_0); //DMA_Memory_0首先被传输
  // DMA_DoubleBufferModeCmd(DMA2_Stream0,ENABLE);
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;//预分频
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;//采样间隔
  ADC_CommonInit(&ADC_CommonInitStructure);
  /* ADC1 初始化 ************************************************ ********************/
  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_ExternalTrigConv = 0;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = ADC_SAMPLE_CNUM;
  ADC_Init(ADC1, &ADC_InitStructure);
  ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
  ADC_DMACmd(ADC1,使能);
  ADC_Cmd(ADC1,使能);
  DMA_Cmd(DMA2_Stream0,启用);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_8,3,ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_9,4,ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_15,5,ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_14,6,ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_11,7,ADC_SampleTime_480Cycles);
  ADC_RegularChannelConfig(ADC1,ADC_Channel_12,8,ADC_SampleTime_480Cycles);
  ADC_SoftwareStartConv(ADC1);
  }
  读取数据
  u16 ADC_Proc_Value(unsigned char nIndex)
  {
  u32 nValue = 0;
  u32 nSum = 0;
  u8 i = 0, j = 0;
  u16 温度 = 0;
  u16 tempArray[ADC_SAMPLE_PNUM] = {0};
  对于(i=0; i《ADC_SAMPLE_PNUM; i++)//采集10组
  {
  tempArray[i] = m_ADCValue[i][nIndex];
  // printf(“ttt******ADCValue:%Xrn”,tempArray[i]);
  }
  对于(i=0; i《ADC_SAMPLE_PNUM-1; i++) // 去掉视角
  {
  对于(j = i+1; j《 ADC_SAMPLE_PNUM ;j++){
  if(tempArray[j] 《 tempArray[i])
  {
  Temp = tempArray[i];
  tempArray[i] = tempArray[j];
  tempArray[j] = Temp;
  }
  }
  }
  对于(i=10; i《ADC_SAMPLE_PNUM-10; i++) //取调试调试
  {
  nSum += tempArray[i] ;
  }
  nValue = nSum / (ADC_SAMPLE_PNUM-20);//调试
  返回 nValue;
  }
  int main(空)
  {
  整数总和;
  u8 i,j;
  RCC_配置();
  GPIO_Config();
  float ADC_Value[ADC_SAMPLE_CNUM];//保存经过转换得到的电压值
  ADC_Config_Conver();
  同时(1)
  {
  对于(i=0; i《ADC_SAMPLE_CNUM ; 我++)
  {
  ADC_Value[i]=ADC_Proc_Value(i);//求娇并转换成电压值
  ADC_Value[i]=(float)(ADC_Value[i]/4096)*3.3;//求急性并转换成电压值
  printf(“ADC_Value[d%]:%.2frn”, i, ADC_Value[i]);
  }
  }
  }
举报

更多回帖

发帖
×
20
完善资料,
赚取积分