AnsweredAssumed Answered

STM32 - Battery level

Question asked by Nicolas Bousmanne on May 11, 2017
Latest reply on May 11, 2017 by Nesrine M

Hi to everyone,

 

I am a newbie in STM32 programation and I m not actually sure that is the right way to ask my question...

I hope I can find some help because I have some issues just for basic stuff.

 

Here is a little description : I have a STM32F405 board with a battery integrated and the voltage measure is plug on the PA2 entry of the board. After some search on internet, I am using ADC to read that measure and the return value is 4095. In the first place, I thought that was done but when I let the battery discharge itself, the return value still is 4095...

 

Can you explain me what's wrong in my code or eventually show me some documentation ?

 

int main(void)

{

   int ConvertedValue = 0;

   adc_initialize();

   while (1)

   {

      ConvertedValue = adc_convert_bat();

      printf("Value battery : %d\n", ConvertedValue);

   }

}

int adc_convert_bat()
{
    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_144Cycles);
    ADC_SoftwareStartConv(ADC1);
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
    return ADC_GetConversionValue(ADC1);
}
void adc_initialize()
{
    ADC_InitTypeDef ADC_init_structure;
    GPIO_InitTypeDef GPIO_initStructre;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOAEN, ENABLE);
 
    GPIO_initStructre.GPIO_Pin = GPIO_Pin_2;
    GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN;
    GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_initStructre);
 
    ADC_DeInit();
    ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_init_structure.ADC_Resolution = ADC_Resolution_12b;
    ADC_init_structure.ADC_ContinuousConvMode = ENABLE;
    ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
    ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_init_structure.ADC_NbrOfConversion = 1;
    ADC_init_structure.ADC_ScanConvMode = DISABLE;
 
    ADC_Init(ADC1, &ADC_init_structure);
    ADC_Cmd(ADC1, ENABLE);
}

 

Thanks to you all and sorry if the message is send at a wrong place...

Nico-

Outcomes