TXT

dht11

By Eric Rose,2015-03-23 15:55
10 views 0
dht11

#include

    #include

    typedef unsigned char unint8;

    typedef unsigned char unint16;

    unsigned char str1[]={" "};

    unsigned char str2[]={" "};

    unsigned char code dis1[] = {"TRH RH: "}; unsigned char code dis2[] = {"TRH T: "}; sbit TRH = P2^0;//ÎÂʪ?È???ÐÆ?DHT11Êý?Ý?ÓÈë sbit ST = P2^1;//×?Ì?ÏÔÊ???ÊÇΪÁË???ãµ?ÊÔ???ßÌåÊ?ÓõÄÊ?ºò?ÉÒÔ??Òª

    sbit LCD_RS = P2^4;

    sbit LCD_RW = P2^5;

    sbit LCD_EN = P2^6;

    unint8 TH_data,TL_data,RH_data,RL_data,CK_data; unint8 TH_temp,TL_temp,RH_temp,RL_temp,CK_temp; unint8 com_data,untemp,temp;

    unint8 respond;

    /*****************************************************************

    ****************/

    void delayNOP()

    {

     _nop_();

     _nop_();

     _nop_();

     _nop_();

    }

    /*****************************************************************

    ****************/

/*****************************************************************

    ****************/

    // ºÁÃë??ÑÓÊ?×Ó?ÌÐò

    /*****************************************************************

    ****************/

    void delay_ms(unsigned char ms)

    {

     unsigned char i;

     while(ms--)

     {

     for(i = 0; i< 150; i++)

     {

     _nop_();

     _nop_();

     _nop_();

     _nop_();

     }

     }

    }

    /*********************************************************************************/

    /*********************************************************************************/

    //5us??ÑÓÊ??ÌÐò

    /*********************************************************************************/

    void delay_us()

    {

     unint8 i;

     i--;

     i--;

     i--;

     i--;

     i--;

     i--;

    }

    /********************************************************************************/

    /********************************************************************************/

    // ?âÊÔLCDÃ?µ×?Ì?

    //lcd_busy()Ϊ1Ê???Ã???µÈ?ý??lcd_busy()Ϊ0Ê?,ÏÐ???ÉÐ?Ö?ÁîÓëÊý?Ý?? /********************************************************************************/

    bit lcd_busy()

    {

     bit result;

     LCD_RS = 0;

     LCD_RW = 1;

     LCD_EN = 1;

     delayNOP();

     result = (bit)(P0&0x80);

     LCD_EN = 0;

     return(result);

    }

    /*****************************************************************

**************/

    /*******************************************************************************/

    // Ð?ÈëÖ?ÁîÊý?ݵ?LCD

    /*******************************************************************************/

    void lcd_wcmd(unsigned char cmd)

    {

     while(lcd_busy());

     LCD_RS = 0;

     LCD_RW = 0;

     LCD_EN = 0;

     _nop_();

     _nop_();

     P0 = cmd;

     delayNOP();

     LCD_EN = 1;

     delayNOP();

     LCD_EN = 0;

    }

    /*****************************************************************************/

    /*****************************************************************************/

    //Ð?ÏÔÊ?Êý?ݵ?LCD //RS=?ßµçÆ???RW=µÍµçÆ???E=?ßÂö?å??D0-D7=Êý?Ý?? /*****************************************************************************/

    void lcd_wdata(unsigned char dat)

    {

     while(lcd_busy());

     LCD_RS = 1;

     LCD_RW = 0;

     LCD_EN = 0;

     P0 = dat;

     delayNOP();

     LCD_EN = 1;

     delayNOP();

     LCD_EN = 0;

    }

    /****************************************************************************/

    /****************************************************************************/

    //LCD?õÊ???Éè??

    /****************************************************************************/

    void lcd_init()

    {

     delay_ms(15);

     lcd_wcmd(0x38); //16*2ÏÔÊ???5*7µãÕó??8Î?Êý?Ý

     delay_ms(5);

     lcd_wcmd(0x38);

     delay_ms(5);

     lcd_wcmd(0x38);

     delay_ms(5);

     lcd_wcmd(0x0c); //ÏÔÊ??ª???Ø?â?ê

     delay_ms(5);

     lcd_wcmd(0x06); //ÒÆ???â?ê

     delay_ms(5);

     //lcd_wcmd(0x01);

    //Çå?ýLCDµÄÏÔÊ?ÄÚÈÝ??Èç?ûÆÁÄ??ý?µ???É??ÕâÁ??äÉ??ý

     //delay_ms(5);

    }

    /****************************************************************************/

    /****************************************************************************/

    //Éè??LCDÏÔÊ?Î?ÖÃ

    /****************************************************************************/

    void lcd_dis_pos(unsigned char pos)

    {

     lcd_wcmd(pos | 0x80); //Êý?ÝÖ?Õë=80+µØÖ??äÁ? }

    /****************************************************************************/

    /****************************************************************************/

    //ÊÕ??ÐźÅ?ì?â??Êý?Ý?ÁÈ?

    /****************************************************************************/

    char receive()

{

     unint8 i;

     ST=0;

     com_data=0;

     for(i=0;i<=7;i++)

     {

     respond=2;

     while((!TRH)&&respond++);

     delay_us();

     delay_us();

     delay_us();

     if(TRH)

     {

     temp=1;

     respond=2;

     while((TRH)&&respond++);

     }

     else

     temp=0;

     com_data<<=1;

     com_data|=temp;

     }

     return(com_data);

    }

    /*****************************************************************

    ***********/

/*****************************************************************

    ***********/

    //ʪ?È?ÁÈ?×Ó?ÌÐò

    //ÎÂ?È?ß8Î?== TL_data

    //ÎÂ?ȵÍ8Î?== TH_data

    //ʪ?È?ß8Î?== RH_data

    //ʪ?ȵÍ8Î?== RH_data

    //Ð?Ñé 8Î? == CK_data

    //µ?ÓõÄ?ÌÐòÓÐ delay();, Delay_5us();,RECEIVE();

    /*****************************************************************

    **********/

    void read_TRH()

    {

     //Ö??úÀ?µÍ18ms

     TRH=0;

     delay_ms(18);

     TRH=1;

     //DATA×ÜÏßÓÉÉÏÀ?µç×èÀ??ß Ö??úÑÓÊ?20us

     delay_us();

     delay_us();

     delay_us();

     delay_us();

     //delay_us();

     //delay_us();delay_us();delay_us();delay_us();

     //Ö??úÉèΪÊäÈë ÅÐ?Ï?Ó?úÏìÓ?ÐźÅ

     TRH=1;

     //ÅÐ?ÏDHT11ÊÇ?ñÓе͵çÆ?ÏìÓ?ÐźŠÈç??ÏìÓ?ÔòÌø?ö??ÏìÓ?ÔòÏòÏÂÔËÐÐ

     if(!TRH)

     {

     respond=2;

     //ÅÐ?ÏDHT11???ö 80us µÄµÍµçÆ?ÏìÓ?ÐźÅÊÇ?ñ?áÊø

     while((!TRH)&& respond++);

     respond=2;

     //ÅÐ?Ï?Ó?úÊÇ?ñ???ö 80us µÄ?ßµçÆ???Èç???öÔò?øÈëÊý?Ý?ÓÊÕ×?Ì?

     while(TRH && respond++);

     //Êý?Ý?ÓÊÕ×?Ì?

     RH_temp = receive();

     RL_temp = receive();

     TH_temp = receive();

     TL_temp = receive();

     CK_temp = receive();

     TRH=1;ST=1;

     //Êý?ÝÐ?Ñé

     untemp=(RH_temp+RL_temp+TH_temp+TL_temp);

     if(untemp==CK_temp)

     {

     RH_data = RH_temp;

     RL_data = RL_temp;

     TH_data = TH_temp;

     TL_data = TL_temp;

     CK_data = CK_temp;

     }

     }

     //ʪ?ÈÕûÊý???Ö

     str1[0] = (char)(0X30+RH_data/10);

     str1[1] = (char)(0X30+RH_data%10);

     str1[2] = 0x2e; //Ð?Êýµã

     //ʪ?ÈÐ?Êý???Ö

     str1[3] = (char)(0X30+RL_data/10);

     str1[5] = 0X25; //"%"

     str1[6] = 0X52; //"R"

     str1[7] = 0X48; //"H"

     //ÎÂ?ÈÕûÊý???Ö

     str2[0] = (char)(0X30+TH_data/10);

     str2[1] = (char)(0X30+TH_data%10);

     str2[2] = 0x2e; //Ð?Êýµã

     //ÎÂ?ÈÐ?Êý???Ö

     str2[3] = (char)(0X30+TL_data/10);

     str2[5] = 0X27; //"'"

     str2[6] = 0X43; //"C"

    }

    /*****************************************************************

    ***********/

    //Ö?º?Êý

    //TH,TL,RH,RL?Ö?ð?ú?íÎÂʪ?ȵÄÕûÊýºÍÐ?Êý???Ö /*****************************************************************

    ***********/

    void main()

    {

     unsigned char i,n=0x40,m;

     lcd_init();

     delay_us();

     while(1)

     {

     read_TRH();

     //Ð?×Ö?û

     for(i=0;i<=7;i++)

     {

     lcd_dis_pos(i); //ÏÔÊ?×Ö?û

     lcd_wdata(dis1[i]);

     lcd_dis_pos(n+i); //ÏÔÊ?×Ö?û

     lcd_wdata(dis2[i]);

     }

     //Ð?ʪ?ÈÊý?Ý

     m=0x08;

     for(i=0;i<=7;i++)

     {

     lcd_dis_pos(m);

     lcd_wdata(str1[i]);

     m++;

     }

     //Ð?ÎÂ?ÈÊý?Ý

     m=0x48;

     for(i=0;i<=7;i++)

     {

     lcd_dis_pos(m);

     lcd_wdata(str2[i]);

     m++;

     }

     //ÑÓÊ?

     delay_ms(1000) ;

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com