I have an ADC with two channels on a nucleo f401re, one who reads values from a termistor(channel 8, rank1) and one who starts to read values from a potentiometer(channel 1, rank 2).Pressing the button on microcontroler should switch between the channels but I can only switch from channel 8 to 1.
I tried to switch their ranks or give them the same rank but it does’n work.
<code>void change_channel(uint32_t channel, uint32_t rank)
{ ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = channel;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
global_channel=sConfig.Channel;
global_rank= sConfig.Rank;
<code>void change_channel(uint32_t channel, uint32_t rank)
{ ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = channel;
sConfig.Rank = rank;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
global_channel=sConfig.Channel;
global_rank= sConfig.Rank;
}
</code>
void change_channel(uint32_t channel, uint32_t rank)
{ ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = channel;
sConfig.Rank = rank;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
global_channel=sConfig.Channel;
global_rank= sConfig.Rank;
}
I also tried to disable the other channel but it still doesn’t work.(it will read values only from the potentiometer).When i try to give sConfig.Rank = 2 when i disable the channels i read a value around 1310 for the termistor and i don’t know where it comes from.
<code>void change_channel(uint32_t channel, uint32_t rank)
ADC_ChannelConfTypeDef sConfig = {0};
for (uint32_t ch = ADC_CHANNEL_0; ch <= ADC_CHANNEL_10; ch++) // Adjust range based on your MCU's ADC channels
sConfig.Rank = 0; // Set rank to 0 to disable the channel (no valid rank)
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
// Enable the desired channel
sConfig.Channel = channel;
sConfig.Rank = rank; // Explicitly set the desired rank
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
// Store the global variables for tracking
global_channel = sConfig.Channel;
global_rank = sConfig.Rank;
<code>void change_channel(uint32_t channel, uint32_t rank)
{
ADC_ChannelConfTypeDef sConfig = {0};
// Disable all channels
for (uint32_t ch = ADC_CHANNEL_0; ch <= ADC_CHANNEL_10; ch++) // Adjust range based on your MCU's ADC channels
{
sConfig.Channel = ch;
sConfig.Rank = 0; // Set rank to 0 to disable the channel (no valid rank)
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
// Enable the desired channel
sConfig.Channel = channel;
sConfig.Rank = rank; // Explicitly set the desired rank
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
// Store the global variables for tracking
global_channel = sConfig.Channel;
global_rank = sConfig.Rank;
}
</code>
void change_channel(uint32_t channel, uint32_t rank)
{
ADC_ChannelConfTypeDef sConfig = {0};
// Disable all channels
for (uint32_t ch = ADC_CHANNEL_0; ch <= ADC_CHANNEL_10; ch++) // Adjust range based on your MCU's ADC channels
{
sConfig.Channel = ch;
sConfig.Rank = 0; // Set rank to 0 to disable the channel (no valid rank)
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
// Enable the desired channel
sConfig.Channel = channel;
sConfig.Rank = rank; // Explicitly set the desired rank
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
// Store the global variables for tracking
global_channel = sConfig.Channel;
global_rank = sConfig.Rank;
}
I read the values in the second task(mod is a variable that changes between 0 and 1 when i press the button to help me select the channel, 0 means automatic mode and 1 means manual mode)text
<code>void StartTask02(void *argument)
/* USER CODE BEGIN StartTask02 */
{osMessageQueueGet (mod_for_ADCHandle, &mod1, 0, 0);
if(HAL_ADC_PollForConversion(&hadc1,1000)==HAL_OK){
readValue=HAL_ADC_GetValue(&hadc1);
voltage=(float)readValue/(float)4095*d;
globalreadValue=readValue;
osMessageQueuePut (queque_voltageHandle, &voltage, 0, 0);
if(HAL_ADC_PollForConversion(&hadc1,1000)==HAL_OK){
readValue=HAL_ADC_GetValue(&hadc1);
voltage=(float)readValue/(float)4095*d;
globalreadValue=readValue;
osMessageQueuePut (queque_voltageHandle, &voltage, 0, 0);
/* USER CODE END StartTask02 */
<code>void StartTask02(void *argument)
{
/* USER CODE BEGIN StartTask02 */
float readValue;
float voltage;
float d=3.3;
int mod1;
/* Infinite loop */
for(;;)
{osMessageQueueGet (mod_for_ADCHandle, &mod1, 0, 0);
if(mod1==0)//
{
change_channel(8,1);
HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1,1000)==HAL_OK){
readValue=HAL_ADC_GetValue(&hadc1);
voltage=(float)readValue/(float)4095*d;
globalvoltage= voltage;
globalreadValue=readValue;
}
HAL_ADC_Stop(&hadc1);
osMessageQueuePut (queque_voltageHandle, &voltage, 0, 0);
osDelay(100);
}
if(mod1==1)//
{
change_channel(1,2);
HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1,1000)==HAL_OK){
readValue=HAL_ADC_GetValue(&hadc1);
voltage=(float)readValue/(float)4095*d;
globalvoltage= voltage;
globalreadValue=readValue;
}
HAL_ADC_Stop(&hadc1);
osMessageQueuePut (queque_voltageHandle, &voltage, 0, 0);
osDelay(100);
}
}
/* USER CODE END StartTask02 */
}
</code>
void StartTask02(void *argument)
{
/* USER CODE BEGIN StartTask02 */
float readValue;
float voltage;
float d=3.3;
int mod1;
/* Infinite loop */
for(;;)
{osMessageQueueGet (mod_for_ADCHandle, &mod1, 0, 0);
if(mod1==0)//
{
change_channel(8,1);
HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1,1000)==HAL_OK){
readValue=HAL_ADC_GetValue(&hadc1);
voltage=(float)readValue/(float)4095*d;
globalvoltage= voltage;
globalreadValue=readValue;
}
HAL_ADC_Stop(&hadc1);
osMessageQueuePut (queque_voltageHandle, &voltage, 0, 0);
osDelay(100);
}
if(mod1==1)//
{
change_channel(1,2);
HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1,1000)==HAL_OK){
readValue=HAL_ADC_GetValue(&hadc1);
voltage=(float)readValue/(float)4095*d;
globalvoltage= voltage;
globalreadValue=readValue;
}
HAL_ADC_Stop(&hadc1);
osMessageQueuePut (queque_voltageHandle, &voltage, 0, 0);
osDelay(100);
}
}
/* USER CODE END StartTask02 */
}
ADC configuration
<code> /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 2;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
sConfig.Channel = ADC_CHANNEL_8;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
<code> /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 2;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
</code>
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 2;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}