cancel
Showing results for 
Search instead for 
Did you mean: 

ADC getting stuck while checking for EOC flag

deep_rune
Associate III

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?

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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".

View solution in original post

5 REPLIES 5
TDK
Guru

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
Associate III

thanks, but if I make it

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

it still gets stuck in the loop ?

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

something AND 0b00000100 can't never result in 1

JW

deep_rune
Associate III

oh yeah, thanks!

Hello @deep_rune​ ,

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

Best Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen