时钟配置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");
}
}
}
串口输出