Implementing DS18B20.bas into code w/ ADC

Post here to let others know of a project you're working on.

Moderators: Chuckt, Garth, bitfogav

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Tue Aug 07, 2012 3:07 pm

I got the LCD working then the code for the ADC section all working together but when I attempt to insert the DS18B20.bas temperature code into the works all I get are syntax errors.
I don't know proper procedure to insert a subroutine as I have indicated.
Been looking at sample codes trying to decipher why certain sections were inserted where they did. Moved the code around but to no avail.
WHY??

Code: Select all

{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 10/6/2011                                                      *
*  Version : 1.0                                                            *
*  Notes   :compute voltage to temperature. Start with sensor at 50ohms.
*          : 180-200 ohms melts solder                                                               *
*          :                                                                *
*****************************************************************************
}
Device = 18F4520
Clock = 20
// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
Include "DS18B20.bas"
Dim ADVal As Word
Dim LED_g As PORTB.0
Dim LED_r As PORTB.1
Dim Vref As Word
Dim  Vsensor As Word
Dim deg_s As Word         //DESIRED TEMP
Dim drf As Word           //DEFROST TIME
Dim TempA As Word
Dim TempB As ShortInt
      
// Read the AD ports and scale 
Function REF_V() As Word
 result = (ADC.Read(1) +1) /100  //1024 is divide by  tip sensor
End Function
Function sensor() As Word
 result = (ADC.Read(0) + 1)/100  // 100  //1024 is divide by  temp ref desired
End Function
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// I need this routine to read the DS18B20 but keep getting syntax errors
// the LCD, ADC sections all worked before putting the DS18B20 into the code
//syntax says a sub routine or  or function is expected
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Public Sub sens_tep()      
        If DS18B20.Find Then 
        While True 
            Convert 
            GetTemp(TempA, TempB)
   // LCD.WriteAt(1,1,"BX temp = "DecToStr(TempA))
         Wend   
        EndIf
       
        End Sub
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
// Start Of Program...
Input(PORTA.0)                  // DESIRED TEMP
Input(PORTA.1)                  // DEFROST TIMER
Low (LED_g)                     //set lEDs OFF
Low (LED_r)
ADCON1 = %10000000              //set up ADC regesters
ADCON0.7 = 1
ADCON0.6 = 1

       
DelayMS (150)
LCD.Cls
//========================================================

 While true
        // call function REF_V() and assign the result to word variable 'Vref'
        Vref = REF_V()
        Vsensor = sensor() 
             
Select Vref
       Case <1 deg_s = 32
       Case <2 deg_s = 33
       Case <3 deg_s = 34
       Case <4 deg_s = 35
       Case <5 deg_s = 36
       Case <6 deg_s = 37
       Case <7 deg_s = 38
       Case <8 deg_s = 39
       Case <9 deg_s = 40
End Select
Select Vsensor
       Case <1 drf = 10
       Case <2 drf = 12
       Case <3 drf = 14
       Case <4 drf = 16
       Case <5 drf = 17
       Case <6 drf = 19
       Case <7 drf = 21
       Case <8 drf = 23
       Case <9 drf = 25
       
End Select

  
   
   

    // main program loop...
 
   
        LCD.MoveCursor (1,1)
        LCD.Write("Desired temp= ", DecToStr(deg_s))
        DelayMS(250)
        
   // call function sensor() and assign the result to word variable 'drf'
       
        LCD.MoveCursor (2,1)
        LCD.Write("DEFROST = ", DecToStr(drf),"    ")
        DelayMS(250)
        Wend
      
     
  
    {   
   // compare Vsensor and Vref values
        If Vsensor < Vref Then      // not hot enough
            LED_r=1 
            LED_g=0
        Else                        // Vsensor must be >= Vref, so desired temperature
            LED_g=1 
            LED_r=0
        End If
        
    Wend


User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Implementing DS18B20.bas into code w/ ADC

Post by bitfogav » Wed Aug 08, 2012 3:01 am

Ive not used the DS18B20.bas module for Swordfish yet but what if you change the subroutine to something like this?

Code: Select all

Public Sub sens_tep()
    dim myTemp as word
      
        If DS18B20.Find Then  
            DS18B20.Convert 
            mytemp = DS18B20.GetTemp()
            LCD.WriteAt(1,1,"BX temp = ",DecToStr(myTemp))
        EndIf
End Sub
Note that the syntax works now when compiled with Swordfish but ive not tested it with a DS18B20? :roll:
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Wed Aug 08, 2012 3:05 am

I will give that a try. Everytime I change something or location withing the code) I get different syntax.
Will keep you informed.
Been looking at the Swordfish Bible for answers but ?

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Implementing DS18B20.bas into code w/ ADC

Post by bitfogav » Wed Aug 08, 2012 3:10 am

But in your code you not actually calling "sens_tep()" subroutine anywhere?? :?
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Implementing DS18B20.bas into code w/ ADC

Post by bitfogav » Wed Aug 08, 2012 5:05 am

The other thing I just noticed aswell with your code is that your Variables for TempA and TempB types aren't correctly configured.

