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

Netduino Plus and DS1620 anyone?


  • Please log in to reply
6 replies to this topic

#1 AdamS

AdamS

    New Member

  • Members
  • Pip
  • 8 posts

Posted 18 January 2012 - 07:17 AM

Hi Guys

hopefully somone can help me out here i've had a hunt over the forums and cant seem to find anything, so hopefully someone will be able to point me in the right direction,

im trying to hook up a DS1620 thermometer to my plus and just cant seem to get it going, not sure what ive stuffed up but im sure its something in here, below is a copy of my class and all i ever get back from it when reading is -65536

Removed old dodgy vb class


hopefully someone can show me the error of my ways

Adam.

#2 AdamS

AdamS

    New Member

  • Members
  • Pip
  • 8 posts

Posted 19 January 2012 - 03:14 AM

Ok, ive gotten a little bit further found some code over at: http://code.tinyclr....7/ds1620-driver
that does what i wanted, and after some tweaking seems to work perfectly, yet when ive converted it over to VB it seems the temp is off by a few degrees,

here are both classes, hopefully i have just made a mistake in my conversion now and its not another works in C# but not in VB thing

the C# Class:
public class DS1620_2    {        
    private TristatePort _data ; 
    private   OutputPort _clock;
    private   OutputPort _reset;
      
     public DS1620_2( Cpu.Pin dq,Cpu.Pin clk, Cpu.Pin rst)   
     {
        _clock = new OutputPort(clk, false);
        _reset = new OutputPort(rst, false);
        _data = new TristatePort(dq, false, false, Port.ResistorMode.Disabled);     
        _data.Active = true;      
    }        
    
    void write_command(int command)    
        /* sends 8 bit command on DQ output, least sig bit first */   
    {           
        int n, bit;          
        for (n = 0; n < 8; n++) 
        {            
            bit = ((command >> n) & (0x01));
            _data.Write((bit == 1));
            _clock.Write(false);
            _clock.Write(true);      
        }   
    }     
           
    int read_raw_data()     
    {    
        int bit, n;   
        int raw_data = 0;            // go into input mode         
        if (_data.Active == true)            
            _data.Active = false;      
        
        for (n = 0; n < 9; n++)     
        {
            _clock.Write(false);        
            if (_data.Read() == true)         
                bit = 1;              
            else                    
                bit = 0;
            _clock.Write(true);      
            raw_data = raw_data | (bit << n);   
        }       
        // make it a output again         
        if (_data.Active == false)     
            _data.Active = true;        
        return (raw_data);  
    }      
    
    public double readTemp()   
    {
        _reset.Write(false);
        _clock.Write(true);  
        _reset.Write(true);          
        write_command(0x0c); // write config command        
        write_command(0x02); // cpu mode       
        _reset.Write(false);        
        Thread.Sleep(200); //wait until the configuration register is written        
        _clock.Write(true);  
        _reset.Write(true);     
        write_command(0xEE); //start conversion     
        _reset.Write(false);    
        Thread.Sleep(200);
        _clock.Write(true);  
        _reset.Write(true);         
        write_command(0xAA);     
        int raw_data = read_raw_data();
        _reset.Write(false);    
        return ((double)raw_data / 2.0);      
    }   
}


