1
votes

I have and STM32f3 Discovery Board, and I'm trying to use 4 ADC channels. This is my read function:

int ADC_read(int channel)
{
    ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_7Cycles5);
    ADC_StartConversion(ADC1);
    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != SET);
    return ADC_GetConversionValue(ADC1);    
}

everything goes perfectly well, until suddenly program stucks in the while loop. is there a way to avoid putting a ADC_FLAG_EOC in a loop? or just some other way to make my program work?

2

2 Answers

0
votes

Is far as I understand depending on which version of the board you are using you must use ADC_SoftwareStartConvCmd or ADC_StartConversion.

So maybe you are not starting ADC conversion properly, please try with this:

ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_7Cycles5);

#if defined(SERIES_STM32F10x)
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
#elif defined(SERIES_STM32F30x)
  ADC_StartConversion(ADC1);
#else
  ADC_SoftwareStartConv(ADC1);
#endif

while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
return ADC_GetConversionValue(ADC1);

Also, Don't forget to you set your pins mode to input with: pinMode(pin, INPUT_ANALOG);

Finally, take a look at this post where the user had a similar problem to yours and solved it by re-enabling external trigger with:

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
0
votes
  • RegularChannelConfig should not need to be run on each call, it probably hasn't had time to complete the config, and hangs when you start it. Put a wait before you start.

    • look at the examples

    • why use the ADC this way when you can get the DMA to run the ADC for you and simply access the value(s) from memory when you need it (them).

STM-Cube generates most of this code for you, correctly.