you have

Code: Select all

Dim TempA As Word
Dim TempB As ShortInt


they should be the other way around like

Code: Select all

Dim TempA As ShortInt
Dim TempB As word
So if you change your original code back to something like this then thats closer to what you had before, so all you have to do is call the subroutine "sens_tep()" in your main programme code, the other thing to note is that I dont think you need the While loop in your subroutine because then your programme will be stuck so to speak within your subroutine, so in other words your LCD will be constantly updating with your Temp data from the DS18B20. Ive also changed your LCD write command so it should output both TempA and TempB. :)

Code: Select all

Public Sub sens_tep()      
    If DS18B20.Find Then 
        While True             ' <- I would remove this while loop
            DS18B20.Convert 
            DS18B20.GetTemp(TempA, TempB)
            LCD.WriteAt(1,1,DecToStr(TempA),".",DecToStr(TempB,4), $BA, "C")        
       Wend                    ' <- I would remove this aswell
    EndIf       
End Sub
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Wed Aug 08, 2012 5:52 am

I may have gotten the TempA and TempB backwards? I tiddied up the code and may have screwed up the TempA and TempB
Will look at that section as well as the other suggestions.
I only want to display the Temperature not the RomID.

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Wed Aug 08, 2012 5:54 am

I want to keep updating the temperature.
This is a beer cooler thermostat. Got to keep that beer cold.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Implementing DS18B20.bas into code w/ ADC

Post by bitfogav » Wed Aug 08, 2012 6:44 am

Well this is what ive quickly put together to read a DS18B20 and display the temp on a LCD, please note mrDeb ive changed the PIC and LCD pins to suit my demo board, it also incorporates your subrouting :)

Code: Select all

Device = 18F452
Clock = 20

// LCD optional settings 
#option LCD_DATA = PORTC.4         
#option LCD_RS = PORTE.0          
#option LCD_EN = PORTE.1

// include modules...
Include "LCD.bas"
Include "convert.bas"
Include "DS18B20.bas"

Dim TempA As ShortInt
Dim TempB As Word
      
Public Sub sens_temp()      
    If DS18B20.Find Then
         DS18B20.Convert
         DS18B20.GetTemp(TempA, TempB)
         LCD.WriteAt(1,1,DecToStr(TempA),".",DecToStr(TempB,1), "'C    ")
    Else
        LCD.WriteAt(1,1,"no device")
    EndIf      
End Sub


// setup PIC...
ADCON1 = $07                

TRISA = %111111    
TRISB = %11111111  
TRISC = %00000000           
TRISD = %00000000    
TRISE = %000       

SetPin(PORTC.0)    ' data pin to DS18B20
DelayMS (150)
LCD.Cls


// main program loop...
While true
    sens_temp()      ' get temp 
    
    ' REST OF YOUR CODE HERE
    
     
    DelayMS(1000)
Wend
To show the LCD output:
pic.jpg
pic.jpg (17.89 KiB) Viewed 22051 times
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Wed Aug 08, 2012 9:04 pm

Thanks Bifogav
I saw this on Digital-DIY and going to give it a go.
No problem changing setpin.
Curious how to know where to put say the setpin statement?
I saw David Barker use it in an OPTION statement.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Implementing DS18B20.bas into code w/ ADC

Post by bitfogav » Wed Aug 08, 2012 9:15 pm

Yes you can setup the setpin in several ways, including using #option statement. all the info is in the help topics of Swordfish IDE mrDeb :)
sfhelp.jpg
sfhelp.jpg (21.75 KiB) Viewed 22045 times
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Fri Aug 10, 2012 12:50 am

Making LOTS of progress this morning.
Depending where I put the sens_tep call sub I get the aDC readings then the temp but then it just stays on Temperature reading.
If I put it after the ADC display statements then it displays the ADC display then gets stuck on reading the temp display.
Going to look at swordfish for a RETURN or?
Thanks for all your help Brad

Code: Select all

