cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F767 ADC Multi-Channel Interrupt Mode Read

Andrew Hazelton
Associate III

Hi.

I am trying to read 4 ADC Channels sequentially in interrupt mode: ADC2_IN7, ADC2_IN14, ADC2_IN15 and ADC2_IN8. However it only reads the last rank channel every time. Please help me understand what I'm doing wrong.

Compiler:          IAR Workbench v8.32.1

MCU: STM32F767II

Hardware:     Custom PCB

HAL Version:    V1.15.0

void main (void)
{
    ...
    Analogue_Inputs_Initialise();
    ...
    while(1)
    {
        ADC_CH_Rank_ctr = 1;
        HAL_ADC_Start_IT(&ADC3Handle);
        HAL_Delay(100);
    }
}
 
 
void  Analogue_Inputs_Initialise (void)
{
    GPIO_InitTypeDef            GPIO_InitStruct;
    ADC_ChannelConfTypeDef      sConfig;
    
  
    /********************************
    *  ADC 3 - AI1, AI2, AI3, AI4   *
    ********************************/
    // Configure ADC Clock
    __HAL_RCC_ADC3_CLK_ENABLE();
    
    // Analogue Input 1 - PF7 - ADC3_IN5
    // Analogue Input 2 - PF8 - ADC3_IN6
    __HAL_RCC_GPIOF_CLK_ENABLE();
    GPIO_InitStruct.Pin                   = GPIO_PIN_7 | GPIO_PIN_8;
    GPIO_InitStruct.Mode                  = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull                  = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
    // Analogue Input 3 - PC1 - ADC3_IN11
    __HAL_RCC_GPIOC_CLK_ENABLE();
    GPIO_InitStruct.Pin                   = GPIO_PIN_1;
    GPIO_InitStruct.Mode                  = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull                  = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    // Analogue Input 4 - PA0 - ADC3_IN0
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin                   = GPIO_PIN_0;
    GPIO_InitStruct.Mode                  = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull                  = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    
    // Configure the ADC3 peripheral
    ADC3Handle.Instance                   = ADC3;
    ADC3Handle.Init.ClockPrescaler        = ADC_CLOCKPRESCALER_PCLK_DIV8;
    ADC3Handle.Init.Resolution            = ADC_RESOLUTION_12B;
    ADC3Handle.Init.ScanConvMode          = ENABLE;  
    ADC3Handle.Init.ContinuousConvMode    = ENABLE;  
    ADC3Handle.Init.DiscontinuousConvMode = DISABLE;   
    ADC3Handle.Init.NbrOfDiscConversion   = 0;
    ADC3Handle.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; 
    ADC3Handle.Init.ExternalTrigConv      = ADC_SOFTWARE_START;
    ADC3Handle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
    ADC3Handle.Init.NbrOfConversion       = 4;
    ADC3Handle.Init.DMAContinuousRequests = DISABLE;
    ADC3Handle.Init.EOCSelection          = ADC_EOC_SEQ_CONV;
    HAL_ADC_Init(&ADC3Handle);
 
    
    // Analogue Input 1 - PF7 - ADC3_IN5
    sConfig.Channel      = ADC_CHANNEL_5;           
    sConfig.Rank         = ADC_REGULAR_RANK_1;       
    sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;  
    sConfig.Offset       = 0;                      
    HAL_ADC_ConfigChannel(&ADC3Handle, &sConfig);   
    
    // Analogue Input 2 - PF8 - ADC3_IN6
    sConfig.Channel      = ADC_CHANNEL_6;     
    sConfig.Rank         = ADC_REGULAR_RANK_2; 
    sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; 
    sConfig.Offset       = 0;                      
    HAL_ADC_ConfigChannel(&ADC3Handle, &sConfig);
    
    // Analogue Input 3 - PC1 - ADC3_IN11
    sConfig.Channel      = ADC_CHANNEL_11;           
    sConfig.Rank         = ADC_REGULAR_RANK_3;          
    sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;     
    sConfig.Offset       = 0;                           
    HAL_ADC_ConfigChannel(&ADC3Handle, &sConfig);
    
    // Analogue Input 4 - PA0 - ADC3_IN0
    sConfig.Channel      = ADC_CHANNEL_0;             
    sConfig.Rank         = ADC_REGULAR_RANK_4;         
    sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;  
    sConfig.Offset       = 0;                           
    HAL_ADC_ConfigChannel(&ADC3Handle, &sConfig);  
 
 
    // NVIC configuration for ADC Reception complete interrupt
    HAL_NVIC_SetPriority(ADC_IRQn, 6, 0);
    HAL_NVIC_EnableIRQ(ADC_IRQn); 
}
 
 
void ADC_IRQHandler(void)
{
    HAL_ADC_IRQHandler(&ADC3Handle);
}
 
 
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
  if( __HAL_ADC_GET_FLAG( AdcHandle, ADC_FLAG_EOC ) )
  {
      switch( ADC_CH_Rank_ctr )
      {
          case 1:   HMI_Inputs_Outputs.Analog_Input_CH_1  = HAL_ADC_GetValue(AdcHandle);    ADC_CH_Rank_ctr++;  break;
          case 2:   HMI_Inputs_Outputs.Analog_Input_CH_2  = HAL_ADC_GetValue(AdcHandle);    ADC_CH_Rank_ctr++;  break;
          case 3:   HMI_Inputs_Outputs.Analog_Input_CH_3  = HAL_ADC_GetValue(AdcHandle);    ADC_CH_Rank_ctr++;  break;
          case 4:   HMI_Inputs_Outputs.Analog_Input_CH_4  = HAL_ADC_GetValue(AdcHandle);    
                    ADC_CH_Rank_ctr++;  
                    HAL_ADC_Stop_IT(&ADC3Handle); 
                    break;
          default:      /* Do Nothing  */        break;
      }
  }
}

0 REPLIES 0