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

QQ登錄

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

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

51單片機(jī)1602時(shí)鐘系統(tǒng)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:273499 發(fā)表于 2018-7-2 09:37 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include"temp.h"
/*******************************************************************************
* 函數(shù)名         : Delay1ms
* 函數(shù)功能                   : 延時(shí)函數(shù)
* 輸入           : 無(wú)
* 輸出                  : 無(wú)
*******************************************************************************/

void Delay1ms(unsigned int y)
{
        unsigned int x;
        for(y;y>0;y--)
                for(x=110;x>0;x--);
}
/*******************************************************************************
* 函數(shù)名         : Ds18b20Init
* 函數(shù)功能                   : 初始化
* 輸入           : 無(wú)
* 輸出                  : 初始化成功返回1,失敗返回0
*******************************************************************************/

unsigned char Ds18b20Init()
{
        unsigned int i;
        DSPORT=0;                         //將總線拉低480us~960us
        i=70;       
        while(i--);//延時(shí)642us
        DSPORT=1;                        //然后拉高總線,如果DS18B20做出反應(yīng)會(huì)將在15us~60us后總線拉低
        i=0;
        while(DSPORT)        //等待DS18B20拉低總線
        {
                i++;
                if(i>5000)//等待>5MS
                        return 0;//初始化失敗       
        }
        return 1;//初始化成功
}

/*******************************************************************************
* 函數(shù)名         : Ds18b20WriteByte
* 函數(shù)功能                   : 向18B20寫(xiě)入一個(gè)字節(jié)
* 輸入           : com
* 輸出                  : 無(wú)
*******************************************************************************/

void Ds18b20WriteByte(unsigned char dat)
{
        unsigned int i,j;
        for(j=0;j<8;j++)
        {
                DSPORT=0;                        //每寫(xiě)入一位數(shù)據(jù)之前先把總線拉低1us
                i++;
                DSPORT=dat&0x01; //然后寫(xiě)入一個(gè)數(shù)據(jù),從最低位開(kāi)始
                i=6;
                while(i--); //延時(shí)68us,持續(xù)時(shí)間最少60us
                DSPORT=1;        //然后釋放總線,至少1us給總線恢復(fù)時(shí)間才能接著寫(xiě)入第二個(gè)數(shù)值
                dat>>=1;
        }
}
/*******************************************************************************
* 函數(shù)名         : Ds18b20ReadByte
* 函數(shù)功能                   : 讀取一個(gè)字節(jié)
* 輸入           : com
* 輸出                  : 無(wú)
*******************************************************************************/


unsigned char Ds18b20ReadByte()
{
        unsigned char byte,bi;
        unsigned int i,j;       
        for(j=8;j>0;j--)
        {
                DSPORT=0;//先將總線拉低1us
                i++;
                DSPORT=1;//然后釋放總線
                i++;
                i++;//延時(shí)6us等待數(shù)據(jù)穩(wěn)定
                bi=DSPORT;         //讀取數(shù)據(jù),從最低位開(kāi)始讀取
                /*將byte左移一位,然后與上右移7位后的bi,注意移動(dòng)之后移掉那位補(bǔ)0。*/
                byte=(byte>>1)|(bi<<7);                                                  
                i=4;                //讀取完之后等待48us再接著讀取下一個(gè)數(shù)
                while(i--);
        }                               
        return byte;
}
/*******************************************************************************
* 函數(shù)名         : Ds18b20ChangTemp
* 函數(shù)功能                   : 讓18b20開(kāi)始轉(zhuǎn)換溫度
* 輸入           : com
* 輸出                  : 無(wú)
*******************************************************************************/

void  Ds18b20ChangTemp()
{
        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);                //跳過(guò)ROM操作命令                 
        Ds18b20WriteByte(0x44);            //溫度轉(zhuǎn)換命令
//        Delay1ms(100);        //等待轉(zhuǎn)換成功,而如果你是一直刷著的話,就不用這個(gè)延時(shí)了

}
/*******************************************************************************
* 函數(shù)名         : Ds18b20ReadTempCom
* 函數(shù)功能                   : 發(fā)送讀取溫度命令
* 輸入           : com
* 輸出                  : 無(wú)
*******************************************************************************/

void  Ds18b20ReadTempCom()
{       

        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);         //跳過(guò)ROM操作命令
        Ds18b20WriteByte(0xbe);         //發(fā)送讀取溫度命令
}
/*******************************************************************************
* 函數(shù)名         : Ds18b20ReadTemp
* 函數(shù)功能                   : 讀取溫度
* 輸入           : com
* 輸出                  : 無(wú)
*******************************************************************************/

int Ds18b20ReadTemp()
{
        int temp=0;
        unsigned char tmh,tml;
        Ds18b20ChangTemp();                                 //先寫(xiě)入轉(zhuǎn)換命令
        Ds18b20ReadTempCom();                        //然后等待轉(zhuǎn)換完后發(fā)送讀取溫度命令
        tml=Ds18b20ReadByte();                //讀取溫度值共16位,先讀低字節(jié)
        tmh=Ds18b20ReadByte();                //再讀高字節(jié)
        temp=tmh;
        temp<<=8;
        temp|=tml;
        return temp;
}



51黑論壇_程序.zip

42.29 KB, 下載次數(shù): 9, 下載積分: 黑幣 -5

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

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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