1602的DHT11温湿度显示程序
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);}。
温湿度传感器DHT11程序编写
/******************DHT11******************************** FileName:dht11.cAuthor:沧海麒麟Date:2011.05.29Version :1.0Description:适用于ATmega16(L)Function: 利用串口发送温度和湿度到电脑,同时用液晶显示*/#include <iom16v.h>#include <macros.h>#include <avrdef.h>#include "delay.c"#include "LCM1602.c"#define uchar unsigned char#define uint unsigned int#define DDR_1 DDRA|=BIT(PA0)#define DDR_0 DDRA&=~BIT(PA0)#define PORT_1 PORTA|=BIT(PA0)#define PORT_0 PORTA&=~BIT(PA0)#define MOSIN (PINA&0X01)uchar U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata; uchar DHT11_5DATA[5];uchar U8temp;uchar flag = 0;uchar humidity[] = "humidity :";uchar temperature[] = "temperature:";void COM(void){uchar i,j,m;for(i=0;i<5;i++){DHT11_5DATA[i]=0X00;for(j=0;j<8;j++){while(!MOSIN);//等待高电平的到来,当高电平到来后便开始计时Delay(60);//for(m=0;m<50;m++); //延时50us后看还是不是高电平,如果是高那么这位就为1,否则为0if(MOSIN){DHT11_5DATA[i] |= BIT(7-j);while(MOSIN);}//end of if}//end of for}//end of for}void DHT11(void){uchar i;DDR_1;PORT_0;Delay_Nms(20);//主机拉低18msPORT_1;//总线由上拉电阻拉高主机延时20usDelay(30);//for(i=0;i<23;i++); //此操作为微秒级的延时DDR_0;//主机设为输入判断从机响应信号Delay(15);//for(i=0;i<10;i++);//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!MOSIN) //{while(!MOSIN);//判断从机是否发出 80us 的低电平响应信号是否结束while(MOSIN);//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态COM();//数据接收状态DDR_1 ;PORT_1 ;U8temp=(DHT11_5DATA[0]+DHT11_5DATA[1]+DHT11_5DATA[2]+DHT11_5DATA[3]);//数据校验if(DHT11_5DATA[4]==U8temp){U8RH_data_H = DHT11_5DATA[0];U8RH_data_L = DHT11_5DATA[1];U8T_data_H = DHT11_5DATA[2];U8T_data_L = DHT11_5DATA[3];U8checkdata = DHT11_5DATA[4];}}}/***********端口初始化函数**********************/void init_port(void){PORTD|= 0x02; //注意这两个管脚要配置为输出DDRD |= 0x02;}/************串口初始化函数*********************/void init_uart0(void){UCSRB = 0x00;UBRRH = 0x00;UBRRL = 0x67;UCSRA = (1 << U2X);//双倍率工作模式UCSRB = (1 << RXEN)|(1 << TXEN);//|(1 << UCSZ2);使能接收器和发射器 UCSRC = 0x80|(1 << UCSZ1)|(1 << UCSZ0);}/************发送一个字节函数********************/void Send_Uart0(uchar data){while(!(UCSRA&(1<<UDRE)));UDR=data;}/************T/C1初始化函数**********************/void Timer1_InterruptMode(void)//T/C1中断方式函数{//stop errant interrupts until set upCLI(); //disable all interruptsTCCR1B = 0x00; //stopTCNT1H = 0xCF; //setupTCNT1L = 0x2C;TCCR1B = 0x03; //start TimerMCUCR = 0x00;GICR = 0x00;TIMSK = 0x04; //timer interrupt sourcesSEI(); //re-enable interrupts//all peripherals are now initialized}/************总初始化函数**********************/void Init_Devices(void){Init_Port();Init_1602(0x18);init_port();init_uart0();Timer1_InterruptMode();}/************中断服务函数**********************/#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF //9 void timer1_ovf_isr(void){//TIMER1 has overflowedTCNT1H = 0xCF; //reload counter high valueTCNT1L = 0x2C; //reload counter low valueflag++;}void main(void){Init_Devices();DisplayString(0,0,humidity);DisplayString(0,1,temperature);while(1){if(flag==10)//1s采集一次{ //调用温湿度读取子程序flag=0;CLI();DHT11();DisplayNumber(12,0,U8RH_data_H);//1602显示湿度Delay_Nms(10);DisplayNumber(12,1,U8T_data_H);//1602显示温度 Send_Uart0(U8RH_data_H);Send_Uart0(U8T_data_H);Send_Uart0(U8checkdata);Send_Uart0(U8temp);SEI();}}}。
单片机温度湿度控制程序(dht11)
1、温度湿度控制主程序(单片机)#include<reg52.h>#include "dht11.h"#define uchar unsigned char#define uint unsigned intsbit beep=P2^3;sbit k1=P3^2;sbit k2=P3^3;sbit k3=P3^6;sbit k4=P3^7;sbit rs=P3^5;sbit lcden=P3^4;uchar table0[]="T:00.00 TM:00.0 ";//初始化液晶温度实测值以及门限值缓存uchar table1[]="H:00.00 HM:00.0 ";//初始化液晶湿度实测值以及门限值缓存uchar table3[]="This is T/H test";//系统开机显示内容初始化缓存uchar table4[]="Result is coming";//系统开机显示内容初始化缓存static uchar flag;extern int temp_value,humi_value;void Delay_1ms(uint ms);uint TM,HM;void delay(uint x)//{uint a,b;for(a=x;a>0;a--)for(b=10;b>0;b--);}void delay1(uint x)//延时程序{uint a,b;for(a=x;a>0;a--)for(b=100;b>0;b--);}void write_com(uchar com)//液晶写指令操作{P0=com;rs=0;lcden=0;delay(10);lcden=1;delay(10);lcden=0;}void write_date(uchar date)//液晶写数据操作{P0=date;rs=1;lcden=0;delay(10);lcden=1;delay(10);lcden=0;}void init()//液晶初始化{write_com(0x38);delay(1);write_com(0x0c);delay(1);write_com(0x06);delay(1);write_com(0x01);delay(1);}void LCD_Display(uchar buff1[],uchar buff2[])//液晶显示程序{uchar aa;init();//液晶初始化write_com(0x80+16);//写入第一行数据指令delay(1);for(aa=0;aa<16;aa++)//写入第一行待显示数据{write_date(buff1[aa]);}write_com(0xc0+16); //写入第二行数据指令for(aa=0;aa<16;aa++)//写入第一行待显示数据{write_date(buff2[aa]);}for(aa=0;aa<16;aa++){write_com(0x18);delay(1);}}void Tem_Ch(uchar AA,uchar nn)//T门限数值替换{switch(AA){case 0:table0[nn]='0';break;case 1:table0[nn]='1';break;case 2:table0[nn]='2';break;case 3:table0[nn]='3';break;case 4:table0[nn]='4';break;case 5:table0[nn]='5';break;case 6:table0[nn]='6';break;case 7:table0[nn]='7';break;case 8:table0[nn]='8';break;case 9:table0[nn]='9';break;}}void Hum_Ch(uchar BB,uchar mm)//H门限数值替换{switch(BB){case 0:table1[mm]='0';break;case 1:table1[mm]='1';break;case 2:table1[mm]='2';break;case 3:table1[mm]='3';break;case 4:table1[mm]='4';break;case 5:table1[mm]='5';break;case 6:table1[mm]='6';break;case 7:table1[mm]='7';break;case 8:table1[mm]='8';break;case 9:table1[mm]='9';break;}}///////////void YZ_Resh()//门限值更新{uchar A1,A2,A3;A1=TM/10;//取十位A2=TM%10;//取个位A3=0;Tem_Ch(A1,11);//写入对应的液晶显示缓存Tem_Ch(A2,12);//写入对应的液晶显示缓存Tem_Ch(A3,14);//写入对应的液晶显示缓存A1=HM/10;A2=HM%10;A3=0;Hum_Ch(A1,11);Hum_Ch(A2,12);Hum_Ch(A3,14);}void key_test()//按键检测/*根据按键的检测结果进行温度湿度门限值的设定*/{bit kk1;bit kk2;bit kk3;bit kk4;uchar temp;k1=0;k2=1;k3=1;k4=1;kk1=k1;kk2=k2;kk3=k3;kk4=k4;if(kk3!=1){delay1(100);if(kk3!=1)//若可开=0且持续一段时间后任然为0,则执行有关操作{temp=P3;//按键一操作TM=TM+1;//提高温度门限值YZ_Resh();////更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}if(kk4!=1){delay1(100);if(kk4!=1){temp=P3;//按键二操作TM=TM-1;//降低温度门限值YZ_Resh();////更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}k1=1;k2=0;k3=1;k4=1;kk1=k1;kk2=k2;kk3=k3;kk4=k4;if(kk3!=1){delay1(100);if(kk3!=1){temp=P3;//按键三操作HM=HM+1;//提高湿度门限YZ_Resh();//更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}if(kk4!=1){delay1(100);if(kk4!=1){temp=P3;//按键四操作HM=HM-1;//降低湿度门限YZ_Resh();//更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}}void TempAndHumi_Resh()//更新液晶显示缓存中的温度湿度数值/*根据测得的结果将液晶显示缓存中的数据进行替换*/{uchar ge,shi,bai;ge=temp_value%10;shi=temp_value%100/10;bai=temp_value%1000/100;Tem_Ch(bai,2);Tem_Ch(shi,3);Tem_Ch(ge,5);ge=humi_value%10;shi=humi_value%100/10;bai=humi_value%1000/100;Hum_Ch(bai,2);Hum_Ch(shi,3);Hum_Ch(ge,5);}void beep_control()//警报状态控制函数/*如果实测温度超过门限值则报警*/{if(temp_value>(TM*10)){beep=0;}else{if(humi_value>(HM*10)){beep=0;}else{beep=1;}}}void main(){uint m=0;TM=25;//设定温度初始门限值:25°CHM=50;//设定湿度初始门限值:50%YZ_Resh();//更新液晶显示缓存的门限值beep=1;//关闭蜂鸣器LCD_Display(table3,table4);//显示系统本系统信息while(1){key_test();//监测按键状态,并根据对应按键改变温度湿度门限值,并显示if (m<100)//循环计时,3秒钟{m++;Delay_1ms(30);}else//当m=100时即为3秒时间到,则进行温度湿度数据采集{m=0;ReadTempAndHumi();//采集温度湿度数据TempAndHumi_Resh();//更新液晶显示缓存中的温度湿度数值LCD_Display(table0,table1);//显示当前温度湿度实测值以及门限值}beep_control();//根据门限值以及实测值进行警报状态设置}}#include <reg52.h>#include "dht11.h"#define uchar unsigned char#define uint unsigned int#define NUMBER 20#define SIZE 5sbit dht11=P2^2;static uchar status;static uchar value_array[SIZE];//定义存放DHT11采集的5个字节的数组int temp_value,humi_value;static uchar ReadValue(void);extern void Delay_1ms(uint ms)//毫秒延时,延时时间为ms毫秒{uint x,y;for(x=ms;x>0;x--){for(y=124;y>0;y--);}}static void Delay_10us(void)//10微秒延时{uchar i;i--;i--;i--;i--;i--;i--;}static uchar ReadValue(void)//读一个字节数据的程序{uchar count,value=0,i;status=OK;//设定标志位正常状态for(i=8;i>0;i--){value<<=1;//高位在前进行接收数据count=0;while(dht11==0&&count++<NUMBER);if(count>=NUMBER)//若大于最大数值则出错{status=ERROR;//设定错误标志return 0;//函数执行过程发生错误就退出函数}Delay_10us();//进行30us的延时Delay_10us();Delay_10us();if(dht11!=0)//再次检测是否为高电平{value++;//进入本函数说明信号为1,则进行自加while(dht11!=0&&count++<NUMBER){dht11=1;}if(count>=NUMBER){status=ERROR;return 0;}}}return(value);//返回读取的结果}extern uchar ReadTempAndHumi(void)//读取一次温度湿度数据,共5个字节{uchar i=0,check_value=0,count=0;EA=0;dht11=0;//拉低数据线20msDelay_1ms(20);dht11=1;//释放数据线,以用于检测低电平应答信号Delay_10us();//延时40usDelay_10us();Delay_10us();Delay_10us();if(dht11!=0)//检测应答信号,应答信号为低电平{EA=1;//无低电平。
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。
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); //延时等待}}。
51单片机 DHT11温控程序可设置温度值
51单片机+DHT11温控程序-可设置温度值基于51单片机的温湿度测量控制程序,使用DHT11数字传感器,用1602液晶屏显示,按键定义如下:sbit key_1=P0^1; //确定按键sbit key_2=P0^2; //按键加sbit key_3=P0^3; // 按键减本程序中用到的头文件 12c5a.h下载:/f/12c5a.rar#include "12c5a.H"#include "intrins.h"unsigned char codenum[]={"0123456789"};//显示字符存储unsigned char codestart_char[]={"Loading...."};unsigned char code t_char[]={"NOW TEMPIS"};unsigned char *pnum=num;unsigned int bbb,ccc,temp,temp_high,temp_low;#define ADC_POWER0x80//ADC power control bit#define ADC_FLAG0x10//ADC complete flag#define ADC_START0x08//ADC start control bit#define ADC_SPEEDLL0x00//540 clocks#define ADC_SPEEDL0x20//360 clocks#define ADC_SPEEDH0x40//180 clocks#define ADC_SPEEDHH0x60//90 clockssbit RS=P3^4; //数据命令选择端sbit RW=P3^5; //读写控制端sbit E=P3^6;//使能信号控制端//sbit key_0=P0^0; //设置按键sbit key_1=P0^1; //确定按键sbit key_2=P0^2; //按键加sbit key_3=P0^3; // 按键减sbit P04=P0^4;sbit P05=P0^5;sbit P06=P0^6;void key_temphigh();void key_templow();void key_scan();void delay_1602(unsigned int a);void init_1602(void);void WR_data_1602(unsigned char adata,unsigned char i);void process(unsigned int shuju);void Delay(unsigned char n){int x;while (n--){x = 500;while (x--);}}void init_ADC(void){P1ASF=0xFF;ADC_RES = 0;ADC_RESL = 0;ADC_CONTR = 0x88;Delay(2);}unsigned int GetResult(void){unsigned char m,n;unsigned int aaa;init_ADC();while (!(ADC_CONTR & ADC_FLAG));//Wait complete flagADC_CONTR =0x80;//Close ADCm=ADC_RES;n=ADC_RESL;aaa=m*4+n;return aaa;}void start_init(void){unsigned char i;WR_data_1602(0x80,0);//显示在第一行for(i=0;i;temp_high-5) {P04=1;P05=0;P06=1;}//上限25else if(temp<temp_low+5) {P04=0;P05=1;P06=1;} // 下限20else {P04=1;P05=1;P06=0;}}void init_1602(void) //初始化{WR_data_1602(0x38,0);WR_data_1602(0x01,0);WR_data_1602(0x06,0);WR_data_1602(0x0c,0);}void WR_data_1602(unsigned char adata,unsigned char i) //写数据i=1,写指令i=0{P2=adata;RS=i;RW=0;E=1;delay_1602(1);E=0;RW=1;RS=0;void process(unsigned int shuju)//显示数据处理{unsigned int a,b,c,d;a=shuju/1000;b=(shuju%1000)/100;c=(shuju%100)/10;d=shuju%10;if(a==0&&b!=0){WR_data_1602(0xc2,0);WR_data_1602(*(pnum+b),1);WR_data_1602(*(pnum+c),1);WR_data_1602('.',1);WR_data_1602(*(pnum+d),1);WR_data_1602('C',1);}if(a==0&&b==0){WR_data_1602(0xc2,0);WR_data_1602(*(pnum+c),1);WR_data_1602('.',1);WR_data_1602(*(pnum+d),1);WR_data_1602('C',1);WR_data_1602(' ',1);}//WR_data_1602(0xc1,0);//显示在第二行//WR_data_1602(' ',1);//WR_data_1602(*(pnum+a),1); //WR_data_1602(*(pnum+b),1); //WR_data_1602(*(pnum+c),1); //WR_data_1602('.',1);//WR_data_1602(*(pnum+d),1); //WR_data_1602('C',1);delay_1602(1);}void main()//主函数{unsigned int mm;init_1602();//初始化start_init();mm=GetResult();temp_high=250;temp_low=90;while(1){key_scan();baojing();ccc=GetResult(); temp=ccc*5.0;process(temp); delay_1602(10); }}。
51单片机+DHT11温控程序-可设置温度值
51单片机+DHT11温控程序-可设置温度值基于51 单片机的温湿度测量控制程序,使用DHT11 数字传感器,用1602液晶屏显示,按键定义如下:sbit key_1=P0;//确定按键sbit key_2=P0 ; //按键加sbit key_3=P0;//按键减本程序中用到的头文件12c5a.h 下载:51hei/f/12c5a.rar#include “12c5a.H”#include“intrins.h”unsigned char code num[]={“0123456789”};//显示字符存储unsigned char code start_char[]={“Loading....”};unsigned char code t_char[]={“NOW TEMP IS”};unsigned char *pnum=num;unsigned int bbb,ccc,temp,temp_high,temp_low;#define ADC_POWER 0x80 //ADC power control bit#define ADC_FLAG 0x10 //ADC completeflag#define ADC_START 0x08 //ADC start control bit#define ADC_SPEEDLL 0x00 //540 clocks#define ADC_SPEEDL 0x20 //360 clocks#define ADC_SPEEDH 0x40 //180 clocks#define ADC_SPEEDHH 0x60 //90 clockssbit RS=P3;//数据命令选择端sbit RW=P3 ; //读写控制端sbit E=P3;//使能信号控制端//sbit key_0=P0; //设置按键sbit key_1=P0;//确定按键sbitkey_2=P0 ; //按键加sbit key_3=P0;//按键减sbit P04=P0;sb it P05=P0 ;sbitP06=P0;void key_temphigh();void key_templow();void key_scan();void delay_1602(unsigned int a);void init_1602(void);void WR_data_1602(unsigned char adata,unsigned char i);void process(unsigned int shuju);void Delay(unsigned char n){ int x; while (n--) { x = 500; while (x--); }}void init_ADC(void){P1ASF=0xFF;ADC_RES = 0;ADC_RESL = 0;ADC_CONTR = 0x88;Delay(2);}unsigned int GetResult(void){unsigned char m,n;unsigned int aaa; init_ADC();。
DHT11温湿度测量电路图及程序
D H T11温湿度测量电路图及程序(总5页)本页仅作为文档封面,使用时可以删除This document is for reference only-rar21year.March#include <>#define uint unsigned int#define uchar unsigned charsbit io=P1^0;//DHT11数据端sbit wela=P2^7;// 数码管位选sbit dula=P2^6;//数码管段选uchar data_byte;uchar RH,RL,TH,TL;uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98}; uchar wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};void delay(uchar z){uchar x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void delay1() //延时10us{uchar i;i--;i--;i--;i--;i--;i--;}void start()//开始信号{io=1;delay1();io=0;delay(20);//>18msio=1;delay1();//20-40usdelay1();delay1();delay1();delay1();}uchar receive_byte(){uchar i,temp,count;for(i=0;i<8;i++){count=2;while((!io)&&count++)//等待50us低电平结束temp=0;delay1();delay1();delay1();delay1();if(io==1)temp=1;count=2;while((io)&&count++);if(count==1)break;data_byte<<=1;data_byte|=temp;}return data_byte;}void receive(){uchar T_H,T_L,R_H,R_L,check,num_check;uchar count;start();//开始信号io=1;if(!io)//读取DHT11响应信号{count=2;while((!io)&&count++);//DHT11高电平80us是否结束count=2;while((io)&&count++);R_H=receive_byte();R_L=receive_byte();T_H=receive_byte();T_L=receive_byte();check=receive_byte();io=0;//拉低延时50usdelay1();delay1();delay1();delay1();delay1();io=1;num_check=R_H+R_L+T_H+T_L;if(num_check=check){RH=R_H;RL=R_L;TH=T_H;TL=T_L;check=num_check;}}}void display(uchar i,uchar j)//数码管显示{P0=wei[i];wela=1;wela=0;P0=0xff;P0=table[j/10];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 main(){while(1){receive();display(0,RH);delay(1);display(2,TH);delay(1);// display(4,TH);// delay(1);// display(6,TL);// delay(1);}}。
51单片机DHT11芯片,1603显示屏程序代码
51单片机DHT11芯片,1603显示屏程序代码#include<reg52.h>#include<intrins。
h>typedefunsignedcharuchar;typedefunsignedintuint;typedefunsignedlongulong;ucharcom,flag;ucharn,m,y,x,v,u,a1,a2;//温湿度上下限的参数,其中,nm是温湿度超标与否的标准,值为0或者1.这代码的温湿度报警没有用LED或者蜂鸣器,只用了一个软件参数n、m存储报警信息。
需要添加报警功能的可以自己写额外的功能代码,然后nm两个参数就是功能传递参数。
也可以写多一个串口输出代码,用串口调试助手输出n、m的值验证温湿度是否超标判断正确。
voidDelay1000ms()//@11.0592MHz延时{unsignedchari,j,k;_nop_();i=8;j=1;k=243;do{do{while(--k);}while(--j);}while(--i);}voidDelay500us()//@11.0592MHz {ucharb;_nop_();b=227;while(--b);}voidDelay10us()//@11.0592MHz {uchara;a=2;while(--a);}voiddelay(uintz){uchari,j;for(i=z;i>0;i--)for(j=110;j>0;j--);//ÑÓʱº¯Êý}//************************引脚定义************* /*******按键控制引脚定义*********/sbity_tem_inse=P1^0;//sbity_tem_duce=P1^1;//sbitx_tem_inse=P1^2;//sbitx_tem_duce=P1^3;//sbitu_hum_inse=P1^4;//sbitu_hum_duce=P1^5;//sbitv_hum_inse=P1^6;//sbitv_hum_duce=P1^7;///***********其他功能引脚*****/sbitDQ=P2^1;//DTH11//****LCD的****sbitRS=P2^7;sbitRW=P2^6;sbitEN=P2^5;#defineDataPortP0//LCD的接线,我的是8线接发//-----------------函数声明------------- voiddelay_us(uchart);voiddelay_ms(uchart);bitLCD_Check_Busy(void);voidLCD_Write_Com(ucharcom);voidLCD_Write_Data(ucharData);voidLCD_Clear(void);voidLCD_Write_String(ucharx,uchary,uchar*s);voidLCD_Write_Char(ucharx,uchary,ucharData);voidLCD_Init(void);voidCOMdata();//*************************以下是函数定义***************************//**************液晶屏显示原函数由此开始*************************bitLCD_Check_Busy(void)//检忙{DataPort=0xFF;RS=0;RW=1;EN=0;_nop_();EN=1;return(bit)(DataPort&0x80);voidLCD_Write_Com(ucharcom)//写指令原函数{while(LCD_Check_Busy());RS=0;RW=0;EN=1;DataPort=com;_nop_();EN=0;}voidLCD_Write_Data(ucharData)//写数据原函数{while(LCD_Check_Busy());RS=1;RW=0;EN=1;DataPort=Data;_nop_();EN=0;}voidLCD_Clear(void)//清屏LCD_Write_Com(0x01);delay(1);}voidLCD_Write_String(ucharx,uchary,uchar*s)//操作-显示LCD屏的哪个位置{if(y==0)LCD_Write_Com(0x80+x);elseLCD_Write_Com(0xC0+x);while(*s){LCD_Write_Data(*s);s++;}}voidLCD_Write_Char(ucharx,uchary,ucharData)//操作-显示的数据(字符类型,显示数字需找到该数字的ASCII码){if(y==0)LCD_Write_Com(0x80+x);elseLCD_Write_Com(0xC0+x);LCD_Write_Data(Data);}voidLCD_Init(void)//LCD初始化(可以理解为唤醒){delay(15);LCD_Write_Com(0x38);delay(5);LCD_Write_Com(0x38);delay(5);LCD_Write_Com(0x38);delay(5);LCD_Write_Com(0x38);LCD_Write_Com(0x08);LCD_Write_Com(0x01);LCD_Write_Com(0x06);delay(5);LCD_Write_Com(0x0C);}//****************LCD源码结束***********//*********DHT11原函数以下开始*******//*************************************************** ucharcodeASCII[]="0123456789%c";//普通数字需用该数组转成字符ucharcodetable0[]="Hum:";ucharcodetable1[]="Tem:";uchartem;ucharhum;display0[2]={0,0};display1[2]={0,0};//*************************初始化DTH11voidCOMdata()//读数据{uchari,t;for(i=0;i<8;i++){flag=2;while((!DQ)&&flag++);Delay10us();Delay10us();Delay10us();//先看懂DHT11的工作时序图就知道我这个延时操作的含义了,读高电平多少秒可以对应数据0或1t=0;if(DQ)t=1;while((DQ)&&flag++);if(flag==1)break;com<<=1;com|=t;}}voidRH(void)//该函数包括唤醒DHT11发送数据给单片机也包括单片机把数据提取{DQ=0;delay(21);DQ=1;Delay10us();Delay10us();Delay10us();Delay10us();DQ=1;if(!DQ){flag=2;while((!DQ)&&flag++);while((DQ)&&flag++);COMdata();hum=com;COMdata();COMdata();tem=com;COMdata();COMdata();//小数位这里我不显示,所以不用读取tem或hum 第二次的COMdata()函数获取的数据DQ=1;}}//*********************************LCD显示温湿度数据voidDisplay_DTH11(){ucharxh,xl,yh,yl,vh,vl,uh,ul;xh=x/10%10;xl=x%10;yh=y/10%10;yl=y%10;uh=u/10%10;ul=u%10;vh=v/10%10;vl=v%10;LCD_Write_String(8,0,"L");LCD_Write_Char(9,0,ASCII[uh]);LCD_Write_Char(10,0,ASCII[ul]);LCD_Write_String(12,0,"H");LCD_Write_Char(13,0,ASCII[vh]);LCD_Write_Char(14,0,ASCII[vl]);LCD_Write_String(8,1,"L");LCD_Write_Char(9,1,ASCII[xh]);LCD_Write_Char(10,1,ASCII[xl]);LCD_Write_String(12,1,"H");LCD_Write_Char(13,1,ASCII[yh]);LCD_Write_Char(14,1,ASCII[yl]);display0[1]=hum/10%10;//读hum十位display0[0]=hum%10;//个位LCD_Write_String(0,0,table0);LCD_Write_Char(4,0,ASCII[display0[1]]);LCD_Write_Char(5,0,ASCII[display0[0]]);LCD_Write_Char(6,0,ASCII[10]);//显示在LCD的第几行第几位(这一行是指在第0行,第6位开始显示ASCII[10]里面的字符数据,下同),运行结果是显示了“hum:”这四个字符display1[1]=tem/10%10;display1[0]=tem%10;LCD_Write_String(0,1,table1);LCD_Write_Char(4,1,ASCII[display1[1]]);LCD_Write_Char(5,1,ASCII[display1[0]]);LCD_Write_Char(6,1,0xdf);LCD_Write_Char(7,1,ASCII[11]);}//***************原函数结束*************//voidkeyscan(){if(y_tem_inse==0){delay(10);if(y_tem_inse==0)//这里是消抖,下同{y=y+5;while(y==55)y=50;while(y_tem_inse==0);}if(y_tem_duce==0){delay(10);if(y_tem_duce==0);{y=y-5;while(y==5)y=10;while(y_tem_duce==0);}}if(x_tem_inse==0){delay(10);if(x_tem_inse==0){x=x+5;while(x==55)x=50;while(x_tem_inse==0);}}if(x_tem_duce==0)delay(10);if(x_tem_duce==0);{x=x-5;while(x==5)x=10;while(x_tem_duce==0);}}if(u_hum_inse==0){delay(10);if(u_hum_inse==0){u=u+5;while(u==85)u=80;while(u_hum_inse==0);}}if(u_hum_duce==0){delay(10);if(u_hum_duce==0);u=u-5;while(u==25)u=30;while(u_hum_duce==0);}}if(v_hum_inse==0){delay(10);if(v_hum_inse==0){v=v+5;while(v==85)v=80;while(v_hum_inse==0);}}if(v_hum_duce==0){delay(10);if(v_hum_duce==0);{v=v-5;while(v==25)v=20;while(v_hum_duce==0);}}}//*******************************上面是按键扫描,以下开始是主函数main(){LCD_Clear();x=10,y=50,u=30,v=80;//温湿度上下限初值,按键函数里可调tem=0;hum=0;//赋初值LCD_Init();Delay1000ms();//DTH11上电一秒后才工作正常while(1){EA=0;keyscan();RH();if((tem>y)||(tem<x))n=1;elsen=0;//LED或蜂鸣器亮灭判断,下同if((hum>v)||(hum<u))m=1;elsem=0;Display_DTH11();Time0_Init();Delay1000ms();Delay1000ms();Delay1000ms();}}。
温湿度检测器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测温湿度程序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; } }。
基于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。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/****************************************************************DHT11使用范例单片机:STC89C52RC功能:1602实时显示温湿度硬件连接:P2.7口为通讯口连接DHT11,DHT11的电源和地连接单片机的电源和地****************************************************************/#include <reg51.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位长度)*/sbit P2_7 = P2^7 ;//定义DHT11通信IO口/**********************************************变量定义***********************************************/U8 U8FLAG,k;U8 U8count,U8temp;U8U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata,RH_shi,RH_ge,T_shi,T_ge;U8U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_te mp;U8 U8comdata;void Delay_1(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_7)&&U8FLAG++);Delay_10us();Delay_10us();Delay_10us();U8temp=0;if(P2_7)U8temp=1;U8FLAG=2;while((P2_7)&&U8FLAG++);if(U8FLAG==1)break; //超时则跳出for循环//判断数据位是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_7=0;Delay_1(180);P2_7=1;//总线由上拉电阻拉高主机延时20usDelay_10us();Delay_10us();Delay_10us();Delay_10us();//主机设为输入判断从机响应信号P2_7=1;//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!P2_7)//T !{U8FLAG=2;//判断从机是否发出80us 的低电平响应信号是否结束while((!P2_7)&&U8FLAG++);U8FLAG=2;//判断从机是否发出80us 的高电平,如发出则进入数据接收状态while((P2_7)&&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;P2_7=1;//数据校验U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);if(U8temp==U8checkdata_temp){U8RH_data_H=U8RH_data_H_temp;U8RH_data_L=U8RH_data_L_temp;U8T_data_H=U8T_data_H_temp;U8T_data_L=U8T_data_L_temp;U8checkdata=U8checkdata_temp;}//fi}//fi}/*********************************************函数功能:数进制转换********************************************/void Shuzhuanhuan(){RH_shi=(unsigned char)U8RH_data_H/10;RH_ge=U8RH_data_H%10;T_shi=U8T_data_H/10;T_ge=U8T_data_H%10;}/*********************************************//*********************************************/sbit RS=P2^0; //寄存器选择位,将RS位定义为P2.0引脚sbit RW=P2^1; //读写选择位,将RW位定义为P2.1引脚sbit E=P2^2; //使能信号位,将E位定义为P2.2引脚sbit BF=P0^7; //忙碌标志位,,将BF位定义为P0.7引脚unsigned char code digit[ ]={"0123456789"}; //定义字符数组显示数字,液晶只能显示ASCII,其中的7为0011 0111//RH_shi中7是0000 0111,unsigned char code User[]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00};//自定义字符℃*//*****************************************************函数功能:延时1ms***************************************************/void delay1ms(){unsigned char i,j;for(i=0;i<10;i++)for(j=0;j<33;j++);}/*****************************************************函数功能:延时若干毫秒入口参数:n***************************************************/void delay(unsigned int n){unsigned int i;for(i=0;i<n;i++)delay1ms();}/*****************************************************函数功能:判断液晶模块的忙碌状态返回值:result。
result=1,忙碌;result=0,不忙***************************************************/bit BusyTest(void){bit result;RS=0; //根据规定,RS为低电平,RW为高电平时,可以读状态RW=1;E=1; //E=1,才允许读写_nop_(); //空操作_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间result=BF; //将忙碌标志电平赋给resultE=0;return result;}/*****************************************************函数功能:将模式设置指令或显示地址写入液晶模块入口参数:dictate***************************************************/void Write_com (unsigned char dictate){while(BusyTest()==1); //如果忙就等待RS=0; //根据规定,RS和R/W同时为低电平时,可以写入指令RW=0;E=0; //E置低电平(写指令时,// 就是让E从0到1发生正跳变,所以应先置"0"_nop_();_nop_(); //空操作两个机器周期,给硬件反应时间P0=dictate; //将数据送入P0口,即写入指令或地址_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=1; //E置高电平_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=0; //当E由高电平跳变成低电平时,液晶模块开始执行命令}/*****************************************************函数功能:指定字符显示的实际地址入口参数:x***************************************************/void WriteAddress(unsigned char x){Write_com(x|0x80); //显示位置的确定方法规定为"80H+地址码x"}/*****************************************************函数功能:将数据(字符的标准ASCII码)写入液晶模块入口参数:y(为字符常量)***************************************************/void WriteData(unsigned char y){while(BusyTest()==1);RS=1; //RS为高电平,RW为低电平时,可以写入数据RW=0;E=0; //E置低电平(写指令时,,// 就是让E从0到1发生正跳变,所以应先置"0"P0=y; //将数据送入P0口,即将数据写入液晶模块_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=1; //E置高电平_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=0; //当E由高电平跳变成低电平时,液晶模块开始执行命令}/*****************************************************函数功能:对LCD的显示模式进行初始化设置***************************************************/void LcdInt(void){delay(15); //延时15ms,首次写指令时应给LCD一段较长的反应时间Write_com(0x38); //显示模式设置:16×2显示,5×7点阵,8位数据接口delay(5); //延时5msWrite_com(0x38);delay(5);Write_com(0x38); //3次写设置模式delay(5);Write_com(0x0F); //显示模式设置:显示开,有光标,光标闪烁delay(5);Write_com(0x06); //显示模式设置:光标右移,字符不移delay(5);Write_com(0x01); //清屏幕指令,将以前的显示内容清除delay(5);}/*****************************************************函数功能:显示自定义图形℃***************************************************/void Zidingyi(void){unsigned char j;Write_com(0x40); //设定CGRAM地址delay(5);for(j=0;j<8;j++ ){WriteData(User[j]);//写入自定义图形℃}WriteAddress(0x4c);//设定屏幕上的显示位置delay(5);WriteData(0x00);//从CGRAM里取出自定义图形显示}void main (void){LcdInt();delay(10);while(1){RH();Shuzhuanhuan();WriteAddress(0x02);//从第一行第三列开始显示WriteData('s');WriteAddress(0x03);WriteData('h');WriteAddress(0x04);WriteData('i');WriteAddress(0x05);WriteData('d');WriteAddress(0x06);WriteData('u');WriteAddress(0x07);WriteData(':');WriteAddress(0x08);WriteData(digit[RH_shi]);WriteAddress(0x09);WriteData(digit[RH_ge]);WriteAddress(0x0a);WriteData('.');WriteAddress(0x0b);WriteData('2');WriteAddress(0x0c);WriteData('%');WriteAddress(0x42);//从第一行第三列开始显示WriteData('w');WriteAddress(0x43);WriteData('e');WriteAddress(0x44);WriteData('n');WriteAddress(0x45);WriteData('d');WriteAddress(0x46);WriteData('u');WriteAddress(0x47);WriteData(':');WriteAddress(0x48);WriteData(digit[T_shi]);WriteAddress(0x49);WriteData(digit[T_ge]);WriteAddress(0x4a);WriteData('.');WriteAddress(0x4b);WriteData('4');WriteAddress(0x4c);Zidingyi();//delay(500);}}。