STM32L4 - ADC Module using the LL
- 2 replies
- 5719 views
Hi guys,
I'm tryingto use the ADC module of STM32L4 using the Low Level Library.
We created a simple project (I have a nucleo board). I used the STM32Cube Mx to generate the Setup Code (the project file is attached) and I saw a few exampletouse the ADC Module by LL.
I've setup the ADC Module in very simple mode: trigger by software and interrupt disabled.
After the STM32Cube code, I enable the ADC Module with the following code:
// ENABLE TIMER2 (SERVICE TIMER)
LL_TIM_EnableIT_UPDATE(TIM2);
LL_TIM_EnableCounter(TIM2);
// Start Internal Regulator
LL_ADC_DisableDeepPowerDown(ADC1);
LL_ADC_EnableInternalRegulator(ADC1);
// Wait the Internal Regulator Startup
Wait_Time = 50;
while( Wait_Time );
// ADC CAlibration
LL_ADC_StartCalibration(ADC1, LL_ADC_DIFFERENTIAL_ENDED);
// Wait ADC Calibration
while ( LL_ADC_IsCalibrationOnGoing(ADC1) );
Wait_Time = 50;
while( Wait_Time );
// Enable ADC
LL_ADC_Enable(ADC1);
// Wait to Start ADC
while ( LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0 );
if( LL_ADC_IsActiveFlag_EOC(ADC1) ) LL_ADC_ClearFlag_EOC(ADC1);
// Enable Timer6 and Interrupt
LL_TIM_EnableIT_UPDATE(TIM6);
LL_TIM_EnableCounter(TIM6);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
Wait_Time variable is decremented by TIMER2 interrupt (1ms), so the 'sleep time' is around 50ms. SysClock is 4Mhz and ADC Clock is 32 Mhz.
In the while loop, the start conversion is managed by the TIMER6 interrupt (500ms), using the following code:
while (1)
{
if( flag_timer6 )
{
LL_ADC_REG_StartConversion(ADC1);
while(LL_ADC_IsActiveFlag_EOC(ADC1) == 0);
if( LL_ADC_IsActiveFlag_EOC(ADC1) ) LL_ADC_ClearFlag_EOC(ADC1);
Buffer_ADC = LL_ADC_REG_ReadConversionData12(ADC1);
LL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
flag_timer6 = 0;
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
The following code works only 'one time', after the first conversion the ADC Module go in loop mode (I noted thatLL_ADC_REG_IsConversionOngoing(ADC1) return always true after the first conversion).
If I add theLL_ADC_REG_StopConversion(ADC1) in the code, the ADC Module 'seems' to work correctly
while (1)
{
if( flag_timer6 )
{
LL_ADC_REG_StartConversion(ADC1);
while(LL_ADC_IsActiveFlag_EOC(ADC1) == 0);
if( LL_ADC_IsActiveFlag_EOC(ADC1) ) LL_ADC_ClearFlag_EOC(ADC1);
Buffer_ADC = LL_ADC_REG_ReadConversionData12(ADC1);
LL_ADC_REG_StopConversion(ADC1);
LL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
flag_timer6 = 0;
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
It is correct procedure to start conversion of ADC ?
In the examples that I've read the StopConversion should not berequired.
Thanks for your support
#stm32l4-adc #ll