CT117E程序设计题。 基于STM32开发板实现USART通信程序设计,具体要求如下: (1)当STM32从串口调试助手接收到‘ef’,实现LCD显示温度功能; (2)当STM32从串口调试助手接收到‘gh’,实现LCD显示湿度功能;
时间: 2024-03-08 17:46:59 浏览: 8
这是一道关于STM32开发板的USART通信程序设计题目,您需要实现根据串口调试助手发送的指定字符,显示对应的温度或湿度数据在LCD上。以下是一个简单的示例代码,供您参考:
```
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "dht11.h"
#include "lcd.h"
void USART_Configuration(void);
void USART1_IRQHandler(void);
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_Configuration();
LCD_Init();
DHT11_Init();
while(1)
{
}
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_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_ITConfig(USART1, USART_IT_RXNE, 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_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint16_t data = USART_ReceiveData(USART1);
if(data == 'e') // 温度
{
float temperature = DHT11_Read_Temperature();
LCD_Clear();
LCD_ShowString(0, 0, "Temperature");
LCD_ShowNum(0, 1, (int)temperature, 2, 16);
LCD_ShowString(2, 1, ".");
LCD_ShowNum(3, 1, (int)((temperature - (int)temperature) * 10), 1, 16);
}
else if(data == 'g') // 湿度
{
float humidity = DHT11_Read_Humidity();
LCD_Clear();
LCD_ShowString(0, 0, "Humidity");
LCD_ShowNum(0, 1, (int)humidity, 2, 16);
LCD_ShowString(2, 1, ".");
LCD_ShowNum(3, 1, (int)((humidity - (int)humidity) * 10), 1, 16);
}
}
}
```
在这个示例代码中,我们首先初始化了USART1串口、LCD显示屏和DHT11温湿度传感器。然后,在USART1接收到数据时,我们判断数据是否为指定的字符(‘e’或‘g’),如果是,就读取对应的温度或湿度数据,并在LCD上显示出来。
请注意,您需要根据您的具体硬件和需求进行相应的修改和调整。同时,您也需要实现DHT11温湿度传感器的读取功能。
希望这个示例代码能对您有所帮助,祝您好运!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)