STM32 温湿度传感器读取温湿度数 据井显示到PC端串口助手
时间: 2023-12-06 08:01:54 浏览: 34
要使用STM32读取温湿度传感器数据,并将数据通过串口传输到PC端串口助手,需要以下步骤:
1. 确定使用的温湿度传感器型号,并连接到STM32的GPIO口上。
2. 在STM32上配置串口通信,确定波特率、数据位、停止位等参数。
3. 在STM32上编写程序,通过读取传感器数据并将其转换为数字信号,将数据通过串口发送到PC端。
4. 在PC端打开串口助手,连接到STM32配置的串口端口上,并设置与STM32相同的串口参数。
5. 在串口助手上设置正确的数据显示格式,以便正确显示温湿度数据。
下面是一个简单的示例代码,使用DHT11温湿度传感器读取数据并通过USART1串口发送到PC端:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define DHT11_PORT GPIOA
#define DHT11_PIN GPIO_Pin_0
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
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 Delay_us(u32 nus)
{
u32 i;
for (i = 0; i < nus; i++);
}
void DHT11_Start(void)
{
GPIO_ResetBits(DHT11_PORT, DHT11_PIN);
Delay_us(18000);
GPIO_SetBits(DHT11_PORT, DHT11_PIN);
Delay_us(30);
GPIO_ResetBits(DHT11_PORT, DHT11_PIN);
}
u8 DHT11_Check_Response(void)
{
u8 response = 0;
u32 timeout = 0;
while (!response && timeout < 10000)
{
if (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == RESET)
{
response = 1;
while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == RESET);
}
timeout++;
}
return response;
}
u8 DHT11_Read_Bit(void)
{
u8 bit = 0;
u32 timeout = 0;
while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == RESET && timeout < 100)
timeout++;
if (timeout >= 100)
return 0;
timeout = 0;
while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == SET && timeout < 100)
timeout++;
if (timeout >= 100)
return 0;
Delay_us(40);
if (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == SET)
bit = 1;
timeout = 0;
while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == SET && timeout < 100)
timeout++;
return bit;
}
u8 DHT11_Read_Byte(void)
{
u8 i;
u8 byte = 0;
for (i = 0; i < 8; i++)
{
byte <<= 1;
byte |= DHT11_Read_Bit();
}
return byte;
}
void DHT11_Read_Data(u8 *temp, u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Start();
if (DHT11_Check_Response())
{
for (i = 0; i < 5; i++)
buf[i] = DHT11_Read_Byte();
if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
{
*humi = buf[0];
*temp = buf[2];
}
}
}
int main(void)
{
u8 temp, humi;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_Configuration();
USART_Configuration();
while (1)
{
DHT11_Read_Data(&temp, &humi);
printf("Temperature: %dC Humidity: %d%%\r\n", temp, humi);
USART_SendData(USART1, temp);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, humi);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
Delay_us(1000000);
}
}
```
在PC端使用串口助手连接到STM32的串口端口上,设置正确的串口参数和数据显示格式,即可实时显示温湿度数据。