时钟配置HSI主频配置64M
勾选打开8个通道的ADC
使能连续转换模式
配置好串口,选择异步模式配置好需要的开发环境并获取代码
修改main.c
串口重定向
#include "stdio.h"
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
串口重定向一定要勾选Use Micro LIB
获取ADC通道值
uint32_t ADC_Get_Average(uint8_t ch)
{
ADC_ChannelConfTypeDef sConfig;
uint32_t value_sum=0;
switch(ch)
{
case 0:sConfig.Channel = ADC_CHANNEL_0;break;
case 1:sConfig.Channel = ADC_CHANNEL_1;break;
case 2:sConfig.Channel = ADC_CHANNEL_2;break;
case 3:sConfig.Channel = ADC_CHANNEL_3;break;
case 4:sConfig.Channel = ADC_CHANNEL_4;break;
case 5:sConfig.Channel = ADC_CHANNEL_5;break;
case 6:sConfig.Channel = ADC_CHANNEL_6;break;
case 7:sConfig.Channel = ADC_CHANNEL_7;break;
}
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
HAL_ADC_ConfigChannel(&hadc1,&sConfig);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,30);
value_sum = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
return value_sum;
}
主函数
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
uint16_t i = 0;
uint16_t Adc_Val = 0;
static uint16_t count = 0;
while (1)
{
count++;
if(count % 100000 == 0)
{
for(i = 0; i < 8; i++)
{
Adc_Val = ADC_Get_Average(i);
printf("CHANNEL%d = %04d, %f V\r\n", i, Adc_Val, (double)Adc_Val / 4095 * 3.3);
}
printf("\r\n\r\n\r\n\r\n\r\n");
}
}
}
串口输出文章来源:https://www.toymoban.com/news/detail-615698.html
文章来源地址https://www.toymoban.com/news/detail-615698.html
到了这里,关于STM32CubeMX配置STM32G031多通道ADC采集(HAL库开发)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!