本文主要是介绍温度检测小系统兼继电器模块和小风扇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 0000RS = 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.085usdatabuffer = 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;//Dwhile(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 ¸ßµçƽ1for(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);}
}
这篇关于温度检测小系统兼继电器模块和小风扇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!