找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1777|回复: 0
收起左侧

DHT11在STC8单片机应用源程序

[复制链接]
ID:539086 发表于 2019-5-15 17:21 | 显示全部楼层 |阅读模式
制作出来的实物图如下:

1

1

2

2


单片机源程序如下:
#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, 下载次数: 33)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表