2018-03-04 01:21 PM
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:
#hal-adc-callback2018-03-04 03:05 PM
Do you config ADC to the continues mode??
2018-03-04 03:43 PM
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).
2018-03-04 04:20 PM
If i set:
hadc1.Init.ContinuousConvMode = ENABLE;
then HAL_ADC_ConvCpltCallback is not called at all
2018-03-04 06:55 PM
you do not need to set up this function, just using callback function is enough. You need to delete that.
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,
It's good to know more about CUBEMX. then we start the project.