当前位置:   article > 正文

STM32 Hal库-ADC电压采集-多通道DMA_stm32 hal adc多通道

stm32 hal adc多通道

1、CubeMx配置ADC时钟,配置成12MHz。

2、ADC参数配置

Scan Conversion Mode( 扫描模式 ) :如果使用多个通道,自动设置ENABLE。 

Continuous Conversion Mode(连续转换模式) :设置为ENABLE,即连续转换。

3、ADC总转换时间TCONV

TCONV = 采样时间+ 12.5个周期

ADC时钟设置为12MHz,采样时间为71.5个周期,则总转换时间T=7us

T=112M×(71.5+12.5)=7us

4、DMA配置

5、bsp_adc.c

  1. #include "bsp_adc.h"
  2. uint16_t adc1_val_buf[ADC1_CHANNEL_CNT*ADC1_VALUE_CNT]; //传递给DMA存放多通道采样值的数组
  3. uint32_t adc1_aver_val[ADC1_CHANNEL_CNT] = {0}; //计算多通道的平均采样值的过程数组
  4. void ADC1_Start_DMA(void)
  5. {
  6. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&adc1_val_buf, (ADC1_CHANNEL_CNT*ADC1_VALUE_CNT)) != HAL_OK)
  7. {
  8. /* Start Conversation Error */
  9. Error_Handler();
  10. }
  11. }
  12. void ADC1_Stop_DMA(void)
  13. {
  14. if(HAL_ADC_Stop_DMA(&hadc1) != HAL_OK)
  15. {
  16. /* Start Conversation Error */
  17. Error_Handler();
  18. }
  19. }
  20. /* x:对应Rank通道 */
  21. uint16_t ADC1INx_AverValue( uint8_t x )
  22. {
  23. uint16_t i=0;
  24. uint16_t value=0;
  25. /* 在采样值数组中分别取出每个通道的采样值并求和 */
  26. adc1_aver_val[x] = 0;
  27. for(i = 0; i < ADC1_VALUE_CNT; i++)
  28. {
  29. adc1_aver_val[x] += adc1_val_buf[i*ADC1_CHANNEL_CNT+x];
  30. }
  31. /* 对通道采样值求平均值 */
  32. value = adc1_aver_val[x] / ADC1_VALUE_CNT;
  33. return value;
  34. }

5、bsp_adc.h

  1. #ifndef __BSP_ADC_H__
  2. #define __BSP_ADC_H__
  3. #include "main.h"
  4. #include "adc.h"
  5. #include "dma.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #define ADC1_CHANNEL_CNT 6 //采样通道数
  9. #define ADC1_VALUE_CNT 20 //单个通道采样次数,用来取平均值
  10. #define Rank1 0
  11. #define Rank2 1
  12. #define Rank3 2
  13. #define Rank4 3
  14. #define Rank5 4
  15. #define Rank6 5
  16. #define Rank7 6
  17. #define Rank8 7
  18. #define Rank9 8
  19. #define Rank10 9
  20. extern uint16_t adc1_val_buf[ADC1_CHANNEL_CNT*ADC1_VALUE_CNT]; //传递给DMA存放多通道采样值的数组
  21. extern uint32_t adc1_aver_val[ADC1_CHANNEL_CNT]; //计算多通道的平均采样值的过程数组
  22. void ADC1_Start_DMA( void );
  23. void ADC1_Stop_DMA( void );
  24. uint16_t ADC1INx_AverValue( uint8_t x );
  25. #endif

6、main.c

  1. /* Infinite loop */
  2. /* USER CODE BEGIN WHILE */
  3. HAL_Delay(1000);
  4. HAL_ADCEx_Calibration_Start(&hadc1); //要是采集的电压不准,上电延时一段时间进行ADC校准
  5. ADC1_Start_DMA();
  6. while (1)
  7. {
  8. /* USER CODE END WHILE */
  9. /* USER CODE BEGIN 3 */
  10. // LED_RUN( 1000 );
  11. OLED_Showdecimal(0,6,ADC1INx_AverValue( Rank5 )*1000/4096*3.3/1000,1,3,16); //采集电压,显示在OLED屏幕
  12. OLED_Showdecimal(60,6,ADC1INx_AverValue( Rank6 )*1000/4096*3.3/1000,1,3,16);
  13. HAL_Delay(1000);
  14. }
  15. /* USER CODE END 3 */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/753064
推荐阅读
相关标签
  

闽ICP备14008679号