1、设计思路温湿度采集传感器:DHT11实物图与单片机连接图DHT11时序主机发送请求信号从机发送应答信号 位数据“0”的格式位数据“1”的格式温湿度显示:LCD1602引脚LCD_1602 初始化一些重要指令:0 x38 设置16*2显示,5*7点阵,8位数据接口0 x01 清屏0 x0F 开显示,显示光标,光标闪烁0 x08 只开显示0 x0e 开显示,显示光标,光标不闪烁0 x0c 开显示,不显示光标0 x06 地址加1,当写入数据的时候光标右移0 x02 地址计数器AC=0;(此时地址为0 x80) 光标归原点,但是DDRAM中断内容不变0 x18 光标和显示一起向左移动 1602读写时序
2、图总电路图程序如下:#include #include typedef unsigned char uchar;typedef unsigned int unint;unsigned char str1=;unsigned char str2=;unsigned char code dis1 = TRH RH: ;unsigned char code dis2 = TRH T: ;sbit TRH = P27;/温湿度传感器DHT11数据接入sbit LCD_RS = P23; sbit LCD_RW = P24;sbit LCD_EN = P25;sbit jiare = P14;sbit
3、jiangwen= P15;sbit zengshi= P16sbit jiangshi= P17;uchar TH_data,TL_data,RH_data,RL_data,CK_data;uchar TH_temp,TL_temp,RH_temp,RL_temp,CK_temp;uchar com_data,untemp,temp;uchar respond; void delayNOP() _nop_(); _nop_(); _nop_(); _nop_();void delay_us() /5us级延时程序 uchar i; i-; i-; i-; i-; i-; i-; void d
4、elay_ms(unsigned char ms) / 毫秒级延时子程序 unsigned char i; while(ms-) for(i = 0; i 150; i+) _nop_(); _nop_(); _nop_(); _nop_(); / 测试LCD忙碌状态lcd_busy()为1时,忙,等待。lcd_busy()为0时,闲,可写指令与数据。 bit lcd_busy() bit result; LCD_RS = 0; LCD_RW = 1; LCD_EN = 1; delayNOP(); result = (bit)(P0&0 x80); LCD_EN = 0; return(re
5、sult); / 写入指令数据到LCDvoid lcd_wcmd(unsigned char cmd) while(lcd_busy(); LCD_RS = 0; LCD_RW = 0; LCD_EN = 0; _nop_(); _nop_(); P0 = cmd; delayNOP(); LCD_EN = 1; delayNOP(); LCD_EN = 0; /写显示数据到LCD:RS=高电平,RW=低电平,E=高脉冲,D0-D7=数据。 void lcd_wdata(unsigned char dat) while(lcd_busy(); LCD_RS = 1; LCD_RW = 0; L
6、CD_EN = 0; P0 = dat; delayNOP(); LCD_EN = 1; delayNOP(); LCD_EN = 0; /LCD初始化设定void lcd_init() delay_ms(15); lcd_wcmd(0 x38); /16*2显示,5*7点阵,8位数据 delay_ms(5); lcd_wcmd(0 x0c); /显示开,关光标 delay_ms(5); lcd_wcmd(0 x06); /移动光标 delay_ms(5); lcd_wcmd(0 x01); /清除LCD的显示内容 delay_ms(5); /设定LCD显示位置 void lcd_dis_po
7、s(unsigned char pos) lcd_wcmd(pos | 0 x80); /数据指针=80+地址变量 /收发信号检测,数据读取char receive() uchar i;com_data=0;for(i=0;i=7;i+) respond=2; while(!TRH)&respond+); delay_us(); delay_us(); delay_us(); if(TRH) temp=1; respond=2; while(TRH)&respond+); else temp=0; com_data=1; com_data|=temp; return(com_data); /读
8、取温湿度子程序void read_TRH()TRH=0;delay_ms(18);TRH=1;delay_us();delay_us();delay_us();delay_us();if(!TRH) respond=2; while(!TRH)& respond+); respond=2; while(TRH & respond+); /数据接收状态 RH_temp = receive(); RL_temp = receive(); TH_temp = receive(); TL_temp = receive(); CK_temp = receive(); TRH=1; /数据校验 untem
9、p=(RH_temp+RL_temp+TH_temp+TL_temp); if(untemp=CK_temp) RH_data = RH_temp; RL_data = RL_temp; TH_data = TH_temp; TL_data = TL_temp; CK_data = CK_temp; str10 = (char)(0X30+RH_data/10); str11 = (char)(0X30+RH_data%10);str12 = 0 x2e; /小数点 str13 = (char)(0X30+RL_data/10); str15 = 0X25; /%str16 = 0X52; /
10、Rstr17 = 0X48; /H str20 = (char)(0X30+TH_data/10); str21 = (char)(0X30+TH_data%10);str22 = 0 x2e; /小数点 str23 = (char)(0X30+TL_data/10); str25 = 0X27; /str26 = 0X43; /C主程序void main() unsigned char i,n=0 x40,m; lcd_init(); delay_us(); while(1) read_TRH(); for(i=0;i=7;i+) lcd_dis_pos(i); /显示字符 lcd_wdata(dis1i); lcd_dis_pos(n+i); /显示字符 lcd_wdata(dis2i); /写湿度数据 m=0 x08; for(i=0;i=7;i+) lcd_dis_pos(m); lcd_wdata(str1i); m+; /写温度数据 m=0 x48; for(i=0;i=7;i+) lcd_dis_pos(m); lcd_wdata(str2i); m+;