cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_ADC_ConvCpltCallback triggers only once (STM32F103)

nikolai2
Associate II
Posted on March 04, 2018 at 22:21

Good afternoon ladies and gentlemen.

I've been trying to put analog read to work and been partially successful.

First of all I tried using CubeMX for generating most of the init code and in 'manual pull mode' everything works fine:

...

HAL_ADC_Start(&hadc1);

...

  while (1)

  {

        if (HAL_ADC_PollForConversion(&hadc1, 10000) == HAL_OK)

        {

            ADC_val = HAL_ADC_GetValue(&hadc1);

        }

        ...

}

...

  hadc1.Init.ContinuousConvMode = ENABLE;

But I want to have this working via callback function that is described in many places. In CubeMX I set up pretty much everything I could find, especially inside NVIC -> ADC1 and ADC2 global interrupts. Got all the generated code and implemented weak methods as necessary

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

{

    ADC_val = HAL_ADC_GetValue(hadc);

}

// I'm not sure about this one cause I cannot see any override reference calls to this one! Something changed recently?

void ADC_IRQHandler()

{

    HAL_ADC_IRQHandler(&hadc1);

}

IRQs are set properly in the generated code:

static void MX_NVIC_Init(void)

{

  /* ADC1_2_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(ADC1_2_IRQn);

}

Also I start ADC with suggested  HAL_ADC_Start_IT(&hadc1); call.

What happens is that the HAL_ADC_ConvCpltCallback is called only once, and not whenever I touch my rotation sensor. So it seems like IRQ handler is not properly registered but after several hours I cannot figure out what's up.

Anyone has suggestions?

P.S. For the sake of stupid test, I put the HAL_ADC_Start_IT(&hadc1); within the while loop and it started triggering every time, but this is of course stupid and shouldn't be like that. Full source of my main file can be seen here:

https://pastebin.com/7pJRtQC4

 

#hal-adc-callback
4 REPLIES 4
William chang
Associate III
Posted on March 05, 2018 at 00:05

Do you config ADC to the continues mode??

nikolai2
Associate II
Posted on March 05, 2018 at 00:43

This is my config

https://i.imgur.com/RBbPsbx.png

You can download my MX project and Keil project from here to take a look if you are interested.

http://www.kennyslabs.com/etc/LED1.zip

Just plug in some analog rotation sensor to port A3.

As the source is right now, it just reads the analog value once and blinks accordingly (delay depends on the analog value).

If I enable the continous conversion mode, it just doesn't work at all.

Any ideas?

This is for STM32F103 (blue pill board here).

Posted on March 05, 2018 at 00:20

If i set:

  hadc1.Init.ContinuousConvMode = ENABLE;

then HAL_ADC_ConvCpltCallback is not called at all

Posted on March 05, 2018 at 02:55

you do not need to set up this function, just using callback function is enough. You need to delete that.

0690X00000609v5QAA.png

then your project sets up properly now. 

I think your project is already functioning.

when you place the adc pin to the ground, the led will looks like in off mode.

when you place the adc pin to the vcc, the led will blink fast.

you can enter the debug mode, then select peripheral, select adc register to see the actual ADC value in the register.

anyway, I suggest you to learn the CUBEMX first.

Here is the tutorial form ST, 

https://st-mooc.udemy.com

 

It's good to know more about CUBEMX. then we start the project.