News:

;) This forum is the property of Proton software developers

Main Menu

DS1820 temperature sensor

Started by Dave-S, Nov 18, 2024, 04:24 PM

Previous topic - Next topic

Dave-S

DS1820 temperature sensor trying to get this to work, using code from mamual.

Device = 18F47K40     
' Select the device to compile for
Declare Xtal = 64' Tell the compiler the device will be operating at 64MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
Symbol DQ_Pin = PORTA.1     
Dim wTemperature as Word     
Dim bCounts as Byte       
Dim bCPerD as Byte       
Do
' Place the DS1820 on bit-1 of PORTA 
' Holds the temperature value
' Holds the counts remaining value
' Holds the Counts per degree C value 
Owrite DQ_Pin, 1, [$CC, $44]  ' Send a Calculate Temperature command
Repeat
DelayMs 25       
' Wait until conversion is complete
Oread DQ_Pin, 4, [bCounts]  ' Keep reading low pulses until
Until bCounts <> 0     
' the DS1820 is finished.
Owrite DQ_Pin, 1, [$CC, $BE]  ' Send a Read ScratchPad command
Oread DQ_Pin, 2,[wTemperature.LowByte, wTemperature.HighByte,_
   bCounts, bCounts, bCounts, bCounts, bCounts,_
   bCPerD]
'
' Calculate the temperature in degrees Centigrade
'
wTemp = (((wTemperature >> 1)*100) - 25) + (((bCPerD - bCounts)*100) / bCPerD)
HRsoutLn Dec wTemperature / 100, ".", Dec2 wTemperature, " C"
 DelayMs 500
Loop

Output I get is 655.35 C

Any ideas why it is not working correctly, have it connected as per diagram in manual.

Thanks David

RGV250

Hi Dave,
Have you got actually an 1820 or is it 18B20 or 18S20. From memory I think they are different regards the resolution.

Bob

xvovanx

#2
Hello David!

It prog fine work DS18B20



TEMP: 
                             
OWrite DQ,1,[$CC,$44]       
Repeat                                                                                                                     
    DelayMS 10                 
    ORead DQ,4,[TEMP_OK]                                             
Until TEMP_OK>0             
OWrite DQ,1,[$CC,$BE]       
ORead DQ,2,[TEMP_C.LowByte,TEMP_C.HighByte]
                         
If TEMP_C.11=1 Then         
    TEMP_MINUS=1            ' flag - temper
    TEMP_C=(~TEMP_C+1)/16   
    TEMP_REAL=0-TEMP_C     
Else                        ' + temper
    TEMP_MINUS=0                                           
    TEMP_C=TEMP_C/16       
    TEMP_REAL=TEMP_C
EndIf

Return

flosigud

First there was 1820 And then 18S20 which was similar but not same. Then they dropped the S so you cant really see what you have in the hand. The B type returns signed results and wont function with code for non B type. In case it is a B type there is a code below that used to work many moons ago.;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F2520

Declare Reminders Off
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
Config OSC = INTIO7 ;Internal oscillator block, CLKO function on RA6, port function on RA7
Config FCMEN = OFF ;Fail-Safe Clock Monitor disabled
Config IESO = OFF ;Oscillator Switchover mode disabled
Config PWRT = OFF ;PWRT disabled
Config BOREN = OFF ;Brown-out Reset disabled in hardware and software
Config BORV = 3 ;Minimum setting
Config WDT = OFF ;WDT disabled (control is placed on the SWDTEN bit)
Config WDTPS = 32768 ;1:32768
Config CCP2MX = PORTC ;CCP2 input/output is multiplexed with RC1
Config PBADEN = OFF ;PORTB<4:0> pins are configured as digital I/O on Reset
Config LPT1OSC = OFF ;Timer1 configured for higher power operation
Config MCLRE = On ;MCLR pin enabled; RE3 input pin disabled
Config STVREN = OFF ;Stack full/underflow will not cause Reset
Config LVP = OFF ;Single-Supply ICSP disabled
Config XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
Config Debug = OFF ;Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
Config Cp0 = OFF ;Block 0 (000800-001FFFh) not code-protected
Config CP1 = OFF ;Block 1 (002000-003FFFh) not code-protected
Config CP2 = OFF ;Block 2 (004000-005FFFh) not code-protected
Config CP3 = OFF ;Block 3 (006000-007FFFh) not code-protected
Config CPB = OFF ;Boot block (000000-0007FFh) not code-protected
Config CPD = OFF ;Data EEPROM not code-protected
Config WRT0 = OFF ;Block 0 (000800-001FFFh) not write-protected
Config WRT1 = OFF ;Block 1 (002000-003FFFh) not write-protected
Config WRT2 = OFF ;Block 2 (004000-005FFFh) not write-protected
Config WRT3 = OFF ;Block 3 (006000-007FFFh) not write-protected
Config WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
Config WRTB = OFF ;Boot block (000000-0007FFh) not write-protected
Config WRTD = OFF ;Data EEPROM not write-protected
Config EBTR0 = OFF ;Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
Config EBTR1 = OFF ;Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
Config EBTR2 = OFF ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
Config EBTR3 = OFF ;Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
Config EBTRB = OFF ;Boot block (000000-0007FFh) not protected from table reads executed in other blocks
Endasm-
Declare Reminders On

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
       
        All_Digital 1
        PLL_Req 1
        Xtal 8

        OSCCON=%01110010                   ' Osc = 8MHz
        'OSCCON=%01101000                    ' Osc = 4MHz
        'OSCCON=%01011000                   ' Osc = 2MHz
        'OSCCON=%01001000                   ' Osc = 1MHz
        While OSCCON.2=0:Wend               ' Wait until osc is stable

    Optimiser_Level 3
        Hserial_Baud = 9600
    Hserial_RCSTA = %10010000           ' Enable serial port and continuous receive
    Hserial_TXSTA = %00100000           ' Enable transmit and asynchronous mode
    Hserial_Clear = 1                   ' Enable Error clearing on received characters

        Dim temp As Word
        Dim c As Byte
        Dim sign As Byte
        Symbol DQ = PORTA.4                 ' one-wire data pin
       
        While 1=1
            OWrite DQ, 1, [$CC, $44]        ' Start temperature conversion
            While ORead DQ, 4 = 0 : Wend    ' Wait for end of conversion
            OWrite DQ, 1, [$CC, $BE]        ' Read the temperature
            ORead DQ, 0, [temp.LowByte, temp.HighByte, c,c,c,c,c,c,c]       
            sign = "+"                    ' Assume positive sign
