您的位置 首页 动态

dth11温湿度传感器的单片机程序

includereg52h>defineuintunsignedintdefineucharunsignedchartypedefbitBOOL;sbitio=P1^0;sbitrs

#include

#define uint unsigned int#define uchar unsigned chartypedef bit BOOL  ;sbit io = P1^0 ;sbit rs = P2^4 ;sbit rw = P2^5 ;sbit ep = P2^6 ;uchar data_byte;uchar RH,RL,TH,TL;/************************************************延时模块***************************************************/void delay(uchar ms){       // 延时子程序uchar i ;while(ms--){for(i = 0 ; i<250;i++) ;}}void delay1()//延时10us{uchar i;i--;i--;i--;i--;i--;i--;}void longdelay(uchar s) //长延时{while(s--){delay(60) ;}}/***********************************************LCD模块********************************************************/BOOL lcd_bz()//测验LCD繁忙状况{     BOOL result ;rs = 0 ;rw = 1 ;ep = 1 ;result = (BOOL)(P0 & 0x80) ;ep = 0 ;return result ;}void write_cmd(uchar cmd)// 写指令{      while(lcd_bz()) ;rs = 0 ;rw = 0 ;ep = 0 ;P0 = cmd ;ep = 1 ;ep = 0 ; }void write_addr(uchar addr)//写地址{      write_cmd(addr|0x80) ;}void write_byte(uchar dat)//写字节{     while(lcd_bz()) ;rs = 1 ;rw = 0 ;ep = 0 ;P0 = dat ;ep = 1 ;ep = 0 ;}void lcd_init()// 初始化{      write_cmd(0x38) ;delay(1);write_cmd(0x08) ; delay(1);write_cmd(0x01) ;delay(1);write_cmd(0x06) ;delay(1);write_cmd(0x0c) ;delay(1);}void display(uchar addr, uchar q)//在某一地址上显现一字节{  delay(10) ;write_addr(addr) ;write_byte(q) ;longdelay(2) ;}/************************************************ DHT11测验模块***********************************************/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 main(){lcd_init();delay(10);while(1){receive();delay(100);display(0x00,R) ;display(0x01,:);display(0x02,RH/10+0x30);display(0x03,RH%10+0x30);display(0x04,%);display(0x40,T) ;display(0x41,:);display(0x42,TH/10+0x30);display(0x43,TH%10+0x30);display(0x44,0xdf);display(0x45,0x43);}}/**********************************************/

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/dongtai/255648.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部