stm32f103c8t6基于标准库使用AHT10串口输出代码
时间: 2024-01-05 11:03:27 浏览: 119
```c
#include "stm32f10x.h"
#include "stdio.h"
#define USART1_DR_Base 0x40013804
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
// USART1_TX -> PA9 , USART1_RX -PA10
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);
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART1_SendChar(char ch)
{
while (!(USART1->SR & USART_FLAG_TXE));
USART1->DR = (ch & 0xFF);
}
int fputc(int ch, FILE *f)
{
USART1_SendChar(ch);
return ch;
}
void AHT10_Init(void)
{
// 初始化AHT10传感器
// ...
}
float AHT10_ReadTemperature(void)
{
// 读取AHT10温度数据
// ...
}
float AHT10_ReadHumidity(void)
{
// 读取AHT10湿度数据
// ...
}
int main(void)
{
USART1_Init();
AHT10_Init();
while (1)
{
float temperature = AHT10_ReadTemperature();
float humidity = AHT10_ReadHumidity();
printf("Temperature: %.2f C\r\n", temperature);
printf("Humidity: %.2f %%\r\n", humidity);
delay_ms(1000);
}
}
```
阅读全文