怎么解決啊
#include <reg52.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LCD_DATA P0 // 數(shù)據(jù)口
sbit LCD_RS = P2^0; // 命令/數(shù)據(jù)選擇位
sbit LCD_RW = P2^1; // 讀/寫選擇位
sbit LCD_EN = P2^2; // 使能信號位
sbit PSB = P2^6;
//定義DHT11傳感器的引腳
sbit DHT11=P2^4;
void _nop_(void);
void delay(unsigned int t) { // 延時函數(shù)
unsigned int i, j;
for (i = t; i > 0; i--) {
for (j = 110; j > 0; j--);
}
}
void delay_us(int us)// 延時子程序
{
char j;
while(us--)
{
for(j=0;j<1200;j++);
}
}
void WriteCommand(unsigned char cmd) { // 寫命令函數(shù)
LCD_RS = 0; // RS=0,選擇命令
LCD_RW = 0; // RW=0,寫入數(shù)據(jù)
LCD_DATA = cmd; // 寫入命令
LCD_EN = 1; // 使能
_nop_(); // 等待
LCD_EN = 0; // 失能
delay(1); // 延時
}
void WriteData(unsigned char dat) { // 寫數(shù)據(jù)函數(shù)
LCD_RS = 1; // RS=1,選擇數(shù)據(jù)
LCD_RW = 0; // RW=0,寫入數(shù)據(jù)
LCD_DATA = dat; // 寫入數(shù)據(jù)
LCD_EN = 1; // 使能
_nop_(); // 等待
LCD_EN = 0; // 失能
delay(1); // 延時
}
void InitLCD12864() { // 初始化函數(shù)
WriteCommand(0x30); // 設置8位數(shù)據(jù)總線,基本指令集
WriteCommand(0x0C); // 打開顯示,關閉光標
WriteCommand(0x01); // 清屏
WriteCommand(0x06); // 設置文字寫入方向,自動加地址
}
void lcd_write_str(unsigned char row, unsigned char col, char *str) { // 寫字符串函數(shù)
unsigned char addr = 0;
if (row == 0) { // 第1行
addr = 0x80 + col;
} else if (row == 1) { // 第2行
addr = 0x90 + col;
} else if (row == 2) { // 第3行
addr = 0x88 + col;
} else if (row == 3) { // 第4行
addr = 0x98 + col;
}
WriteCommand(addr); // 設置DDRAM地址
while (*str != '\0') { // 寫入字符串
WriteData(*str++);
}
}
void DHT11_start() { // 啟動DHT11傳感器函數(shù)
DHT11 = 0; // 拉低數(shù)據(jù)線
delay_us(18000); // 延時18ms
DHT11 = 1; // 拉高數(shù)據(jù)線
delay_us(40); // 延時40us
while (!DHT11); // 等待DHT11響應
while (DHT11); // 等待DHT11響應結(jié)束
}
void DHT11_read_data(unsigned char *humi, unsigned char *temp) {
unsigned char i, j, f[5] = {0, 0, 0, 0, 0};
DHT11_start(); // 啟動DHT11傳感器
// 發(fā)送起始信號
DHT11 = 1;
delay_us(40);
DHT11 = 0;
delay_us(20);
DHT11 = 1;
// 等待DHT11響應
while (!DHT11);
while (DHT11);
while (!DHT11);
// 讀取數(shù)據(jù)
for (i = 0; i < 5; i++) {
for (j = 0; j < 8; j++) {
while (!DHT11);
delay_us(30);
if (DHT11) {
f[ i] |= (1 << (7 - j));
}
while (DHT11);
}
}
// 驗證數(shù)據(jù)
if ((f[0] + f[1] + f[2] + f[3]) == f[4]) {
*humi = f[0]; // 濕度
*temp = f[2]; // 溫度
} else {
*humi = 0;
*temp = 0;
}
delay(2000); // 延時2秒等待下一次讀取
}
void main() {
unsigned char humi, temp; // 定義溫濕度變量
char temp_str[5], humi_str[5]; // 定義字符串變量,用于存放溫濕度值
InitLCD12864(); // 初始化LCD12864顯示屏
while (1) {
DHT11_read_data(&humi, &temp); // 讀取溫濕度數(shù)據(jù)
sprintf(temp_str, "%d", temp); // 將溫度值轉(zhuǎn)換為字符串類型
sprintf(humi_str, "%d", humi); // 將濕度值轉(zhuǎn)換為字符串類型
lcd_write_str(0, 0, "當前溫度:"); // 在第1行第1列顯示"當前溫度:"
lcd_write_str(1, 0, "當前濕度:"); // 在第2行第1列顯示"當前濕度:"
lcd_write_str(0, 7, temp_str); // 在第1行第7列顯示當前溫度
lcd_write_str(1, 7, humi_str); // 在第2行第7列顯示當前濕度
delay(1000); // 延時1秒
}
}
|