编写stm32f103c8t6利用AD采集电压信号并通过串口输出的程序
时间: 2023-08-01 07:06:55 浏览: 102
STM32的AD采集信号,并发送到串口
下面是一个简单的示例程序,它使用STM32F103C8T6的ADC模块来采集电压信号,并通过串口输出到PC上:
```
#include "stm32f10x.h"
#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_Configuration(void);
void ADC_Configuration(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
uint16_t ADCValue;
float Voltage;
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
ADC_Configuration();
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
ADCValue = ADC_GetConversionValue(ADC1);
Voltage = ADCValue * 3.3 / 4096.0; // 根据板子电压调整系数
printf("ADC value: %d, Voltage: %.2fV\r\n", ADCValue, Voltage);
Delay(0xFFFFF);
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_ADC1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART1_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_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_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
void Delay(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t)ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
```
在这个示例程序中,我们使用了PA0引脚作为ADC输入通道。在ADC初始化中,我们配置了ADC模块的工作模式、采样通道、采样时间等参数。在主循环中,我们通过ADC_SoftwareStartConvCmd函数启动ADC转换,并使用ADC_GetConversionValue函数获取转换结果。然后,我们将结果转换为电压值,并通过printf函数输出到串口。最后,我们使用Delay函数延迟一段时间,以控制输出频率。
请注意,在使用此代码之前,您需要在STM32CubeMX或其他相关工具中配置UART和ADC模块,并生成正确的初始化代码。
阅读全文