制作出来的实物图如下:
单片机源程序如下:
#include "STC15Fxxxx.H"
#include "dh.h"
sbit DQ = P2^7;
unsigned char flag,temp;
unsigned char rhh,rhl,rth,rtl,rcc;
void Delay2(unsigned int j)
{
unsigned int i;
for(;j>0;j--)
{
for(i=0;i<150;i++);
}
}
void Delay_10us(void)
{
unsigned int i;
for(i=16;i>0;i--) ;
}
unsigned char com()
{
unsigned char i,comdata;
for(i = 0;i < 8; i++)
{
flag = 2;
while(!DQ && flag ++);
Delay_10us();
Delay_10us();
Delay_10us();
temp = 0;
if(DQ)
temp = 1;
while(DQ && flag++);
if(flag == 1)
break;
comdata<<=1;
comdata|=temp;
}
return comdata;
}
void RH()
{
unsigned char RH_h,RH_l,RT_h,RT_l,checkdata;
DQ = 0;
Delay2(180);
DQ = 1;
//总线由上拉电阻拉高 主机延时20us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
//主机设为输入 判断从机响应信号
DQ = 1;
//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行
if(!DQ)//判断是否为低
{
flag = 2;
while((!DQ) && flag ++);
flag = 2;
while(DQ && flag ++);
RH_h = com();
RH_l = com();
RT_h = com();
RT_l = com();
checkdata = com();
DQ = 1;
//数据校验
if((RH_h + RH_l + RT_h + RT_l) == checkdata)
{
rhh = RH_h;
rhl = RH_l;
rth = RT_h;
rtl = RT_l;
rcc = checkdata;
}
}
}
全部资料51hei下载地址:
温湿度传感器.zip
(165.2 KB, 下载次数: 34)
|