4452|0

426

帖子

4

TA的资源

纯净的硅(初级)

楼主
 

【 STM32WB55 测评】7# STM32WB开发板ADC的使用和配置 [复制链接]

本帖最后由 北方 于 2019-5-16 11:32 编辑

1. ADC的使用和配置STM32WB55标称ADC的精度达到16位,这个是一个专用ADC芯片所能达到的标准。不过,再其文档中,只是提到能达到12位的精度。因此这里面还是有些差异的,应该是用其他方法达到的16位。
总共的19个通道,其中有3个事内部使用的,分别的hi温度,电池电压,和参考电压,对应于不同的地址。
ADC还有一个有意思的watchdog功能,可以实现ADC超过范围时的闭锁功能。低功耗ADC是STM32的传统,这里仍然保留。
2. 主要特性
- 12,10,8,6bit的设置精度,
- 转换时间,0.234us --12bit,或者0.297us--12bit,更低的精度如10位可以达到0.203us。
- 数据自我矫正,这个就减少了一个需要calibration的功能。
- oversampler,这个可以取样16位,但是应该是硬件仍然12位
具体结构参考下图,

具体时钟逻辑图,

3. 例程分析
3.1 连接开发板的arduino-A2接口,对应ADC_CHANNEL_6,这样可以显示其中连接在A2的电压变化
3.2 核心代码如下,
  1. int main(void)
  2. {
  3.   uint32_t tmp_index_adc_converted_data = 0;
  4.   HAL_Init();

  5.   SystemClock_Config();
  6.   MX_TIM2_Init();
  7.   for (tmp_index_adc_converted_data = 0; tmp_index_adc_converted_data < ADC_CONVERTED_DATA_BUFFER_SIZE; tmp_index_adc_converted_data++)
  8.   {
  9.     aADCxConvertedData[tmp_index_adc_converted_data] = VAR_CONVERTED_DATA_INIT_VALUE;
  10.   }
  11.   
  12.   BSP_LED_Init(LED2);
  13.   
  14.   Configure_ADC();
  15.   
  16.   /* Run the ADC calibration in single-ended mode */
  17.   if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
  18.   {
  19.     /* Calibration Error */
  20.     Error_Handler();
  21.   }

  22.   if (HAL_TIM_Base_Start(&htim2) != HAL_OK)
  23.   {
  24.     /* Counter enable error */
  25.     Error_Handler();
  26.   }
  27.   
  28.   /* Start ADC group regular conversion with DMA */
  29.   if (HAL_ADC_Start_DMA(&AdcHandle,
  30.                         (uint32_t *)aADCxConvertedData,
  31.                         ADC_CONVERTED_DATA_BUFFER_SIZE
  32.                        ) != HAL_OK)
  33.   {
  34.     /* ADC conversion start error */
  35.     Error_Handler();
  36.   }  
  37.   
  38.   while (1)
  39.   {
  40. }
  41. }
复制代码

3.3 代码简析
首先,初始化硬件,设定系统时钟和外设设定,
  HAL_Init();
SystemClock_Config();
MX_TIM2_Init();
然后设置ADC
  Configure_ADC();
最后就是启动DMA状态下的ADC,
HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED);
HAL_TIM_Base_Start(&htim2);
HAL_ADC_Start_DMA(&AdcHandle, (uint32_t *)aADCxConvertedData, ADC_CONVERTED_DATA_BUFFER_SIZE);

3.4 ADC 的设置
对应ADC的设置,是在config中确定的,如下代码,这个设定了用户手册中需要设定的时钟,精度,通道,矫正方式等,虽然内容很多,但是,注解和使用很直观,
  1. AdcHandle.Init.ClockPrescaler        = ADC_CLOCK_SYNC_PCLK_DIV2;
  2.   AdcHandle.Init.Resolution            = ADC_RESOLUTION_12B;
  3.   AdcHandle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
  4.   AdcHandle.Init.ScanConvMode          = ADC_SCAN_DISABLE;              /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
  5.   AdcHandle.Init.EOCSelection          = ADC_EOC_SINGLE_CONV;
  6.   AdcHandle.Init.LowPowerAutoWait      = DISABLE;
  7.   AdcHandle.Init.ContinuousConvMode    = ENABLE;                        /* Continuous mode to have maximum conversion speed (no delay between conversions) */
  8.   AdcHandle.Init.NbrOfConversion       = 1;                             /* Parameter discarded because sequencer is disabled */
  9.   AdcHandle.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */
  10.   AdcHandle.Init.NbrOfDiscConversion   = 1;                             /* Parameter discarded because sequencer is disabled */
  11.   AdcHandle.Init.ExternalTrigConv      = ADC_SOFTWARE_START;            /* Software start to trig the 1st conversion manually, without external event */
  12.   AdcHandle.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because trig of conversion by software start (no external event) */
  13.   AdcHandle.Init.DMAContinuousRequests = ENABLE;                        /* ADC with DMA transfer: continuous requests to DMA to match with DMA configured in circular mode */
  14.   AdcHandle.Init.Overrun               = ADC_OVR_DATA_OVERWRITTEN;
  15.   AdcHandle.Init.OversamplingMode      = DISABLE;

  16.   if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
  17.   {
  18.     /* ADC initialization error */
  19.     Error_Handler();
  20.   }
  21.   
  22.   
  23.   /*## Configuration of ADC hierarchical scope: ##############################*/
  24.   /*## ADC group injected and channels mapped on group injected ##############*/
  25.   
  26.   /* Note: ADC group injected not used and not configured in this example.    */
  27.   /*       Refer to other ADC examples using this feature.                    */
  28.   /* Note: Call of the functions below are commented because they are         */
  29.   /*       useless in this example:                                           */
  30.   /*       setting corresponding to default configuration from reset state.   */
  31.   
  32.   
  33.   /*## Configuration of ADC hierarchical scope: ##############################*/
  34.   /*## channels mapped on group regular         ##############################*/
  35.   
  36.   /* Configuration of channel on ADCx regular group on sequencer rank 1 */
  37.   /* Note: On this STM32 serie, ADC group regular sequencer is                */
  38.   /*       fully configurable: sequencer length and each rank                 */
  39.   /*       affectation to a channel are configurable.                         */
  40.   /* Note: Considering IT occurring after each ADC conversion                 */
  41.   /*       (IT by ADC group regular end of unitary conversion),               */
  42.   /*       select sampling time and ADC clock with sufficient                 */
  43.   /*       duration to not create an overhead situation in IRQHandler.        */
  44.   sConfig.Channel      = ADCx_CHANNELa;               /* ADC channel selection */
  45.   sConfig.Rank         = ADC_REGULAR_RANK_1;          /* ADC group regular rank in which is mapped the selected ADC channel */
  46.   sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;  /* ADC channel sampling time */
  47.   sConfig.SingleDiff   = ADC_SINGLE_ENDED;            /* ADC channel differential mode */
  48.   sConfig.OffsetNumber = ADC_OFFSET_NONE;             /* ADC channel affected to offset number */
  49.   sConfig.Offset       = 0;                           /* Parameter discarded because offset correction is disabled */
  50.   
复制代码












此内容由EEWORLD论坛网友北方原创,如需转载或用于商业用途需征得作者同意并注明出处






此帖出自无线连接论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
快速回复 返回顶部 返回列表