新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于ARM9的18B20溫度傳感器程序

        基于ARM9的18B20溫度傳感器程序

        作者: 時間:2016-11-24 來源:網絡 收藏
        18B20溫度傳感器,典型的時序操作器件,用51單片機寫很方便,可是把這現成的程序移植到FPGA、ARM、430上面,還需要一定的翻譯功底。

        本文引用地址:http://www.104case.com/article/201611/320703.htm

        //-------------------------------------------------------------------------------------
        //函數名稱:dmsec //入口參數:count //函數功能:延時子程序
        //-------------------------------------------------------------------------------------
        void dmsec(uint count) { uchar i;
        while(count--) {
        for(i=0;
        i<125;
        i++);
        }
        }
        //-------------------------------------------------------------------------------------
        //函數名稱:tmreset //入口參數:無 //函數功能:
        //-------------------------------------------------------------------------------------
        void tmreset(void) {
        ushort i;
        dq=0;
        i=343;
        while(i>0) i--;
        //大約900us dq=1;
        i=13;
        while(i>0) i--;
        } //-------------------------------------------------------------------------------------
        //函數名稱:tmpre //入口參數:無 //函數功能:等待DS18B20應答
        //-------------------------------------------------------------------------------------
        void tmpre(void) {
        uchar i;
        while(dq);
        while(~dq);
        i=13;
        while(i>0) i--;
        } //-------------------------------------------------------------------------------------
        //函數名稱:tmrbit //入口參數:無 //返回值: dat //函數功能:在總線上讀一位
        //-------------------------------------------------------------------------------------
        bit tmrbit(void) {
        uchar i;
        bit dat;
        dq=0;
        i++;
        dq=1;
        i++;
        i++;
        dat=dq;
        i=30;
        while(i>0) i--;
        return(dat);
        } //-------------------------------------------------------------------------------------
        //函數名稱:tmrbyte //入口參數:無 //返回值: dat //函數功能:讀一個字節
        //-------------------------------------------------------------------------------------
        uchar tmrbyte(void) {
        uchar i,j,dat=0;
        for(i=1;i<=8;i++) {
        j=tmrbit();
        dat=(j<<7)|(dat>>1);
        }
        return(dat);
        } //-------------------------------------------------------------------------------------
        //函數名稱:tmwbyte //入口參數:dat //函數功能:寫命令
        //-------------------------------------------------------------------------------------
        void tmwbyte(uchar dat) {
        uint i;
        uchar j;
        bit testb;
        for(j=1;j<=8;j++) {
        testb=dat&0x01;
        dat=dat>>1;
        if(testb) {
        dq=0;
        //寫1 i++;
        i++;
        dq=1;
        i=27;
        while(i>0) i--;
        }
        else {
        dq=0;
        //寫0 i=27;
        while(i>0) i--;
        dq=1;
        i++;
        i++;
        }
        }
        } //-------------------------------------------------------------------------------------
        //函數名稱:tmrbyte //入口參數:無 //函數功能:讀取溫度值
        //-------------------------------------------------------------------------------------
        void read_tr() {
        uchar a,b;
        ushort y;
        tmreset();
        tmpre();
        dmsec(1);
        tmwbyte(0xcc);
        tmwbyte(0x44);
        dmsec(100);
        tmreset();
        tmpre();
        dmsec(1);
        tmwbyte(0xcc);
        tmwbyte(0xbe);
        a=tmrbyte();
        //l b=tmrbyte();
        //h y=b&0x0f;
        y=y<<8;
        y=a|y;
        if(y>0x800) {
        y=0x1000-y;
        bjTr=cal12bit(y);
        bjTr=bjTr+0x8000;
        }
        else
        {
        bjTr=cal12bit(y);
        }
        } //12位精度
        void temp_init(void) {
        tmreset();
        tmpre();
        tmwbyte(0xcc);
        tmwbyte(0x4e);
        tmwbyte(0x88);
        tmwbyte(0x44);
        tmwbyte(0x7f);
        tmreset();
        }



        關鍵詞: ARM918B20溫度傳感

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 凭祥市| 德化县| 阜南县| 兴化市| 清原| 新余市| 江川县| 明星| 弋阳县| 比如县| 吴堡县| 达尔| 陆良县| 敦煌市| 锦屏县| 云安县| 定南县| 乌拉特后旗| 黄石市| 武鸣县| 崇义县| 旅游| 宜君县| 佛冈县| 上栗县| 柯坪县| 灵川县| 高雄县| 湘乡市| 池州市| 毕节市| 朔州市| 鄢陵县| 江油市| 安平县| 呼伦贝尔市| 庆阳市| 安徽省| 苏州市| 桐柏县| 宜丰县|