Another DS18B20 problem


Closed Thread
Results 1 to 9 of 9
  1. #1

    Default Another DS18B20 problem

    Hi all,

    why is it so hard to get the DS18B20 sensor to work ?
    I'm using a 16F88 pic with internal osc.

    The code i used most of CocaColaKid's ( thanks ) posted here:
    http://www.picbasic.co.uk/forum/showthread.php?t=7533

    I'm getting only a fixed value ( tempd = 1360 and tempc = 8500 ).

    It is not a bad sensor nor a bad pic ( as i tryed a second one ).

    Here is the code i used:

    ;************************************************* ******************************

    ;------------------------ Port Initialization -------------------------------

    SSPCON.5 = 0 ; Disable SSP Module
    TXSTA.5 = 0 ; Disable AUSART Tx
    RCSTA.7 = 0 ; Disable Serial Port
    CMCON = %00000111
    cmd con 254 ; Control byte

    'OSCCON= %01101110
    'OSCCON= %01100000
    OSCCON= %01100100

    INTCON=0
    'PIE1 = 0
    'PIE2 = 0
    'CMCON=7
    CVRCON=0
    CCP1CON=0 ; Disable CCP Module
    OPTION_REG.6 = 0

    '************************************************* ****************************
    'DEFINEs
    '************************************************* ****************************
    'internal OSC used
    DEFINE osc 4
    '************************************************* ****************************
    'ADC

    DEFINE ADC_BITS 10 ' Set number of bits in result ( 8 or 10 bits )
    DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

    '************************************************* ****************************
    'ADC parameters

    'ADCON0=%11000001 ' clock source
    ADCON1=%10000000 ' set VREF Off, porta.0 and .1 analog and left justify result
    ANSEL=%00000001 ' Porta.0 and .1 used by ADC

    '************************************************* ****************************
    ' DEFINE LCD pins
    DEFINE LCD_DREG PORTB
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 3
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 2
    DEFINE LCD_BITS 4

    DEFINE LCD_COMMANDUS 2000
    '************************************************* ****************************

    INCLUDE "modedefs.bas"

    'settings for serial comm off for now

    'DEFINE debug_reg PORTA
    'DEFINE debug_bit 2
    'DEFINE debug_baud 9600
    'DEFINE debug_mode 1

    '************************************************* ****************************
    ' variables

    i var byte
    x var byte
    dummy var word
    databit var bit
    databyte var byte
    va1 var word
    va2 var word
    tempa var word
    tempd var word
    tempc var word
    store var word
    DQ var byte[9]
    crccalc var byte
    negbit var tempd.bit11
    signc var byte
    signf var byte
    crcerror var byte

    'tempin con 0

    CLEAR

    '************************************************* ****************************
    'I/O's

    PORTA=0
    PORTB=0
    TRISA=%00000101
    TRISB=%00000001

    '************************************************* ****************************
    'PINS
    analogin var PORTA.0
    a1 var PORTA.1
    digitalin var PORTA.2
    a3 var PORTA.3
    a4 var PORTA.4
    MCLR var PORTA.5
    led1 var PORTA.6
    led2 var PORTA.7

    but var PORTB.0 '
    memo var PORTB.1 '
    ebit var PORTB.2 '
    rsbit var PORTB.3
    lcd1 var PORTB.4 '
    lcd2 var PORTB.5 '
    lcd3 var PORTB.6 '
    lcd4 var PORTB.7

    '************************************************* ****************************

    start:
    pause 200 'init LCD
    lcdout $fe,1 'clear LCD
    led1=0
    led2=0

    '************************************************* *

    mease:


    if but=1 then
    gosub rec
    endif

    analog:

    ADCIN 0, tempa

    va2=tempa
    va1=va2
    va1 = (va1 */ 5000 ) >> 2


    digital:
    OWOUT digitalin,1,[$CC,$44] 'TELL SENSOR TO START A TEMPERATURE CONVERSION
    OWOUT digitalin,1,[$CC,$BE] 'TELL SENSOR YOU WANT TO READ THE TEMPERATURE
    OWIN digitalin,0,[STR DQ\9] 'RECEIVE TEMPERATURE FROM SENSOR

    tempd.byte0=DQ[0]
    tempd.byte1=DQ[1]

    gosub GetCRC

    gosub convert

    gosub show

    goto mease

    GetCRC:

    for x = 0 to 7 ; Get CRC for each of the 8 bytes
    DataByte = DQ[x] ; Assign array pointer using value of x
    gosub CalcCRC ; Get CRC value
    next x ; Repeat until all bytes are done
    if DQ[8] <> CRCCalc then ; Do the CRC values match?
    CRCError = 1 ; Set flag indicating a problem with this sensor
    else
    CRCError = 0 ; Set flag indicating no problem with this sensor
    endif
    CRCCalc = 0 ; Reset CRC calculation variable
    return

    CalcCRC:
    for i = 0 to 7 ; Do for all 8 bits in DataByte
    DataBit = CRCCalc.0 ^ DataByte.0 ; XOR bit0 of DataByte and CRC
    DataByte = DataByte >> 1 ; Position DataByte for next bit test
    if DataBit = 0 then Shift ; If test bit not set, just shift CRCCalc
    CRCCalc = CRCCalc ^ $18 ; If set, account for EXOR feedback

    shift:

    CRCCalc = CRCCalc >> 1 ; Shift right the CRCCalc byte
    CRCCalc.7 = DataBit ; CRC bit 0 to bit bucket
    next i ; Data bit rotates into CRC bit 7
    return

    convert:
    if negbit = 1 then
    SignC = "-"
    else
    SignC = "+"
    endif
    if SignC = "-" then
    dummy = 0
    tempd.Byte0 = tempd.Byte0 ^ 255
    tempd.Byte1 = tempd.Byte1 ^ 255
    dummy = 625 * tempd + 1
    TempC = DIV32 100
    else
    dummy = 625 * tempd
    TempC = DIV32 100
    endif
    return

    show:

    'lcdout $fe,2,"Analog ",dec va1," "
    lcdout $fe,2,"tempd ",dec tempd," "
    lcdout $fe,$c0,"tempc ",dec tempc," "
    return

    rec:
    led1=1
    return

    end

    ******************************


    Thanks

  2. #2
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    I ran into this issue when I first used this sensor. Make 100% sure you have the DS18B20 hooked up correctly. I wired up my first and second sensor wrong and fried both of them.

  3. #3
    Join Date
    Mar 2006
    Location
    Pennsylvania, USA.
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Bruce at Rentron has a great tutorial on using these chips on his website. It's worth a look!

    Jerry.
    If your oscilloscope costs more than your car...

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Thumbs down

    digital:
    OWOUT digitalin,1,[$CC,$44] 'TELL SENSOR TO START A TEMPERATURE CONVERSION
    OWOUT digitalin,1,[$CC,$BE] 'TELL SENSOR YOU WANT TO READ THE TEMPERATURE
    OWIN digitalin,0,[STR DQ\9] 'RECEIVE TEMPERATURE FROM SENSOR


    missing : the waiting loop for DS ready to send...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Thanks all,

    Alain, is this what you mean?

    digital:

    owout digitalin,1,[$CC,$44] 'TELL SENSOR TO START A TEMPERATURE CONVERSION

    Wait_Up:
    OWIN digitalin,4,[Busy] ' Read busy-bit
    IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!

    OWOUT digitalin,1,[$CC,$BE] 'TELL SENSOR YOU WANT TO READ THE TEMPERATURE
    OWIN digitalin,0,[STR DQ\9] 'RECEIVE TEMPERATURE FROM SENSOR

    .

  6. #6


    Did you find this post helpful? Yes | No

    Default

    I just tested the change but the result is the same

    I've placed a led and added a : toggle led1 right at the beggining of the label digital.

    I can see that if i remove the sensor's data conection the led freezes, so it means that the waiting lines are doing their job.

    However i'm still stuck with the same values on my lcd

    .

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Angry S.e.a.r.c.h.

    As the title says ... make a little SEARCH on this forum and you'll find tons of WORKING examples about DS 1820 and 18B20 or 18S20 ...

    Then just "cut and paste" ... instead of trying to re-invent the wheel !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8


    Did you find this post helpful? Yes | No

    Default No help there :(

    Thanks Alain,

    But that's what i did in the first place...that's how i found CocaColaKid's example... and it didnt helped me much

    .

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ruijc View Post
    But that's what i did in the first place...that's how i found CocaColaKid's example... and it didnt helped me much
    Hate to say it, but it must be a hardware or keyboard problem.

Similar Threads

  1. DS18B20 error reading
    By Gaetano in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th August 2007, 16:21
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. DS18B20, 16F88 problem
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 10th June 2007, 16:11
  4. Reading 5 DS18B20 Problem
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th August 2005, 20:51
  5. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts