一 配置
二 代码
uint16_t Get_adc()
{
//启动ADC1
HAL_ADC_Start(&hadc1);
//等待ADC转换完成,超时为100ms
HAL_ADC_PollForConversion(&hadc1,100);
//判断ADC是否转换成功
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1),HAL_ADC_STATE_REG_EOC)){
//读取
return HAL_ADC_GetValue(&hadc1);
}
return 0;
}
int main(void)
{
MX_GPIO_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
uint16_t ADC_Value[]={0};
while (1)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET);//GPIO_PIN_SET
Delay_ms(100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_SET);//GPIO_PIN_RESET
Delay_ms(100);
for(uint8_t i=0;i<4;i++)
{
ADC_Value[i]=Get_adc();//分别存放通道1 2 3的ADC
float voltage = ADC_Value[i] * 3.3 / 4096 ;
char buffer[50];
sprintf(buffer, "channel%d: %d, volt: %.2f V\r\n", i, ADC_Value[i],voltage);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY);
Delay_ms(100);
}
}
/* USER CODE END 3 */
}
标签:HAL,PIN,多通道,Init,ADC,串口,GPIO,hadc1
From: https://blog.csdn.net/weixin_52233445/article/details/143476131