编写stm32f103c8t6利用AD采集电压信号并通过串口输出的程序
时间: 2023-08-01 10:06:54 浏览: 53
下面是一个使用STM32F103C8T6的ADC模块进行电压采集并通过串口输出的示例代码,仅供参考:
```c
#include "stm32f10x.h"
#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void ADC_Configuration(void);
void delay_us(uint32_t nCount);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
ADC_Configuration();
while (1)
{
uint16_t adc_value = 0;
float voltage = 0.0;
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adc_value = ADC_GetConversionValue(ADC1);
voltage = (float)adc_value * 3.3 / 4096; // 根据stm32f103c8t6的ADC参考电压计算电压值
char buffer[50];
sprintf(buffer, "Voltage: %.2fV\r\n", voltage);
USART_SendString(USART1, buffer);
delay_us(100000); // 延时一段时间再进行下一次采样
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// ADC采样引脚配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 串口引脚配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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);
USART_Cmd(USART1, ENABLE);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
// ADC参数设置
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);
// ADC采样时钟配置
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
// ADC通道设置
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_28Cycles5);
// ADC使能
ADC_Cmd(ADC1, ENABLE);
// ADC校准
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
void delay_us(uint32_t nCount)
{
uint32_t i;
for(i=0; i<nCount*8; i++);
}
void USART_SendString(USART_TypeDef* USARTx, char* str)
{
while(*str)
{
USART_SendData(USARTx, *str++);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
}
```
上述代码的主要功能是:初始化ADC模块进行连续采样,将采样结果转换为电压值并通过串口输出。需要注意的是,在使用ADC模块时需要先进行初始化和校准。同时在采样期间需要等待ADC转换完成,并在每次采样之间加入一定的延时,以避免连续采样时间过短导致结果不准确。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)