新聞中心

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

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

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


        // 功能函數定義

        unsigned char DS18B20_Init(void){
        unsigned char result;
        DS18B20_DIR |= DS18B20_DQ;// ow output
        DS18B20_OUT &= ~DS18B20_DQ;// DS18B20_DQ=0;
        DelayX10us(48);// Bus master pulling low 480us minimum;
        DS18B20_OUT |= DS18B20_DQ;// DS18B20_DQ=1;
        DelayX10us(6);// Resister pull up 15-60us;
        DS18B20_DIR &= ~DS18B20_DQ;// ow input
        result = DS18B20_IN & DS18B20_DQ;
        DS18B20_DIR |= DS18B20_DQ;// ow output
        DelayX10us(48);// End of timeslot total 480us;
        return(result);// any 1 wire device ?result:=1 no devide; ?result:=0 have device;
        }//Intialization the 1-wire devices;

        unsigned char DS18B20_Init_2(void){
        unsigned char result;
        DS18B20_DIR |= DS18B20_DQ_2;// ow output
        DS18B20_OUT &= ~DS18B20_DQ_2;// DS18B20_DQ=0;
        DelayX10us(48);// Bus master pulling low 480us minimum;
        DS18B20_OUT |= DS18B20_DQ_2;// DS18B20_DQ=1;
        DelayX10us(6);// Resister pull up 15-60us;
        DS18B20_DIR &= ~DS18B20_DQ_2;// ow input
        result = DS18B20_IN & DS18B20_DQ_2;
        DS18B20_DIR |= DS18B20_DQ_2;// ow output
        DelayX10us(48);// End of timeslot total 480us;
        return(result);// any 1 wire device ?result:=1 no devide; ?result:=0 have device;
        }//Intialization the 1-wire devices;


        unsigned char DS18B20_ReadBit(void){
        unsigned char result;
        DS18B20_DIR |= DS18B20_DQ;// ow output
        DS18B20_OUT &= ~DS18B20_DQ;// DS18B20_DQ=0;
        _NOP();// Start of timeslot;
        DS18B20_OUT |= DS18B20_DQ;// DS18B20_DQ=1;
        _NOP();_NOP();_NOP();_NOP();
        // Wait from the start;
        DS18B20_DIR &= ~DS18B20_DQ;// ow input
        result = DS18B20_IN & DS18B20_DQ;
        DS18B20_DIR |= DS18B20_DQ;// ow output
        return(result);// return the result of the 1-wire devide;
        }//Read a bit on the 1-wire bus;

        unsigned char DS18B20_ReadBit_2(void){
        unsigned char result;
        DS18B20_DIR |= DS18B20_DQ_2;// ow output
        DS18B20_OUT &= ~DS18B20_DQ_2;// DS18B20_DQ=0;
        _NOP();// Start of timeslot;
        DS18B20_OUT |= DS18B20_DQ_2;// DS18B20_DQ=1;
        _NOP();_NOP();_NOP();_NOP();
        // Wait from the start;
        DS18B20_DIR &= ~DS18B20_DQ_2;// ow input
        result = DS18B20_IN & DS18B20_DQ_2;
        DS18B20_DIR |= DS18B20_DQ_2;// ow output
        return(result);// return the result of the 1-wire devide;
        }//Read a bit on the 1-wire bus;


        void DS18B20_WriteBit(unsigned char oww_dat){
        DS18B20_DIR |= DS18B20_DQ;// ow output
        DS18B20_OUT &= ~DS18B20_DQ;// DS18B20_DQ=0;
        if (1 == oww_dat)
        DS18B20_OUT |= DS18B20_DQ;// DS18B20_DQ=1;
        DelayX10us(10);// Remain the state for 100us;
        DS18B20_OUT |= DS18B20_DQ;// DS18B20_DQ=1;
        }//Write a bit to the 1-wire bus;

        void DS18B20_WriteBit_2(unsigned char oww_dat){
        DS18B20_DIR |= DS18B20_DQ_2;// ow output
        DS18B20_OUT &= ~DS18B20_DQ_2;// DS18B20_DQ=0;
        if (1 == oww_dat)
        DS18B20_OUT |= DS18B20_DQ_2;// DS18B20_DQ=1;
        DelayX10us(10);// Remain the state for 100us;
        DS18B20_OUT |= DS18B20_DQ_2;// DS18B20_DQ=1;
        }//Write a bit to the 1-wire bus;


        unsigned char DS18B20_ReadByte(void){
        unsigned char i;
        unsigned char result=0;
        for(i = 0; i8; i++){
        if(DS18B20_ReadBit())
        result |= 0x01 < i;
        DelayX10us(12);// ??
        }
        return(result);// return the result of the 1-wire device;
        }//Read a byte from the 1-wire bus;

        unsigned char DS18B20_ReadByte_2(void){
        unsigned char i;
        unsigned char result=0;
        for(i = 0; i8; i++){
        if(DS18B20_ReadBit_2())
        result |= 0x01 < i;
        DelayX10us(12);// ??
        }
        return(result);// return the result of the 1-wire device;
        }//Read a byte from the 1-wire bus;


        void DS18B20_WriteByte(unsigned char oww_dat){
        unsigned char i,temp;
        for(i = 0; i8; i++){
        temp = oww_dat >> i;
        temp &= 0x01;
        DS18B20_WriteBit(temp);
        }
        DelayX10us(7);// ??
        }//Write a byte to the 1-wire bus;

        void DS18B20_WriteByte_2(unsigned char oww_dat){
        unsigned char i,temp;
        for(i = 0; i8; i++){
        temp = oww_dat >> i;
        temp &= 0x01;
        DS18B20_WriteBit_2(temp);
        }
        DelayX10us(7);// ??
        }//Write a byte to the 1-wire bus;


        關鍵詞: msp430單片機ds18b2

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 萍乡市| 嘉鱼县| 乌兰察布市| 迁西县| 江油市| 汤阴县| 图木舒克市| 高密市| 康马县| 曲水县| 本溪| 晋城| 巴林右旗| 奉贤区| 宁都县| 郎溪县| 柘荣县| 邵东县| 荥阳市| 无棣县| 科尔| 洛川县| 彰化县| 赫章县| 莱芜市| 务川| 公安县| 肇源县| 曲靖市| 湖州市| 隆子县| 池州市| 呼图壁县| 小金县| 香港| 兴安盟| 栖霞市| 水富县| 舟曲县| 行唐县| 明光市|