当前位置:   article > 正文

STM32CubeMX配置STM32G031多通道ADC采集(HAL库开发)_stm32g031adc采样

stm32g031adc采样

时钟配置HSI主频配置64M

 勾选打开8个通道的ADC

 使能连续转换模式

 配置好串口,选择异步模式配置好需要的开发环境并获取代码

 修改main.c

串口重定向

  1. #include "stdio.h"
  2. int fputc(int ch, FILE *f)
  3. {
  4. HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  5. return ch;
  6. }

 串口重定向一定要勾选Use Micro LIB

 获取ADC通道值

  1. uint32_t ADC_Get_Average(uint8_t ch)
  2. {
  3. ADC_ChannelConfTypeDef sConfig;
  4. uint32_t value_sum=0;
  5. switch(ch)
  6. {
  7. case 0:sConfig.Channel = ADC_CHANNEL_0;break;
  8. case 1:sConfig.Channel = ADC_CHANNEL_1;break;
  9. case 2:sConfig.Channel = ADC_CHANNEL_2;break;
  10. case 3:sConfig.Channel = ADC_CHANNEL_3;break;
  11. case 4:sConfig.Channel = ADC_CHANNEL_4;break;
  12. case 5:sConfig.Channel = ADC_CHANNEL_5;break;
  13. case 6:sConfig.Channel = ADC_CHANNEL_6;break;
  14. case 7:sConfig.Channel = ADC_CHANNEL_7;break;
  15. }
  16. sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
  17. sConfig.Rank = ADC_REGULAR_RANK_1;
  18. HAL_ADC_ConfigChannel(&hadc1,&sConfig);
  19. HAL_ADC_Start(&hadc1);
  20. HAL_ADC_PollForConversion(&hadc1,30);
  21. value_sum = HAL_ADC_GetValue(&hadc1);
  22. HAL_ADC_Stop(&hadc1);
  23. return value_sum;
  24. }

主函数 

  1. int main(void)
  2. {
  3. HAL_Init();
  4. SystemClock_Config();
  5. MX_GPIO_Init();
  6. MX_ADC1_Init();
  7. MX_USART1_UART_Init();
  8. uint16_t i = 0;
  9. uint16_t Adc_Val = 0;
  10. static uint16_t count = 0;
  11. while (1)
  12. {
  13. count++;
  14. if(count % 100000 == 0)
  15. {
  16. for(i = 0; i < 8; i++)
  17. {
  18. Adc_Val = ADC_Get_Average(i);
  19. printf("CHANNEL%d = %04d, %f V\r\n", i, Adc_Val, (double)Adc_Val / 4095 * 3.3);
  20. }
  21. printf("\r\n\r\n\r\n\r\n\r\n");
  22. }
  23. }
  24. }

 串口输出

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

闽ICP备14008679号