2023-03-13 05:36 AM
I am trying to read several adc channels on the stm32 microcontroller, but it won't let me, it only reads the first one. I have seen in the registers that when changing channel what it does is that it sets the selected channel bit to 1 and does not modify the others, so if other channels have been previously selected they are still selected. Here is my configuration and the code:
ADC_Select_CH0();
HAL_ADC_Start(&hadc1);
if( HAL_ADC_PollForConversion(&hadc1, 5) == HAL_OK) //timeout
{
val1 = HAL_ADC_GetValue(&hadc1);
//temp2 = get_temp(CC1_temp);
}
HAL_ADC_Stop(&hadc1);
ADC_Select_CH1();
HAL_ADC_Start(&hadc1);
if( HAL_ADC_PollForConversion(&hadc1, 5) == HAL_OK) //timeout
{
val4 = HAL_ADC_GetValue(&hadc1);
//temp2 = get_temp(CC1_temp);
}
HAL_ADC_Stop(&hadc1);
ADC_Select_CH4();
HAL_ADC_Start(&hadc1);
if( HAL_ADC_PollForConversion(&hadc1, 5) == HAL_OK) //timeout
{
CC1_H1_value = HAL_ADC_GetValue(&hadc1);
sprintf(CC1_HV_print, "CC1_HV: %d\r\n", CC1_H1_value);
//temp2 = get_temp(CC1_temp);
}
HAL_ADC_Stop(&hadc1);
the channel select functions are this:
void ADC_Select_CH0(){
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 0;
if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){
Error_Handler();
}
}
void ADC_Select_CH1(){
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1;
if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){
Error_Handler();
}
}
void ADC_Select_CH4(){
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = 4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){
Error_Handler();
}
}
andd the hadc config is this:
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.OversamplingMode = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
2023-03-16 09:37 PM
Hello @lroca.2,
Can you please specify the STM32 product, and also use the code snippet function to paste your code
Regards,
Ryan