STM32F103C8T6 电压采集的程序
时间: 2023-07-29 09:12:14 浏览: 76
好的,以下是基于STM32F103C8T6的电压采集程序,你可以参考一下:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C) // ADC1 data register address
void RCC_Configuration(void);
void GPIO_Configuration(void);
void ADC_Configuration(void);
void USART_Configuration(void);
void USART_SendString(USART_TypeDef* USARTx, uint8_t* string);
volatile uint16_t ADC_Value;
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
USART_Configuration();
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // start ADC conversion
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); // wait for conversion complete
ADC_Value = ADC_GetConversionValue(ADC1); // get ADC conversion value
float voltage = ADC_Value * 3.3 / 4096; // calculate voltage
printf("Voltage: %.2fV\n", voltage); // print voltage value to USART
delay_ms(1000); // delay 1 second
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Configure PA1 as analog input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
// Configure ADC1
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
// Configure ADC1 channel 1
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5);
// Enable ADC1
ADC_Cmd(ADC1, ENABLE);
// Calibrate ADC1
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
// Configure USART1
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
// Enable USART1
USART_Cmd(USART1, ENABLE);
}
void USART_SendString(USART_TypeDef* USARTx, uint8_t* string)
{
while (*string)
{
USART_SendData(USARTx, *string++);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
}
void delay_ms(uint32_t ms)
{
volatile uint32_t nCount;
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
nCount = (RCC_Clocks.HCLK_Frequency / 10000) * ms;
for (; nCount != 0; nCount--);
}
```
这个程序使用了STM32F103C8T6的ADC模块来采集PA1引脚的电压值,并通过串口输出打印出来。你可以根据自己的需求进行修改。
阅读全文