stm32f103c8t6ADC读取电压
时间: 2025-04-19 11:52:14 浏览: 21
### STM32F103C8T6 ADC Voltage Read Example Code and Configuration
For the STM32F103C8T6 microcontroller, configuring and using the Analog-to-Digital Converter (ADC) involves several steps including setting up the clock system, initializing GPIO pins connected to analog channels, enabling DMA or interrupts if necessary, and finally writing a function that reads from an ADC channel.
The following is an example of how one might configure and use the ADC on this device:
#### Hardware Initialization
Firstly, it's important to initialize hardware resources such as clocks and GPIOs before starting any conversion process. The RCC must be configured properly so that peripherals like ADC can operate at their required frequencies[^1].
```c
// Enable peripheral clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
```
#### ADC Channel Selection & Configuration
Next comes selecting which pin will serve as input for our measurements along with its corresponding ADC channel number. In addition, settings related specifically to each individual channel need adjustment too – these include resolution mode selection among others[^2].
```c
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/* Configure PA0 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Initialize ADC structure parameters */
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // Single-channel conversion
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;// One-shot conversion
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
```
#### Starting Conversion Process
Once everything has been set correctly according to your needs then you may proceed by initiating conversions either through software trigger method calls provided within HAL libraries or directly manipulating registers depending upon what level of abstraction suits best during development phase.[^3]
```c
// Start ADC calibration
ADC_Cmd(ADC1, DISABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
// Enable ADC
ADC_Cmd(ADC1, ENABLE);
// Wait until ADON bit is reset
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)==RESET);
// Start single conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
```
#### Reading Results
Finally after all preparations have completed successfully we are ready now not only start but also obtain results produced out from performed operations above mentioned earlier hereafter:[^4]
```c
uint16_t adcValue;
adcValue = ADC_GetConversionValue(ADC1);
float voltage = ((3.3f / 4095) * adcValue); // Assuming Vref=3.3V and 12-bit resolution
```
阅读全文
相关推荐


















