微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32的ADC实测

STM32的ADC实测

时间:11-13 来源:互联网 点击:
  测试芯片:STM32F103R8T6 (双AD)

编译平台:IAR5.40
  测试概述:使用片内振荡器时钟HSI,双ADC同步采样,ADC1采用DMA1存放,ADC2采用直接读取。
       每读取十个采样值计数一次,定时一秒钟,通过串口将采样次数送出,测试ADC采样速率。

一、主频设为:56MHz
  使用HSI,STM32F103最高频率可达72MHz,主频在56MHz时AD速率最大。
  RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_14);   // 8 ÷ 2 × 14 = 56MHz

二、ADC工作频率:14MHz
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  RCC_HCLKConfig(RCC_SYSCLK_Div1);
  RCC_PCLK2Config(RCC_HCLK_Div1);
  RCC_PCLK1Config(RCC_HCLK_Div2);
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  // 56 ÷ 4 = 14MHz

三、TIM2设置1s中断
  TIM_TimeBaseStructure.TIM_Period = 2240; // 1s// for 103
  TIM_TimeBaseStructure.TIM_Prescaler = 0x00; // for 103
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  TIM_PrescalerConfig(TIM2, 25000-1, TIM_PSCReloadMode_Immediate);// for 103
  // TIM2CLK = 56 ÷2 = 28MHz
  // 2240 × 25000 ÷ (28MHz × 2) = 1s

四、DMA设置
  只有ADC1和ADC3有DMA功能,而STM32F103只有ADC1和ADC2,所以DMA只设置存放ADC1的数据,ADC2直接读取。
  缓存大小设为10个单位数据,每存放10个数据FLAG_TC1置高,通知记录采样次数。
  #define ADC1_DR_Address ((uint32_t)ADC1_BASE+0x4C)
  #define ADC2_DR_Address ((uint32_t)ADC2_BASE+0x4C)
  __IO uint32_t ADC1_ConvertedValueTab[2];
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1_ConvertedValueTab;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 10;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  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);
  DMA_Cmd(DMA1_Channel1, ENABLE);

五、ADC设置
  双AD同步采样模式,不扫描,连续采样,1个规则通道
  ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
  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;
  ADC_Init(ADC1, &ADC_InitStructure);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);
 ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);
  ADC_DMACmd(ADC1, ENABLE);

 ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
  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;
  ADC_Init(ADC2, &ADC_InitStructure);
  ADC_RegularChannelConfig(ADC2, ADC_Channel_5, 1, ADC_SampleTime_1Cycles5);
  ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);

六、主程序
while(1)
{
  while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC)==RESET);
  DMA_ClearFlag(DMA1_FLAG_TC1);
  dd = (u16)ADC2->DR;
  ADC_ClearITPendingBit(ADC2, ADC_IT_EOC);
  min_cnt++;
}

七、中

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top