新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于ATMEGA16的DS18B20測溫程序

        基于ATMEGA16的DS18B20測溫程序

        作者: 時(shí)間:2012-08-04 來源:網(wǎng)絡(luò) 收藏

        //讀取溫度值先讀取暫存器的值在進(jìn)行溫度轉(zhuǎn)換否則會(huì)意外出錯(cuò)
        unsigned int readTemp(void)
        {
        unsigned char tempL,tempH;
        unsigned int temp;
        //開始讀取溫度
        ds18b20_reset();//18B20復(fù)位
        ds18b20_write_byte(0xcc);//跳過ROM
        ds18b20_write_byte(0xbe);//命令讀取暫存器
        tempL=ds18b20_read_byte();//從暫存器中讀取數(shù)據(jù)
        tempH=ds18b20_read_byte();//從暫存器中讀取數(shù)據(jù)
        temp=(tempH8)|tempL;//總值為高位*256+低位
        temp=temp*0.625;//為了保留1位小數(shù),最小單位為0.0625
        ds18b20_reset();//18B20初始化
        ds18b20_write_byte(0xcc);//對ROM進(jìn)行操作,因?yàn)橹唤恿?個(gè)器件所以寫跳過指令
        ds18b20_write_byte(0x44);//啟動(dòng)溫度轉(zhuǎn)換
        Delay_ms(1);//給硬件一點(diǎn)時(shí)間讓其進(jìn)行轉(zhuǎn)換
        return(temp);
        }

        --------------------------------------------------------


        display.c:
        #include iom16v.h>
        #include macros.h>
        #include"display.h"
        #define uchar unsigned char
        #define uint unsigned int

        uchar table1[]="0123456789";
        uchar table2[]="design:zhubo";
        uchar table3[]="temperature:";
        uchar table4[]=".";

        extern int temperture;
        void delay(unsigned int ms)
        {
        unsigned int i,j;
        for(i=0;ims;i++)
        {
        for(j=0;j1141;j++);
        }
        }

        void write_com(uchar com)
        {
        PORTD=~BIT(4);
        PORTD=~BIT(5);
        PORTB=com;
        PORTD|=BIT(6);
        delay(1);
        PORTD=~BIT(6);
        }

        void write_dat(uchar dat)
        {
        PORTD|=BIT(4);
        PORTD=~BIT(5);
        PORTB=dat;
        PORTD|=BIT(6);
        delay(1);
        PORTD=~BIT(6);
        }
        void LCD_init()
        {
        DDRB=0XFF;
        DDRD|=BIT(4)|BIT(5)|BIT(6);
        PORTD=~BIT(6);

        write_com(0X38);
        delay(5);
        write_com(0X01);
        delay(5);
        write_com(0X0C);
        delay(5);
        write_com(0X06);
        delay(5);

        }
        void display()
        {
        uint i;

        write_com(0X80+0);
        delay(5);
        for(i=0;i12;i++)
        {
        write_dat(table3[i]);
        delay(5);
        }

        write_com(0X80+12);
        delay(5);
        write_dat(table1[temperture/100%10]);
        delay(5);
        write_com(0X80+13);
        delay(5);
        write_dat(table1[temperture/10%10]);
        delay(5);
        write_com(0X80+14);
        delay(5);
        write_dat(table4[0]);
        delay(5);
        write_com(0X80+15);
        delay(5);
        write_dat(table1[temperture%10]);
        delay(5);

        write_com(0X80+0X40);
        delay(5);
        for(i=0;i12;i++)
        {
        write_dat(table2[i]);
        delay(5);
        }
        }


        --------------------------------------------------------
        接口函數(shù):
        18B20.h:
        #define uchar unsigned char
        #define uint unsigned int

        extern uchar ds18b20_reset();
        extern void ds18b20_write_byte(uchar value);
        extern uint ds18b20_read_byte(void);
        extern uint readTemp(void);
        display.h:
        #define uchar unsigned char
        #define uint unsigned int
        #ifndef display_h
        #define display_h
        extern void write_com(uchar com);
        extern void write_dat(uchar dat);
        extern void LCD_init();
        extern void display();
        extern uchar table[];
        #endif
        delay.h:

        /**************************************************************
        **函數(shù)功能:延時(shí)1us
        **輸入?yún)?shù):無
        **返回值:無
        **在本函數(shù)外定義變量:無
        **調(diào)用的函數(shù):NOP()
        **************************************************************/
        void Delay_1us(void);

        /**************************************************************
        **函數(shù)功能:us級(jí)延時(shí)
        **輸入?yún)?shù):xus:延時(shí)us數(shù)
        **返回值:無
        **在本函數(shù)外定義變量:無
        **調(diào)用的函數(shù):Delay_1us()
        **************************************************************/
        void Delay_Us(unsigned int xus);

        /**************************************************************
        **函數(shù)功能:延時(shí)1ms
        **輸入?yún)?shù):無
        **返回值:無
        **在本函數(shù)外定義變量:無
        **調(diào)用的函數(shù):NOP()
        **************************************************************/
        void Delay_1ms(void);

        /**************************************************************
        **函數(shù)功能:ms級(jí)延時(shí)
        **輸入?yún)?shù):xms:延時(shí)ms數(shù)
        **返回值:無
        **在本函數(shù)外定義變量:無
        **調(diào)用的函數(shù):Delay_1ms()
        **************************************************************/
        void Delay_ms(unsigned int xms);

        /**************************************************************
        **函數(shù)功能:10us級(jí)延時(shí)
        **輸入?yún)?shù):x10us:延時(shí)x10us數(shù)
        **返回值:無
        **在本函數(shù)外定義變量:無
        **調(diào)用的函數(shù):無
        **************************************************************/
        void Delay10us(int x10us);


        void Delay_1us()
        {
        unsigned char t=2;
        t--;
        }

        void Delay_Us(unsigned int xus)
        {
        while (xus--)
        Delay_1us();
        }

        void Delay_1ms(void)
        {
        unsigned int i;
        for(i=0;i1142;i++)
        ;
        }

        void Delay_ms(unsigned int x)
        {
        int i,j; //variable:declare int
        for(i=0; ix; i++)
        for(j=0; j498;j++)
        {
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        NOP();
        }
        }

        void Delay10us(int x10us)//crystal=8MHz
        {
        int i;int j; //variable:declare int
        for(i=0; ix10us; i++)
        {
        for(j=0;j2;j++)
        {
        NOP();
        NOP();
        NOP();
        }
        }
        }


        上一頁 1 2 下一頁

        關(guān)鍵詞: ATMEGA16 DS18B20 測溫程序

        評論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 洮南市| 华池县| 农安县| 吉隆县| 福泉市| 克什克腾旗| 米林县| 靖江市| 崇左市| 阿克苏市| 咸丰县| 大理市| 二连浩特市| 邵阳市| 榕江县| 隆昌县| 合川市| 临高县| 遂宁市| 资溪县| 桓台县| 乌拉特后旗| 通许县| 巨鹿县| 白银市| 东源县| 分宜县| 新平| 邵阳县| 天镇县| 南充市| 新营市| 鄄城县| 札达县| 三江| 昌平区| 永修县| 秭归县| 当阳市| 石门县| 衡山县|