STM32 ADC gets battery voltage

Publisher:初入茅庐Latest update time:2018-12-13 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

adc.c


#include "adc.h"


void  Adc_Init(void)

{    

  ADC_InitTypeDef ADC_InitStructure; 

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1, ENABLE); //Enable ADC1 channel clock

RCC_ADCCLKConfig(RCC_PCLK2_Div6); //Set ADC division factor 6 72M/6=12, ADC maximum time cannot exceed 14M

//PA4 is used as analog channel input pin                         

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Analog input pin

GPIO_Init(GPIOA, &GPIO_InitStructure);

ADC_DeInit(ADC1); //Reset ADC1 and reset all registers of peripheral ADC1 to default values

ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC working mode: ADC1 and ADC2 work in independent mode

ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Analog-to-digital conversion works in single-channel mode

ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Analog-to-digital conversion works in single conversion mode

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //Conversion is started by software instead of external trigger

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC data right aligned

ADC_InitStructure.ADC_NbrOfChannel = 1; //Number of ADC channels for sequential regular conversion

ADC_Init(ADC1, &ADC_InitStructure); //Initialize the registers of the peripheral ADCx according to the parameters specified in ADC_InitStruct   

ADC_Cmd(ADC1, ENABLE); //Enable the specified ADC1

ADC_ResetCalibration(ADC1); //Enable reset calibration   

while(ADC_GetResetCalibrationStatus(ADC1)); //Wait for reset calibration to end

ADC_StartCalibration(ADC1); //Start AD calibration

while(ADC_GetCalibrationStatus(ADC1)); //Wait for calibration to end

}


u16 Get_Adc(u8 ch)   

{

  //Set the specified ADC rule group channel, a sequence, sampling time

ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1, ADC channel, sampling time is 239.5 cycles        

ADC_SoftwareStartConvCmd(ADC1, ENABLE); //Enable the software conversion start function of the specified ADC1  

while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )); //Wait for the conversion to end

return ADC_GetConversionValue(ADC1); //Return the most recent conversion result of the ADC1 rule group

}


int Get_battery_volt(void)   

{  

int Volt; //Battery voltage

Volt=Get_Adc(Battery_Ch)*3.3*11.5*100/1.5/4096; //Resistor voltage division, according to the schematic diagram, a simple analysis can be obtained

if(Volt>1260)Volt=1260;

return Volt;

}


adc.h


#ifndef _ADC_H

#define _ADC_H



#include "config.h"


#define Battery_Ch 4

void Adc_Init(void);

u16 Get_Adc(u8 ch);

int Get_battery_volt(void); 


#endif

Keywords:STM32 Reference address:STM32 ADC gets battery voltage

Previous article:Solution to "no target connect" when downloading STM32 SWD
Next article:STM32 encoder mode learning

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号