找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

51單片機18B20溫度傳感器編程方式設(shè)計程序 求幫助

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:437709 發(fā)表于 2018-12-2 15:54 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
初學(xué)單片機,老師比較傾向讓學(xué)生自學(xué),所以很多東西要在網(wǎng)上自己學(xué)習(xí),針對這個設(shè)計感覺無從下手,求大神們幫忙,基于我們這種板子幫我出個程序我學(xué)習(xí)一下,謝謝!

IMG_20181202_155020.jpg (4.43 MB, 下載次數(shù): 42)

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

使用道具 舉報

沙發(fā)
ID:375092 發(fā)表于 2018-12-2 21:29 | 只看該作者
#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)                 //當(dāng)        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;                //當(dāng)至高時,傳送數(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());
        }
}

拿去吧,這個是c51的板子程序,管腳改了,數(shù)碼管程序按照你的板子寫就可以顯示了,只能幫你到這里了,別的靠你自己加油。
回復(fù)

使用道具 舉報

板凳
ID:407961 發(fā)表于 2018-12-3 12:58 | 只看該作者
這種百度一下就很多的,我是百度里學(xué)會的C51
回復(fù)

使用道具 舉報

地板
ID:438360 發(fā)表于 2018-12-3 14:08 | 只看該作者
這個程序要自己把管腳改了
回復(fù)

使用道具 舉報

5#
ID:421499 發(fā)表于 2018-12-3 17:13 | 只看該作者
網(wǎng)上自己隨便找都有的,ds18b20主要去學(xué)它的單串口通信線
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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