[原创] 【 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位 具体结构参考下图, 1.PNG 具体时钟逻辑图, 2.PNG 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. BSP_LED_Init(LED2);
  12. Configure_ADC();
  13. /* Run the ADC calibration in single-ended mode */
  14. if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
  15. {
  16. /* Calibration Error */
  17. Error_Handler();
  18. }
  19. if (HAL_TIM_Base_Start(&htim2) != HAL_OK)
  20. {
  21. /* Counter enable error */
  22. Error_Handler();
  23. }
  24. /* Start ADC group regular conversion with DMA */
  25. if (HAL_ADC_Start_DMA(&AdcHandle,
  26. (uint32_t *)aADCxConvertedData,
  27. ADC_CONVERTED_DATA_BUFFER_SIZE
  28. ) != HAL_OK)
  29. {
  30. /* ADC conversion start error */
  31. Error_Handler();
  32. }
  33. while (1)
  34. {
  35. }
  36. }
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. /*## Configuration of ADC hierarchical scope: ##############################*/
  22. /*## ADC group injected and channels mapped on group injected ##############*/
  23. /* Note: ADC group injected not used and not configured in this example. */
  24. /* Refer to other ADC examples using this feature. */
  25. /* Note: Call of the functions below are commented because they are */
  26. /* useless in this example: */
  27. /* setting corresponding to default configuration from reset state. */
  28. /*## Configuration of ADC hierarchical scope: ##############################*/
  29. /*## channels mapped on group regular ##############################*/
  30. /* Configuration of channel on ADCx regular group on sequencer rank 1 */
  31. /* Note: On this STM32 serie, ADC group regular sequencer is */
  32. /* fully configurable: sequencer length and each rank */
  33. /* affectation to a channel are configurable. */
  34. /* Note: Considering IT occurring after each ADC conversion */
  35. /* (IT by ADC group regular end of unitary conversion), */
  36. /* select sampling time and ADC clock with sufficient */
  37. /* duration to not create an overhead situation in IRQHandler. */
  38. sConfig.Channel = ADCx_CHANNELa; /* ADC channel selection */
  39. sConfig.Rank = ADC_REGULAR_RANK_1; /* ADC group regular rank in which is mapped the selected ADC channel */
  40. sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5; /* ADC channel sampling time */
  41. sConfig.SingleDiff = ADC_SINGLE_ENDED; /* ADC channel differential mode */
  42. sConfig.OffsetNumber = ADC_OFFSET_NONE; /* ADC channel affected to offset number */
  43. sConfig.Offset = 0; /* Parameter discarded because offset correction is disabled */
此内容由EEWORLD论坛网友北方原创,如需转载或用于商业用途需征得作者同意并注明出处 本帖最后由 北方 于 2019-5-16 11:32 编辑

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复