2020-07-27 06:33 AM
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?
Solved! Go to Solution.
2020-07-27 08:46 AM
Your code waits for the EOC flag to be reset, not set. Your logic is backwards.
2020-07-27 08:46 AM
Your code waits for the EOC flag to be reset, not set. Your logic is backwards.
2020-07-28 03:35 AM
thanks, but if I make it
while(((ADC1->ISR) & 0b00000100)!=1)
{
}
it still gets stuck in the loop ?
2020-07-28 03:39 AM
> while(((ADC1->ISR) & 0b00000100)!=1)
something AND 0b00000100 can't never result in 1
JW
2020-07-28 03:44 AM
oh yeah, thanks!
2020-07-30 08:34 AM
Hello @deep_rune ,
Please select the post as a "Best Answer" if it solved your problem or answered your question.
Best Regards,
Imen