Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

DS1821 temperature sensor over OneWire on Netduino Plus 2


  • Please log in to reply
4 replies to this topic

#1 Thomas Rankin

Thomas Rankin

    Member

  • Members
  • PipPip
  • 26 posts

Posted 19 December 2012 - 02:35 AM

I've ported my DS1821 code over to use Microsoft.SPOT.Hardware.OneWire. Thought I would share it out in case anyone else had these devices.



public class Program
    {
        static OutputPort owPort = new OutputPort(Pins.GPIO_PIN_D0, false);
        static OneWire DS1821 = new OneWire(owPort);

        public static void Main()
        {

            lcdDisplay.Clear();
         
            while (true)
            {
                string tmp = GetTemperature().ToString("F2");
                lcdDisplay.SetCursorPosition(0, 0);
                lcdDisplay.Write(tmp);
                Thread.Sleep(100);
            }

        }

        public static float GetTemperature()
        {
            ushort temp_read = 0;
            ushort count_remain = 0;
            ushort count_per_c = 0;
            ushort configuration_register = 0;
            ushort whileCount = 0;

            // One -Shot Convert Temp
            DS1821.TouchReset();
            DS1821.WriteByte(0xEE);

            // Loop and sleep a bit until it is done converting the Temperature data.
            do
            {
                Thread.Sleep(10);

                configuration_register = 0;
                DS1821.TouchReset();
                DS1821.WriteByte(0xAC);

                // Read the configuration Register from the DS1821
                configuration_register = ReadBytes(1);
                whileCount++;

            } while (((configuration_register & (1 << 7)) == 0) && whileCount < 100); // If Bit #8 is 1 then we are finished converting the temp

            if (whileCount >= 100) return -1;
            // Get Temp
            DS1821.TouchReset();
            DS1821.WriteByte(0xAA);
            temp_read = ReadBytes(1); ;

            // Get Count Remaining
            DS1821.TouchReset();
            DS1821.WriteByte(0xA0);
            count_remain = ReadBytes(2);

            // Load The Counter to populate the slope accumulator
            DS1821.TouchReset();
            DS1821.WriteByte(0x41);

            // Read Count Per Deg
            DS1821.TouchReset();
            DS1821.WriteByte(0xA0);
            count_per_c = ReadBytes(2);

            // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
            if (temp_read >= 200) temp_read -= 256;

            var highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);

            return (float)((1.80 * highResTemp) + 32.00);

        }

        static ushort ReadBytes(ushort count)
        {
            ushort val = 0;
            for (ushort i = 0; i < count; i++)
            {
                val |= (ushort)(DS1821.ReadByte() << i * 8);
            }
            return val;
        }


    }



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 20 December 2012 - 01:18 AM

Thanks Thomas! Chris

#3 Don King

Don King

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts
  • LocationLawrence, KS

Posted 23 December 2012 - 02:43 AM

Hi Thomas - Thanks for posting this. I have the DS18B20 One Wire temperature Sensor (https://www.sparkfun.../products/11050) and was wondering if this would work the same for what I have? I am a .NET Developer who is a complete newbie to electornics. Is there a chance you could post how you wired up your temperature sensor to the Netduino Plus if it is not too much trouble. I have the NP 1 but would assume there is not much of a difference? Thanks! Don

#4 Thomas Rankin

Thomas Rankin

    Member

  • Members
  • PipPip
  • 26 posts

Posted 26 December 2012 - 06:35 PM

Hi Thomas - Thanks for posting this. I have the DS18B20 One Wire temperature Sensor (https://www.sparkfun.../products/11050) and was wondering if this would work the same for what I have? I am a .NET Developer who is a complete newbie to electornics. Is there a chance you could post how you wired up your temperature sensor to the Netduino Plus if it is not too much trouble. I have the NP 1 but would assume there is not much of a difference?

Thanks!

Don

I'm not sure if the protocol is the same for the DS18B20, but I know that I had trouble finding DS1821 solutions because it is non-addressable.  I think there are many example solutions for the DS18B20.    http://www.tinyclr.c...earch?q=DS18B20  http://forums.netdui...-using-onewire/ also has some descriptions for wiring.

 

Best of luck.



#5 Thomas Rankin

Thomas Rankin

    Member

  • Members
  • PipPip
  • 26 posts

Posted 31 December 2012 - 03:01 PM

Hi Thomas - Thanks for posting this. I have the DS18B20 One Wire temperature Sensor (https://www.sparkfun.../products/11050) and was wondering if this would work the same for what I have? I am a .NET Developer who is a complete newbie to electornics. Is there a chance you could post how you wired up your temperature sensor to the Netduino Plus if it is not too much trouble. I have the NP 1 but would assume there is not much of a difference?

Thanks!

Don

 

Don, I just realized that you indicated that you've got an NP1, and while that would not effect wiring much if at all, the NP1 does not have built in OneWire support.  You have a couple of options as I can see.

 

1: Build a custom firmware with OneWire support (there are posts on the forum about it)

2: Use a hardware OneWire translater or build one yourself with an attiny or pic

3: Upgrade to the NP2

 

Thomas






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.