The following are two examples of library functions and direct register manipulation. I have tested them all.
Before using this program, you must set the GPIO to analog input mode.
=====================================Library Function Version==========================================
void AD_CONFIG_SINGLE(void)
{
//Configure the IO port first:
ADC_InitTypeDef adcInitStruct;
// //PB1 is used as the analog channel input pin
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC and GPIOC clock
//Configure ADC:
//Configure ADC to non-scanning mode (that is, not all ports in this group will be scanned each time processing), because there is only one register to save the adc result when using the rule group; single mode.
adcInitStruct.ADC_Mode = ADC_Mode_Independent;
adcInitStruct.ADC_ScanConvMode = DISABLE;
adcInitStruct.ADC_ContinuousConvMode = DISABLE;
adcInitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
adcInitStruct.ADC_DataAlign = ADC_DataAlign_Right;
InitStruct.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &adcInitStruct);
ADC_Cmd(ADC1, ENABLE);
ADC_TempSensorVrefintCmd(ENABLE);
ADC_ResetCalibration(ADC1);
while((ADC_GetResetCalibrationStatus(ADC1)));// && (timeOut--));
ADC_StartCalibration(ADC1);
while((ADC_GetCalibrationStatus(ADC1)));// && (timeOut--));
}
//Call function during each scan:
u16 AD_sysGetAdcResult(void)
{
u16 ad;
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(A DC_GetFlagStatus(ADC1, ADC_FLAG_EOC)==0);
ad = ADC_GetConversionValue(ADC1);
//ADC_TempSensorVrefintCmd(DISABLE);
//ADC_SoftwareStartConvCmd(ADC1, DISABLE);
//ADC_Cmd(ADC1, DISABLE);
return ad;
}
=====================================Register Version==========================================
void AD_CONFIG_SINGLE(void)
{
//Initialize IO port first
//RCC->APB2ENR|=1<<2; //Enable PORTA port clock
//GPIOA->CRL&=0XFFFF0000; //PA0 1 2 3 anolog input
//Channel 10/11 setting
RCC->APB2ENR|=1<<9; //ADC1 clock enable
RCC->APB2RSTR|=1<<9; //ADC1 reset
RCC->APB2RSTR&=~(1<<9); //Reset end
RCC->CFGR&=~(3<<14); //Division factor cleared
//SYSCLK/DIV2=12M ADC clock is set to 12M, ADC maximum clock cannot exceed 14M!
//Otherwise, the ADC accuracy will decrease!
RCC->CFGR|=2<<14;
ADC1->CR1&=0XF0FFFF; //Working mode clear
ADC1->CR1|=0<<16; //Independent working mode
ADC1->CR1&=~(1<<8); //Non-scanning mode
ADC1->CR2&=~(1<<1); //Single conversion mode
ADC1->CR2&=~(7<<17);
ADC1->CR2|=7<<17; //Software control conversion
ADC1->CR2|=1<<20; //Use external trigger (SWSTART)!!! An event must be used to trigger
ADC1->CR2&=~(1<<11); //Right-align
ADC1->SQR1&=~(0XF<<20);
ADC1->SQR1&=0<<20; //1 conversion in regular sequence, that is, only convert regular sequence 1
//Set the sampling time of channels 0~3
ADC1->SMPR1|=7; //Channel 10 239.5 cycles, increasing the sampling time can improve the accuracy
ADC1->CR2|=1<<0; //Turn on AD Converter
ADC1->CR2|=1<<3; //Enable reset calibration
while(ADC1->CR2&1<<3); //Wait for calibration to end
//This bit is set by software and cleared by hardware. This bit will be cleared after the calibration register is initialized.
ADC1->CR2|=1<<2; //Start AD calibration
while(ADC1->CR2&1<<2); //Wait for calibration to end
//This bit is set by software to start calibration and cleared by hardware when calibration ends
}
//Get ADC value
//ch: channel value 0~3
u16 AD_sysGetAdcResult(void)
{
//Set conversion sequence
ADC1->SQR3&=0XFFFFFFE0; //Rule sequence 1 channel ch
ADC1->SQR3|=10;
ADC1->CR2|=1<<22; //Start rule conversion channel
while(!(ADC1->SR&1<<1)); //Wait for conversion to end
return ADC1->DR; //Return adc value
}
Previous article:STM32 peripheral programming steps
Next article:STM32 HSE LSE crystal oscillator official recommendation
Recommended ReadingLatest update time:2024-11-17 04:25
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- Conaide C2000 switch quantity acquisition
- Vivado "Failed to generate and synthesize debug IPs." error solution
- Help
- Creating a Superior Smart Display Experience with DLP Pico Technology
- instaSPIN-FOC Motor Starting
- What to do if AD10 exceeds the PCB area when importing CAD
- TGF4042 Function Signal Generator Evaluation: Pulse Waveform Generation
- Practical Hardware Manual
- Let's talk about the calculation method of RCD
- Which option is the best?