1、STC15单片机DHT11在LCD1602上显示程序敬告:没有51单片机基础的人请慎重下载高质量实用性51单片机STC15W系列程序(4),STC8A系列可参考STC15单片机DHT11在LCD1602上显示程序注:在本节关于DHT11的程序有两种/*/版本一:/*/*/main函数程序:#include Library.hunsigned char strbuf4;/void ValToStr(unsigned char *str,unsigned char *source,unsigned char len);unsigned char tmrflag = 0;unsigned char
2、DHTbuf5;void Delay1000ms() /11.0592MHz unsigned char i, j, k; _nop_(); _nop_(); i = 43; j = 6; k = 203; do do while (-k); while (-j); while (-i);void main() unsigned char DHTstr8; P3M1 &= 0xBF; P3M0 &= 0xBF; P2M1 &= 0xE5; P2M0 &= 0xE5; P0M1 = 0x00; P0M0 = 0x00; LCD1602_init(); LCD1602_wBytes(2,0,.,1
3、); LCD1602_wBytes(8,0,humi,4); LCD1602_wBytes(2,1,.,1); LCD1602_wBytes(8,1,temp,4); while(1) DHT11_start(DHTbuf); / ValToStr(DHTstr,DHTbuf,sizeof(DHTbuf)-1); DHTstr0 = DHTbuf0/10 + 0; DHTstr1 = DHTbuf0%10 + 0; DHTstr2 = DHTbuf1/10 + 0; DHTstr3 = DHTbuf1%10 + 0; DHTstr4 = DHTbuf2/10 + 0; DHTstr5 = DH
4、Tbuf2%10 + 0; DHTstr6 = DHTbuf3/10 + 0; DHTstr7 = DHTbuf3%10 + 0; LCD1602_wBytes(0,0,DHTstr,2); LCD1602_wBytes(3,0,DHTstr+2,2); LCD1602_wBytes(0,1,DHTstr+4,2); LCD1602_wBytes(3,1,DHTstr+6,2); Delay1000ms(); Delay1000ms(); /void ValToStr(unsigned char *str,unsigned char *source,unsigned char len)/ un
5、signed char i;/ while(len0)/ / stri*2 = sourcei/10+0;/ str1+i*2 = sourcei%10+0; / i+;/ len-; / /*/DHT11程序:/*8bit humidity integer data + 8bit humidity decimal data8bit temperature integer data + 8bit temperature decimal data8bit check sum,high bit ahead*/#include Library.hunsigned char u8flag;unsign
6、ed char check_buf5;unsigned char tmp;void delay_1s() /11.0592MHz unsigned char i, j, k; _nop_(); _nop_(); i = 43; j = 6; k = 203; do do while (-k); while (-j); while (-i);void Delay10us() /11.0592MHz unsigned char i; _nop_(); i = 25; while (-i);void Delay20ms() /11.0592MHz unsigned char i, j, k; _no
7、p_(); _nop_(); i = 1; j = 216; k = 35; do do while (-k); while (-j); while (-i);unsigned char Rec_8bit() unsigned char i; unsigned char ret_8bit; unsigned char tmp; for(i=0;i8;i+) while(!DHT11port); Delay10us(); Delay10us(); Delay10us(); tmp = 0; if(DHT11port) tmp=1; u8flag = 2; while(DHT11port)&u8f
8、lag+); if(u8flag=1)break; ret_8bit0) LCD1602_wdat(*buf+); buf_len-; void OnCursor() LCD1602_wcmd(0x0F);void OffCursor() LCD1602_wcmd(0x0C);void LCD1602_init()/ Delay15ms();/ LCD1602_wcmd(0x38);/ Delay5ms(); LCD1602_wcmd(0x38);/ LCD1602_wcmd(0x08); LCD1602_wcmd(0x06); LCD1602_wcmd(0x0C); LCD1602_wcmd
9、(0x01);/*/Library.h#ifndef _Library_H#define _Library_H#include #include #define MAIN_Fosc 11059200Lsbit DHT11port = P36;void DHT11_start(unsigned char *rec_buf);sbit rs = P24;sbit rw = P23;sbit en = P21;void LCD1602_init();void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsig
10、ned char buf_len);#endif/*/版本二(结构体):/*/*/main程序:#include Library.h#include string.hvoid Delay1000ms() /11.0592MHz unsigned char i, j, k; _nop_(); _nop_(); i = 43; j = 6; k = 203; do do while (-k); while (-j); while (-i);void main() unsigned char DHTstr8; P3M1 &= 0xBF; P3M0 &= 0xBF; P2M1 &= 0xE5; P2M
11、0 &= 0xE5; P0M1 = 0x00; P0M0 = 0x00; LCD1602_init(); LCD1602_wBytes(2,0,.,1); LCD1602_wBytes(8,0,humi,4); LCD1602_wBytes(2,1,.,1); LCD1602_wBytes(8,1,temp,4); while(1) DHT11_start();/ memset(DHTstr, 0, 8); DHTstr0 = ht.humi_h +0 ; DHTstr1 = ht.humi_l+0 ; DHTstr2 = ht.humi_dh +0 ; DHTstr3 = ht.humi_d
12、l +0 ; DHTstr4 = ht.temp_h +0 ; DHTstr5 = ht.temp_l +0 ; DHTstr7 = ht.temp_dh +0 ; DHTstr6 = ht.temp_dl +0 ; LCD1602_wBytes(0,0,DHTstr,2); LCD1602_wBytes(3,0,DHTstr+2,2); LCD1602_wBytes(0,1,DHTstr+4,2); LCD1602_wBytes(3,1,DHTstr+6,2); Delay1000ms(); Delay1000ms(); /*/DHT11程序:/*8bit humidity integer
13、data + 8bit humidity decimal data8bit temperature integer data + 8bit temperature decimal data8bit check sum,high bit ahead*/#include Library.hunsigned char u8flag;unsigned char check_buf5;unsigned char tmp;unsigned char rec_buf4;htstruct ht;void delay_1s() /11.0592MHz unsigned char i, j, k; _nop_()
14、; _nop_(); i = 43; j = 6; k = 203; do do while (-k); while (-j); while (-i);void Delay10us() /11.0592MHz unsigned char i; _nop_(); i = 25; while (-i);void Delay20ms() /11.0592MHz unsigned char i, j, k; _nop_(); _nop_(); i = 1; j = 216; k = 35; do do while (-k); while (-j); while (-i);unsigned char R
15、ec_8bit() unsigned char i; unsigned char ret_8bit; unsigned char tmp; for(i=0;i8;i+) while(!DHT11port); Delay10us(); Delay10us(); Delay10us(); tmp = 0; if(DHT11port) tmp=1; u8flag = 2; while(DHT11port)&u8flag+); if(u8flag=1)break; ret_8bit=1; ret_8bit |= tmp; return ret_8bit;void DHT11_start()/ dela
16、y_1s(); DHT11port = 0; Delay20ms(); DHT11port = 1; Delay10us(); Delay10us(); Delay10us(); Delay10us(); /DHT11port = 1; if(!DHT11port) u8flag=2; while(!DHT11port)&u8flag+); u8flag=2; while(DHT11port)&u8flag+); check_buf0 = Rec_8bit(); check_buf1 = Rec_8bit(); check_buf2 = Rec_8bit(); check_buf3 = Rec
17、_8bit(); check_buf4 = Rec_8bit(); if(!DHT11port) while(!DHT11port); DHT11port = 1; tmp = check_buf0+check_buf1+check_buf2+check_buf3; if(tmp=check_buf4) rec_buf0 = check_buf0; rec_buf1 = check_buf1; rec_buf2 = check_buf2; rec_buf3 = check_buf3; ht.humi_h = rec_buf0/10 ; ht.humi_l = rec_buf0%10; ht.h
18、umi_dh = rec_buf1/10; ht.humi_dl = rec_buf1%10; ht.temp_h = rec_buf2/10; ht.temp_l = rec_buf2%10; ht.temp_dh = rec_buf3/10; ht.temp_dl = rec_buf3%10; else ht.humi_h = 0; ht.humi_l = 0; ht.humi_dh = 0; ht.humi_dl = 0; ht.temp_h = 0; ht.temp_l = 0; ht.temp_dh = 0; ht.temp_dl = 0; /*/LCD1602程序:#include
19、 Library.hvoid LCD1602_rsta() unsigned char tmp; P0 = 0xFF;/this is a must rs = 0; rw = 1; do en = 1; /Delay1us(); tmp = P0; /Delay1us(); en = 0; while(tmp&0x80);void LCD1602_wdat(unsigned char dat) LCD1602_rsta(); rs=1; rw=0; P0 = dat; en = 1; /Delay1us(); en = 0;void LCD1602_wcmd(unsigned char cmd) LCD1602_rsta(); rs=0; rw=0; P0 = cmd; en = 1; /Delay1us(); en = 0;void Setcursor(unsigned char x,unsigned char y) if(y=0) x = x + 0x00; else if(y=1) x = x + 0x40; LCD1602_wcmd(
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2