News:

;) This forum is the property of Proton software developers

Main Menu

DS18B20

Started by tolyan249, Today at 10:14 AM

Previous topic - Next topic

tolyan249

How can you find out whether the DS18B20 sensor is connected or not, since it might break or the wires might come loose, and so on?

Thank you.

trastikata

Check the serial number.

Pepe

#2
Add this to Les's program.


'  /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\    \/\\\                                                /\\\          /\\\
'  \/\\\\\\\\\\\/        /\\\\\    /\\\\\\\\\\    /\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'    \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\    \/\\\        \/\\\        /\\\\\\\\\\
'      \/\\\    \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\    \/\\\ /\\  /\\\/////\\\
'      \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\  \//\\\\\\\\/\\
'        \///        \///    \/////    \//////////    \//////////      \/////        \/////    \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Read a DS18B20 temperature sensor with a 12-bit resolution
' And display the temperature in Celsius on a serial terminal
' This version uses integers only for the calculations, but still gives a 0.1 degree temperature resolution.
'
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F26K22                                      ' Tell the compiler what device to compile for
    Declare Xtal = 64                                      ' Tell the compiler what frequency the device is operating at
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600                            ' Setup the Baud rate of USART1
    Declare HRSOut1_Pin = PORTC.6                          ' Setup the pin used for USART1 TX

    Symbol DQ_Pin = PORTC.0                                ' Set the data pin (DQ) for the DS18B20 device
'
' Create a variable for the demo
'
    Dim wTemperature As SWord                              ' Holds the temperature returned from the DS18B20_Read procedure

'------------------------------------------------------------------
' The main program starts here
' Read a DS18B20 device and transmit the temperature in Celsius to a serial terminal
'
Main:
    Internal_Osc_64MHz()                                        ' Set the internal oscillator to operate at 64MHz

    Do                                                          ' Create a loop
        wTemperature = DS18B20_Read()                          ' Read the temperature from the DS18B20 device
        If wTemperature <> 32767 Then                          ' Was the temperature read?
            HRSOutLn "Temperature = ", SDec wTemperature / 10,  ' \
                    ".", SDec wTemperature // 10, " C"        ' / Yes. So transmit the temperature value to a serial terminal
                                  Else
            HRSOutln "Sensor error"
        EndIf
    Loop                                                        ' Do it forever

'-------------------------------------------------------------------
' Read a DS18B20 device with its default 12-bit resolution
' Input    : None
' Output    : Returns the integer temperature value x10, in Celsius
'          : If the device cannot be read, the value returned is 32767
' Notes    : Uses integer calculations, so the temperature returned will need to be divided
'            for the whole and fraction numbers to be displayed
'          : Because the CRC value is not being used from the DS18B20 device,
'            there is no need to read all 9 data bytes from it
'
Proc DS18B20_Read(), SWord
    Dim bTimeOut As Result.Byte0  = $FF                    ' A timeout for the conversion wait loop
    OWrite DQ_Pin, 1, [$CC, $44]                            ' Start a temperature conversion
'
' Wait until the conversion is complete
'
    Repeat                                                  ' Create a loop
        If ORead DQ_Pin, 4 = 1 Then ReadTemp                ' Exit the loop if the conversion is complete
        DelayMS 10                                          ' A small delay within the loop
        Dec bTimeOut                                        ' Decrement the timeout counter
    Until STATUSbits_Z = 1                                  ' Finish the loop when it reaches zero
    Result = 32767                                          ' Return 32767 because the loop completed
    ExitProc                                                ' Exit the procedure prematurely because it timed out
ReadTemp:
    OWrite DQ_Pin, 1, [$CC, $BE]                            ' Set to read the temperature
    ORead DQ_Pin, 0, [Result.Byte0, Result.Byte1]          ' Read the temperature data only from the DS18B20 device
    Result = Result * 10                                    ' Multiply the raw temperature value by 10
    Result = Result / 16                                    ' Convert to Celsius
EndProc

'------------------------------------------------------------------
' Set the PIC18F26K22's internal oscillator to operate at 64MHz
' Input    : None
' Output    : None
' Notes    : Waits for the internal oscillator to become stable
'
Proc Internal_Osc_64MHz()
    OSCCON  = %01110000
    OSCCON2 = %10000100
    Repeat : Until OSCCONbits_HFIOFS = 1        ' Keep looping until the oscillator is stable
    Set OSCTUNEbits_PLLEN                      ' Enable the 4xPLL
EndProc

'------------------------------------------------------------------
' Setup the configuration fuses for 64MHz operation using the internal oscillator for a PIC18F26K22 device
'
Config_Start
    FOSC = INTIO67      ' Internal oscillator block. Port function on RA6 and RA7
    PLLCFG = On        ' Oscillator not multiplied by 4
    PRICLKEN = Off      ' Primary clock disabled
    IESO = Off          ' Oscillator Switchover mode disabled
    Debug = Off        ' Background debugger disabled. RB6 and RB7 Configured as general purpose I/O pins
    WDTEN = Off        ' WDT disabled (Control is placed on SWDTEN bit)
    XINST = Off        ' Standard mnemonic mode enabled
    FCMEN = On          ' Fail-Safe Clock Monitor enabled
    PWRTEN = On        ' PWRT enabled
    BOREN = On          ' Brown-out Reset enabled and Controlled by software (SBOREN is enabled)
    BORV = 190          ' VBOR set to 1.9 V nominal
    WDTPS = 128        ' Watchdog prescaler set to 1:128
    MCLRE = EXTMCLR    ' MCLR pin enabled, RE3 input pin disabled
    HFOFST = Off        ' The system clock is held Off until the HF-INTOSC is stable.
    PBADEN = Off        ' PORTB<4:0> pins are Configured as digital I/O on Reset
    CCP2MX = PORTC1    ' CCP2 input/output is multiplexed with RC1
    CCP3MX = PORTB5    ' P3A/CCP3 input/output is multiplexed with RB5
    T3CMX = PORTC0      ' T3CKI is on RC0
    P2BMX = PORTB5      ' P2B is on RB5
    STVREN = On        ' Stack full/underflow will cause Reset
    LVP = Off          ' Single-Supply ICSP disabled
    Cp0 = Off          ' Block 0 ($000800 - $001FFF) not code-protected
    CP1 = Off          ' Block 1 ($002000 - $003FFF) not code-protected
    CPB = Off          ' Boot block ($000000 - $0007FF) not code-protected
    CPD = Off          ' Data EEPROM not code-protected
    WRT0 = Off          ' Block 0 ($000800 - $001FFF) not write-protected
    WRT1 = Off          ' Block 1 ($002000 - $003FFF) not write-protected
    WRTB = Off          ' Boot block ($000000 - $0007FF) not write-protected
    WRTC = Off          ' Configuration registers ($300000 - $3000FF) not write-protected
    WRTD = Off          ' Data EEPROM not write-protected
    EBTR0 = Off        ' Block 0 ($000800 - $001FFF) not protected from table reads executed in other blocks
    EBTR1 = Off        ' Block 1 ($002000 - $003FFF) not protected from table reads executed in other blocks
    EBTRB = Off        ' Boot block ($000000 - $0007FF) not protected from table reads executed in other blocks
Config_End

tolyan249

#3
If ORead DQ_Pin, 4 = 1 Then
 flagds=1
 else
 flagds=0
 endif

Thank you, this works: 0 — there is a sensor, 1 — there is no sensor.



Thanks for the help

▲