|
我打算设计一个室内环境监测系统 这是第一阶段的设计使用DHT11测量温湿度显示在TFT显示屏上,目前可以通过串口调试助手接收信息。
如果温度或湿度超过阈值蜂鸣器会发出响声
但是发现DHT11误差比较大
后面我打算再加一个光敏传感器和PM2.5的检测或者噪音分贝的测量,通过ESP8266能够把检测的消息发送到手机端或PC
下面是我用的器件
主要代码如下
- #include "stm32f10x.h"
- #include "beep.h"
- #include "delay.h"
- #include "dht11.h"
- #include "key.h"
- #include "lcd.h"
- #include "led.h"
- #include "usart.h"
- #include "picture.h"
- #include "stdio.h"
- int main(void)
- {
- u8 T;
- u8 H;
- u8 t=0,i;
- char TTT[20],HHH[20];
- USART1_init();
- BEEP_Init();
- DHT11_Init();
- LCD_Init();
- LCD_BLK = 0;
- delay_ms(6000);
- LCD_Clear(YELLOW);
- LCD_DrawFont_GBK16(10,40,BLACK,YELLOW, "系统正在启动中");
- delay_ms(100);
- LCD_DrawFont_GBK16(10,60,BLACK,YELLOW, "正在进入...");
- for(i=3;i>0;i--)
- {
- LCD_DrawFont_Num32(50,80,RED,YELLOW,i);
- delay_ms(1000);
- }
- LCD_Clear(GREEN);
- LCD_DrawFont_GBK16(25,0,BLUE,GREEN, "运行中...");
- LCD_DrawFont_GBK16(5,20,BLACK,GREEN, "温度:");
- LCD_DrawFont_GBK16(5,40,BLACK,GREEN, "湿度:");
- LCD_DrawFont_GBK16(5,60,BLACK,GREEN, "光照:");
-
- LCD_DrawFont_GBK16(61,20,BLACK,GREEN, "度");
- LCD_DrawFont_GBK16(61,40,BLACK,GREEN, "%RH");
- LCD_DrawFont_GBK16(61,60,BLACK,GREEN, "CD");
-
- while(1)
- {
- if(t%10==0)
- {
- DHT11_Read_Data(&T,&H); //读取温湿度值
- sprintf(TTT,"%d",T); //将温度值转换为字符格式
- sprintf(HHH,"%d",H); //将湿度值转换为字符格式
- printf("温度: %d 度\r\n ",T);
- printf("湿度: %d RH\r\n ",H);
- }
- if(T>25||H>94) //报警阈值
- {
- if(T>25)
- {
- LCD_DrawFont_GBK16(45,20,RED,GREEN,TTT);
- }
- if(H>94)
- {
- LCD_DrawFont_GBK16(45,40,RED,GREEN,HHH);
- }
- GPIO_SetBits(BEEP_PORT,BEEP_PIN);
- delay_ms(10);
- GPIO_ResetBits(BEEP_PORT,BEEP_PIN);
- }
- else
- {
- LCD_DrawFont_GBK16(45,20,BLACK,GREEN,TTT);
- LCD_DrawFont_GBK16(45,40,BLACK,GREEN,HHH);
- }
- delay_ms(100);
- t++;
- }
- }
复制代码- #ifndef __DHT11_H
- #define __DHT11_H
- #include "sys.h"
- #define DHT11_IO_IN() {GPIOA->CRH&=0XFFFF0FFF;GPIOA->CRH|=8<<12;}
- #define DHT11_IO_OUT() {GPIOA->CRH&=0XFFFF0FFF;GPIOA->CRH|=3<<12;}
- ////IO操作函数
- #define DHT11_DQ_OUT PAout(11) //数据端口 PA11
- #define DHT11_DQ_IN PAin(11) //数据端口 PA11
- u8 DHT11_Init(void);//初始化DHT11
- u8 DHT11_Read_Data(u8 *temp,u8 *humi);//读取温湿度
- u8 DHT11_Read_Byte(void);//读出一个字节
- u8 DHT11_Read_Bit(void);//读出一个位
- u8 DHT11_Check(void);//检测是否存在DHT11
- void DHT11_Rst(void);//复位DHT11
- #endif
复制代码
|
评分
-
查看全部评分
|