《DHT11测温湿度程序lcd1602显示.doc》由会员分享,可在线阅读,更多相关《DHT11测温湿度程序lcd1602显示.doc(5页珍藏版)》请在三一办公上搜索。
1、#include#include#define uchar unsigned char#define uint unsigned int#define Data P0 /数据端口sbit RS=P24;sbit RW=P25;sbit E=P26;sbit DHT=P10;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
2、char flag;unsigned int n=20,m;void delay_1ms(unsigned int i)unsigned int j=88;for(;i0;i-)while(j0)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;i8;i+)flag=2;while(!DHT)&flag+);delay_10us();delay_10us();delay_10us();if(DHT=
3、1) temp=1;flag=2;while(DHT&flag+);elsetemp=0;num0;i-)for(j=100;j0;j-);void DelayUs(unsigned char us)/-延时函数 unsigned char uscnt; uscnt=us1; /*12MHz频率*/ while(-uscnt);void DelayMs(unsigned char ms) while(-ms) DelayUs(250); DelayUs(250); DelayUs(250); DelayUs(250); void lcd_write_com(uchar c) /写命令 Dela
4、yMs(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 mode lcd_write_com(0x38); /display mode lcd_write_com(0x3
5、8); /display mode lcd_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位为0xc else p=pos+0x80; /是第二行则命令代码高4位为0x8 lcd_write_com(p);/写命令 lcd_write_dat(c); /写数据void ShowStri
6、ng (uchar line,char *ptr) /显示字符串 unsigned char l,*p; p=ptr; l=line4; 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高电平2040us,并等待从机相应delay_10us
7、();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;
8、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;FirstLine4=0+wenZ/10;FirstLine5=0+wenZ%10;FirstLine8=0+wenX/10;FirstLine9=0+wenX%10; SecondLine4=0+shiZ/10;SecondLine5=0+shiZ%10;SecondLine8=0+shiX/10;SecondLine9=0+shiX%10;