OneWire DS18B20

Hi,
I have a Lemur and SDK 4.3 with VS 2015. I’m trying to read a DS18B20 on a TinyRTC board. I’ve looked at the examples in Codeshare, but can’t get them to work. So far I have the following errors, it doesn’t recognise Reset() or SearchRom(). Can someone help please.

PROGRAM.CS
using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHI.IO;
using GHI.Pins;

namespace onewire2
{
public class Program
{
public static void Main()
{
using (var op = new OutputPort((Cpu.Pin)FEZLemur.Gpio.D8, false))
{
var ow = new OneWire(op);
var devs = onewire2.OneWireBus.Scan(ow, onewire2.OneWireBus.Family.Unknown);

            foreach (var d in devs)
                Debug.Print(d.Family.ToString());

            using (var s = new onewire2.DS18B20(ow, devs[0]))
            {
                // do the conversion and read the temperature all at once
                Debug.Print(s.ConvertAndReadTemperature().ToString());
                Thread.Sleep(1000);
                // start the conversion (returns quickly)
                s.StartConversion();
                // wait for the conversion to happen
                Thread.Sleep(1000);
                // now, read the temp
                Debug.Print(s.ReadTemperature().ToString());
            }
        }
        
    }
}

}

DS18B20.CS
using System;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.SPOT.Hardware;

namespace onewire2
{
public class DS18B20 : IDisposable
{
private OutputPort m_op;
private OneWire m_ow;
private OneWireBus.Device m_dev;

    public DS18B20(Cpu.Pin pin) : this(pin, null) { }

    public DS18B20(Cpu.Pin pin, OneWireBus.Device dev)
    {
        m_op = new OutputPort(pin, false);
        m_ow = new OneWire(m_op);

        if (dev == null)
        {
            var devs = OneWireBus.Scan(m_ow, OneWireBus.Family.DS18B20);

            if (devs == null || devs.Length < 1)
                throw new InvalidOperationException("No DS18B20 devices found on OneWire bus");

            dev = devs[0];
        }

        m_dev = dev;
    }

    public DS18B20(OneWire ow) : this(ow, null) { }

    public DS18B20(OneWire ow, OneWireBus.Device dev)
    {
        m_ow = ow;

        if (dev == null)
        {
            var devs = OneWireBus.Scan(ow, OneWireBus.Family.DS18B20);

            if (devs == null || devs.Length < 1)
                throw new InvalidOperationException("No DS18B20 devices found on OneWire bus");

            dev = devs[0];
        }

        m_dev = dev;
    }

    public void Dispose()
    {
        if (m_op != null)
            m_op.Dispose();
    }

    public float ConvertAndReadTemperature()
    {
        var data = 0L;

        // if reset finds no devices, just return 0
        if (m_ow.Reset() == 0)
            return 0;

        // address the device
        m_ow.WriteByte(Command.MatchROM);
        WriteBytes(m_dev.Address);

        // tell the device to start temp conversion
        m_ow.WriteByte(Command.StartTemperatureConversion);

        // wait for as long as it takes to do the temp conversion,
        // data sheet says ~750ms
        while (m_ow.ReadByte() == 0)
            System.Threading.Thread.Sleep(1);

        // reset the bus
        m_ow.Reset();

        // address the device
        m_ow.WriteByte(Command.MatchROM);
        WriteBytes(m_dev.Address);

        // read the data from the sensor
        m_ow.WriteByte(Command.ReadScratchPad);

        // read the two bytes of data
        data = m_ow.ReadByte(); // LSB
        data |= (ushort)(m_ow.ReadByte() << 8); // MSB

        // reset the bus, we don't want more data than that
        m_ow.Reset();

        // returns C
        // F would be:  (float)((1.80 * (data / 16.00)) + 32.00);
        return (float)data / 16f;
    }

    public void StartConversion()
    {
        // if reset finds no devices, just return 0
        if (m_ow.Reset() == 0)
            return;

        // address the device
        m_ow.WriteByte(Command.MatchROM);
        WriteBytes(m_dev.Address);

        // tell the device to start temp conversion
        m_ow.WriteByte(Command.StartTemperatureConversion);
    }

