cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 - Battery level

Nicolas Bousmanne
Associate
Posted on May 11, 2017 at 09:25

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 4 In the first place, I thought that was done but when I let the battery discharge itself, the return value still is 4..

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-

3 REPLIES 3
RomainR.
ST Employee
Posted on May 11, 2017 at 11:48

Hello Nicolas,

It seems that your adc_initialize () function is incomplete.

You must also configure the channel (PA2 = ADC1 Channel 2) after ADC_init()

ADC_RegularChannelConfig() configure ADC Sample time.

...
ADC_Init (ADC1, & ADC_init_structure);
ADC_RegularChannelConfig (ADC1, ADC_Channel_2, 1, ADC_SampleTime_3Cycles);�?�?
ADC_Cmd (ADC1, ENABLE);
...�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on May 11, 2017 at 14:11

Thank you it seems to work fine.

I was actually doing it after the ADC_Cmd which seems to be the error.

Well it's working now, thanks you !

Posted on May 11, 2017 at 15:29

Hi

Bousmanne.Nicolas

,

Thank you it seems to work fine.

https://community.st.com/0D50X00009bMM5DSAW

-Nesrine-