cancel
Showing results for 
Search instead for 
Did you mean: 

About Starting ADC

Mmee
Associate II

Hello.​

I am using the STM32L4 series.

I want to send the ADC converted sensor value to the PC via UART communication.

I started ADC in /* USER CODE BEGIN 2 */ zone of "main.c" output from CubeIDE, but it failed.

When the start instruction is given twice as shown below, the code is executed, but the sensor value cannot be obtained.

if(HAL_ADC_Start_IT(&hadc2)!=HAL_OK){ //result: TRUE

int aaa=0;

aaa=1;

if(HAL_ADC_Start_IT(&hadc2)!=HAL_OK){ //result: FALSE...why?

aaa=0;

}

}

I am at a loss because I have no idea what the cause is.

I am sorry to trouble you, but please reply regarding this matter.

8 REPLIES 8
KnarfB
Principal III

If the result is not HAL_OK, find out what it is. And/or debug-step into the second call. HAL_ADC_Start_IT - do you have interrupt enabled and a callback?

Does the first ADC "process" finished by the time you try to run it the second time?

JW

I'll try check the details of the result.

I have enabled ADC2, but not specifically enabled interrupts (see image).

​I am thinking to enable continuous conversion and get the value as below with timer interrupt.

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

if(htim == &htim7){ //1ms timer

iAdVal=HAL_ADC_GetValue(&hadc2); �? this

sprintf(sbuff, ",%d", iAdVal);

memcpy(&td[strlen(td)],sbuff,strlen(sbuff));

cnt_tim7++;

if (cnt_tim7>=TD_CNT_MAX)

{

if(huart2.gState==HAL_UART_STATE_READY){

HAL_UART_Transmit(&huart2, (uint8_t*)td, strlen(td), 0xFFFF);

memset(td, '\0', cnt_tim7);

cnt_tim7=0;

}

}

}

}

0693W000003Q2pFQAS.png

Mmee
Associate II

The ADC is set to continuous conversion mode. Need to care about the timing of the process?

The ADC value is obtained in the timer interrupt process.

iAdVal=HAL_ADC_GetValue(&hadc2);

The ADC is set to continuous conversion mode. Need to care about the timing of the process?

The ADC value is obtained in the timer interrupt process.

iAdVal=HAL_ADC_GetValue(&hadc2);

> The ADC is set to continuous conversion mode.

Then why do you start it the second time?

JW

When you start IT version then ADC runs on background. Your code continues immediately to next start end return false = cant start twice.

Plus continuos mode dont need next start without any stop used.

When parenthes in code is ok second start never occur when first is true, But if example code is repeated then both results is false,..