DHT11温湿度传感器驱动程序.docx
《DHT11温湿度传感器驱动程序.docx》由会员分享,可在线阅读,更多相关《DHT11温湿度传感器驱动程序.docx(10页珍藏版)》请在冰点文库上搜索。
![DHT11温湿度传感器驱动程序.docx](https://file1.bingdoc.com/fileroot1/2023-5/14/8271b263-26de-4456-b3ae-a675b990c403/8271b263-26de-4456-b3ae-a675b990c4031.gif)
/*本程序适用于DHT11温湿度传感器由于DHT11使用拉高总线的不同时常来代替0和1028US170us同时数据送入1602a显示*/
#include
#include
#include
#defineDataP0//数据端口
sbitRS=P2^4;//Pin4
sbitRW=P2^5;//Pin5
sbitE=P2^6;//Pin6
sbitdat=P1^2;
unsignedintshidu_h=0,shidu_l=0,wendu_h=0,wendu_l=0,i,xiaoyan=0;//定义湿度温度变量
unsignedcharcodetab[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};//定义1602a显示0-9
unsignedinta[16]={0};
voiddelayus(unsignedchari)//延时2us
{
while(--i);
}
voiddelayms(unsignedcharj)//延时1ms
{
while(--j)
{
delayus(125);
delayus(125);
delayus(125);
delayus(125);
}
}
/******************************************************************/
/*写入命令函数1602*/
/******************************************************************/
voidWriteCommand(unsignedintc)
{
delayms(10);//操作前短暂延时,保证信号稳定代替状态监测
E=0;
RS=0;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
}
/******************************************************************/
/*写入数据函数*/
/******************************************************************/
voidWriteData(unsignedcharc)
{
delayms(10);;//操作前短暂延时,保证信号稳定
E=0;
RS=1;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
RS=0;
}
/******************************************************************/
/*写入字节函数*/
/******************************************************************/
voidShowChar(unsignedcharpos,unsignedintc)
{
unsignedcharp;
if(pos>=0x10)
p=pos+0xb0;//是第二行则命令代码高4位为0xcp低两位表示位置显示代码只用DDRAM前16位
else
p=pos+0x80;//是第一行则命令代码高4位为0x8p低两位表示位置显示代码只用DDRAM前16位
WriteCommand(p);//写命令写入数据指针确定行中哪一位显示
WriteData(c);//写数据
}
/******************************************************************/
/*写入字符串函数*/
/******************************************************************/
voidShowString(unsignedcharline,unsignedint*ptr)
{
unsignedcharl,i;//l表示行中某一位显示
l=line<<4; //L为0x01时左移四位为了与b0H相加变为0xc
for(i=0;i<16;i++)
ShowChar(l++,*(ptr+i));//循环显示16个字符
}
/******************************************************************/
/*初始化函数*/
/******************************************************************/
voidInitLcd()
{
WriteCommand(0x38);//displaymode
WriteCommand(0x38);//displaymode
WriteCommand(0x38);//displaymode
WriteCommand(0x06);//显示光标移动位置
WriteCommand(0x0c);//显示开及光标设置
WriteCommand(0x01);//显示清屏
}
voidmain(void)
{
InitLcd();//初始化LCD
delayms(30);
while
(1)
{
dat=0;
delayms(18);//延时18ms拉低主线
dat=1;//主机拉高总线
/*delayus(20);*/
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
while(!
dat);//判断从机是否相应完毕相应完毕会拉高总线
while(dat);//等待从机拉高总线准备开始传送数据
while(!
dat);//判断是否开始1bit开始会拉高总线
for(i=0;i<8;i++)
{
shidu_h<<=1;//右移1位
delayus(14);//延时30us判断是否为1
if(dat)
{
shidu_h++;
}
while(dat);//为一则等待从机拉低总线为零直接跳出
while(!
dat);//判断是否开始1bit开始会拉高总线
}
for(i=0;i<8;i++)
{
delayus(14);
shidu_l<<=1;
if(dat)
{
shidu_l++;
}
while(dat);
while(!
dat);
}
for(i=0;i<8;i++)
{
delayus(14);
wendu_h<<=1;
if(dat)
{
wendu_h++;
}
while(dat);
while(!
dat);
}
for(i=0;i<8;i++)
{
delayus(14);
wendu_l<<=1;
if(dat)
{
wendu_l++;
}
while(dat);
while(!
dat);
}
for(i=0;i<8;i++)
{
delayus(14);
xiaoyan<<=1;
if(dat)
{
xiaoyan++;
}
while(dat);
while(!
dat);
}
{
a[0]=tab[(wendu_h)/10];
a[1]=tab[(wendu_h)%10];
a[2]=tab[(shidu_h)/10];
a[3]=tab[(shidu_h)%10];
a[4]=tab[xiaoyan/10];
a[5]=tab[xiaoyan%10];
ShowString(0,a);
}
delayms(100);
shidu_h=0;
wendu_h=0;
shidu_l=0;
wendu_l=0;
xiaoyan=0;
}
}