码迷,mamicode.com
首页 > 其他好文 > 详细

FS_11C14温湿度传感器(二)

时间:2014-06-06 21:56:54      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   a   http   

作者:刘老师,华清远见嵌入式学院讲师。

在FS_11C14平台DHT11传感器程序:

bubuko.com,布布扣

bubuko.com,布布扣

/*******************************************************************************
        * Function Name : Read_Temp_Hum
        * Description : Read Temp & Hum.
        * Input : - temp : temp buf pointer.
        * - hum : hum buf pointer.
        * Output : - temp[0] : temp x. part
        * - temp[1] : temp .x part
        * - hum[0] : hum x. part
        * - hum[1] : hum .x part
        * Return : - 1, checksum ok
        * - 0, checksum fail
        *******************************************************************************/
        uint32_t Read_Temp_Hum(uint8_t *temp, uint8_t *hum)
        {
                uint32_t cnt_last;
                uint8_t hum_10, hum_01, temp_10, temp_01, chksum, chk;
                uint32_t tc1, tc;
                uint32_t i;

                p3_2_counter = 0;
                cnt_last = p3_2_counter;

                GPIOIntDisable(PORT1, 5);
                GPIOSetDir(PORT1, 5, 1);      // Set PIO1_5 to output

                GPIOSetValue(PORT1, 5, 0);
                delay_ms(30);
                GPIOSetValue(PORT1, 5, 1);

                GPIOSetDir(PORT1, 5, 0);

                GPIOSetInterrupt(PORT1, 5, 0, 0, 0);
                GPIOIntEnable(PORT1, 5);

                for(i=0; i<3; i++)             //waiting 80uS low level and 80 uS high level
                {
                        GPIOSetInterrupt(PORT1, 5, 0, 0, i&0x01);
                        while(p3_2_counter == cnt_last);
                        cnt_last = p3_2_counter;
                }

                for(i=0; i<40; i++)
                {                    // Loop to receive 40 bit data
                        GPIOSetInterrupt(PORT1, 5, 0, 0, 1);
                        while(p3_2_counter == cnt_last);
                        cnt_last = p3_2_counter;
                        tc1 = p3_2_tc;

                        GPIOSetInterrupt(PORT1, 5, 0, 0, 0);
                        while(p3_2_counter == cnt_last);
                        cnt_last = p3_2_counter;

                        if(p3_2_tc < tc1)
                        {
                                tc = tc1 - p3_2_tc;
                        }
                        else
                        {
                                tc = 48000 - (p3_2_tc - tc1);
                        }

                        if(i < 8)                         //one byte
                        {
                                hum_10 <<= 1;
                                if(tc >= 2328)
                                hum_10 |= 0x01;
                        }
                        else if(i < 16) // two byte
                        {
                                hum_01 <<= 1;
                                if(tc >= 2328)
                                hum_01 |= 0x01;
                        }
                        else if(i < 24) // three byte
                        {
                                temp_10 <<;= 1;
                                if(tc >= 2328)
                                temp_10 |= 0x01;
                        }
                        else if(i < 32) // four byte
                        {
                                temp_01 <<= 1;
                                if(tc >= 2328)
                                temp_01 |= 0x01;
                        }
                        else // five byte checksum
                        {
                                chksum <<= 1;
                                if(tc >= 2328)
                                chksum |= 0x01;
                        }
                }

                GPIOSetInterrupt(PORT1, 5, 0, 0, 1);
                while(p3_2_counter == cnt_last);

                GPIOIntDisable(PORT1, 5);

                *temp = temp_10;
                *(temp+1) = temp_01;

                *hum = hum_10;
                *(hum+1) = hum_01;

                chk = hum_10;          // Calculate to Checksum
                chk += hum_01;
                chk += temp_10;
                chk += temp_01;

                if(chk == chksum)        // Received data is OK
                return 1;
                else
                return 0;
        }

        /*******************************************************************************
        * Function Name : Temp_Hum_Test
        * Description : Temp & Hum func test.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void Temp_Hum_Test(void)
        {
                uint8_t temp[2], hum[2];
                char buf[20];
                uint32_t i;

                OLED_ClearScreen();
                OLED_DisStrLine(0, 0, "Temp & Hum");

                while(1)
                {
                        i = Read_Temp_Hum(temp, hum);

                        if(i)
                        {
                                if(temp[0]&0x80)
                                {
                                        temp[0] &= 0x7f;
                                        snprintf(buf, 16, "Temp: -%d.%d ", temp[0], temp[1]);
                                }
                                else
                                {
                                        snprintf(buf, 16, "Temp: %d.%d ", temp[0], temp[1]);
                                }
                                OLED_DisStrLine(2, 0, (uint8_t *)buf);
                                snprintf(buf, 16, "Hum: %d.%d ", hum[0], hum[1]);
                                OLED_DisStrLine(3, 0, (uint8_t *)buf);

                                printf("\r\nTemp: %d.%d Hum: %d.%d", temp[0], temp[1], hum[0], hum[1]);
                        }
                        else
                        {
                                printf("\r\nfail");
                        }
                        delay_ms(500);
                }
        }

        /**** End of File ****/

文章来源:华清远见嵌入式学院原文地址:http://www.embedu.org/Column/Column858.htm

更多相关嵌入式免费资料查看华清远见讲师博文>>

bubuko.com,布布扣
bubuko.com,布布扣

FS_11C14温湿度传感器(二),布布扣,bubuko.com

FS_11C14温湿度传感器(二)

标签:des   c   style   class   a   http   

原文地址:http://www.cnblogs.com/farsight2011/p/3765663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!