I can’t switch between the channels of the ADC

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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();
  }

New contributor

liviu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

I can’t switch between the channels of the ADC

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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();
  }

New contributor

liviu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật