找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3411|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

求多路DHT11或者DS18B20的程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
#
ID:239793 發(fā)表于 2019-1-14 17:15 | 只看該作者 回帖獎(jiǎng)勵(lì) |正序?yàn)g覽 |閱讀模式
50黑幣
有沒(méi)有能詳細(xì)講下單總線(xiàn)掛多個(gè)DHT11或者DS18B20的程序或者分享一下掛兩個(gè)或者兩個(gè)以上這種單總線(xiàn)器件的程序

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

地板
ID:239793 發(fā)表于 2019-1-15 08:53 | 只看該作者
609763691 發(fā)表于 2019-1-14 21:04
這個(gè)得看你是什么單片機(jī),因?yàn)镈S18B20是單總線(xiàn)通信,對(duì)時(shí)序的要求很高,然后不同單片機(jī)的處理速度也是不同 ...

有 89C52 STC15w413s  STC12c5410ad 想了解其中一款 然后改時(shí)序
回復(fù)

使用道具 舉報(bào)

板凳
ID:375092 發(fā)表于 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;                         //時(shí)序很重要,一定要準(zhǔn)確;
                delayus(149,1);                 //延時(shí)750us
                DSPORT = 1;
                delayus(4,4);                 //延時(shí)15~60 us;
                if(DSPORT == 0)                 //當(dāng)        DSPORT=0時(shí),初始化才是正確的;
                        st = 1;
                else st = 0;
                delayus(99,1);                 //延時(shí)500us
        }
}
void Ds18b20WriteByte(uchar dat)        //寫(xiě)是判斷數(shù)據(jù)再寫(xiě)入,
{
        uchar i,temp;
        DSPORT = 1;
        _nop_();_nop_();
        for(i=0;i<8;i++)
        {
                DSPORT = 0;
                delayus(0,0);                 //延時(shí)20us
                temp=dat&0x01;
//                delayus(9,1);
                if(temp == 1)                //判斷,然后把數(shù)據(jù)置給總線(xiàn)(把數(shù)據(jù)送走,好判斷)
                        DSPORT = 1;
                delayus(2,2);                 //延時(shí)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;                //當(dāng)至高時(shí),傳送數(shù)據(jù);
                _nop_();_nop_();_nop_();_nop_();
        //        j=DSPORT;
                if(DSPORT)           //判斷總線(xiàn),高位傳送數(shù)據(jù);
                        dat=dat|0x80;
                delayus(1,1);
        }
        return dat;
}
void Ds18b20ChangTemp()
{
        Ds18b20Init();
        delayus(1,100);
        Ds18b20WriteByte(0xcc);        //跳過(guò)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ù)點(diǎn)前兩位,加0.5使其四舍五入;
        return temp;
}
void display(int temp)
{
        uchar i;
/***在此不能這樣轉(zhuǎn)換,因?yàn)樯厦娴膖emp已經(jīng)轉(zhuǎn)換為一個(gè)數(shù)值,取反出來(lái)有錯(cuò),
                這樣只是數(shù)碼管上的數(shù)值取反了,而不是DS18B20公式取反! ****/
/*        int tp;                                // float 不能取位;
        if(temp<0)                                  
        {
                DisplayData[0]=0x40;   //if小于0,則前面加一個(gè) '-' ;
                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);                 //延時(shí)必須寫(xiě)在清屏前面,
                P0=0x00;
        }
}       
void main()
{
        while(1)
        {
                display(Ds18b20ReadTemp());
        }
}


回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:375092 發(fā)表于 2019-1-14 21:04 | 只看該作者
這個(gè)得看你是什么單片機(jī),因?yàn)镈S18B20是單總線(xiàn)通信,對(duì)時(shí)序的要求很高,然后不同單片機(jī)的處理速度也是不同的,比如89c51,15系列的,430單片機(jī),stm32單片機(jī)這些芯片的處理速度都是不同得多,所以即使你要求要現(xiàn)有的程序你也得說(shuō)你使用單片機(jī)的型號(hào),不然你拿去也是不會(huì)成功的
回復(fù)

使用道具 舉報(bào)

樓主
ID:303383 發(fā)表于 2019-1-14 19:41 | 只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表