If temp.15=1 Then               '
                sign = "-"
                temp = ~temp + 1 ' invert and add 1
            EndIf           
            temp = temp / 10 *625
            'temp = temp * 625
            HRSOut 13,sign, Dec temp/ 1000,"." ,Dec3 temp,13
            DelayMS 1000                    ' Display once a second
        Wend

Dave-S

Thanks for the replies.
Yes it is a "B" have tried the code but I get 0 zero

thanks David

Fanie

Try this

                Dim Temp As Word               ' temp genl use reg
        Dim Tin As Word                ' Temperature input

        Tin = 0
        OWrite DQ , 1 , [$CC , $44]       ' skip ROM command & convert temp       
        While ORead DQ , 4 = 0 : Wend ' Check for still busy converting 
        OWrite DQ , 1 , [$CC , $BE]      ' skip ROM command & send read scratchpad command
        ORead DQ , 2 , [Tin.LowByte , Tin.HighByte]   'read temp
        Temp = Tin
        Temp = Temp * 0.645

       

Dave-S

I get Temp = 42270 both Low and High show 255

David

Craig

Hi Dave

Do you have a Resistor 4K7 Between Data Pin and +3v on the DS18B20 ?
Craig

Dave-S


Fanie

#9
Dave-S, I have used that exact code in hundreds of boards, 5v as well as 3V3, and it worked every time.
If I remember right, in one app I replaced the 4k7 pull-up resistor with a 2k7 and that worked then.

Try a simple routine where you can see that you get the correct temperature first, then modify to get what you want from it.  Almost like the flashing LED first before you do more complicated.

ORead DQ , 2 , [Tin.LowByte , Tin.HighByte]   'read tempIn the above the more-than-8-bits-long data is put into the word variable Tin.  It is now one value, you don't have to fiddle with high or low values.

If you heat the sensor up (ie with your hand), what happens to the value ?
If it does go more with temperature, then perhaps you process incorrectly to get the correct value.

Fanie

#10
Here is basically the same code I used in an agriculture project.  I make my own LED displays because they are further visible than LCD, so the digits are extracted and modified in a lookup table to show the digits correctly.

TRISA = %00000000               ' Configure I/O, all outputs
Symbol DQ = PORTA.2 ' One Wire I/O
Dim Tin As Word

Start0:
        Sound Buz ,[111,40,0,2,105,20]
        TRISC = %00000000
        Buz = 0
       
        OWrite DQ , 1 , [$CC , $4E , $7F , $80 , %01111111]
        DelayMS 2000
       


Start1:
       
       

        OWrite DQ , 1 , [$CC , $44]       ' skip ROM command & convert temp
       
        While ORead DQ , 4 = 0 : Wend ' Check for still busy converting
       

        OWrite DQ , 1 , [$CC , $BE]      ' skip ROM command & send read scratchpad command
        ORead DQ , 2 , [Tin.LowByte , Tin.HighByte]   'read temp
       
        Temp = Tin
       
        Temp = Temp * 100
        Temp = Temp / 163
       
        Tin = Temp       
                               
        Dis1 = Dig Tin , 0                    ' Extract values for display
        Dis2 = Dig Tin , 1
        Dis3 = Dig Tin , 2
        If Triac = 1 Then Dis4 = 10
        If Triac = 0 Then Dis4 = Dig Tin , 3  ' Display 3 bars when triac is on
       
               
        DelayMS 100
       
        GoSub Display
       
        DelayMS 400

flosigud

I have now tested my code and it works as expected. If I disconnect DQ I get 0.000

Fanie

What is the temperature you are reading, or what is the value you get from the 18B20 ?

How do you see it ?  Do you have an LCD ?

flosigud

Room temperature and the temperature in my fridge. I use only the serial port. It is the code I posted above, slightly amended after seeing yours.