Stm32接外置adc相关程序是什么
时间: 2023-05-26 17:01:30 浏览: 250
stm32_usb_audio-master.zip
A:下面是Stm32接外置ADC的程序示例:
1.配置ADC工作模式和采样时间
ADC_HandleTypeDef hadc;
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.ScanConvMode = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.NbrOfDiscConversion = 0;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.NbrOfConversion = 1;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
hadc.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
2.启动ADC转换
uint32_t adc_raw_val = 0;
if (HAL_ADC_Start(&hadc) != HAL_OK)
{
Error_Handler();
}
if (HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) != HAL_OK)
{
Error_Handler();
}
adc_raw_val = HAL_ADC_GetValue(&hadc);
3.停止ADC转换
if (HAL_ADC_Stop(&hadc) != HAL_OK)
{
Error_Handler();
}
阅读全文