AnsweredAssumed Answered

stm32f4 adc problem

Question asked by ctc.ctc on Apr 21, 2013
Latest reply on Apr 21, 2013 by ctc.ctc
I would like to use stm32f4 to measure a battery voltage, like AA1.5v 
I connect the postive line to PC5 and gnd line to GND.

I reference the example code on the Internet. But the code is stop at the while of adc flag. I don't know why it didn't work. If I delete the flag while. the adc value keep 0, it's not i want. I have no idea how to modify my code. And I use gnu tool to build the .bin file. Is it possible that I lose some important compile parameter?

this is me code. thx

uint32_t adcValue=0;
uint32_t adcVoltage=0;
int main()
{
     init_adc();
     ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_3Cycles);
     
     while(1){
          //Start ADC1 Conversion
          ADC_SoftwareStartConv(ADC1);


          while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
          adcValue= ADC_GetConversionValue(ADC1);
          adcVoltage = adcValue * (5.0/4096.0);
     }


}
void init_adc()
{
     ADC_InitTypeDef ADC_InitStructure;
     GPIO_InitTypeDef GPIO_InitStructure;
     ADC_CommonInitTypeDef ADC_CommonInitStructure;


            //Enable ADC1 
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);


     //Enable the ADC1 GPIO Clock     
           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 


     //Initializing GPIO for ADC
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOC, &GPIO_InitStructure);


     //ADC Common Init


     //Use Independent mode
     ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
     ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
     ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
     ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
     ADC_CommonInit(&ADC_CommonInitStructure);


     //ADC1 Init
     ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
     ADC_InitStructure.ADC_ScanConvMode = DISABLE;
     ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
     ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
     ADC_InitStructure.ADC_NbrOfConversion = 1;
     ADC_Init(ADC1, &ADC_InitStructure);


     //Enabling Temperature sensor
     ADC_TempSensorVrefintCmd(ENABLE);


     //Enable ADC1
     ADC_Cmd(ADC1, ENABLE);
}

Outcomes