    public float ReadTemperature()
    {
        var data = 0L;

        // reset the bus
        m_ow.Reset();

        // address the device
        m_ow.WriteByte(Command.MatchROM);
        WriteBytes(m_dev.Address);

        // read the data from the sensor
        m_ow.WriteByte(Command.ReadScratchPad);

        // read the two bytes of data
        data = m_ow.ReadByte(); // LSB
        data |= (ushort)(m_ow.ReadByte() << 8); // MSB

        // reset the bus, we don't want more data than that
        m_ow.Reset();

        // returns C
        // F would be:  (float)((1.80 * (data / 16.00)) + 32.00);
        return (float)data / 16f;
    }

    public static float ToFahrenheit(float tempC)
    {
        return (9f / 5f) * tempC + 32f;
    }

    private void WriteBytes(byte[] data)
    {
        for (var i = 0; i < data.Length; i++)
            m_ow.WriteByte(data[i]);
    }

    private static class Command
    {
        public const byte SearchROM = 0xF0;
        public const byte ReadROM = 0x33;
        public const byte MatchROM = 0x55;
        public const byte SkipROM = 0xCC;
        public const byte AlarmSearch = 0xEC;
        public const byte StartTemperatureConversion = 0x44;
        public const byte ReadScratchPad = 0xBE;
        public const byte WriteScratchPad = 0x4E;
        public const byte CopySratchPad = 0x48;
        public const byte RecallEEPROM = 0xB8;
        public const byte ReadPowerSupply = 0xB4;
    }
}

}

OneWireBus.CS
using System;
using System.Collections;
using Microsoft.SPOT.Hardware;

namespace onewire2
{
public static class OneWireBus
{
public class Device
{
public byte[] Address { get; private set; }

        public Family Family { get; private set; }

        internal Device(byte[] addr)
        {
            Address = addr;

            switch (addr[0])
            {
                case 0x10:
                    Family = OneWireBus.Family.DS18S20;
                    break;

                case 0x28:
                    Family = OneWireBus.Family.DS18B20;
                    break;

                default:
                    Family = OneWireBus.Family.Unknown;
                    break;
            }
        }
    }

    public enum Family : byte
    {
        Unknown = 0x00,
        DS18S20 = 0x10,
        DS18B20 = 0x28,
    }

    public static Device[] Scan(OneWire ow, params Family[] includeFamilies)
    {
        var list = new ArrayList();
        var all = false;
        var devs = ow.SearchRom();

        if (includeFamilies != null)
        {
            foreach (var f in includeFamilies)
            {
                if (f == Family.Unknown)
                    all = true;
            }
        }

        foreach (byte[] da in devs)
        {
            if (includeFamilies == null || includeFamilies.Length == 0 || all)
            {
                list.Add(new Device(da));
            }
            else
            {
                foreach (var f in includeFamilies)
                {
                    if (da[0] == (byte)f)
                        list.Add(new Device(da));
                }
            }
        }

        return (Device[])list.ToArray(typeof(Device));
    }

    public static Device[] Scan(Cpu.Pin pin, params Family[] includeFamilies)
    {
        using (var op = new OutputPort(pin, false))
            return Scan(new OneWire(op), includeFamilies);
    }
}

}

Use the code tags to make it more readable.

How is the DS18B20 wired to your Lemur? Do you have a pull up on the DATA lines and power and ground to the other 2 pins?

Thanks Dave,
Sorry about the code, just learning how to use the system.
Yes the device is on a module diagram here.http://www.hobbyist.co.nz/sites/default/files/docs/RTC/Tiny_RTC_schematic.pdf
It has 3.3v and 0v and a 3.3K resistor pulling up the siganl line.
The code has errors during build.
Alastair

@ al555bike - Have you tried this? https://bitbucket.org/StablePoint/stablepoint.hardware.onewire/wiki/Home This is a confirmed working code. So if it is not working, then there’s a hardware problem…

Many thanks Simon, that worked a treat.

Alastair