the vb class:
Public Class DS1620
    Private _data As TristatePort
    Private _clock As OutputPort
    Private _reset As OutputPort
    Private _high As Int32 = 125
    Private _low As Int32 = -55

    Sub New(ByVal data As Cpu.Pin, ByVal clock As Cpu.Pin, ByVal reset As Cpu.Pin)
        _data = New TristatePort(data, False, False, Port.ResistorMode.Disabled)
        _clock = New OutputPort(clock, False)
        _reset = New OutputPort(reset, False)
        _data.Active = True
    End Sub


    Public Function ReadTemp() As Double
        _reset.Write(False)
        _clock.Write(True)
        _reset.Write(True)
        write_command(&HC)
        write_command(&H2)
        _reset.Write(False)
        Thread.Sleep(200)

        _clock.Write(True)
        _reset.Write(True)
        write_command(&HEE)
        _reset.Write(False)
        Thread.Sleep(200)

        _clock.Write(True)
        _reset.Write(True)
        write_command(&HAA)
        Dim temp As Integer = read_raw_data()
        _reset.Write(False)

        Return (CDbl(temp) / 2.0)
    End Function

    Private Sub write_command(command As Integer)
        Dim bit As Int32
        For n = 0 To 8 Step 1
            bit = ((command >> n) And (&H1))
            If bit = 1 Then
                _data.Write(True)
            Else
                _data.Write(False)
            End If

            _clock.Write(False)
            _clock.Write(True)
        Next
    End Sub

    Private Function read_raw_data() As Int32
        Dim bit, n As Int32
        Dim raw_data As Int32 = 0

        If _data.Active Then
            _data.Active = False
        End If

        For n = 0 To 9 Step 1
            _clock.Write(False)
            If _data.Read = True Then
                bit = 1
            Else
                bit = 0
            End If
            _clock.Write(True)
            raw_data = raw_data Or (bit << n)
        Next
        If Not _data.Active Then
            _data.Active = True
        End If

        Return raw_data
    End Function
End Class


#3 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 20 January 2012 - 03:58 PM

With these type of sensors there is always a small tolerance difference. Some are 2-5 degrees different. I've never had a temp sensor that was right on the mark.

#4 AdamS

AdamS

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 January 2012 - 05:10 AM

With these type of sensors there is always a small tolerance difference. Some are 2-5 degrees different. I've never had a temp sensor that was right on the mark.



i would expect there to be a difference between sensors, but the same sensor and setup only difference being the language the classes were written in and there was a diference of 13 degrees, C# class was pretty much spot on

#5 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

Posted 21 January 2012 - 05:37 AM


the vb class:

Public Class DS1620
    Private _data As TristatePort
    Private _clock As OutputPort
    Private _reset As OutputPort
    Private _high As Int32 = 125
    Private _low As Int32 = -55

    Sub New(ByVal data As Cpu.Pin, ByVal clock As Cpu.Pin, ByVal reset As Cpu.Pin)
        ...
    End Sub


    Public Function ReadTemp() As Double
        ...
    End Function

    Private Sub write_command(command As Integer)
        Dim bit As Int32
        For n = 0 To 8 Step 1
            ....
        Next
    End Sub

    Private Function read_raw_data() As Int32
        ....

        For n = 0 To 9 Step 1
            ....
        Return raw_data
    End Function
End Class


It looks like your for loop range does not match with C#. Don't know it that's the problem though... The two loops should be like this...

C# -> for (n = 0; n < 8; n++)
VB -> For n = 0 To 7 Step 1

C# -> for (n = 0; n < 9; n++)
VB -> For n = 0 To 8 Step 1

P.S. My DS18B20 sensors are accurate to 0.25 degrees Fahrenheit. According the to spec sheet, if you supply 3VDC - 5.5VDC, your output will be +/- 0.5 Degrees Celcius on the DS1620.

-Valkyrie-MT

#6 AdamS

AdamS

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 January 2012 - 05:41 AM

It looks like your for loop range does not match with C#. Don't know it that's the problem though... The two loops should be like this...

For n = 0 To 7 Step 1

For n = 0 To 8 Step 1

-Valkyrie-MT



the second VB class posted does match up there, i have a feeling that its the math in vb that is the issue...

#7 AdamS

AdamS

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 January 2012 - 06:36 AM

It looks like your for loop range does not match with C#. Don't know it that's the problem though... The two loops should be like this...

C# -> for (n = 0; n < 8; n++)
VB -> For n = 0 To 7 Step 1

C# -> for (n = 0; n < 9; n++)
VB -> For n = 0 To 8 Step 1

P.S. My DS18B20 sensors are accurate to 0.25 degrees Fahrenheit. According the to spec sheet, if you supply 3VDC - 5.5VDC, your output will be +/- 0.5 Degrees Celcius on the DS1620.

-Valkyrie-MT



Valkyrie-MT,

thankyou thankyou thankyou!!

that was it, made that adjustment and all seems to be working well now

i have attatched the class file incase it is of help to anyone else,


regards,
Adam

Attached Files






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.