基于STC15单片机的DHT11采样

1 DHT11的介绍

http://wenku.baidu.com/view/505af3126edb6f1aff001f6d.html

2 程序代码

#include "STC15F2K60S2.h"
#include 
#include 
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f
};
sbit wela = P2^7;
sbit dula = P2^6;
sbit dht = P3^7;
uchar RH_data,RL_data,TH_data,TL_data,CK_data;
uchar undata;
void Delay35us()        //@11.0592MHz
{
    unsigned char i;

    _nop_();
    i = 94;
    while (--i);
}
void Delay8us()     //@11.0592MHz
{
    unsigned char i;

    _nop_();
    _nop_();
    i = 19;
    while (--i);
}
void Delay800us()       //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    _nop_();
    _nop_();
    i = 9;
    j = 151;
    do
    {
        while (--j);
    } while (--i);
}
void Delay30us()        //@11.0592MHz
{
    unsigned char i;

    _nop_();
    _nop_();
    i = 80;
    while (--i);
}
void Delay18ms()        //@11.0592MHz
{
    unsigned char i, j, k;

    i = 1;
    j = 194;
    k = 160;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}
uchar read_8bit()            //¶ÁÈ¡Ò»¸ö×Ö½Ú
{
    uint i;
    uchar temp = 0;
    uchar dat;
    for(i = 0;i < 8;i++)
    {
        while(!dht);
        Delay30us();
        temp = 0;
        if(dht == 1)
        {
            temp = 1;
        }
        while(dht);
        dat<<=1;
        dat = dat|temp;
    }
    return dat;
}
uchar read_data()
{
    uint i;
    dht = 1;
    Delay8us();
    dht = 0;
    Delay18ms();
    dht = 1;
  Delay35us();
    if(!dht)
    {
        while(!dht);
        while(dht); 
        RH_data = read_8bit();
        RL_data = read_8bit();
        TH_data = read_8bit();
        TL_data = read_8bit();
        CK_data = read_8bit();
        undata = RH_data + RL_data + TH_data + TL_data;
    }
        if(CK_data == undata)
        {
            i = 1;
        }
        else
        {
            i = 0;
        }
    return i;
}
void main()
{
    uchar i;
    while(1)
    {
        i = read_data();
        if(i == 1)
        {
            i = 0;
            wela = 1;
            P0=0xfe;
            wela = 0;
            dula = 1;
            P0 = table[RH_data/10];
            dula = 0;
      Delay800us();

            wela = 1;
            P0=0xfd;
            wela = 0;
            dula = 1;
            P0 = table[RH_data%10];
            dula = 0;
            Delay800us();

            wela = 1;
            P0=0xf7;
            wela = 0;
            dula = 1;
            P0 = table[TH_data/10];
            dula = 0;
            Delay800us();

            wela = 1;
            P0=0xef;
            wela = 0;
            dula = 1;
            P0 = table[TH_data%10];
            dula = 0;
            Delay800us();
    }
    }
}

你可能感兴趣的:(STC15)