cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 ADC Scan Int Issue

Brother Theo
Associate II
Posted on April 18, 2018 at 03:16

Hi, I am trying to set up an STM32f030 to do ADC conversions on 4 channels using interrupts and no DMA. The first ADC input gets good values but the second ADC inf put is wonky.I used CubeMX to generate the code. I am a noob and wish to get the ADC working before diving into DMA. Any help would be appreciated.#

Here is the code:

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)

{

    switch(CurChannel)

    {

    case 0:

        Adc_Raw_X = HAL_ADC_GetValue(AdcHandle);

        CurChannel++;

        AvgAdc[0]+=Adc_Raw_X;

        cntAvgAdc[0]+=1;

        if(cntAvgAdc[0]>=16)

        {

            cntAvgAdc[0] = 0;

            AvgAdc[0] = AvgAdc[0] / 16;

            flgInputChanged=1;

        }

        break;

    case 1:

        Adc_Raw_Y = HAL_ADC_GetValue(AdcHandle);

        CurChannel++;

        AvgAdc[1]+=Adc_Raw_Y;

        cntAvgAdc[1]+=1;

        if(cntAvgAdc[1]>=16)

        {

            cntAvgAdc[1] = 0;

            AvgAdc[1] = AvgAdc[1] / 16;

            flgInputChanged=1;

        }

        break;

    case 2:

        Adc_Spare1 = HAL_ADC_GetValue(AdcHandle);

        CurChannel++;

        break;

    case 3:

        Adc_Spare2 = HAL_ADC_GetValue(AdcHandle);

        CurChannel++;

        HAL_ADC_Stop_IT(&hadc);

        break;

    }

}

static void MX_ADC_Init(void)

{

  ADC_ChannelConfTypeDef sConfig;

    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

    */

  hadc.Instance = ADC1;

  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

  hadc.Init.Resolution = ADC_RESOLUTION_12B;

  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

  hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

  //hadc.Init.EOCSelection = ADC_EOC_SINGLE_SEQ_CONV;

  hadc.Init.LowPowerAutoWait = DISABLE;

  hadc.Init.LowPowerAutoPowerOff = DISABLE;

  hadc.Init.ContinuousConvMode = ENABLE;

  hadc.Init.DiscontinuousConvMode = DISABLE;

  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

  hadc.Init.DMAContinuousRequests = DISABLE;

  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;

  if (HAL_ADC_Init(&hadc) != HAL_OK)

  {

    Error_Handler();

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_1;

  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    Error_Handler();

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_1;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    Error_Handler();

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_2;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    Error_Handler();

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_3;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    Error_Handler();

  }

}
1 REPLY 1
Posted on April 18, 2018 at 03:20

Multi-channel ADC really needs to DMA into an array.

You'd want to be incrementing Rank in your initialization code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..