新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > msp430單片機的ds18b20測溫度c程序

        msp430單片機的ds18b20測溫度c程序

        作者: 時間:2016-11-27 來源:網絡 收藏


        void DS18B20_ReadTemp(void)
        {unsigned char tempH,tempL,tempH_2,tempL_2;
        DS18B20_Init();
        DS18B20_Init_2();
        DS18B20_WriteByte(SkipRom);
        _NOP();
        DS18B20_WriteByte_2(SkipRom);
        _NOP();
        //There is just one DS1820 on the bus;
        DS18B20_WriteByte(ConvertTemperature);
        DelayX10us(10);
        DS18B20_WriteByte_2(ConvertTemperature);
        DelayX10us(10);
        //Start to convert temperature;
        DS18B20_Init();
        DS18B20_Init_2();
        DS18B20_WriteByte(SkipRom);
        _NOP();
        DS18B20_WriteByte_2(SkipRom);
        _NOP();
        DS18B20_WriteByte(ReadScratchpad);
        DS18B20_WriteByte_2(ReadScratchpad);
        GetScratchpad[0]=DS18B20_ReadByte();
        //Master samples the LSB temperature from the scratchpad;
        GetScratchpad[1]=DS18B20_ReadByte();
        //Master samples the MSB temperature from the scratchpad;
        GetScratchpad[2]=DS18B20_ReadByte();
        //Master samples the Th register or userbyte1 from the scratchpad;
        GetScratchpad[3]=DS18B20_ReadByte();
        //Master samples the Tl register or userbyte0 from the scratchpad;
        GetScratchpad[4]=DS18B20_ReadByte();
        //Master samples the configuration register from the scratchpad;
        GetScratchpad[5]=DS18B20_ReadByte();
        //Master samples the reservedbyte from the scratchpad;
        GetScratchpad[6]=DS18B20_ReadByte();
        //Master samples the reservedbyte from the scratchpad;
        GetScratchpad[7]=DS18B20_ReadByte();
        //Master samples the reservedbyte from the scratchpad;
        GetScratchpad[8]=DS18B20_ReadByte();
        //Master samples the CRC from the scratchpad;
        tempH=(GetScratchpad[1] < 4) | (GetScratchpad[0] >> 4);
        tempL=(GetScratchpad[0] & 0x0f);
        DS18B20_Init();

        GetScratchpad_2[0]=DS18B20_ReadByte_2();
        //Master samples the LSB temperature from the scratchpad;
        GetScratchpad_2[1]=DS18B20_ReadByte_2();
        //Master samples the MSB temperature from the scratchpad;
        GetScratchpad_2[2]=DS18B20_ReadByte_2();
        //Master samples the Th register or userbyte1 from the scratchpad;
        GetScratchpad_2[3]=DS18B20_ReadByte_2();
        //Master samples the Tl register or userbyte0 from the scratchpad;
        GetScratchpad_2[4]=DS18B20_ReadByte_2();
        //Master samples the configuration register from the scratchpad;
        GetScratchpad_2[5]=DS18B20_ReadByte_2();
        //Master samples the reservedbyte from the scratchpad;
        GetScratchpad_2[6]=DS18B20_ReadByte_2();
        //Master samples the reservedbyte from the scratchpad;
        GetScratchpad_2[7]=DS18B20_ReadByte_2();
        //Master samples the reservedbyte from the scratchpad;
        GetScratchpad_2[8]=DS18B20_ReadByte_2();
        //Master samples the CRC from the scratchpad;
        tempH_2=(GetScratchpad_2[1] < 4) | (GetScratchpad_2[0] >> 4);
        tempL_2=(GetScratchpad_2[0] & 0x0f);
        DS18B20_Init_2();
        //Issue a reset to terminate left parts;
        //if(tempH & 0x80)
        //{ tempH=~tempH;
        //tempL=~tempL+1;
        //ResultSignal=1;
        ////Negative temperature;
        //}
        ResultTemperatureH=tempH;
        ResultTemperatureLL=decimalL[tempL];
        ResultTemperatureLH=decimalH[tempL];

        ResultTemperatureH_2=tempH_2;
        ResultTemperatureLL_2=decimalL[tempL_2];
        ResultTemperatureLH_2=decimalH[tempL_2];
        //Result of temperature;
        }//Read the byte0 and byte1 from scratchpad;



        void Delay10us(void){
        unsigned char i;
        for (i=0; i<(SMCLK/500-3); i++);
        }

        //Time is accurately !!
        void DelayX10us(unsigned char x10us){
        unsigned int i;
        for (i=0; i
        Delay10us
        }

        上一頁 1 2 3 下一頁

        關鍵詞: msp430單片機ds18b2

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 正宁县| 唐海县| 兴业县| 红原县| 封开县| 平定县| 皮山县| 宜春市| 敦煌市| 静乐县| 浏阳市| 富阳市| 且末县| 上饶县| 淅川县| 白朗县| 墨竹工卡县| 余庆县| 清镇市| 榆树市| 宿州市| 赤水市| 横峰县| 阿巴嘎旗| 汤原县| 西城区| 柞水县| 方城县| 耿马| 苍溪县| 涟水县| 灵台县| 全椒县| 西吉县| 汕尾市| 镇安县| 龙游县| 新余市| 子洲县| 尖扎县| 司法|