DHT11_LCD1602温湿度检测与显示程序

合集下载

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从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显示程序

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上显示程序

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温湿度传感器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显示程序

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显示程序

DHT11的LCD1602显示程序
while(lcd_busy()); LCD_RS = 0; LCD_RW = 0;
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显示程序

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。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档