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
Previous article:Solution to "no target connect" when downloading STM32 SWD
Next article:STM32 encoder mode learning
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- Internal structure of ESP8266 chip
- Primary side issues of push-pull circuits
- [Free trial of Pingtouge Bluetooth Mesh Gateway Development Kit] + Gateway module and sub-device cloud platform and problem solving supplement
- How many components do you think are needed to make an adjustable power supply?
- Purgatory Legend-PS2 Interface War
- A brief introduction to the third generation semiconductors
- STM32H745I-DISCO
- How to program the C2000 on-chip Flash?
- EEWORLD University ---- Tektronix uses mixed signal oscilloscope to explore the secrets of the bus
- New RTOS Tutorial: Getting Started with RT-Thread Kernel in 15 Days