51单片机用LCD1602显示温度、湿度、光度,分别写明DHT11头文件、LCD1602头文件、主程序。
时间: 2023-10-06 10:12:36 浏览: 83
DHT11温湿度传感器51单片机在LCD1602显示程序.doc
5星 · 资源好评率100%
DHT11头文件:
```c
#ifndef _DHT11_H_
#define _DHT11_H_
#include <reg51.h>
sbit DHT11 = P2^0;
void DHT11_Delay(unsigned int i);
void DHT11_Start(void);
unsigned char DHT11_ReadData(void);
#endif
```
LCD1602头文件:
```c
#ifndef _LCD1602_H_
#define _LCD1602_H_
#include <reg51.h>
sbit LCD1602_RS = P1^0;
sbit LCD1602_RW = P1^1;
sbit LCD1602_EN = P1^2;
void LCD1602_Delay(unsigned int i);
void LCD1602_WriteCmd(unsigned char cmd);
void LCD1602_WriteData(unsigned char dat);
void LCD1602_Init(void);
void LCD1602_Clear(void);
void LCD1602_SetCursor(unsigned char x, unsigned char y);
void LCD1602_WriteString(unsigned char x, unsigned char y, unsigned char *str);
void LCD1602_WriteChar(unsigned char x, unsigned char y, unsigned char dat);
#endif
```
主程序:
```c
#include <reg51.h>
#include "DHT11.h"
#include "LCD1602.h"
void main()
{
unsigned char temp_hum[5];
unsigned char light;
unsigned char str[16];
LCD1602_Init();
DHT11_Start();
while(1)
{
temp_hum[0] = DHT11_ReadData();
temp_hum[1] = DHT11_ReadData();
temp_hum[2] = DHT11_ReadData();
temp_hum[3] = DHT11_ReadData();
temp_hum[4] = DHT11_ReadData();
light = P0;
LCD1602_Clear();
sprintf(str, "Temp:%dC Hum:%d%%", temp_hum[0], temp_hum[2]);
LCD1602_WriteString(0, 0, str);
sprintf(str, "Light:%d", light);
LCD1602_WriteString(0, 1, str);
DHT11_Delay(500);
}
}
```
阅读全文