找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

PIC單片機(jī)DS18B20讀取溫度程序,可直接移植換個IO口就行

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1004920 發(fā)表于 2023-4-1 16:09 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
/////////////////////////////////////////////
//名 稱 :DS18B20溫度的讀取
//開發(fā)者:zhangchunhua
//環(huán) 境 :PIC單片機(jī)
//時 序 :4.00 晶振
/////////////////////////////////////////////
#include<pic.h>
/////////////////////////////////////////////
#define uchar unsigned char
#define uint  unsigned int
/////////////////////////////////////////////
#define DQ  RC3
#define DQ_HIGH()  TRISC3=1
#define DQ_LOW()  TRISC3=0;DQ=0
/////////////////////////////////////////////
void reset(void);
void write_byte(uchar date);
uchar read_byte(void);
void yanshius(uint t);
void start_t(void);
signed int read_t(void);
/////////////////////////////////////////////
void yanshius(uint t)
{
        uint i;
        for(i=0;i<t;i++)
        {        NOP();        }
}
void reset(void)
{
        uchar st=1;
        while(st)
        {
                DQ_LOW();
                yanshius(29);//510uS
                DQ_HIGH();
                yanshius(2);//60uS
                if(DQ==1)
                        st=1;
                else
                {        st=0;
                         yanshius(25);//480uS
                }
        }
}
void write_byte(uchar date)
{
        uchar i,temp;
        for(i=8;i>0;i--)
        {
                temp=date & 0x01;//01010101
                DQ_LOW();
                if(temp == 1)
                {        NOP();NOP();
                        DQ_HIGH();
                        yanshius(2);       
                }
                else
                {
                        yanshius(2);//60uS
                        DQ_HIGH();       
                }       
                NOP();NOP();
                date = date>>1;//00101010yanshius(0);
        }
}
uchar read_byte(void)
{
        uchar i,date;
        static bit j;
        for(i=8;i>0;i--)
        {
                date = date>>1;
                DQ_LOW();
                NOP();NOP();
                DQ_HIGH();
                NOP();NOP();NOP();NOP();NOP();NOP();NOP();
                j = DQ;
                if(j == 1)
                {
                        date=date|0x80;//1000 0000                       
                }       
                yanshius(1);//47uS
        }
        return date;
}

void start_t(void)
{
        reset();  //復(fù)位
        write_byte(0xCC);//跳過ROM
        write_byte(0x44);//溫度轉(zhuǎn)換
}
//////讀取的溫度數(shù)值是放大10倍
signed int read_t(void)
{
        uchar tem1,tem2;
        uint tt,mm=0;
        double aaa;
        signed int temper;
        reset();
        write_byte(0xCC);
        write_byte(0xBE);
        tem1=read_byte();
        tem2=read_byte();
        tt = tem2;
        tt = tt<<8|tem1;
        if(tt&0x8000)
        {
                mm = ~tt + 1;
                aaa = mm * (-0.625);
        }
        else
        {
                aaa = tt*0.625;
        }
        temper=(signed int)aaa;
        return temper;       
}

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

使用道具 舉報

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

本版積分規(guī)則

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

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

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