新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 51測溫度并顯示華氏和攝氏程序

        51測溫度并顯示華氏和攝氏程序

        作者: 時間:2016-11-18 來源:網絡 收藏
        ///////頭文件區//////////////////////////////////////

        #include"reg51.h"
        #include"intrins.h"
        ////////////////////////////////////////////////////
        /////對于按鍵的位定義//////////////////////////////
        sbit k_TH=P1^0; // 10 16 *
        sbit k_TL=P1^1; //11 15 *
        sbit k_E=P1^2; //12 14 *
        ////////////////////////////////////////////////////
        /////對于溫度傳感器DS18B20的引腳及全局變量定義////////
        sbit DQ_D=P3^2;
        char TH=10; //定義高位寄存器 需要符號
        char TL=0; //定義低位寄存器 需要符號
        float T=0; //定義全局溫度值寄存器
        bit flag_h=0; //定義高溫報警標志 1有效
        bit flag_l=0; //定義低溫報警標志 1有效
        sbit HEATER=P1^5; //低電平有效
        sbit FAN=P1^3; //低電平有效
        sbit SPEAKER=P1^6; //低電平有效 P15 *
        unsigned int n=0; //風扇速度采集當中 定時器1用到的倍數軟件計數器
        bit flag_COUNTER=0; //計數器0溢出中斷標志 不使用查詢方式 節省系統資源
        bit flag_capture=0; //速度采樣是否完成標志
        unsigned int speed=0;//風扇速度
        /////////////////////////////////////////////
        /////對于液晶顯示的引腳及全局變量定義//////////////
        //////////////////////////////////////////////////
        #define DQ_L P2 //聲明數據端口
        sbit BG=P2^6; //低電平有效
        sbit E=P1^2;
        sbit RW=P1^1;
        sbit RS=P1^0;
        unsigned char code fig[10]={"0123456789"};
        unsigned char code tab0[]={"CTemper:"};
        unsigned char code tab1[]={"HTemper:"};
        ///////////////////////////////////////////
        //////////中斷標志////////////////////////
        bit flag_X0=0;
        ////////////////////////////////////////////
        void delay_us(unsigned int us)
        {
        while(us--)
        {
        _nop_();
        }
        return;
        }
        void delay_ms(unsigned int ms)
        {
        unsigned char i=0;
        while(ms--)
        {
        for(i=0;i<250;i++)
        {
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        }
        }
        return;
        }
        void delay(unsigned int i)
        {
        while(i--);
        }
        ///////////////////以上是通用代碼區//////////////////////
        ////////下列程序是對溫度傳感器DS18B20控制的實現//////////
        //初始化函數
        Init_DS18B20(void)
        {
        unsigned char x=0;
        DQ_D=1; //DQ復位
        delay(8); //稍做延時
        DQ_D=0; //單片機將DQ拉低
        delay(80); //精確延時 大于 480us
        DQ_D=1; //拉高總線
        delay(14);
        x=DQ_D; //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
        delay(20);
        }
        //讀一個字節
        ReadOneChar(void)
        {
        unsigned char i=0;
        unsigned char dat = 0;
        for (i=8;i>0;i--)
        {
        DQ_D=0; // 給脈沖信號
        dat>>=1;
        DQ_D=1; // 給脈沖信號
        if(DQ_D)
        dat|=0x80;
        delay(4);
        }
        return(dat);
        }
        //寫一個字節
        WriteOneChar(unsigned char dat)
        {
        unsigned char i=0;
        for (i=8; i>0; i--)
        {
        DQ_D=0;
        DQ_D=dat&0x01;
        delay(5);
        DQ_D=1;
        dat>>=1;
        }
        }
        //讀取溫度
        unsigned int ReadTemperature(void)
        {
        unsigned char a=0;
        unsigned char b=0;
        unsigned int t=0;
        float tt=0;
        Init_DS18B20();
        WriteOneChar(0xCC); // 跳過讀序號列號的操作
        WriteOneChar(0x44); // 啟動溫度轉換
        Init_DS18B20();
        WriteOneChar(0xCC); //跳過讀序號列號的操作
        WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
        a=ReadOneChar();
        b=ReadOneChar();
        t=b;
        t<<=8;
        t=t|a;
        tt=t*0.0625; //將溫度的高位與低位合并 賦予tt的是測得的溫度 是一個單精度的浮點數
        T=tt;
        //////////// 寫溫度標記
        if(T>TH)
        {
        flag_h=1;
        flag_l=0;
        }
        else if(T{
        flag_h=0;
        flag_l=1;

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

        }
        else if(T>=TL&&T<=TH)
        {
        flag_h=0;
        flag_l=0;
        }
        ///////////////////
        t=tt*10+0.5; //對結果進行4舍5入 //這部算法非常重要
        return(t);
        }
        void DS18B20_deal_data() //溫度數據處理
        {
        unsigned char high,mid,low,high1,mid1,low1;
        unsigned int temp,temp2,l=0,l1=0;
        void DisplayOneChar(unsigned char x,unsigned char y,unsigned char __data);
        //////////////溫度數據處理////////////////
        temp=ReadTemperature();
        temp2=(temp-10)*9/5+50;
        if(temp>=1000)
        {
        temp/=10;l=1;
        }
        if(temp2>=1000)
        {
        temp2/=10; l1=1;
        }
        high=temp/100%10;
        mid=temp/10%10;
        low=temp%10;
        high1= temp2/100%10;
        mid1= temp2/10%10;
        low1= temp2%10;
        ///////////數據顯示/////////////////////////////////////////
        if(temp<0) DisplayOneChar(0x08,0,-);
        if(l==1)
        {
        DisplayOneChar(0x09,0,1);/////位數顯示
        DisplayOneChar(0x0a,0,fig[high]);
        DisplayOneChar(0x0b,0,fig[mid]);
        DisplayOneChar(0x0c,0,.);
        DisplayOneChar(0x0d,0,fig[low]);
        }
        else
        {
        DisplayOneChar(0x09,0,fig[high]);/////位數顯示
        DisplayOneChar(0x0a,0,fig[mid]);
        DisplayOneChar(0x0b,0,.);
        DisplayOneChar(0x0c,0,fig[low]);
        }
        if(temp2<0)
        DisplayOneChar(0x48,1,-);
        if(l1==1)
        {
        DisplayOneChar(0x49,1,1);/////位數顯示
        DisplayOneChar(0x4a,1,fig[high]);
        DisplayOneChar(0x4b,1,fig[mid]);
        DisplayOneChar(0x4c,1,.);
        DisplayOneChar(0x4d,1,fig[low]);
        }
        else
        {
        DisplayOneChar(0x49,1,fig[high1]);/////位數顯示
        DisplayOneChar(0x4a,1,fig[mid1]);
        DisplayOneChar(0x4b,1,.);
        DisplayOneChar(0x4c,1,fig[low1]);
        }
        }
        /////////////////////////////////////////////////////////////////
        ///////////////////以下是液晶代碼區/////////////////////////////
        //////////////////////////////////液晶指令集/////////////
        void lcd_findbusy() //忙查詢
        {
        E=0;
        RS=0;
        RW=1;
        E=1;
        while(DQ_L&0x80);
        delay_us(4);
        E=0;
        }
        void lcd_wcmd(unsigned char cmd)//寫命令
        {
        lcd_findbusy();
        delay_us(1);
        E=0;
        RS=0;
        RW=0;
        DQ_L=cmd;
        E=1;
        lcd_findbusy();
        delay_us(4);
        E=0;
        }
        void lcd_wdata(unsigned char _data) //寫數據
        {
        lcd_findbusy();
        delay_us(1);
        E=0;
        RS=1;
        RW=0;
        DQ_L=_data;
        E=1;
        lcd_findbusy();
        delay_us(4);
        E=0;
        }
        void lcd_pos(unsigned char adress) //設定顯示位置
        {
        lcd_findbusy();
        delay_us(1);
        lcd_wcmd(adress|0x80); //注意用或運算,否則第一位不是1,地址數據寫入錯誤
        lcd_findbusy();
        return;
        }
        void lcd_first() //液晶顯示初始化
        {
        BG=0;
        delay_ms(1);
        lcd_wcmd(0x38); //顯示設置 雙行 8線 5*10點陣
        delay_ms(1);
        lcd_wcmd(0x08); //顯示關閉
        delay_ms(1);
        lcd_wcmd(0x01); //顯示清屏
        delay_ms(1);
        lcd_wcmd(0x06); //光標移動設置__向右
        delay_ms(1);
        lcd_wcmd(0x0c); //顯示開 不顯示光標 光標不閃爍
        delay_ms(1);
        lcd_findbusy();
        return;
        }
        void DisplayOneChar(unsigned char x,unsigned char y,unsigned char __data)
        {
        y=y&0x01;
        x=x&0x0f;
        if(y)
        x=x|0x40;/// 如果要顯示第二行
        lcd_pos(x);
        lcd_wdata(__data);
        return;
        }
        void DisplayListChar(unsigned char x,unsigned char y,unsigned char code *__data)
        {
        unsigned char j=0;
        y=y&0x01;
        x=x&0x0f;
        while(x<=0x0f&&__data[j]!=

        主站蜘蛛池模板: 剑川县| 闻喜县| 东安县| 同德县| 隆德县| 云和县| 青铜峡市| 睢宁县| 涟源市| 怀柔区| 南平市| 永顺县| 大足县| 正安县| 什邡市| 海口市| 陇川县| 江门市| 南阳市| 如皋市| 托里县| 南川市| 建宁县| 论坛| 曲麻莱县| 阳谷县| 怀仁县| 奉节县| 锡林郭勒盟| 成武县| 邯郸市| 沂源县| 孟连| 浙江省| 寿光市| 平乐县| 哈巴河县| 昔阳县| 吉林市| 平湖市| 涡阳县|