编写stm32f103c8t6利用AD采集电压信号并通过串口输出的程序
时间: 2023-08-01 14:07:54 浏览: 96
STM32F103C8T6例程.zip_STM32F103C8T6 AD_courtwai_exclaimedty8_stm32
5星 · 资源好评率100%
好的,这是一个简单的示例程序,可以帮助你了解如何在STM32F103C8T6上使用ADC和USART:
```c
#include "stm32f10x.h"
void RCC_Configuration(void)
{
// 打开GPIOA、USART1和ADC1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE);
// 打开ADC1的DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 配置PA0为模拟输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置PA9为USART1的TX引脚
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);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
// ADC1配置为单次模式,扫描模式,使用软件触发
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
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);
// 配置ADC1的通道0为采样通道
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
// 启用ADC1
ADC_Cmd(ADC1, ENABLE);
// 开始ADC1的校准
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
// USART1配置为9600波特率,8位数据位,1位停止位,无校验位
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);
// 启用USART1
USART_Cmd(USART1, ENABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
// 配置DMA1通道1,从ADC数据寄存器读取数据,传输到USART1的数据寄存器
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(&ADC1->DR);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
// 启用DMA1通道1
DMA_Cmd(DMA1_Channel1, ENABLE);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
USART_Configuration();
DMA_Configuration();
while (1) {
// 启动ADC转换
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// 等待ADC转换完成
while (!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
// 将ADC转换结果通过USART发送出去
uint16_t adc_value = ADC_GetConversionValue(ADC1);
char str[16];
sprintf(str, "%d\r\n", adc_value);
USART_SendData(USART1, (uint16_t)str);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
```
这个程序会周期性地读取ADC通道0的值,并通过USART1发送出去。你可以在串口调试工具中查看输出的结果。请注意,这个程序仅供参考,你需要根据自己的实际情况进行调整。
阅读全文