ADC多路通道转换
一个ADC有16个外部模拟输入通道,外加温度传感器,参考电压,备份电压三个片内通道。在扫描模式下,一个ADC可以按顺序采样多个信号源,最大化利用ADC。
↓↓配置四路ADC转换
ADC_HandleTypeDef h_ADC1;
ADC_ChannelConfTypeDef h_ADC1_Channel_Init;
void ADC_Init(void)
{
/*ADC配置*/
__HAL_RCC_ADC1_CLK_ENABLE();
h_ADC1.Instance = ADC1;
h_ADC1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;//ADC_CCR_ADCPRE
h_ADC1.Init.Resolution = ADC_RESOLUTION_12B;//ADC_CR1_RES
h_ADC1.Init.DataAlign = ADC_DATAALIGN_RIGHT;//ADC_CR2_ALIGN
h_ADC1.Init.ScanConvMode = ENABLE;//ADC_CR1_SCAN
h_ADC1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;//ADC_CR2_EOCS
h_ADC1.Init.ContinuousConvMode = ENABLE;//ADC_CR2_CONT
h_ADC1.Init.NbrOfConversion = 4;//ADC_SQR1_L
h_ADC1.Init.DiscontinuousConvMode = DISABLE;//ADC_CR1_DISCEN
h_ADC1.Init.NbrOfDiscConversion = 0;//ADC_CR1_DISCNUM
h_ADC1.Init.ExternalTrigConv = ADC_SOFTWARE_START;//ADC_CR2_EXTSEL
h_ADC1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;//ADC_CR2_EXTEN
h_ADC1.Init.DMAContinuousRequests = DISABLE;//ADC_CR2_DDS
HAL_ADC_Init(&h_ADC1);
/*ADC通道配置*/
h_ADC1_Channel_Init.Channel = ADC_CHANNEL_1;
h_ADC1_Channel_Init.Rank = 1;
h_ADC1_Channel_Init.SamplingTime = ADC_SAMPLETIME_480CYCLES;
HAL_ADC_ConfigChannel(&h_ADC1,&h_ADC1_Channel_Init);
h_ADC1_Channel_Init.Channel = ADC_CHANNEL_2;
h_ADC1_Channel_Init.Rank = 2;
h_ADC1_Channel_Init.SamplingTime = ADC_SAMPLETIME_480CYCLES;
HAL_ADC_ConfigChannel(&h_ADC1,&h_ADC1_Channel_Init);
h_ADC1_Channel_Init.Channel = ADC_CHANNEL_4;
h_ADC1_Channel_Init.Rank = 3;
h_ADC1_Channel_Init.SamplingTime = ADC_SAMPLETIME_480CYCLES;
HAL_ADC_ConfigChannel(&h_ADC1,&h_ADC1_Channel_Init);
h_ADC1_Channel_Init.Channel = ADC_CHANNEL_6;
h_ADC1_Channel_Init.Rank = 4;
h_ADC1_Channel_Init.SamplingTime = ADC_SAMPLETIME_480CYCLES;
HAL_ADC_ConfigChannel(&h_ADC1,&h_ADC1_Channel_Init);
/*ADC通道引脚配置*/
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_Init;
GPIO_Init.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_6;
GPIO_Init.Mode = GPIO_MODE_ANALOG;
GPIO_Init.Pull = GPIO_NOPULL;
GPIO_Init.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA,&GPIO_Init);
}
/*获取ADC采样值*/
uint32_t ADC1_Start_GetValue(void)
{
HAL_ADC_Start(&h_ADC1);
HAL_ADC_PollForConversion(&h_ADC1,20);
return HAL_ADC_GetValue(&h_ADC1);
}
↓四路通道ADC采样结果