DHT11_LCD1602温湿度检测与显示程序
DHT11测温湿度程序lcd1602显示
DHT11测温湿度程序lcd1602显示#include<reg52.h>#include<intrins.h>#define uchar unsigned char #define uint unsigned int #define Data P0 //数据端口sbit RS=P2^4;sbit RW=P2^5;sbit E=P2^6;sbit DHT=P1^0;uchar FirstLine[] ="wen:00.00"; //第一行数据uchar SecondLine[]="shi:00.00"; //第二行数据unsigned char shiZ,shiX,wenZ,wenX,check;unsigned char tr_shiZ,tr_shiX,tr_wenZ,tr_wenX;unsigned char flag;unsigned int n=20,m;void delay_1ms(unsigned int i) {unsigned int j=88;for(;i>0;i--){while(j>0)j--;}}void delay_10us(){unsigned char i;i--;i--;i--;i--;i--;i--;}/******************************************/ /*************温湿度读取函数***************/ /******************************************/ char read_data(){unsigned char i,num,temp;num=0;for(i=0;i<8;i++){flag=2;while((!DHT)&&flag++);delay_10us();delay_10us();delay_10us();if(DHT==1){temp=1;flag=2;while(DHT&&flag++);}elsetemp=0;num<<=1;num|=temp;}return(num);}void delay(uchar ms) // 延时函数ms毫秒 { uchar i,j;for(i=ms;i>0;i--) for(j=100;j>0;j--); }void DelayUs(unsigned char us) //--延时函数 { unsigned char uscnt;uscnt=us>>1; /*12MHz频率*/while(--uscnt); }void DelayMs(unsigned char ms){while(--ms){DelayUs(250);DelayUs(250);DelayUs(250);DelayUs(250);}}void lcd_write_com(uchar c) //写命令 { DelayMs(5);//操作前短暂延时,保证信号稳定E=0;RS=0;RW=0;_nop_();E=1;Data=c;E=0;}void lcd_write_dat(uchar c) //写数据 { DelayMs(5); //操作前短暂延时,保证信号稳定E=0;RS=1;RW=0;_nop_();E=1;Data=c;E=0;RS=0;}void lcd_init() //LCD初始化{DelayMs(15);lcd_write_com(0x38); //display modelcd_write_com(0x38); //display modelcd_write_com(0x38); //display modelcd_write_com(0x06); //显示光标移动位置lcd_write_com(0x0c); //显示开及光标设置lcd_write_com(0x01); //显示清屏}void ShowChar(uchar pos,uchar c) //显示单个字符 { unsigned char p;if (pos>=0x10)p=pos+0xb0; //是第二行则命令代码高4位为0xcelsep=pos+0x80; //是第二行则命令代码高4位为0x8lcd_write_com(p);//写命令lcd_write_dat(c); //写数据}void ShowString (uchar line,char *ptr) //显示字符串{unsigned char l,*p;p=ptr;l=line<<4;while((*p)!='\0'){ShowChar(l++,*(p));p++;}}void disp(void) //主函数调用的显示函数 {ShowString(0,FirstLine);ShowString(1,SecondLine); }/******************************************//************初始化及采集程序**************//******************************************/ void read_init(){DHT=0; //主机使DHT11低电平并延时至少18msdelay_1ms(21);DHT=1; //主机置DHT11高电平20~40us,并等待从机相应delay_10us();delay_10us();delay_10us();delay_10us();DHT=1;if(!DHT) //从机发出相应信号{flag=2;while((!DHT)&&flag++);flag=2;while(DHT&&flag++); //开始采集数据tr_shiZ=read_data();//采集湿度整数部分tr_shiX=read_data();//采集湿度小数部分tr_wenZ=read_data();//采集温度整数部分tr_wenX=read_data();//采集温度小数部分check=read_data(); //采集校验位DHT=1;}}void main(){unsigned char temp;lcd_init();delay(50);while(1){disp();read_init();temp=tr_shiZ+tr_shiX+tr_wenZ+tr_wenX;if(check==temp){shiZ=tr_shiZ;shiX=tr_shiX;wenZ=tr_wenZ;wenX=tr_wenX;}FirstLine[4]='0'+wenZ/10; FirstLine[5]='0'+wenZ%10; FirstLine[8]='0'+wenX/10; FirstLine[9]='0'+wenX%10; SecondLine[4]='0'+shiZ/10; SecondLine[5]='0'+shiZ%10; SecondLine[8]='0'+shiX/10; SecondLine[9]='0'+shiX%10; }}。
Arduino从DHT11读取温湿度数据并显示在1602LCD
Arduino从DHT11读取温湿度数据并显⽰在1602LCD硬件清单Arduino NANO1602LCD + PCF8574T模块YL-47 DHT11模块连线1. 连接LCD: PCF8574T模块4pin(Gnd, Vcc, SDA i2c数据, SCL i2c时钟) 连接⾄Arduino接⼝ Gnd -> Gnd, Vcc -> Vcc, SDA -> A4, SDL -> A52. 连接YL-47 DHT11: Gnd -> Gnd, Vcc -> Vcc, Data-> D4Library除了1602需要的库以外, 需要安装两个⾃带的库: DHT Sensor Library by Adafruit, Adafruit Unified Sensor测试代码#include <Wire.h>#include <LiquidCrystal_I2C.h>#include <DHT.h>#define DHTPIN 4#define DHTTYPE DHT11// I2C地址, ⼀般为0x3F, 0x20或0x27LiquidCrystal_I2C lcd(0x27,16,2);// 初始化DHTDHT dht(DHTPIN, DHTTYPE);void setup() {lcd.init();lcd.backlight(); // 打开背光Serial.begin(9600);dht.begin();lcd.setCursor(0,0); // line 0, pos 0lcd.print("Good Day!");lcd.setCursor(0,1); // line 1, pos 0lcd.print("H: % T:");delay(1000);}void loop() {// Reading temperature or humidity takes about 250 milliseconds!// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)float h = dht.readHumidity();// Read temperature as Celsius (the default)float t = dht.readTemperature();// Read temperature as Fahrenheit (isFahrenheit = true)float f = dht.readTemperature(true);// Check if any reads failed and exit early (to try again).if (isnan(h) || isnan(t) || isnan(f)) {Serial.println("Failed to read from DHT sensor!");return;}// Compute heat index in Fahrenheit (the default)float hif = puteHeatIndex(f, h);// Compute heat index in Celsius (isFahreheit = false)float hic = puteHeatIndex(t, h, false);Serial.print("Humidity: ");Serial.print(h);Serial.print(" %\t");Serial.print("Temperature: ");Serial.print(t);Serial.print(" *C ");Serial.print(f);Serial.print(" *F\t");Serial.print("Heat index: ");Serial.print(hic);Serial.print(" *C ");Serial.print(hif);Serial.println(" *F");lcd.setCursor(2,1); // line 1, pos 0lcd.print(h);lcd.setCursor(11,1); // line 1, pos 0lcd.print(t);delay(1000);}代码说明1. DHT11启动到读取数据需要等待1~2秒2. 温湿度的精度都为1, 没有⼩数部分3. DHT库⾥⾯带了计算热指数的⽅法 computeHeatIndex(), ⽤于⽣成综合温湿度计算得到的热指数值改进拼接字符串改进后的代码, 注意: arduino⾥的sprintf只能格式化整数, 不能格式化浮点#include <Wire.h>#include <LiquidCrystal_I2C.h>#include <DHT.h>#include <DS3231.h>#define DHTPIN 4#define DHTTYPE DHT11// I2C地址, ⼀般为0x3F, 0x20或0x27LiquidCrystal_I2C lcd(0x27,16,2);DHT dht(DHTPIN, DHTTYPE);DS3231 Clock;bool century=false;bool h12;bool PM;void setup() {lcd.init();//lcd.backlight(); // 打开背光Serial.begin(9600);dht.begin();lcd.setCursor(0,0); // line 0, pos 0lcd.print("Good Day Jessie~~");lcd.setCursor(0,1); // line 1, pos 0lcd.print("H: % T: T:");delay(1000);}void loop() {char str[17];sprintf(str,"%02d-%02d %02d:%02d:%02d ",Clock.getMonth(century),Clock.getDate(),Clock.getHour(h12, PM),Clock.getMinute(),Clock.getSecond());lcd.setCursor(0,0); // line 0, pos 0lcd.print(str);// Reading temperature or humidity takes about 250 milliseconds!// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)float h = dht.readHumidity();// Read temperature as Celsius (the default)float t = dht.readTemperature();// Read temperature as Fahrenheit (isFahrenheit = true)float f = dht.readTemperature(true);// Check if any reads failed and exit early (to try again).if (isnan(h) || isnan(t) || isnan(f)) {Serial.println("Failed to read from DHT sensor!");return;}// Compute heat index in Fahrenheit (the default)float hif = puteHeatIndex(f, h);// Compute heat index in Celsius (isFahreheit = false)float hic = puteHeatIndex(t, h, false);Serial.print("Humidity: ");Serial.print(h);Serial.print(" %\t");Serial.print("Temperature: ");Serial.print(t);Serial.print(" *C ");Serial.print(f);Serial.print(" *F\t");Serial.print("Heat index: ");Serial.print(hic);Serial.print(" *C ");Serial.print(hif);Serial.println(" *F");lcd.setCursor(2,1); // line 1, pos 0lcd.print((int)h);lcd.setCursor(8,1); // line 1, pos 0lcd.print((int)t);lcd.setCursor(13,1);lcd.print((int)(Clock.getTemperature()*10)); delay(1000);}。
C51温湿度传感器DHT11驱动LCD1602显示程序
DHT11.c 文件#include<reg52.h>#include<Time_Delay.h>//the main only needs to call getDHT11(),then the temperature and huminity was geted in F16T,F16RH as floatsbit bit11=P2^0;unsigned char U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;//用于最终读取的温湿度数据// read 8 bits onicechar COM(void){char i,U8temp,U8comdata;for(i=0;i<8;i++){while(!bit11); //表示读取的高电位延时大于20多us则读取的是1否则读取的是0//通过U8FLAG可判断Delay_us(35);U8temp=0;if(bit11)U8temp=1;while(bit11);U8comdata<<=1;U8comdata|=U8temp; //0}//rofreturn U8comdata;}//--------------------------------//-----温湿度读取子程序------------//--------------------------------//----以下变量均为全局变量--------//----温度高8位== U8T_data_H------//----温度低8位== U8T_data_L------//----湿度高8位== U8RH_data_H-----//----湿度低8位== U8RH_data_L-----//----校验8位== U8checkdata-----//----调用相关子程序如下----------//---- Delay();, Delay_10us();,COM();bit11显示数据的脉长//--------------------------------void getDHT11(void){//主机拉低18msGO1: bit11=0;Delay_ms(20);bit11=1;//总线由上拉电阻拉高主机延时20usDelay_us(60);//主机设为输入判断从机响应信号// bit11=1;//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!bit11) {while(!bit11); while(bit11); //数据接收状态//T !//wait DHT goto highU8RH_data_H=COM();U8RH_data_L=COM();U8T_data_H=COM();U8T_data_L=COM();U8checkdata=COM();bit11=1;//数据校验if((U8T_data_H+U8T_data_L+U8RH_data_H+U8RH_data_L)!=U8checkdata) check wrong,read againgoto GO1;}//fiF16T=U8T_data_H+(float)U8T_data_L/256; //change integer to floatF16RH=U8RH_data_H+(float)U8RH_data_L/256;}//ifLCD1602 文件#include<reg52.h>#include <stdio.h>#include <INTRINS.H> #include <Lcd_1602.h> #include <Time_Delay.h>#define LCD_DATA P0#define uint unsigned int#define uchar unsigned char /*只由主函数调用的有Init_Lcd()LCD_write_str(uchar X,uchar Y,uchar *s) //LCD1602 data transfer defineLCD_value(unsigned char x,unsigned char y,float f) */sbit LCD_RS = P2^5; sbit RW = P2^6;sbit LCD_E = P2^7; //1602 control define/***************************************************************************//显示开//显示关#define LCD_CURSOR_ON 0x0A //显示光标//无光标//有光标,光标闪动//有光标,光标不闪动//进入模式设置指令//新数据后光标右移//新数据后光标左移//画面可平移//画面不可平移//设定显示屏或光标移动方向指令//光标左移1格,且AC值减1//光标右移1格,且AC值加1//显示器上字符全部左移一格,但光标不动//显示器上字符全部右移一格,但光标不动***************************************************************************/ //注有主函数调用的函数都已作说明其他函数一般不由主函数调用/*****************************************************************************名*功称:Init_Lcd()主函数调用能:Lcd初始化*入口参数:无*出口参数:无*范例:在主函数中直接调用****************************************************************************///LCD初始化{LCD_write_char(0x38,0);Delay_ms(1);LCD_write_char(0x38,0);Delay_ms(1);LCD_write_char(0x38,0);Delay_ms(1);LCD_write_char(0x0c,0);Delay_ms(1);LCD_write_char(0x06,0);Delay_ms(1);LCD_write_char(0x0c,0);Delay_ms(1);//}/*****************************************************************************名*功称:LCD_write_str(uchar X,uchar Y,uchar *s)主函数调用能:在指定地址写一个字符串eg:Y=0,1,2,3,4,5,6,7,8,9,10...15。
STC15单片机DHT11在LCD1602上显示程序
敬告:没有51单片机基础的人请慎重下载高质量实用性51单片机STC15W系列程序(4),STC8A系列可参考STC15单片机DHT11在LCD1602上显示程序注:在本节关于DHT11的程序有两种/*****************************************/版本一:/****************************************//****************************************/main函数程序:#include "Library.h"unsigned char strbuf[4];//void ValToStr(unsigned char *str,unsigned char *source,unsigned char len);unsigned char tmrflag = 0;unsigned char DHTbuf[5];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 DHTstr[8];P3M1 &= 0xBF; P3M0 &= 0xBF; P2M1 &= 0xE5; P2M0 &= 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(DHTbuf);// ValToStr(DHTstr,DHTbuf,sizeof(DHTbuf)-1);DHTstr[0] = DHTbuf[0]/10 + '0';DHTstr[1] = DHTbuf[0]%10 + '0';DHTstr[2] = DHTbuf[1]/10 + '0';DHTstr[3] = DHTbuf[1]%10 + '0';DHTstr[4] = DHTbuf[2]/10 + '0';DHTstr[5] = DHTbuf[2]%10 + '0';DHTstr[6] = DHTbuf[3]/10 + '0';DHTstr[7] = DHTbuf[3]%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)//{// unsigned char i;// while(len>0)// {// str[i*2] = source[i]/10+'0';// str[1+i*2] = source[i]%10+'0';// i++;// len--;// }//}/*********************************************/DHT11程序:/******************************8bit humidity integer data + 8bit humidity decimal data 8bit temperature integer data + 8bit temperature decimal data8bit check sum,high bit ahead*******************************/#include "Library.h"unsigned char u8flag;unsigned char check_buf[5];unsigned char tmp;void delay_1s() //@11.0592MHz{unsigned char i, j, k;_nop_();_nop_();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;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;i<8;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(unsigned char *rec_buf) {// delay_1s();DHT11port = 0;Delay20ms();DHT11port = 1;Delay10us();Delay10us();Delay10us();Delay10us();//DHT11port = 1;if(!DHT11port){u8flag=2;// while(!DHT11port);while((!DHT11port)&&u8flag++);// if(DHT11port)// {// while(DHT11port);u8flag=2;while((DHT11port)&&u8flag++);check_buf[0] = Rec_8bit();check_buf[1] = Rec_8bit();check_buf[2] = Rec_8bit();check_buf[3] = Rec_8bit();check_buf[4] = Rec_8bit();if(!DHT11port){while(!DHT11port);}DHT11port = 1;tmp = check_buf[0]+check_buf[1]+check_buf[2]+check_buf[3] ;if(tmp==check_buf[4]){rec_buf[0] = check_buf[0];rec_buf[1] = check_buf[1];rec_buf[2] = check_buf[2];rec_buf[3] = check_buf[3];}// }}else{rec_buf[0] = 0;rec_buf[1] = 0;rec_buf[2] = 0;rec_buf[3] = 0;}}/*********************************************/ LCD1602程序:#include "Library.h"void LCD1602_rsta(){unsigned char tmp;P0 = 0xFF;//this is a mustrs = 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(x|0x80);}void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len){Setcursor(x,y);while(buf_len>0){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(0x01);}/*****************************************/ Library.h#ifndef _Library_H#define _Library_H#include <STC15.h>#include <intrins.h>#define MAIN_Fosc 11059200Lsbit DHT11port = P3^6;void DHT11_start(unsigned char *rec_buf);sbit rs = P2^4;sbit rw = P2^3;sbit en = P2^1;void LCD1602_init();void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len);#endif/*****************************************/ 版本二(结构体):/****************************************/ /****************************************/ main程序:#include "Library.h"#include "string.h"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 DHTstr[8];P3M1 &= 0xBF; P3M0 &= 0xBF;P2M1 &= 0xE5; P2M0 &= 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);DHTstr[0] = ht.humi_h +'0' ;DHTstr[1] = ht.humi_l+'0' ;DHTstr[2] = ht.humi_dh +'0' ;DHTstr[3] = ht.humi_dl +'0' ;DHTstr[4] = ht.temp_h +'0' ;DHTstr[5] = ht.temp_l +'0' ;DHTstr[7] = ht.temp_dh +'0' ;DHTstr[6] = 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 data + 8bit humidity decimal data 8bit temperature integer data + 8bit temperature decimal data8bit check sum,high bit ahead*******************************/#include "Library.h"unsigned char u8flag;unsigned char check_buf[5];unsigned char tmp;unsigned char rec_buf[4];htstruct ht;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;_nop_();_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;i<8;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(){// delay_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_buf[0] = Rec_8bit();check_buf[1] = Rec_8bit();check_buf[2] = Rec_8bit();check_buf[3] = Rec_8bit();check_buf[4] = Rec_8bit();if(!DHT11port){while(!DHT11port);}DHT11port = 1;tmp = check_buf[0]+check_buf[1]+check_buf[2]+check_buf[3] ;if(tmp==check_buf[4]){rec_buf[0] = check_buf[0];rec_buf[1] = check_buf[1];rec_buf[2] = check_buf[2];rec_buf[3] = check_buf[3];}ht.humi_h = rec_buf[0]/10 ;ht.humi_l = rec_buf[0]%10;ht.humi_dh = rec_buf[1]/10;ht.humi_dl = rec_buf[1]%10;ht.temp_h = rec_buf[2]/10;ht.temp_l = rec_buf[2]%10;ht.temp_dh = rec_buf[3]/10;ht.temp_dl = rec_buf[3]%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 "Library.h"void LCD1602_rsta(){unsigned char tmp;P0 = 0xFF;//this is a mustrs = 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(x|0x80);}void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len){Setcursor(x,y);while(buf_len>0){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(0x01);}/******************************************/ Library.h#ifndef _Library_H#define _Library_H#include <STC15.h>#include <intrins.h>#define MAIN_Fosc 11059200Lsbit DHT11port = P3^6;typedef struct htstruct{unsigned char humi_h;unsigned char humi_l;unsigned char humi_dh;unsigned char humi_dl;unsigned char temp_h;unsigned char temp_l;unsigned char temp_dh;unsigned char temp_dl;}htstruct;extern htstruct ht;void DHT11_start();sbit rs = P2^4;sbit rw = P2^3;sbit en = P2^1;void LCD1602_init();void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len);#endif。
DHT11温湿度传感器C程序测试可以用(有说明)
DHT11温湿度传感器C程序说明:DHT11温湿度传感器只有整数位没有小数,传感器内部小数位留空备用,使用该程序时,只需要在while循环里面调用RH函数即可,间隔时间大于1秒,读取以下几个效验后的变量可以获取温湿度值:U8RH_data_H 湿度高8位整数位U8RH_data_L 湿度低8位小数位〔空的〕U8T_data_H 温度高8位整数位U8T_data_L 温度低8位整数位〔空的〕1,如果是用数码管显示,按时序延时18毫秒后如果有中断得关中断,取完40个Bit数据后开中断,防止MCU内部中断打断时序时间,引起读数误差或读不出来的问题,LCD显示器无需该操作。
2,循环读取传感器时间得大于1秒,否那么读不准。
自己做的实验板温度25,湿度45%#include <reg52.h>#include <intrins.h>//typedef unsigned char U8; /* defined for unsigned 8-bits integer variable 无符号8位整型变量*/typedef signed char S8; /* defined for signed 8-bits integer variable 有符号8位整型变量*/typedef unsigned int U16; /* defined for unsigned 16-bits integer variable 无符号16位整型变量*/typedef signed int S16; /* defined for signed 16-bits integer variable 有符号16位整型变量*/typedef unsigned long U32; /* defined for unsigned 32-bits integer variable 无符号32位整型变量*/typedef signed long S32; /* defined for signed 32-bits integer variable 有符号32位整型变量*/typedef float F32; /* single precision floating point variable (32bits) 单精度浮点数〔32位长度〕*/typedef double F64; /* double precision floating point variable (64bits) 双精度浮点数〔64位长度〕*///#define uchar unsigned char#define uint unsigned int#define Data_0_time 4//----------------------------------------------////----------------IO口定义区--------------------////----------------------------------------------//sbit P2_0 = P3^2 ;//----------------------------------------------////----------------定义区--------------------////----------------------------------------------//U8 U8FLAG,k;U8 U8count,U8temp;U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;U8U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_t emp;U8 U8comdata;U8 outdata[5]; //定义发送的字节数U8 indata[5];U8 count, count_r=0;U8 str[5]={"RS232"};U16 U16temp1,U16temp2;void Delay(U16 j){ U8 i;for(;j>0;j--){for(i=0;i<27;i++);}}void Delay_10us(void){U8 i;i--;i--;i--;i--;i--;i--;}void COM(void){U8 i;for(i=0;i<8;i++){U8FLAG=2;while((!P2_0)&&U8FLAG++);Delay_10us();Delay_10us();Delay_10us();U8temp=0;if(P2_0)U8temp=1;U8FLAG=2;while((P2_0)&&U8FLAG++);//超时那么跳出for循环if(U8FLAG==1)break;//判断数据位是0还是1// 如果高电平高过预定0高电平值那么数据位为1U8comdata<<=1;U8comdata|=U8temp; //0}//rof}//--------------------------------//-----湿度读取子程序------------//--------------------------------//----以下变量均为全局变量--------//----温度高8位== U8T_data_H------//----温度低8位== U8T_data_L------//----湿度高8位== U8RH_data_H-----//----湿度低8位== U8RH_data_L-----//----校验8位== U8checkdata-----//----调用相关子程序如下----------//---- Delay();, Delay_10us();,COM();//--------------------------------void RH(void){//主机拉低18msP2_0=0;Delay(180);P2_0=1;//总线由上拉电阻拉高主机延时20usEA=0;//关中断,如果是LCD删除此行。
89C52RC温湿度传感器DHT11液晶1602显示程序
89C52RC温湿度传感器DHT11液晶1602显示程序#include#define uint unsigned int#define uchar unsigned charuchar DHT11[5],RTflag=0;uchar FLAG; //超时标志位uchar a;sbit dat=P2^3;sbit RS=P3^4;sbit RW=P3^6;sbit EN=P3^7;uchar table[5];uint wd,sd;void Delay_t(uint j){ uchar i;for(;j>0;j--){for(i=0;i<27;i++);}}void Delay_10us(void) //10us延时函数{uchar i;i--;i--;i--;i--;i--;i--;}void delay(uint z)//1毫秒延时函数{uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void lcd_write_com(uchar com) //1602写指令{RS=0;RW=0;EN=1;P0=com;delay(1);EN=0;}void lcd_init() //1602初始化{lcd_write_com(0x38);delay(1);lcd_write_com(0x08);delay(1);lcd_write_com(0x01);//1602清屏指令delay(1);lcd_write_com(0x06);delay(1);lcd_write_com(0x0C);delay(1);}void lcd_write_data(uchar date)//1602写数据{RS=1;RW=0;EN=1;P0=date;delay(1);EN=0;}void write_str(uchar x,uchar y,uchar *s)//在任意地址写符号字母或数字{if(y==0)lcd_write_com(0x80+x);elselcd_write_com(0xc0+x);while(*s){lcd_write_data(*s);s++;}}void write_shu(uchar x,uchar y,uchar num)//数据显示函数{uchar s,g;if(y==0)lcd_write_com(0x80+x);elselcd_write_com(0xc0+x);s=num/10;// 数据分离显示lcd_write_data(0x30+s);g=num%10;//数据分离显示lcd_write_data(0x30+g);}uchar write_byte1() //读一个字节{uchar i,comdata,temp1;for(i=0;i<8;i++){FLAG=2;while((!dat)&&FLAG++);//判断数据位是0还是1Delay_10us();Delay_10us();Delay_10us();temp1=0;if(dat)temp1=1; // 如果高电平高过预定0高电平值则数据位为 1 FLAG=2;while((dat)&&FLAG++);//flag先与后加1 如果dat一直为1uchar型变量 flag 溢出变为0 再自加1if(FLAG==1)break; //超时则跳出for循环comdata<<=1;//左移一位高位在前低位在后comdata|=temp1;}return (comdata);}void DHT11_5() //读5个字节数据两个字节为温度数据两个字节为湿度数据最后一个字节为校验{uchar i,temp;//主机拉低18msdat=0;Delay_t(180);dat=1;//总线由上拉电阻拉高主机延时20usDelay_10us();Delay_10us();Delay_10us();Delay_10us();//主机设为输入判断从机响应信号dat=1;//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!dat) //T !{FLAG=2; //超时标志位while((!dat)&&FLAG++);//判断从机是否发出80us 的低电平响应信号是否结束FLAG=2;while((dat)&&FLAG++); //判断从机拉高80us是否结束for(i=0;i<5;i++)//数据接收状态{DHT11[i]=write_byte1();}dat=1; //释放数据总线为下一次读取做好准备temp=(DHT11[0]+DHT11[1]+DHT11[2]+DHT11[3]);if(temp==DHT11[4]) //数据校验{RTflag=1;}if(RTflag==1) //如果RTflag=1 说明读取到得数据正确{RTflag=0;// tm[0]=DATARHT[0]/10;// tm[1]=DATARHT[0]%10;// tm[2]=DATARHT[1]/10; //湿度// tm[3]=DATARHT[2]/10;// tm[4]=DATARHT[2]%10;// tm[5]=DATARHT[3]/10; //温度write_str(0,0,"measurement ");//第一行显示湿度write_shu(12,0,DHT11[0]);write_str(14,0,"RH");write_str(0,1,"Temperature ");//第二行为显示温度write_shu(12,1,DHT11[2]);write_str(14,1,"^C");}}}void main(){lcd_init(); //1602初始化delay(1000); //等待DHT11温湿度传感器数据稳定开始激活DHT11while(1)//循环读取并更新数据显示{delay(1000);//等待DHT11温湿度传感器数据稳定开始激活DHT11write_byte1();//读一个字节DHT11_5(); //读数据delay(1000); //延时等待}}。
DHT11的LCD1602显示程序
LCD_EN = 0; _nop_(); _nop_(); P0 = cmd; delayNOP(); LCD_EN = 1; delayNOP(); LCD_EN = 0; } /********************************************************* ********************/
TRH=1; //判断 DHT11 是否有低电平响应信号 如不响应则跳出,响应则向 下运行
if(!TRH) {
respond=2; //判断 DHT11 发出 80us 的低电平响应信号是否结束 while((!TRH)&& respond++); respond=2; //判断从机是否发出 80us 的高电平,如发出则进入数据接收 状态 while(TRH && respond++); //数据接收状态 RH_temp = receive(); RL_temp = receive(); TH_temp = receive(); TL_temp = receive(); CK_temp = receive(); TRH=1;ST=1; //数据校验 untemp=(RH_temp+RL_temp+TH_temp+TL_temp); if(untemp==CK_temp) {
//主机拉低 18ms TRH=0; delay_ms(18); TRH=1; //DATA 总线由上拉电阻拉高 主机延时 20us delay_us(); delay_us(); delay_us(); delay_us(); //delay_us(); //delay_us();delay_us();delay_us();delay_us(); //主机设为输入 判断从机响应信号
C51_温湿度传感器DHT11驱动_LCD1602显示程序
DHT11.c 文件#include<reg52.h>#include<Time_Delay.h>//the main only needs to call getDHT11(),then the temperature and huminity was geted in//F16T,F16RH as floatsbit bit11=P2^0;unsigned char U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;//用于最终读取的温湿度数据// read 8 bits onicechar COM(void){char i,U8temp,U8comdata;for(i=0;i<8;i++) {while(!bit11); //表示读取的高电位延时大于20多us则读取的是1否则读取的是//通过U8FLAG可判断Delay_us(35);U8temp=0;if(bit11)U8temp=1;while(bit11);U8comdata<<=1;U8comdata|=U8temp; //0 }//rofreturn U8comdata;}//--------------------------------//-----温湿度读取子程序------------ //-------------------------------- //----以下变量均为全局变量-------- //----温度高8位== U8T_data_H------ //----温度低8位== U8T_data_L------ //----湿度高8位== U8RH_data_H----- //----湿度低8位== U8RH_data_L----- //----校验8位== U8checkdata----- //----调用相关子程序如下---------- //---- Delay();, Delay_10us();,COM();//--------------------------------void getDHT11(void) {//主机拉低18msGO1:bit11=0;Delay_ms(20);bit11=1;//总线由上拉电阻拉高主机延时20usDelay_us(60);//主机设为输入判断从机响应信号 //bit11=1;//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!bit11) {while(!bit11);while(bit11); //数据接收状态//T !//wait DHT goto highU8RH_data_H=COM();U8RH_data_L=COM();U8T_data_H=COM();U8T_data_L=COM();U8checkdata=COM();bit11=1; //数据校验if((U8T_data_H+U8T_data_L+U8RH_data_H+U8RH_data_L)!=U8checkdata) //If check wrong,read againgoto GO1; }//fiF16T=U8T_data_H+(float)U8T_data_L/256; //change integer to floatF16RH=U8RH_data_H+(float)U8RH_data_L/256;}LCD1602 文件#include<reg52.h>#include <stdio.h>#include <INTRINS.H>#include <Lcd_1602.h>#include <Time_Delay.h>#define LCD_DATA P0#define uint unsigned int#define uchar unsigned char/*只由主函数调用的有 Init_Lcd()LCD_write_str(uchar X,uchar Y,uchar *s) //LCD1602 data transfer defineLCD_value(unsigned char x,unsigned char y,float f)*/sbit LCD_RS = P2^5;sbit RW = P2^6;sbit LCD_E = P2^7;//1602 control define/***************************************************************************//显示开//显示关#define LCD_CURSOR_ON 0x0A //显示光标//无光标 //有光标,光标闪动 //有光标,光标不闪动//进入模式设置指令//新数据后光标右移//新数据后光标左移 //画面可平移 //画面不可平移//设定显示屏或光标移动方向指令//光标左移1格,且AC值减1 //光标右移1格,且AC值加1//显示器上字符全部左移一格,但光标不动 //显示器上字符全部右移一格,但光标不动***************************************************************************/ //注有主函数调用的函数都已作说明其他函数一般不由主函数调用/*****************************************************************************名*功称:Init_Lcd()主函数调用能:Lcd初始化 *入口参数:无 *出口参数:无 *范例:在主函数中直接调用****************************************************************************///LCD初始化{LCD_write_char(0x38,0); Delay_ms(1);LCD_write_char(0x38,0); Delay_ms(1);LCD_write_char(0x38,0); Delay_ms(1);LCD_write_char(0x0c,0); Delay_ms(1);LCD_write_char(0x06,0); Delay_ms(1);LCD_write_char(0x0c,0); Delay_ms(1); //}/************************名*功称:LCD_write_str(uchar X,uchar Y,uchar *s)主函数调用能:在指定地址写一个字符串eg:Y=0,1,2,3,4,5,6,7,8,9,10...15。
温湿度检测器DHT11程序及显示程序
附录1:#include <regx52.h>#include <intrins.h>#include "DHT11.H"#include "DHT11.C"unsigned char range[4]={55,75,10,30}; //温湿度上下限初值unsigned char Humi_Temp_Tab[8]={6,9,0,0,7,8,0,0}; //数码管显示初值unsigned char numt1=0; //T1中断计数标志unsigned char numt0=0;unsigned char codeTab_Seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//数码管段码 0-9unsigned char code Tab_Dig[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//位选第一位到第八位unsigned char code units[4]={0x39,0x71,0x77,0x76}; //单位C/F/RH/**********************************************//* 温湿度采集函数 *//**********************************************/void getdata(){if(start_DHT11()){read_DHT11();}if(check_sum()){Humi_Temp_Tab[0]=DHT_data.DH_H/10;Humi_Temp_Tab[1]=DHT_data.DH_H%10;Humi_Temp_Tab[2]=DHT_data.DH_L/10; //存储湿度数据if(flag == 0){Humi_Temp_Tab[4]=DHT_data.T_H/10;Humi_Temp_Tab[5]=DHT_data.T_H%10;Humi_Temp_Tab[6]=DHT_data.T_L/10;//存储摄氏温度数据}else{Humi_Temp_Tab[4]=(9*DHT_data.T_H/5+32)/10;Humi_Temp_Tab[5]=(9*DHT_data.T_H/5+32)%10;Humi_Temp_Tab[6]=(18*DHT_data.T_H+320)%100%10;//存储华氏温度数据}}}/**********************************************//* 主函数 *//**********************************************/void main(){delay_ms(500); //先进行延时等待进入稳定状态P0 = 0;P1 = 0x0C; //初始化P1口EA = 0;TR1 = 0;TR0 = 0;TMOD = 0x11; //设置定时器 T0和T1,且工作方式都为方式1TH1 = (65536-5000)/256;TL1 = (65536-5000)%256;TH0 = (65536-2000)/256;TL0 = (65536-2000)%256; //设定初值2msTR1 = 1;TR0 = 1;EA = 1;ET0 = 1;ET1 = 1; //打开中断定时器T0和T1PT1 = 0;PT0 = 1; //强制设置优先级delay_ms(1000);while(1){if(DHT_data.DH_H<range[0]) //湿度小于下限{bee = 0;delay_ms(100);bee = 1;delay_ms(100);}if(DHT_data.DH_H>range[1]) //湿度大于上限{bee = 0;delay_ms(100);bee = 1;delay_ms(100);}if(DHT_data.T_H<range[2]) //温度小于下限{bee = 0;delay_ms(10);bee = 1;delay_ms(10);}if(DHT_data.T_H>range[3]) //温度大于上限{bee = 0;delay_ms(10);bee = 1;delay_ms(10);}}}/**********************************************//* 定时器T0中断 *//**********************************************/void T0_timer() interrupt 1{unsigned char KData = 0x00;TR0 = 0; //进入T0后将T0中断关闭TH0 = (65536-2000)/256;TL0 = (65536-2000)%256;switch(numt0){case 0: P0 = 0; Seg_ce = 1; Seg_ce = 0; //段选开关if(flag2 == 1)P0 = Tab_Seg[range[0]/10];//显示湿度下限的十位elseP0 = Tab_Seg[Humi_Temp_Tab[0]];//显示读取的湿度的十位Seg_ce = 1; Seg_ce = 0;.P0 = Tab_Dig[0]; //位选第一位Dig_ce = 1; Dig_ce = 0;numt0++;break;case 1: P0 = 0; Seg_ce = 1; Seg_ce = 0;if(flag2 == 1)P0 = Tab_Seg[range[0]%10];//显示湿度下限的个位elseP0 = Tab_Seg[Humi_Temp_Tab[1]];//显示读取的湿度的个位Seg_ce = 1; Seg_ce = 0;P0 = Tab_Dig[1]; //位选第二位Dig_ce = 1; Dig_ce = 0;numt0++;break;case 2: P0 = 0; Seg_ce = 1; Seg_ce = 0;if(flag2 == 1)P0 = Tab_Seg[range[1]/10];//显示湿度上限的十位elseP0 = units[2];//显示单位RSeg_ce = 1; Seg_ce = 0;P0 = Tab_Dig[2]; //位选第三位Dig_ce = 1; Dig_ce = 0;numt0++;break;case 3: P0 = 0; Seg_ce = 1; Seg_ce = 0;if(flag2 == 1)P0 = Tab_Seg[range[1]%10];//显示湿度上限的个位elseP0 = units[3]; //显示单位H Seg_ce = 1; Seg_ce = 0;P0 = Tab_Dig[3]; //位选第四位Dig_ce = 1; Dig_ce = 0;numt0++;break;case 4: P0 = 0; Seg_ce = 1; Seg_ce = 0;P0 = Tab_Dig[4];//位选第五位,且同时拉低键盘第四行Dig_ce = 1; Dig_ce = 0;Key_ce = 0;KData = P0; //扫描键盘第四行switch(KData){case 0xfe:case 0xfd:case 0xfb:case 0xf7:default:break;}while(KData != 0xff){KData = P0;}Key_ce = 1;if(flag2 == 1)P0 = Tab_Seg[range[2]/10];//显示温度下限的十位elseP0 = Tab_Seg[Humi_Temp_Tab[4]];//显示读取的温度的十位Seg_ce = 1; Seg_ce = 0;numt0++;break;case 5: P0 = 0; Seg_ce = 1; Seg_ce = 0;P0 = Tab_Dig[5];//位选第六位,且同时拉低键盘第三行Dig_ce = 1; Dig_ce = 0;Key_ce = 0;KData = P0;//扫描键盘第三行switch(KData){case 0xfe:if(range[0]<range[1]&&flag2==1)range[0]++;break; //湿度下限加case 0xfd:if(range[1]<90&&flag2==1)range[1]++;break; //湿度上限加case 0xfb:if(range[2]<range[3]&&flag2==1range[2]++;break; //温度下限加case 0xf7:if(range[3]<50&&flag2==1)range[3]++;break; //温度上限加default:break;}while(KData != 0xff){KData = P0;}Key_ce = 1;if(flag2 == 1)P0 = Tab_Seg[range[2]%10];//显示温度下限的个位elseP0 = Tab_Seg[Humi_Temp_Tab[5]]-0x80; //显示读取温度的个位(带小数点的)Seg_ce = 1; Seg_ce = 0;numt0++;break;case 6: P0 = 0; Seg_ce = 1; Seg_ce = 0;P0 = Tab_Dig[6];//位选第七位,且同时拉低键盘第二行Dig_ce = 1; Dig_ce = 0;Key_ce = 0;KData = P0; //扫描键盘第二行switch(KData){case 0xfe:if(range[0]>20&&flag2==1)range[0]--;break; //湿度下限减case 0xfd:if(range[0]<range[1]&&flag2==1)range[1]--;break; //湿度上限减case 0xfb:if(range[2]>0&&flag2==1)range[2]--;break; //温度下限减case 0xf7:if(range[2]<range[3]&&flag2==1)range[3]--;break; //温度上限减default:break;}while(KData != 0xff){KData = P0;}Key_ce = 1;if(flag2 == 1)P0 = Tab_Seg[range[3]/10];//显示温度上限的十位elseP0 = Tab_Seg[Humi_Temp_Tab[6]];//显示读取温度的小数位的十位Seg_ce = 1; Seg_ce = 0;numt0++;break;case 7: P0 = 0; Seg_ce = 1; Seg_ce = 0;P0 = Tab_Dig[7];//位选第八位,且同时拉低键盘第一行Dig_ce = 1; Dig_ce = 0;Key_ce = 0;KData = P0; //扫描键盘第一行switch(KData){case 0xfe:flag2 = ~flag2;TR1 = ~TR1;break;//进入和退出限制调整模式case 0xfd:flag = ~flag;break;//进行华氏摄氏温度的转换设置case 0xfb:case 0xf7:default:break;}while(KData != 0xff){KData = P0;}Key_ce = 1;if(flag == 0&&flag2 == 0)P0 = units[0]; //显示单位Celse if(flag == 1&&flag2 == 0)P0 = units[1]; //显示单位Felse if(flag2 == 1)P0 = Tab_Seg[range[3]%10];//显示温度上限的个位Seg_ce = 1; Seg_ce = 0;numt0 = 0;break;default:numt0 = 0;break;}TR0 = 1; //打开T0}/**********************************************//* 定时器T1中断 *//**********************************************/void T1_timer() interrupt 3{TR1 = 0; //关闭T0TH1 = (65536-50000)/256;TL1 = (65536-50000)%256;if(numt1 == 25){getdata(); //采集数据numt1 = 0;}elsenumt1++;TR1 = 1; //打开T0}#ifndef __DHT11_h__#define __DHT11_h__#include <REGX52.H>/**********************************************//* 引脚定义 *//**********************************************/sbit DHT_bus = P2^0 ; //DHT11数据传输口sbit Key_ce=P1^3; //按键输出使能sbit Seg_ce=P1^0; //段选位sbit Dig_ce=P1^1; //位选位sbit bee = P2^1; //蜂鸣器控制口/**********************************************//* 函数声明 *//**********************************************/bit start_DHT11(void); //开始void read_DHT11(void); //读取void delay_20us(void); //20us延时void delay_ms(unsigned char m); //N ms延时bit check_sum(void); //和校验/**********************************************//* 宏定义 *//**********************************************/#define HIGH 1#define LOW 0/**********************************************//* 变量定义 *//**********************************************/#define DHT_timeover 5 //高电平维持时间,用于识别“数据0”和“数据1”bit flag2 = 0; //设置调节上下限模式转换标志bit flag=0; //设置摄氏和华氏温度模式转换标志/**********************************************//* 结构体 *//**********************************************/struct DHT_data{unsigned char DH_H; //湿度整数unsigned char DH_L; //湿度小数unsigned char T_H; //温度整数unsigned char T_L; //温度小数unsigned char Checksum; //校验和}DHT_data;#endif#include "DHT11.h"#include <intrins.h>/**********************************************//* 开始 DHT11 温湿度计 *//* 输入:无 *//* 输出:应答标志 0:应答失败 1:应答成功 *//**********************************************/bit start_DHT11(void){bit DHT_start;DHT_start = 0;DHT_bus = HIGH;DHT_bus = LOW; //拉低18ms以上delay_ms(18);TR0 = 0;DHT_bus = HIGH;delay_20us();delay_20us(); //拉高20~40uswhile(!DHT_bus){DHT_start = 1;} //DHT应答,DHT拉低80us后拉高80us,然后开始传输数据//数据(40bit)=8bit湿度整数+8bit湿度小数+8bit温度整数+8bit 温度小数+8bit校验和while(DHT_bus){};return(DHT_start); //应答成功返回1}/**********************************************//* 读取 DHT11 温湿度计 *//* 读取结果存在DHT_data结构体内 *//* 输入:无输出:无 *//**********************************************/void read_DHT11(void){unsigned char m,n,timer_dht;unsigned char *p;p=&DHT_data.DH_H; //数据放在DHT_date的结构体中for(m=0;m<5;m++){for(n=0;n<8;n++){while(~DHT_bus); //DHT拉低12-14us表示1bit数据开始timer_dht=0x00;while(DHT_bus) //随后DHT拉高总线,单片机通过高电平维持的时间判断“数据0”还是“数据1”{ //数据0维持26~28us高电平,数据1维持116~118us高电平timer_dht++; //由于此处对延时时间的长度要求很高,所以采用另一种办法判断}if(timer_dht>DHT_timeover){*p<<=1;*p|=0x01;}else{*p<<=1;*p&=0xfe;}}p++;}TR0 = 1;}/**********************************************//* 20us 精确延时 *//* 51用在12Mhz晶振下 *//* 调用函数使用LCALL和RET指令,共花费4个周期 *//* 因此只有16个NOP *//**********************************************/.void delay_20us(void){_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();_nop_ ();}/**********************************************//* N ms 延时 *//* while()额外占用约5周期 *//* 因此内层while(40--)20us 大约1ms *//* Nms延时函数(未测试) *//**********************************************/void delay_ms(unsigned char m){unsigned char n = 38;while(m--){while(n--){delay_20us();}}}/**********************************************//* 校验和判断 *//* 校验位 = 湿度整数位+湿度小数位+温度整数位+温度小数位之和 *//* 校验正确返回:1 失败返回:0 *//**********************************************/bit check_sum(void){if(DHT_data.Checksum==(DHT_data.DH_H+DHT_data.DH_L+DHT_data.T _H+DHT_data.T_L))return(1); //校验正确elsereturn(0); //校验失败}Word 资料。
dht11模块测量温湿度的流程
DHT11模块测量温湿度的流程概述本文将介绍D HT11模块的使用方法,包括连接电路、读取数据的流程以及温湿度的计算方法。
连接电路首先,我们需要将DH T11模块与单片机进行连接。
需要使用3个引脚:V C C、GN D和数据引脚。
具体的连接方式如下:-将DH T11模块的VC C引脚连接到单片机的3.3V或5V电源引脚上。
-将DH T11模块的GN D引脚连接到单片机的地(GN D)引脚上。
-将DH T11模块的数据引脚连接到单片机的任意可用的数字引脚上。
连接完成后,我们可以开始测量温湿度了。
测量温湿度的流程1.初始化在开始测量之前,我们需要对DH T11模块进行初始化。
初始化的步骤包括向D HT11发送一个低电平的信号,并延时至少18毫秒。
这个低电平信号将引导D HT11进入测量模式。
2.接收数据初始化完成后,D HT11模块会将测量到的温湿度数据以串行的形式发送回来。
我们需要准备好接收数据的缓冲区,并准备接收数据的引脚。
3.解析数据接收到数据后,我们需要对它进行解析。
D H T11模块发送的数据包括温度和湿度的整数部分和小数部分。
我们需要按照一定的规则将这些数据进行解析,得到最终的温度和湿度数值。
4.计算温湿度解析完数据后,我们可以根据DH T11模块的计算公式来得到真实的温度和湿度数值。
这个公式在D HT11模块的数据手册中有详细的说明。
5.显示结果最后,我们可以将测量得到的温湿度数据显示在单片机的L CD屏幕上,或者通过串口进行输出。
以上就是使用DH T11模块测量温湿度的完整流程。
通过连接电路、初始化、接收数据、解析数据和计算温湿度,我们可以准确地测量环境中的温度和湿度,为后续的应用提供数据支持。
小结本文介绍了使用D HT11模块测量温湿度的流程。
通过连接电路、初始化、接收数据、解析数据和计算温湿度,我们可以轻松地获取环境的温湿度数据。
这对于许多物联网和环境监测应用来说是非常重要的。
希望通过本文的介绍,你能够更好地理解和应用DH T11模块。
lcd1602显示及测试程序
本程序共分为三个文件:Main.c Lcd1602.c Lcd1602.h/************文件Main.c*************/#include<reg52.h>#include<Lcd1602.h>#define uchar unsigned char#define uint unsigned intvoid delay_ms(uint x){uint i,j;for(i=0;i<x;i++)for(j=0;j<120;j++);}void main(){Lcd_init();Write_char(0,10,'a');Write_string(1,0,"b b");delay_ms(1000);Clear_line(0);Write_string(0,0,"clear 0 line OK!");delay_ms(1000);Clear_line(1);Write_string(1,0,"clear 1 line OK!");delay_ms(1000);Clear_all();Write_string(0,1,"Clear all Ok!") ;while(1);}/********文件Lcd1602.c**************/#include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int#define Nop _nop_()#define Lcd_data P0 /*数据口*/#define busy 0x80 /*检测忙状态*/sbit rs = P2^4;sbit rw = P2^5;sbit en = P2^6;void delay(uchar x){uchar i,j;for(i=0;i<x;i++)for(j=0;j<125;j++); }/*写命令*/void Write_com(uchar com) {rs = 0;delay(5);rw = 0;delay(5);Lcd_data = com;delay(5);en = 0;delay(10);en =1;delay(10);}/*写数据*/void Write_data(uchar dat) {rs = 1;delay(5);rw = 0;delay(5);Lcd_data = dat;delay(10);en =0;delay(5);en = 1;delay(10);}/*初始化*/void Lcd_init(){Lcd_data = 0x00;delay(15);Write_com(0x38);delay(5);Write_com(0x38);delay(5);Write_com(0x38);delay(5);Write_com(0x38); /*显示模式设置*/Write_com(0x08); /*显示关闭*/Write_com(0x01); /*显示清屏*/Write_com(0x06); /*显示光标移动设置*/Write_com(0x0c); /*显示开及光标设置*/}void Write_char(uchar x,uchar y,uchar dat){x &=0x1; /*限制x不能大于1*/y &=0xf; /*限制y不能大于15*/if(x)y |= 0x40 ; /*如果y大于1,则在第二行显示*/ y |= 0x80;Write_com(y);Write_data(dat);}void Write_string(uchar x,uchar y,uchar *str){uchar length = 0;x &=0x1; /*限制x不能大于1*/y &=0xf; /*限制y不能大于15*/while(*str!='\0'){Write_char(x,y,*str);y++;str++;}}void Clear_line(uchar line){Write_string(line,0," ");switch(line){case 0: Write_com(0x80);case 1: Write_com(0x80+0x40);default : break;}}void Clear_all(){Write_com(0x01);Write_com(0x80);}/**********文件Lcd1602.h**************/#define uchar unsigned char#define uint unsigned int#ifndef Lcd1602_h#define Lcd1602_hextern Lcd_init();extern Write_data(uchar dat);extern Write_char(uchar x,uchar y,uchar dat);extern Write_string(uchar x,uchar y,uchar *dat);extern Clear_line(uchar line);extern Clear_all();#endif已经测试通过,由于百度文库不能上传压缩包,故分三个文件。
DHT11测温湿度程序lcd1602显示
DHT11测温湿度程序lcd1602显示#include<reg52.h>#include<intrins.h>#define uchar unsigned char #define uint unsigned int #define Data P0 // 数据端口sbit RS=P2A4;sbit RW=P2A5;sbit E=P2A6;sbit DHT=P1A0;uchar FirstLine[] ="wen:00.00"; // 第一行数据uchar SecondLine[]="shi:00.00"; // 第二行数据unsigned char shiZ,shiX,wenZ,wenX,check;unsigned char tr_shiZ,tr_shiX,tr_wenZ,tr_wenX;unsigned char flag;unsigned int n=20,m;void delay_1ms(unsigned int i) {unsigned int j=88;for(;i>0;i--){while(j>0)j--;}}void delay_10us()unsigned char i; i--;i--;i--;i--;i--;i--;char read_data(){unsigned char i,num,temp;num=0;for(i=0;i<8;i++){flag=2;while((!DHT)&&flag++);delay_10us();delay_10us();delay_10us();if(DHT==1)}温湿度读取函数************** */temp=1;flag=2;while(DHT&&flag++);}elsetemp=0;num<<=1; num|=temp;}return(num);}void delay(uchar ms) // 延时函数uchar i,j;for(i=ms;i>0;i--) for(j=100;j>0;j--); } voidDelayUs(unsigned char us) //--unsigned char uscnt; uscnt=us>>1; /*12MHz 频率 */ while(--uscnt); }void DelayMs(unsigned char ms) { while(--ms){DelayUs(250);ms 毫秒{ 延时函数 {DelayUs(250);DelayUs(250);DelayUs(250);}}void lcd_write_com(uchar c) // 写命令{DelayMs(5);// 操作前短暂延时,保证信号稳定E=0;RS=0;RW=0;_nop_();E=1;Data=c;E=0;}void lcd_write_dat(uchar c) // 写数据{ DelayMs(5); // 操作前短暂延时,保证信号稳定E=0;RS=1;RW=0;_nop_();E=1;Data=c;E=0;RS=0;}void lcd_init() //LCD 初始化{DelayMs(15);lcd_write_com(0x38); //display modelcd_write_com(0x38); //display modelcd_write_com(0x38); //display modevoid ShowChar(uchar pos,uchar c) //unsigned char p;if (pos>=0x10) p=pos+0xb0; // 是第二行则命令代码高 else p=pos+0x80; // 是第二行则命令代码高 4 位为 0x8 lcd_write_com(p);// 写命令 lcd_write_dat(c); // 写数据}void ShowString (uchar line,char *ptr) //显示字符串 {unsigned char l,*p;lcd_write_com(0x06); //显示光标移动位置 lcd_write_com(0x0c); //显示开及光标设置 lcd_write_com(0x01); //显示清屏 显示单个字符 {4 位为 0xcp=ptr;l=line<<4;while((*p)!='\0'){ShowChar(l++,*(p));p++;}}void disp(void) // 主函数调用的显示函数{ ShowString(0,FirstLine);ShowString(1,SecondLine); } void read_init()初始化及采集程序**************/{DHT=O; //主机使DHT11低电平并延时至少18msdelay_1ms(21);DHT=1; //主机置DHT11高电平20~40us,并等待从机相应delay_1Ous();delay_10us();delay_10us();delay_10us();DHT=1;if(!DHT) // 从机发出相应信号{flag=2; while((!DHT)&&flag++);flag=2; while(DHT&&flag++); // tr_shiZ=read_data();// tr_shiX=read_data();//tr_wenZ=read_data();//tr_wenX=read_data();//check=read_data(); //DHT=1;}}void main(){unsigned char temp; lcd_init();delay(50); while(1){ disp();read_init();temp=tr_shiZ+tr_shiX+tr_wenZ+tr_wenX; if(check==temp) {shiZ=tr_shiZ; shiX=tr_shiX; wenZ=tr_wenZ; wenX=tr_wenX; }FirstLine[4]='0'+wenZ/10;FirstLine[5]='0'+wenZ%10;FirstLine[8]='0'+wenX/10;FirstLine[9]='0'+wenX%10;SecondLine[4]='0'+shiZ/10;开始采集数据 采集湿度整数部分 采集湿度小数部分 采集温度整数部分 采集温度小数部分 采集校验位SecondLine[5]='0'+shiZ%10; SecondLine[8]='0'+shiX/10; SecondLine[9]='0'+shiX%10; } }。
DHT11温湿度测量电路图及程序
PO=Oxff;
PO=table[j/1O];
dula=1;
dula=0;
delay(1);
P0=0xff;
P0=wei[i+1];
wela=1;
wela=0;
P0=0xff;
P0=table[j%10];
dula=1;
dula=0;
delay(1);
}
void mai n()
{
while(1)
}
retur n data_byte;
}
void receive()
uchar T_H,T_L,R_H,R_L,check, num _check;
uchar count;
start();〃开始信号
io=1;
if(!io)〃读取DHT11响应信号
{Байду номын сангаас
coun t=2;
while((!io)&&count++);//DHT11高电平80us是否结束
LLAU
五门
「1H
91啊
N工
frl91
口U
丁】初
廿]利
□□A
0
i
■<■1
II
G
H
ll
J
n
L
他
J
n
9
tod
0
fi
g
p
91
t
阳
J
u
r
IfM
q
fir
c
IJ
6(
i
CtNU
9H—
j
(IND
UK
□L
□9
基于51单片机的DHT11温湿度监测+液晶LCD1602显示程序源代码
基于51单片机的DHT11温湿度监测+液晶LCD1602显示程序源代码/***************DHT11温湿度监测+液晶LCD1602显示程序源代码******************单片机型号:STC15W4K56S4,内部晶振:22.1184M。
功能:DHT11温湿度监测+液晶LCD1602显示。
操作说明:通过温湿度传感器DHT11监测温湿度数值,并将温湿度数值显示在液晶LCD1602上。
**************************************************************************/#include "stc15.h" //包含头文件stc15.h#include <intrins.h> //包含头文件intrins.h#define Busy 0x80 //LCD忙sbit LCD_D0 = P0^0; //LCD_D0对应P0.0sbit LCD_D1 = P0^1; //LCD_D1对应P0.1sbit LCD_D2 = P0^2; //LCD_D2对应P0.2sbit LCD_D3 = P0^3; //LCD_D3对应P0.3sbit LCD_D4 = P0^4; //LCD_D4对应P0.4sbit LCD_D5 = P0^5; //LCD_D5对应P0.5sbit LCD_D6 = P0^6; //LCD_D6对应P0.6sbit LCD_D7 = P0^7; //LCD_D7对应P0.7sbit LCD_RS = P1^0; //LCD_RS对应P1.0sbit LCD_RW = P1^1; //LCD_RW对应P1.1sbit LCD_EN = P3^4; //LCD_EN对应P3.4sbit DHT11_PIN = P4^0; //DHT11管脚对应P4.0void delay(unsigned int t); //delay延时函数void delay_us(unsigned int t); //delay_us延时函数void delay_ms(unsigned int t); //delay_ms延时函数void Delay5Ms(void); //5Ms延时函数void GPIO_1602_Configuration(void); //LCD1602液晶IO口初始化void WriteDataLCD(unsigned char WDLCD); //LCD写数据函数void WriteCommandLCD(unsigned char WCLCD,BuysC); //LCD写命令函数unsigned char ReadDataLCD(void); //LCD读数据函数unsigned char ReadStatusLCD(void); //LCD读状态函数void LCDInit(void); //LCD初始化void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData);//LCD显示一个字符void DisplayListChar(unsigned char X,unsigned char Y,unsigned char code *DData); //LCD显示一个字符串void DHT11_Init(void); //初始化DHT11void DHT11_Delay(unsigned int j); //延时函数,用于DHT11 void DHT11_Delay_10us(void); //延时函数,用于DHT11 void COM(void);void RH(unsigned char *temp,unsigned char *humi);void DHT11_Display(void);unsigned char code welcome[] = {"DHT 11"}; //LCD显示内容DHT 11 unsigned char code Dht11[] = {"T: H: "}; //LCD显示内容T: H: unsigned char code Space[] = {" "};//LCD显示内容空白unsigned char U8FLAG,k;unsigned char U8count,U8temp;unsigned char U8T_data_H_temp,U8T_data_L_temp;unsigned char U8RH_data_H_temp,U8RH_data_L_temp;unsigned char U8checkdata_temp;unsigned char U8comdata;unsigned char temperature;unsigned char humidity;unsigned char disbuff_T[4]={0,0,0,0};unsigned char disbuff_H[4]={0,0,0,0};void delay(unsigned int t) //delay延时函数{while(t--);}void delay_us(unsigned int t) //delay_us延时函数{unsigned char i;while(t--){i = 3;while(i--) delay(1);}}void delay_ms(unsigned int t) //delay_ms延时函数{while(t--){delay_us(t);}}void Delay5Ms(void) //5ms延时函数{unsigned int TempCyc = 3552;while(TempCyc--);}void GPIO_1602_Configuration(void) //LCD1602液晶IO口初始化{P0M1 = P3M1&0x00;P0M0 = P3M0&0x00;P1M1 = P3M1&0xfc;P1M0 = P3M0&0xfc;P3M1 = P4M1&0xef;P3M0 = P4M0&0xef;}unsigned char ReadStatusLCD(void) //测试LCD忙碌状态{LCD_D7 = 1; //LCD的D7置1LCD_RS = 0; //LCD管脚RS设置成低电平LCD_RW = 1; //LCD管脚RW设置成高电平LCD_EN = 0; //LCD管脚E设置成低电平LCD_EN = 0; //LCD管脚E设置成低电平LCD_EN = 1; //LCD管脚E设置成高电平while(LCD_D7); //检测忙信号return(Busy); //表示当前忙}void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC为0时忽略忙检测{if(BuysC) ReadStatusLCD(); //根据需要检测忙LCD_EN = 0; //LCD管脚E设置成低电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_RS = 0; //LCD管脚RS设置成低电平LCD_RW = 0; //LCD管脚RW设置成低电平_nop_(); //空操作,延时_nop_(); //空操作,延时P0 = WCLCD; //将数据送入P0口,即写入指令或地址 _nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 1; //E置高电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 0;//当E由高电平跳变成低电平时,液晶模块开始执行命令}void WriteDataLCD(unsigned char WDLCD) //LCD写数据函数{ReadStatusLCD(); //读取LCD状态LCD_EN = 0; //LCD管脚E设置成低电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_RS = 1; //LCD管脚RS设置成高电平LCD_RW = 0; //LCD管脚RW设置成低电平P0 = WDLCD; //将数据送入P0口_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 1; //E置高电平_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时_nop_(); //空操作,延时LCD_EN = 0;//当E由高电平跳变成低电平时,液晶模块开始执行命令}void LCDInit(void) //LCD初始化{WriteCommandLCD(0x38,0); //三次显示模式设置,不检测忙信号Delay5Ms();WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,0);WriteCommandLCD(0x08,1); //关闭显示WriteCommandLCD(0x01,1); //显示清屏WriteCommandLCD(0x06,1); //显示光标移动设置WriteCommandLCD(0x0C,1); //显示开及光标设置}void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData){Y &= 0x1;X &= 0xF; //限制X不能大于15,Y不能大于1if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;X |= 0x80; //算出指令码WriteCommandLCD(X,0); //这里不检测忙信号,发送地址码WriteDataLCD(DData); //发送数据}void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData) {unsigned char ListLength;ListLength = 0;Y &= 0x1;X &= 0xF; //限制X不能大于15,Y不能大于1while (DData[ListLength]>=0x20) //若到达字串尾则退出{if (X <= 0xF) //X坐标应小于0xF{DisplayOneChar(X, Y, DData[ListLength]);//显示单个字符ListLength++;X++;}}}void DHT11_Init(void) //初始化DHT11 {P4M1 = P4M1&0xfe;P4M0 = P4M0&0xfe;}void DHT11_Delay(unsigned int j){unsigned char i;for(;j>0;j--){for(i=0;i<250;i++);}}void DHT11_Delay_10us(void){unsigned char i;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;i--;}void COM(void){unsigned char i;for(i=0;i<8;i++){U8FLAG=2;while((!DHT11_PIN)&&U8FLAG++);DHT11_Delay_10us();DHT11_Delay_10us();DHT11_Delay_10us();U8temp=0;if(DHT11_PIN)U8temp=1;U8FLAG=2;while((DHT11_PIN)&&U8FLAG++); //超时则跳出for循环if(U8FLAG==1)break;//判断数据位是0还是1,如果高电平高过预定0高电平值则数据位为 1U8comdata<<=1;U8comdata|=U8temp;}}void RH(unsigned char *temp,unsigned char *humi){DHT11_PIN = 0;DHT11_Delay(180);DHT11_PIN = 1; //总线由上拉电阻拉高主机延时20us DHT11_Delay_10us();DHT11_Delay_10us();DHT11_Delay_10us();DHT11_Delay_10us(); //主机设为输入判断从机响应信号DHT11_PIN = 1;//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!DHT11_PIN){U8FLAG=2;//判断从机是否发出 80us 的低电平响应信号是否结束while((!DHT11_PIN)&&U8FLAG++);U8FLAG=2;//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态while((DHT11_PIN)&&U8FLAG++); //数据接收状态COM();U8RH_data_H_temp=U8comdata;COM();U8RH_data_L_temp=U8comdata;COM();U8T_data_H_temp=U8comdata;COM();U8T_data_L_temp=U8comdata;COM();U8checkdata_temp=U8comdata;DHT11_PIN=1; //数据校验U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp); if(U8temp==U8checkdata_temp){*temp = U8T_data_H_temp;*humi = U8RH_data_H_temp;}}}void DHT11_Display(void){RH(&temperature,&humidity);disbuff_T[2]=temperature/100+0x30;disbuff_T[1]=temperature/10%10+0x30;disbuff_T[0]=temperature%10+0x30;disbuff_H[2]=humidity/100+0x30;disbuff_H[1]=humidity/10%10+0x30;disbuff_H[0]=humidity%10+0x30;DisplayOneChar(2,1,disbuff_T[2]);delay_ms(10); //延时DisplayOneChar(3,1,disbuff_T[1]);delay_ms(10); //延时DisplayOneChar(4,1,disbuff_T[0]);delay_ms(10); //延时DisplayOneChar(8,1,disbuff_H[2]);delay_ms(10); //延时DisplayOneChar(9,1,disbuff_H[1]);delay_ms(10); //延时DisplayOneChar(10,1,disbuff_H[0]);delay_ms(10); //延时}void main(void){GPIO_1602_Configuration(); //LCD1602液晶IO口初始化delay_ms(10); //延时LCDInit(); //LCD1602初始化delay_ms(10); //延时DHT11_Init(); //初始化DHT11DisplayListChar(5,0,welcome); //LCD1602显示Hello My Friends delay_ms(10); //延时while(1){DisplayListChar(0,1,Space); //LCD1602显示P: K1delay_ms(10); //延时DisplayListChar(0,1,Dht11); //LCD1602显示delay_ms(10); //延时DHT11_Display();delay_ms(200); //延时}}程序源代码是编译通过的DHT11温湿度监测模块接口电路图该程序的实际运行效果。
STC15单片机DHT11在LCD1602上显示程序
敬告:没有51单片机基础的人请慎重下载高质量实用性51单片机STC15W系列程序(4),STC8A系列可参考STC15单片机DHT11在LCD1602上显示程序注:在本节关于DHT11的程序有两种/*****************************************/版本一:/****************************************//****************************************/main函数程序:#include "Library.h"unsigned char strbuf[4];//void ValToStr(unsigned char *str,unsigned char *source,unsigned char len);unsigned char tmrflag = 0;unsigned char DHTbuf[5];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 DHTstr[8];P3M1 &= 0xBF; P3M0 &= 0xBF; P2M1 &= 0xE5; P2M0 &= 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(DHTbuf);// ValToStr(DHTstr,DHTbuf,sizeof(DHTbuf)-1);DHTstr[0] = DHTbuf[0]/10 + '0';DHTstr[1] = DHTbuf[0]%10 + '0';DHTstr[2] = DHTbuf[1]/10 + '0';DHTstr[3] = DHTbuf[1]%10 + '0';DHTstr[4] = DHTbuf[2]/10 + '0';DHTstr[5] = DHTbuf[2]%10 + '0';DHTstr[6] = DHTbuf[3]/10 + '0';DHTstr[7] = DHTbuf[3]%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)//{// unsigned char i;// while(len>0)// {// str[i*2] = source[i]/10+'0';// str[1+i*2] = source[i]%10+'0';// i++;// len--;// }//}/*********************************************/DHT11程序:/******************************8bit humidity integer data + 8bit humidity decimal data 8bit temperature integer data + 8bit temperature decimal data8bit check sum,high bit ahead*******************************/#include "Library.h"unsigned char u8flag;unsigned char check_buf[5];unsigned char tmp;void delay_1s() //@11.0592MHz{unsigned char i, j, k;_nop_();_nop_();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;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;i<8;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(unsigned char *rec_buf) {// delay_1s();DHT11port = 0;Delay20ms();DHT11port = 1;Delay10us();Delay10us();Delay10us();Delay10us();//DHT11port = 1;if(!DHT11port){u8flag=2;// while(!DHT11port);while((!DHT11port)&&u8flag++);// if(DHT11port)// {// while(DHT11port);u8flag=2;while((DHT11port)&&u8flag++);check_buf[0] = Rec_8bit();check_buf[1] = Rec_8bit();check_buf[2] = Rec_8bit();check_buf[3] = Rec_8bit();check_buf[4] = Rec_8bit();if(!DHT11port){while(!DHT11port);}DHT11port = 1;tmp = check_buf[0]+check_buf[1]+check_buf[2]+check_buf[3] ;if(tmp==check_buf[4]){rec_buf[0] = check_buf[0];rec_buf[1] = check_buf[1];rec_buf[2] = check_buf[2];rec_buf[3] = check_buf[3];}// }}else{rec_buf[0] = 0;rec_buf[1] = 0;rec_buf[2] = 0;rec_buf[3] = 0;}}/*********************************************/ LCD1602程序:#include "Library.h"void LCD1602_rsta(){unsigned char tmp;P0 = 0xFF;//this is a mustrs = 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(x|0x80);}void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len){Setcursor(x,y);while(buf_len>0){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(0x01);}/*****************************************/ Library.h#ifndef _Library_H#define _Library_H#include <STC15.h>#include <intrins.h>#define MAIN_Fosc 11059200Lsbit DHT11port = P3^6;void DHT11_start(unsigned char *rec_buf);sbit rs = P2^4;sbit rw = P2^3;sbit en = P2^1;void LCD1602_init();void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len);#endif/*****************************************/ 版本二(结构体):/****************************************/ /****************************************/ main程序:#include "Library.h"#include "string.h"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 DHTstr[8];P3M1 &= 0xBF; P3M0 &= 0xBF;P2M1 &= 0xE5; P2M0 &= 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);DHTstr[0] = ht.humi_h +'0' ;DHTstr[1] = ht.humi_l+'0' ;DHTstr[2] = ht.humi_dh +'0' ;DHTstr[3] = ht.humi_dl +'0' ;DHTstr[4] = ht.temp_h +'0' ;DHTstr[5] = ht.temp_l +'0' ;DHTstr[7] = ht.temp_dh +'0' ;DHTstr[6] = 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 data + 8bit humidity decimal data 8bit temperature integer data + 8bit temperature decimal data8bit check sum,high bit ahead*******************************/#include "Library.h"unsigned char u8flag;unsigned char check_buf[5];unsigned char tmp;unsigned char rec_buf[4];htstruct ht;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;_nop_();_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;i<8;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(){// delay_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_buf[0] = Rec_8bit();check_buf[1] = Rec_8bit();check_buf[2] = Rec_8bit();check_buf[3] = Rec_8bit();check_buf[4] = Rec_8bit();if(!DHT11port){while(!DHT11port);}DHT11port = 1;tmp = check_buf[0]+check_buf[1]+check_buf[2]+check_buf[3] ;if(tmp==check_buf[4]){rec_buf[0] = check_buf[0];rec_buf[1] = check_buf[1];rec_buf[2] = check_buf[2];rec_buf[3] = check_buf[3];}ht.humi_h = rec_buf[0]/10 ;ht.humi_l = rec_buf[0]%10;ht.humi_dh = rec_buf[1]/10;ht.humi_dl = rec_buf[1]%10;ht.temp_h = rec_buf[2]/10;ht.temp_l = rec_buf[2]%10;ht.temp_dh = rec_buf[3]/10;ht.temp_dl = rec_buf[3]%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 "Library.h"void LCD1602_rsta(){unsigned char tmp;P0 = 0xFF;//this is a mustrs = 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(x|0x80);}void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len){Setcursor(x,y);while(buf_len>0){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(0x01);}/******************************************/ Library.h#ifndef _Library_H#define _Library_H#include <STC15.h>#include <intrins.h>#define MAIN_Fosc 11059200Lsbit DHT11port = P3^6;typedef struct htstruct{unsigned char humi_h;unsigned char humi_l;unsigned char humi_dh;unsigned char humi_dl;unsigned char temp_h;unsigned char temp_l;unsigned char temp_dh;unsigned char temp_dl;}htstruct;extern htstruct ht;void DHT11_start();sbit rs = P2^4;sbit rw = P2^3;sbit en = P2^1;void LCD1602_init();void LCD1602_wBytes(unsigned char x,unsigned char y,unsigned char *buf,unsigned char buf_len);#endif。