{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 8/8/2012                                                       *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
    Device = 18F4520
    Clock = 20
    // some LCD options...
    #option LCD_DATA = PORTD.4
    #option LCD_RS = PORTD.2
    #option LCD_EN = PORTD.3
    // uses LCD and AD libraries...
    Include "LCD.bas"
    Include "ADC.bas"
    Include "convert.bas"
    Include "DS18B20.bas"

    Dim LED_g As PORTc.4
    Dim LED_r As PORTc.5
    Dim Vref As Word
    Dim Vsensor As Word
    Dim Desired_T As Word          //DESIRED TEMP
    //Dim D_Frost As Word          //DEFROST TIME
    Dim deg_s As Word         //DESIRED TEMP
   // Dim drf As Word 
//  Dim myTemp As Word          //DEFROST TIME


         
    // Read the AD ports and scale
    Function Dsired_T() As Word
     result = (ADC.Read(1) +1) /100  //Read DESIRED TEMP Adjustment
    End Function

    Function sensor() As Word
     result = (ADC.Read(0) + 1)/100  // 100  //Read DEFROST TIMER ADJUSTMENT
    End Function
    Dim drf As Word
    
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    // I need this routine to read the DS18B20 buit keep getting syntax errors
    // the LCD, ADC sections all worked before putting the DS18B20 into the code
    //syntax says a sub routine or  or function is expected
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
       
   Sub sens_tep()
        Dim myTemp As Word
        Dim TempA As ShortInt
        Dim TempB As Word
            If DS18B20.Find Then
            While true
                DS18B20.Convert
                DS18B20.GetTemp(TempA,TempB)
                LCD.Cls
                LCD.WriteAt(1,1,"bx temp",DecToStr(TempA),".",DecToStr(TempB,1), "'C    ")
                myTemp = (TempB)
                DelayMS(3000)
                Wend
            EndIf
   End Sub
    
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    {
    Public Sub sens_temp()                                                                     
    If DS18B20.Find Then
         DS18B20.Convert
         DS18B20.GetTemp(TempA, TempB)
         LCD.WriteAt(1,1,DecToStr(TempA),".",DecToStr(TempB,1), "'C    ")
    Else
        LCD.WriteAt(1,1,"no device")
    EndIf     
End Sub
}
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    // Start Of Program...
    Input(PORTA.0)                  // DESIRED TEMP
    Input(PORTA.1)                  // DEFROST TIMER
    Low (LED_g)                     //set lEDs OFF
    Low (LED_r)
    ADCON1 = %10000000              //set up ADC regesters
    ADCON0.7 = 1
    ADCON0.6 = 1
   
    ADCON1 = $07               
    TRISA = %111111   
    TRISB = %11111111 
    TRISC = %00000000           
    TRISD = %00000000   
    TRISE = %000   

 SetPin(PORTB.3)                  // sensor pin    

           
    DelayMS (150)
    LCD.Cls
    //========================================================

     While true
            // call function REF_V() and assign the result to word variable 'Vref'
            Vref =Dsired_T ()           //DESIRED TEMP
            Vsensor = sensor()       //DEFROST TIMER
                 
    Select Vref
           Case <1 deg_s = 32
           Case <2 deg_s = 33
           Case <3 deg_s = 34
           Case <4 deg_s = 35
           Case <5 deg_s = 36
           Case <6 deg_s = 37
           Case <7 deg_s = 38
           Case <8 deg_s = 39
           Case <9 deg_s = 40
    End Select
    Select Vsensor
           Case <1 drf = 10
           Case <2 drf = 12
           Case <3 drf = 14
           Case <4 drf = 16
           Case <5 drf = 17
           Case <6 drf = 19
           Case <7 drf = 21
           Case <8 drf = 23
           Case <9 drf = 25
           
    End Select
    // main program loop...
           
          //  sens_tep()                    //put here and only temp display
          //  DelayMS(3000)
            LCD.MoveCursor (1,1)
            LCD.WriteAt(1,1,"Desired temp= ", DecToStr(deg_s))  // Display DESIRED TEMP
         //   DelayMS(000)
           
     // call function sensor() and assign the result to word variable 'drf'
           
            LCD.MoveCursor (2,1)
            LCD.Write("DEFROST = ", DecToStr(drf),"    ")   //Display DEFROST TIMER
            DelayMS(3000)
            LCD.Cls
          //  LCD.MoveCursor (2,1)
          //  LCD.Write("bx temp = ", DecToStr(myTemp),"    ")   //Display DEFROST TIMER
           sens_tep()                     // put here and ADC displays but then only temp displays
           // need to get outta the sub back to the main code
            
     Wend

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Fri Aug 10, 2012 1:18 am

Found that putting EXIT in the right place works!!

Code: Select all

       
noinline   Sub sens_tep()
        Dim myTemp As Word
        Dim TempA As ShortInt
        Dim TempB As Word
            If DS18B20.Find Then
            While true
                DS18B20.Convert
                DS18B20.GetTemp(TempA,TempB)
                LCD.Cls
                LCD.WriteAt(1,1,"bx temp",DecToStr(TempA),".",DecToStr(TempB,1), "'C    ")
                myTemp = (TempB)
                DelayMS(3000)
                exit    //[b][b] this works[/b][/b]
                Wend
            EndIf
   End Sub

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Fri Aug 10, 2012 2:07 am

SLIGHT issue with temp conversion Bitfogav
according to your picture of 26C your temperature is a chilling 46.8F
C * 9 / 5
at least thats what I think the cal is??
maybe wrong its got to be incorrect??
mine says 23C but its got to be around 70+F

MrDEB
I practically live here!
I practically live here!
Posts: 372
Joined: Fri Feb 18, 2011 4:24 am
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Implementing DS18B20.bas into code w/ ADC

Post by MrDEB » Fri Aug 10, 2012 2:11 am

MY BAD
found a different calculation
C * 1.8 +32

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Implementing DS18B20.bas into code w/ ADC

Post by bitfogav » Fri Aug 10, 2012 2:21 am

Yeah your calculation was wrong, you could also use 26C Multiply by 9, then divide by 5, then add 32

26 * 9 / 5 + 32
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 9 guests