DS18B20SetResolution

Syntax:

For Single Channel/Device only. The method assumes a single DS18B20 device on the OneWire bus.

    DS18B20SetResolution ( [DS18B20SetResolution_CONTSTANT] )

Command Availability:

Available on all microcontrollers.

Explanation:

Set the DS18B20 operating resolution.   The configuration register of the DS18B20 allows the user to set the resolution of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.    This method set the operating resolution to either 9, 10, 11, or 12 bits.  

Calling the method with no parameter will set the operating resolution of the DS18B20 to 12 bits.   See example 3 below.

Constants

CONSTANT Operating resolution Temprature resolution

DS18B20_TEMP_9_BIT

9 bits

0.5c

DS18B20_TEMP_10_BIT

10 bits

0.25c

DS18B20_TEMP_11_BIT

11 bits

0.125c

DS18B20_TEMP_12_BIT

12 bits

0.0625c


Example Usage 1

The follow example sets the operating resolution of the DS18B20 to 12 bits.

    #include <DS18B20.h>
    #define DQ PortC.3 ; change port configuration as required
    DS18B20SetResolution ( DS18B20_TEMP_12_BIT )

Example Usage 2

The follow example sets the operating resolution of the DS18B20 to 9 bits.

    #include <DS18B20.h>
    #define DQ PortC.3 ; change port configuration as required
    DS18B20SetResolution ( DS18B20_TEMP_9_BIT )

Example Usage 3

The follow example sets the operating resolution of the DS18B20 to the default value of 12 bits.

    #include <DS18B20.h>
    #define DQ PortC.3 ; change port configuration as required
    DS18B20SetResolution ( )

Working Example Program

The following program will display the temperature on a serial attached LCD.   Change the DS18B20SetResolution () method to set the resolution of a specific setting.

You may need to change the chip, edit/remove PPS, and/or the change LCD settings to make this program work with your configuration.  

#chip 16f18313
#config MCLR=ON
#option Explicit
#include <ds18b20.h>

    'Generated by PIC PPS Tool for GCBASIC
    'PPS Tool version: 0.0.6.1
    'PinManager data: v1.79.0
    'Generated for 16f18313
    '
    'Template comment at the start of the config file
    '
    #startup InitPPS, 85
    #define PPSToolPart 16f18313

    Sub InitPPS

            'Module: EUSART
            RA5PPS = 0x0014    'TX > RA5

    End Sub
    'Template comment at the end of the config file

    'USART settings for USART1
    #define USART_BAUD_RATE 115200
    #define USART_TX_BLOCKING
    #define USART_DELAY OFF

    #define LCD_IO 107   'K107
    #define LCD_WIDTH 20                ;specified lcd width for clarity only.  20 is the default width


    ; ----- Constants
    ' DS18B20 port settings
    #define DQ RA4

; ----- Variables
  dim TempC_100 as LONG   ' a variabler to handle the temperature calculations
  Dim DSdata,WHOLE, FRACT, DIG as word
  Dim CCOUNT, SIGNBIT as Byte


; ----- Main body of program commences here.

    ccount = 0
    CLS
    print "GCBasic 2021"
    locate 1,0
    print "DS18B20 Demo"
    wait 2 s
    CLS

    DS18B20SetResolution ( DS18B20_TEMP_12_BIT )

    do forever
       ' The function readtemp returns the integer value of the sensor
       DSdata = readtemp

       ' Display the integer value of the sensor on the LCD
       locate 0,0
       print hex(ccount)
       print " Ceil"
       locate 0,8
       print DSdata
       print chr(223)+"C"



       ' Display the integer and decimal value of the sensor on the LCD

       ' The function readtemp12 returns the raw value of the sensor.
       ' The sensor is read as a 12 bit value therefore each unit equates to 0.0625 of a degree
       DSdata = readtemp12


       SignBit = DSdata / 256 / 128
       If SignBit = 0 Then goto Positive
       ' its negative!
       DSdata = ( DSdata # 0xffff ) + 1 ' take twos comp


    Positive:
       ' Convert value * 0.0625 by factorisation
       TempC_100 =  DSdata *  625
       Whole = TempC_100 / 10000
       Fract = TempC_100 % 10000


       If SignBit = 0 Then goto DisplayTemp
       Print "-"

    DisplayTemp:
       Locate 3,0
       Print Whole
       Print "."
       Print leftpad( str(Fract),4,"0")

       wait 2 s
       ccount++

    loop