標題: 求多路DHT11或者DS18B20的程序 [打印本頁]

作者: Royal丶豪    時間: 2019-1-14 17:15
標題: 求多路DHT11或者DS18B20的程序
有沒有能詳細講下單總線掛多個DHT11或者DS18B20的程序或者分享一下掛兩個或者兩個以上這種單總線器件的程序

作者: 小貓貓愛吃魚    時間: 2019-1-14 19:41
你好!你沒有把具體功能要求提出來;
給你幾個完成的實物,你參考看看吧;
可以按照具體要求,來修改設計。。。

16路DS18B20單總線測溫顯示仿真+hex文件 - 資料共享 單片機論壇
http://www.torrancerestoration.com/bbs/dpj-148955-1.html

DS18B20多點溫度,ESP8266 WIFI無線顯示系統(tǒng) - 資料共享 單片機論壇
http://www.torrancerestoration.com/bbs/dpj-148750-1.html

Arduino單總線多個 DS18B20 溫度傳感器實驗 - 供求信息發(fā)布專區(qū) 單片機論壇
http://www.torrancerestoration.com/bbs/dpj-143295-1.html

Arduino+DS18B20+LCD1602測溫(4個溫度) - 資料共享 單片機論壇
http://www.torrancerestoration.com/bbs/dpj-143510-1.html

WIFI無線(ESP8266)多點溫度(2個18B20)報警系統(tǒng),實物效果 - 51單片機 單片機論壇
http://www.torrancerestoration.com/bbs/dpj-119171-1.html

作者: 609763691    時間: 2019-1-14 21:04
這個得看你是什么單片機,因為DS18B20是單總線通信,對時序的要求很高,然后不同單片機的處理速度也是不同的,比如89c51,15系列的,430單片機,stm32單片機這些芯片的處理速度都是不同得多,所以即使你要求要現(xiàn)有的程序你也得說你使用單片機的型號,不然你拿去也是不會成功的
作者: 609763691    時間: 2019-1-14 21:06
#include"reg51.h"
#include"intrins.h"

#define uchar unsigned char
#define uint unsigned int

sbit DSPORT=P3^7;

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

//uchar DisplayData[8];
void delay(uint z)
{
        while(z--);
}
void delayus(uint x,uchar y)
{
        uchar i,j;
        for(i=x;i>0;i--)
                for(j=y;j>0;j--);
}
void Ds18b20Init()
{
        uchar st=1;
        DSPORT = 1;
        _nop_();_nop_();
        while(st)
        {
                DSPORT = 0;                         //時序很重要,一定要準確;
                delayus(149,1);                 //延時750us
                DSPORT = 1;
                delayus(4,4);                 //延時15~60 us;
                if(DSPORT == 0)                 //當        DSPORT=0時,初始化才是正確的;
                        st = 1;
                else st = 0;
                delayus(99,1);                 //延時500us
        }
}
void Ds18b20WriteByte(uchar dat)        //寫是判斷數(shù)據(jù)再寫入,
{
        uchar i,temp;
        DSPORT = 1;
        _nop_();_nop_();
        for(i=0;i<8;i++)
        {
                DSPORT = 0;
                delayus(0,0);                 //延時20us
                temp=dat&0x01;
//                delayus(9,1);
                if(temp == 1)                //判斷,然后把數(shù)據(jù)置給總線(把數(shù)據(jù)送走,好判斷)
                        DSPORT = 1;
                delayus(2,2);                 //延時45us
                dat>>=1;
                DSPORT = 1;
        }       
}
uchar Ds18b20ReadByte()                //讀是有了數(shù)據(jù)再給數(shù)據(jù)
{
        uchar i,dat;
//        static bit j;
        for(i=0;i<8;i++)
        {
                dat>>=1;
                DSPORT=1;
                _nop_();_nop_();
                DSPORT=0;
                _nop_();_nop_();
                _nop_();_nop_();
                _nop_();_nop_();
                DSPORT=1;                //當至高時,傳送數(shù)據(jù);
                _nop_();_nop_();_nop_();_nop_();
        //        j=DSPORT;
                if(DSPORT)           //判斷總線,高位傳送數(shù)據(jù);
                        dat=dat|0x80;
                delayus(1,1);
        }
        return dat;
}
void Ds18b20ChangTemp()
{
        Ds18b20Init();
        delayus(1,100);
        Ds18b20WriteByte(0xcc);        //跳過ROM
        Ds18b20WriteByte(0x44);
}
void Ds18b20ReadTempCom()
{
        Ds18b20Init();
        delayus(1,100);
        Ds18b20WriteByte(0xcc);          
        Ds18b20WriteByte(0xbe);       
}
int Ds18b20ReadTemp()
{
        int temp=0;
        uchar tmh,tml;
        Ds18b20ChangTemp();
        Ds18b20ReadTempCom();
        tml=Ds18b20ReadByte();
        tmh=Ds18b20ReadByte();
        temp=(tmh*256+tml)*0.0625*100+0.5; //乘100使其小數(shù)點前兩位,加0.5使其四舍五入;
        return temp;
}
void display(int temp)
{
        uchar i;
/***在此不能這樣轉(zhuǎn)換,因為上面的temp已經(jīng)轉(zhuǎn)換為一個數(shù)值,取反出來有錯,
                這樣只是數(shù)碼管上的數(shù)值取反了,而不是DS18B20公式取反! ****/
/*        int tp;                                // float 不能取位;
        if(temp<0)                                  
        {
                DisplayData[0]=0x40;   //if小于0,則前面加一個 '-' ;
                temp=temp-1;
                temp=~temp;
                tp=temp;
        }
        else                                                 //大于0,則前面的不顯示        ;
        {
                DisplayData[0]=0x00;
                tp=temp;
        }        */
        for(i=0;i<4;i++)
        {
                switch(i)
                {
//                        case 0:        LSA=0;LSB=0;LSC=1; P0=DisplayData[0]; break;
                        case 0: LSA=1;LSB=1;LSC=0; P0=smgduan[temp/1000]; break;
                        case 1: LSA=0;LSB=1;LSC=0; P0=smgduan[temp%1000/100]|0x80; break;
                        case 2: LSA=1;LSB=0;LSC=0; P0=smgduan[temp%100/10]; break;
                        case 3: LSA=0;LSB=0;LSC=0; P0=smgduan[temp%10]; break;
                }
                delay(100);                 //延時必須寫在清屏前面,
                P0=0x00;
        }
}       
void main()
{
        while(1)
        {
                display(Ds18b20ReadTemp());
        }
}



作者: Royal丶豪    時間: 2019-1-15 08:53
609763691 發(fā)表于 2019-1-14 21:04
這個得看你是什么單片機,因為DS18B20是單總線通信,對時序的要求很高,然后不同單片機的處理速度也是不同 ...

有 89C52 STC15w413s  STC12c5410ad 想了解其中一款 然后改時序




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1