Skip to main content
deep_rune
Associate III
July 27, 2020
Solved

ADC getting stuck while checking for EOC flag

  • July 27, 2020
  • 4 replies
  • 1482 views

Im trying to write code that will average samples from the ADC of an STM32F01K6. I am starting a conversion and waiting while the conversion takes place by writing a while loop which checks the finish flag

 ADC1->CR |= ADC_CR_ADSTART;
	 	 while(((ADC1->ISR) & 0b00000100)!=0)
	 	 {	 		 
	 	 }
for(j=0;j<4;j++)
		{
		 ADC_result_pre[i][j] = ADC1->DR; //fetch adc data
		}
		ADC_result[i]=(ADC_result_pre[i][0]+ADC_result_pre[i][1]+ADC_result_pre[i][2]+ADC_result_pre[i][3])/4;
 
		 switch(ADC1->CHSELR) //switch dac channel to the next
		 {
		 case 0b1:
			 ADC1 ->CHSELR = 0b10;
			 i=1;
			 break;
 
		 case 0b10:
			 ADC1 ->CHSELR = 0b1;
			 i=0;
			 break;
		 }

If i write code which doesn't wait for the EOC flag, the ADC works fine. However, with this code it gets stuck in the while loop, the EOC flag never appears. Where have I gone wrong?

This topic has been closed for replies.
Best answer by TDK

Your code waits for the EOC flag to be reset, not set. Your logic is backwards.

4 replies

TDK
TDKBest answer
Super User
July 27, 2020

Your code waits for the EOC flag to be reset, not set. Your logic is backwards.

"If you feel a post has answered your question, please click ""Accept as Solution""."
deep_rune
deep_runeAuthor
Associate III
July 28, 2020

thanks, but if I make it

while(((ADC1->ISR) & 0b00000100)!=1)
	 	 {	 		 
	 	 }

it still gets stuck in the loop ?

waclawek.jan
Super User
July 28, 2020

> while(((ADC1->ISR) & 0b00000100)!=1)

something AND 0b00000100 can't never result in 1

JW

deep_rune
deep_runeAuthor
Associate III
July 28, 2020

oh yeah, thanks!

Technical Moderator
July 30, 2020

Hello @deep_rune​ ,

Please select the post as a "Best Answer" if it solved your problem or answered your question.

Best Regards,

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks