#include "stm32f10x.h"
/*RCC clock configuration*/
void RCC_config(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC registers are set to default configuration */
RCC_DeInit();
/* Turn on external high speed clock */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait for the external high-speed clock to stabilize*/
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Set HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* Set PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* Set PCLK1 = HCLK / 2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
// /* Set FLASH code delay */
// FLASH_SetLatency(FLASH_Latency_2);
// /* Enable prefetch cache */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Set the PLL clock source to HSE multiplier 9 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait for PLL to stabilize */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Set PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait for the system clock source to switch to PLL */
while(RCC_GetSYSCLKSource() != 0x08);
}
}
/* Millisecond delay function */
void delay_ms(uint16_t time)
{
uint16_t i = 0;
while(time--)
{
i = 12000;
while(i--);
}
}
/* GPIO configuration */
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Clock configuration */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/* ADC configuration */
void ADC_config(void)
{
ADC_InitTypeDef ADC_InitStructure;
/* Enable clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Configure ADC clock 12MHz */
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /* Independent mode*/
ADC_InitStructure.ADC_ScanConvMode = DISABLE; /* Single channel mode*/
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; /* Single conversion mode*/
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; /* Software trigger*/
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /* Data right aligned*/
ADC_InitStructure.ADC_NbrOfChannel = 1; /* 1 channel */
ADC_Init(ADC1, &ADC_InitStructure);
/* Analog channel 0, sampling sequence 1, sampling period 55.5 */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC */
ADC_Cmd(ADC1, ENABLE);
/* Reset calibration registers */
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start calibration */
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
/* Main function */
int main(void)
{
uint32_t value;
volatile float V;
/*RCC clock configuration*/
RCC_config();
/* IO placement */
GPIO_config();
/* ADC configuration */
ADC_config();
while(1)
{
/* Start conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
/* Wait for the conversion to finish */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
/* Get the conversion value */
value = ADC_GetConversionValue(ADC1);
/* Convert digital quantity to voltage value*/
V = (float)value / 4096 * 3.3;
delay_ms(1000);
}
}
Previous article:STM32 SPI slave DMA routine
Next article:STM32 ADC principle
Recommended ReadingLatest update time:2024-11-16 13:30
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- "Play Board" + Shared Bicycle Control Board - GPS Module ublox MAX-M8Q
- HPM6750 Review of Pioneer Semiconductor Step 3 (RT-Thread + TinyUSB)
- Application and classification of energy storage technology in photovoltaic power generation
- MSP430 - G2553 Serial Port Operation
- [ESK32-360 Review] + Sensor Measurement and Output Display
- ARM editing and compilation tools
- EEWORLD University - What is an antenna vibrator?
- [RISC-V MCU CH32V103 Review] First Look at CH32V103
- May was quite busy, and my hand broke.
- Pre-registration for the prize-winning live broadcast | Infineon system solutions make electric motorcycle design more reliable and efficient!