Programming Tips
Define
GPIO
ADC_InitTypeDef
ADC Clock
Channel conversion order and sampling time
Enable ADC conversion complete interrupt
Enable ADC conversion start
Calibrating the ADC
Select ADC conversion trigger source
Configuring interrupt priority
Writing an interrupt service function
main function
/*-----GPIO-----*/
static void ADC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_GPIO_APBxClock_FUN ( ADC_GPIO_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = ADC_PIN; //Must be analog input
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_PORT, &GPIO_InitStructure);
}
/*-----ADC_InitTypeDef-----*/
static void ADCx_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
// Turn on the ADC clock
ADC_APBxClock_FUN ( ADC_CLK, ENABLE );
//ADC mode configuration
// Only one ADC is used, which is independent mode
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// Disable scanning mode, only required for multi-channel, not required for single channel
ADC_InitStructure.ADC_ScanConvMode = DISABLE ;
// Continuous conversion mode
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
// No external trigger conversion required, just start the software
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//Conversion result is right aligned
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
//Convert channel 1
ADC_InitStructure.ADC_NbrOfChannel = 1;
// Initialize ADC
ADC_Init(ADCx, &ADC_InitStructure);
// Configure the ADC clock to be divided by 8 of PCLK2, i.e. 9MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
//Configure ADC channel conversion order and sampling time
ADC_RegularChannelConfig(ADCx, ADC_CHANNEL, 1,
ADC_SampleTime_55Cycles5);
// ADC conversion ends and generates an interrupt. Read the conversion value in the interrupt service routine
ADC_ITConfig(ADCx, ADC_IT_EOC, ENABLE);
// Turn on ADC and start conversion
ADC_Cmd(ADCx, ENABLE);
// Initialize ADC calibration register
ADC_ResetCalibration(ADCx);
// Wait for calibration register initialization to complete
while(ADC_GetResetCalibrationStatus(ADCx));
//ADC starts calibration
ADC_StartCalibration(ADCx);
// Wait for calibration to complete
while(ADC_GetCalibrationStatus(ADCx));
// Since no external trigger is used, the ADC conversion is triggered by software
ADC_SoftwareStartConvCmd(ADCx, ENABLE);
}
/*-----NVIC-----*/
static void ADC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// Priority grouping
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//Configure interrupt priority
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*-----Heat-----*/
void ADCx_Init(void)
{
ADCx_GPIO_Config();
ADCx_Mode_Config();
ADC_NVIC_Config();
}
/*-----Interrupt service subfunction-----*/
void ADC_IRQHandler(void)
{
if (ADC_GetITStatus(ADCx,ADC_IT_EOC)==SET)
{
// Read the conversion value of ADC
ADC_ConvertedValue = ADC_GetConversionValue(ADCx);
}
ADC_ClearITPendingBit(ADCx,ADC_IT_EOC);
}
Previous article:STM32 basic design (1) --- Light up the LED
Next article:Voltage display based on STM32F103
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Thinkpad Advanced Docking Station 40A80045CN Disassembly & Minor Modification (2)
- [Mill Edge AI Computing Box FZ5 Review] Real-time Video Filling
- How to learn this new skill? I want to use the HAL library of STM32 and I want to use cubemx software
- Dissecting a small chip
- MSP430 Introduction and Software Installation
- TMS320C6748 Fixed-point and Floating-point DSP
- BlueNRG-LP sometimes cannot enter low power consumption
- 10 Tips for Writing Better Embedded Software Code
- FPGA Timing Constraints Practice: Multi-Cycle Path Constraints
- Didn’t Vicor apply for a Chinese patent?