1.思路:
代码还要封装!
延迟1秒;串口初始化;LCD1602显示屏初始化;延迟两秒;ledone不亮;
while循环,延迟1秒,DHT模块读取数据;封装接收数据函数;发送湿度数据;发送温度数据
注:在这段代码中;ledOne = P3^7; dht = P3^3; datas [5] (接收DHT11的数据); temp[8] ,huma[8] (用来存取接受的数据);
2.加继电器和小风扇模块
sbit fengshan = P1^6;
//datas[2] 是温度
if(datas[2] > 25){
fengshan = 0; // 超过25度给我转
}
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;
char datas[5];
sfr AUXR = 0x8E;
#define databuffer P0
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;
sbit fengshan = P1^6;
char temp[8];
char huma[8];
void Delay15ms() //@11.0592MHz
{
unsigned char i, j;
i = 27;
j = 226;
do
{
while (--j);
} while (--i);
}
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 9;
j = 244;
do
{
while (--j);
} while (--i);
}
void check_busy() //?
{
char tmp = 0x80;
databuffer = 0x80;
while(tmp & 0x80){ //1000 0000
RS = 0;
RW = 1;
EN = 0; //??????·
_nop_(); //???????,??
EN = 1; //??2?
_nop_();
_nop_();
tmp = databuffer;
EN = 0; //??
_nop_();
}
}
void Write_Cmd_Func(char cmd)
{
check_busy();
RS = 0;
RW = 0;
EN = 0;
_nop_();
databuffer = cmd;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Write_Data_Func(char dataShow)
{
check_busy();
RS = 1;
RW = 0;
EN = 0;
_nop_(); //??1.085us
databuffer = dataShow;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void UartInit(void)
{
AUXR = 0x01;
SCON = 0x40;
TMOD &= 0xF0;
TMOD |= 0x20;
TH1 =0xFD;
TL1 =0xFD;
TR1 = 1;
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI); //(TI==0)
TI = 0;
}
void sendString(char *str)
{
while(*str != '\0'){
sendByte(*str);
str++;
}
}
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay30ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay40us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
void DHT11_Start()
{
dht = 1;
dht = 0;
Delay30ms();
dht = 1;
//D
while(dht);
while(!dht);
while(dht);
}
void Read_Data_From_DHT()
{
int i; //ÂÖ
int j; // ÿÂÖ¶Á¶àÉÙ´Î
char tmp;
char flag;
DHT11_Start();
for(i = 0;i<5;i++){
//¿¨gµã: while(!dht) ÓÐЧÊý¾ÝÊǸߵçƽ£¬³ÖÐøʱ¼ä²»Ò»Ñù£¬50us¶Á£¬µÍµçƽ0 ¸ßµçƽ1
for(j = 0;j<8;j++){
while(!dht); // µÈ´ý¿¨gµã
Delay40us();
if(dht==1){
flag = 1;
while(dht); //
}else{
flag = 0;
}
tmp = tmp << 1;
tmp |= flag; //
}
datas[i] = tmp;
}
}
void LCD1602_showLine(char row,char col,char *string)
{
switch(row){
case 1:
Write_Cmd_Func(0x80+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
case 2:
Write_Cmd_Func(0x80+0x40+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
}
}
void LCD1602_INIT()
{
Delay15ms();
Write_Cmd_Func(0x38);
Delay5ms();
Write_Cmd_Func(0x38);
Write_Cmd_Func(0x08);
Write_Cmd_Func(0x01);
Write_Cmd_Func(0x06);
Write_Cmd_Func(0x0c);
}
void Bulid_Datas()
{
huma[0] = 'H';
huma[1] = datas[0]/10 + 0x30;
huma[2] = datas[0]%10 + 0x30;
huma[3] = '.';
huma[4] = datas[1]/10 + 0x30;
huma[5] = datas[1]%10 + 0x30;
huma[6] = '%';
huma[7] = '\0';
temp[0] = 'T';
temp[1] = datas[2]/10 + 0x30;
temp[2] = datas[2]%10 + 0x30;
temp[3] = '.';
temp[4] = datas[3]/10 + 0x30;
temp[5] = datas[3]/10 + 0x30;
temp[6] = 'C';
temp[7] = '\0';
}
void main()
{
Delay1000ms();
UartInit();
if(datas[2] > 25){
fengshan = 0; // 超过25度给我转
}
LCD1602_INIT();
Delay1000ms();
Delay1000ms();
ledOne = 1;
while(1){
Delay1000ms();
Read_Data_From_DHT();
Bulid_Datas();
sendString(huma);
sendString("\r\n");
sendString(temp);
sendString("\r\n");
LCD1602_showLine(1,2,huma);
LCD1602_showLine(2,2,temp);
}
}
代码优化:
知识点一:跨函数调用datas[ ] 时,要定义外部变量 extern char datas[5]
bug: 在LCD1602.c文件中
//void check_busy()
//void Write_Cmd_Func(char cmd)
//void Write_Data_Func(char dataShow)
//其他地方没有调用它,函数内部调用它,在头文件中就不用声明
//大bug:LCD1602.h里面要声明 ,为什么呢?
//因为在在LCD1602.c文件中,在定义这两个函数之前 就调用了下面两个函数,所以会报错
//void Write_Cmd_Func(char cmd)
//void Write_Data_Func(char dataShow)
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#include "Uart.h"
#include "LCD1602.h"
#include "dht11.h"
#include "config.h"
char temp[8];
char huma[8];
extern char datas[5];
void Bulid_Datas()
{
huma[0] = 'H';
huma[1] = datas[0]/10 + 0x30;
huma[2] = datas[0]%10 + 0x30;
huma[3] = '.';
huma[4] = datas[1]/10 + 0x30;
huma[5] = datas[1]%10 + 0x30;
huma[6] = '%';
huma[7] = '\0';
temp[0] = 'T';
temp[1] = datas[2]/10 + 0x30;
temp[2] = datas[2]%10 + 0x30;
temp[3] = '.';
temp[4] = datas[3]/10 + 0x30;
temp[5] = datas[3]/10 + 0x30;
temp[6] = 'C';
temp[7] = '\0';
}
void main()
{
Delay1000ms();
UartInit();
LCD1602_INIT();
Delay1000ms();
Delay1000ms();
ledOne = 0;
while(1){
Delay1000ms();
Read_Data_From_DHT(datas);
if(datas[2] > 25){
fengshan = 0;
}else{
fengshan = 1;
}
Bulid_Datas();
sendString(huma);
sendString("\r\n");
sendString(temp);
sendString("\r\n");
LCD1602_showLine(1,2,huma);
LCD1602_showLine(2,2,temp);
}
}