This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DS18B20

Other Parts Discussed in Thread: MSP430G2553

Hello,

i am trying to comunicate with DS18B20, with library from internet. But "int Temp" always is 0,0625. I am analyze 1wire by Logic analyzer and DS18B20 correct response 12bit data. Why is "Temp" always constant? Thank You.

#include <msp430g2553.h>
#include <stdint.h>
#include "DS18B20.h"
#include "delay.h"

void ow_portsetup()
{
    OWPORTDIR |= OWPORTPIN;
    OWPORTOUT |= OWPORTPIN;
    OWPORTREN |= OWPORTPIN;
}

/***************************************************************/

float ReadDS1820(void)
{
    unsigned int i;
    uint16_t byte = 0;
    for (i = 16; i > 0; i--)
    {
        byte >>= 1;
        if (onewire_read_bit())
        {
            byte |= 0x8000;
        }
    }
    return byte;
}

float GetData(void)
{
    uint16_t temp;
    onewire_reset();
    onewire_write_byte(0xcc); // skip ROM command
    onewire_write_byte(0x44); // convert T command
    OW_HI
    DELAY_MS(750); // at least 750 ms for the default 12-bit resolution
    onewire_reset();
    onewire_write_byte(0xcc); // skip ROM command
    onewire_write_byte(0xbe); // read scratchpad command
    temp = ReadDS1820();

    if (temp < 0x8000)
    {
        return (temp * 0.0625);
    }

    else
    {
        temp = (~temp) + 1;
        return (temp * 0.0625);
    }
}

int onewire_reset()
{
    OW_LO
    DELAY_US(480); // 480us minimum
    OW_RLS
    DELAY_US(40); // slave waits 15-60us
    if (OWPORTIN & OWPORTPIN)
        return 1; // line should be pulled down by slave
    DELAY_US(300); // slave TX presence pulse 60-240us
    if (!(OWPORTIN & OWPORTPIN))
        return 2; // line should be "released" by slave
    return 0;
}

//#####################################################################

void onewire_write_bit(int bit)
{
    //  DELAY_US(1); // recovery, min 1us
    OW_HI
    if (bit)
    {
        OW_LO
        DELAY_US(5); // max 15us
        OW_RLS
        // input
        DELAY_US(56);
    }
    else
    {
        OW_LO
        DELAY_US(60); // min 60us
        OW_RLS
        // input
        DELAY_US(1);
    }
}

//#####################################################################

int onewire_read_bit()
{
    int bit = 0;
    //  DELAY_US(1); // recovery, min 1us
    OW_LO
    DELAY_US(5); // hold min 1us
    OW_RLS
    DELAY_US(10); // 15us window
    if (OWPORTIN & OWPORTPIN)
    {
        bit = 1;
    }
    DELAY_US(46); // rest of the read slot
    return bit;
}

//#####################################################################

void onewire_write_byte(uint8_t byte)
{
    int i;
    for (i = 0; i < 8; i++)
    {
        onewire_write_bit(byte & 1);
        byte >>= 1;
    }
}

//#####################################################################

uint8_t onewire_read_byte()
{
    unsigned int i;
    uint8_t byte = 0;
    for (i = 0; i < 8; i++)
    {
        byte >>= 1;
        if (onewire_read_bit())
            byte |= 0x80;
    }
    return byte;
}

  • Marek Prochazka said:
    Why is "Temp" always constant? Thank You.

    Perhaps your float ReadDS1820(void) function does not work as expected? Or it's implementation or return value is wrong?

    Pity that before posting you did not debug your code, like mentioned function or value it returns..

  • I tried various changes. But here I put the library in its original form. This change also does not work.

     

    #include <msp430g2553.h>
    #include <stdint.h>
    #include "DS18B20.h"
    #include "delay.h"

    void ow_portsetup()
    {
        OWPORTDIR |= OWPORTPIN;
        OWPORTOUT |= OWPORTPIN;
        OWPORTREN |= OWPORTPIN;
    }

    /***************************************************************/

    float ReadDS1820(unsigned int byte)
    {
        unsigned int i;
        byte = 0;
        for (i = 16; i > 0; i--)
        {
            byte >>= 1;
            if (onewire_read_bit())
            {
                byte |= 0x8000;
            }
        }
        return byte;
    }

    float GetData()
    {
        unsigned int temp;

        onewire_reset();
        onewire_write_byte(0xcc); // skip ROM command
        onewire_write_byte(0x44); // convert T command
        OW_HI
        DELAY_MS(750); // at least 750 ms for the default 12-bit resolution
        onewire_reset();
        onewire_write_byte(0xcc); // skip ROM command
        onewire_write_byte(0xbe); // read scratchpad command

        temp = ReadDS1820(0x00);

        if (temp < 0x8000)
        {
            return (temp * 0.0625);
        }

        else
        {
            temp = (~temp) + 1;
            return (temp * 0.0625);
        }
    }

    int onewire_reset()
    {
        OW_LO
        DELAY_US(480); // 480us minimum
        OW_RLS
        DELAY_US(40); // slave waits 15-60us
        if (OWPORTIN & OWPORTPIN)
            return 1; // line should be pulled down by slave
        DELAY_US(300); // slave TX presence pulse 60-240us
        if (!(OWPORTIN & OWPORTPIN))
            return 2; // line should be "released" by slave
        return 0;
    }

    //#####################################################################

    void onewire_write_bit(int bit)
    {
        DELAY_US(1); // recovery, min 1us
        OW_HI
        if (bit)
        {
            OW_LO
            DELAY_US(5); // max 15us
            OW_RLS
            // input
            DELAY_US(56);
        }
        else
        {
            OW_LO
            DELAY_US(60); // min 60us
            OW_RLS
            // input
            DELAY_US(1);
        }
    }

    //#####################################################################

    int onewire_read_bit()
    {
        int bit = 0;
        DELAY_US(1); // recovery, min 1us
        OW_LO
        DELAY_US(5); // hold min 1us
        OW_RLS
        DELAY_US(10); // 15us window
        if (OWPORTIN & OWPORTPIN)
        {
            bit = 1;
        }
        DELAY_US(46); // rest of the read slot
        return bit;
    }

    //#####################################################################

    void onewire_write_byte(uint8_t byte)
    {
        int i;
        for (i = 0; i < 8; i++)
        {
            onewire_write_bit(byte & 1);
            byte >>= 1;
        }
    }

    //#####################################################################

    uint8_t onewire_read_byte()
    {
        unsigned int i;
        uint8_t byte = 0;
        for (i = 0; i < 8; i++)
        {
            byte >>= 1;
            if (onewire_read_bit())
                byte |= 0x80;
        }
        return byte;
    }

  • sorry , May I ask  a Issue?

    My MSP430G2553  detect to DS18B20 is OK, but read data is 0x00,

    how can I do ?

**Attention** This is a public forum