stm32f103c8t67路adc读取并通过串口发送工程文件
时间: 2023-08-01 21:12:19 浏览: 98
以下是一个使用 STM32F103C8T6 的 ADC 模块读取电压值,并通过串口发送的示例工程代码:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
void UART1_Init(void);
void ADC_Configuration(void);
void ADC1_Init(void);
void Delay_us(uint32_t nCount);
int main(void)
{
float voltage;
char str[32];
UART1_Init();
ADC_Configuration();
ADC1_Init();
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
voltage = (float)ADC_GetConversionValue(ADC1) / 4096.0f * 3.3f;
sprintf(str, "Voltage: %.2f V\r\n", voltage);
printf(str);
Delay_us(1000000); // 延时 1 秒钟
}
}
void UART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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);
USART_Cmd(USART1, ENABLE);
}
void ADC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
}
void ADC1_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
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);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void Delay_us(uint32_t nCount)
{
uint32_t i, j;
for (i = 0; i < nCount; i++)
{
for (j = 0; j < 8; j++);
}
}
```
这个工程的实现流程是:
1. 配置 ADC1 模块,将引脚 PA0 配置为模拟输入,同时配置 ADC1 的采样时间等参数;
2. 配置串口,使用 USART1 和 PA9、PA10 引脚进行通讯;
3. 在 while 循环中,使用 ADC_SoftwareStartConvCmd 函数启动 ADC1 的采样,并通过 ADC_GetFlagStatus 函数等待采样完成;
4. 使用 ADC_GetConversionValue 函数获取 ADC1 采样结果,并将其转换为电压值,最后使用 sprintf 函数格式化输出到串口上。
需要注意的是,这个工程中使用了 printf 函数来输出数据到串口,因此需要在工程中添加 stdio.h 库,并在编译选项中启用 printf 的重定向功能。
阅读全文