stm32 problem with ADC conversion - Page 1
Forum section: electronics / general

author: mikewax
author_rank: Contributor
timestamp: March 26, 2017, 03:09:44 am
content: can someone check my setup please? in line 1 the MCU, an stm32F070RB, does a conversion and in line 5 it does another one. but the second conversion fails and the program hangs up at line 7. if i suppress line 7 the program will continue uninterrupted.the ADC register values are shown below.Code:1 ADC1->CR |= (uint32_t)ADC_CR_ADSTART; // start first conversion2 count = 0;3 while(((ADC1->ISR & ADC_ISR_EOC) == (uint32_t)reset) && (count < timeout)) count++; // wait for end of conversion4 capVL = (uint16_t)ADC1->DR; // read value5 ADC1->CR |= (uint32_t)ADC_CR_ADSTART; // start second conversion6 count = 0;7(//) while(((ADC1->ISR & ADC_ISR_EOC) == (uint32_t)reset) && (count < timeout)) count++;8 capVH = (uint16_t)ADC1->DR; // read value

author: mbless
author_rank: Regular Contributor
timestamp: March 26, 2017, 05:59:11 pm
content: Someone correct me if I wrong, but from what I understand if you're only doing single conversions you have to stop the ADC before starting it again.

author: mikewax
author_rank: Contributor
timestamp: March 26, 2017, 07:33:20 pm
content: yes that's what the "while(((ADC1->ISR & ADC_ISR_EOC)" is for, waiting for the End Of Conversion bit to set. but you're not supposed to have to explicitely stop the ADC process. at least not in my experience.

author: mbless
author_rank: Regular Contributor
timestamp: March 26, 2017, 07:56:03 pm
content: I know you're not using the HAL but according to it, you have to call the stop function HAL_ADC_Stop() then call HAL_ADC_Start() to read another value in single, polling mode. See the excerpt from stm32f4xx_hal_adc.c below. A similar questions was asked onstack exchange.Code:[..](#) ADC driver can be used among three modes: polling, interruption,transfer by DMA.*** Polling mode IO operation ***=================================[..](+) Start the ADC peripheral using HAL_ADC_Start()(+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stageuser can specify the value of timeout according to his end application(+) To read the ADC converted values, use the HAL_ADC_GetValue() function.(+) Stop the ADC peripheral using HAL_ADC_Stop()*** Interrupt mode IO operation ***===================================[..](+) Start the ADC peripheral using HAL_ADC_Start_IT()(+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine(+) At ADC end of conversion HAL_ADC_ConvCpltCallback() function is executed and user canadd his own code by customization of function pointer HAL_ADC_ConvCpltCallback(+) In case of ADC Error, HAL_ADC_ErrorCallback() function is executed and user canadd his own code by customization of function pointer HAL_ADC_ErrorCallback(+) Stop the ADC peripheral using HAL_ADC_Stop_IT()*** DMA mode IO operation ***==============================[..](+) Start the ADC peripheral using HAL_ADC_Start_DMA(), at this stage the user specify the lengthof data to be transferred at each end of conversion(+) At The end of data transfer by HAL_ADC_ConvCpltCallback() function is executed and user canadd his own code by customization of function pointer HAL_ADC_ConvCpltCallback(+) In case of transfer Error, HAL_ADC_ErrorCallback() function is executed and user canadd his own code by customization of function pointer HAL_ADC_ErrorCallback(+) Stop the ADC peripheral using HAL_ADC_Stop_DMA()

author: Jeroen3
author_rank: Super Contributor
timestamp: March 26, 2017, 08:17:16 pm
content: Just wait for the ADSTART bit to clear. Should work.Quote from: RM0091Anyway, I recommend setting up a timer triggered sequence and the DMA. You save power by not letting it run free, and you'll still have recent values.

author: mikewax
author_rank: Contributor
timestamp: March 26, 2017, 08:22:25 pm
content: ok i gotta look into that cause i AM using HAL. but that's for the usb, not for the ADC.the essential code goes like this:Code:uint8_t i, v;uint8_t word = { 'w', 'o', 'r', 'd', 32, 32, 32 };int main(void){// usb setup-----------------HAL_Init();SystemClock_Config();MX_GPIO_Init();MX_USB_DEVICE_Init();// ADC setup-----------------ADC1_Config();v = 1;while (1){i = checkVcap(); // <--ADC routine where it hangsif(v++ % 25 == 0) VCP_write(word, strlen((const char *)word);// <- usb send wordHAL_Delay(10);v++;}}the ADC1_Config() came from the standard peripheral library. the usb setup routines came from the (puke) stm32CubeMX library. it didn't occur to me that the HAL functions could interfere.i'll go back and try that.thanx.

author: mikewax
author_rank: Contributor
timestamp: March 26, 2017, 09:01:39 pm
content: Quote from: Jeroen3 on March 26, 2017, 08:17:16 pmwell there's a possible solution that i didn't think about. i'll try it. but i would still like to know why it seems that the EOC bit is not setting.thanx

author: mikewax
author_rank: Contributor
timestamp: March 26, 2017, 10:55:56 pm
content: Quote from: Jeroen3 on March 26, 2017, 08:17:16 pmwell i'm not using DMA i just read the ADC and calculate the output. it's doing ADC readings at about 100Hz. you think it would save power if i have it disable the ADC between readings?

author: mikewax
author_rank: Contributor
timestamp: March 26, 2017, 11:07:34 pm
content: Quote from: mbless on March 26, 2017, 07:56:03 pmthanx for the idea, it did lead me to a rather ambiguous resolution. i Xed out the usb function and ran the program again. Lo and behold,THAT WAS IT!the program stopped hanging at the ADC routine. the usb, it seems, was interfering.here's where it gets weird. i always repeat the test 2 or 3 times. i re-included the usb function and not only does the program continue to run correctly, it outputs the right #s, so i know the ADC is reading the accurate voltage. so i can't repeat the test because now the original problem is gone.four days i been at it, and i still have no clue what was wrong.so the problem is resolved but never solved. i just hope it stays that way.go figure....and thanx guys for the assistance

author: rimpi
author_rank: Newbie
timestamp: September 21, 2018, 04:37:44 pm
content: can u plz hlp to make a code for start and stop the counter using adc in stm32f0.m stuck in this code

author: rimpi
author_rank: Newbie
timestamp: September 22, 2018, 05:24:46 am
content: plz hlp to write code to start n stop counter using adc in stm32plzzzzz.......