DS1820 - Help I'm rusty !


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default Board and sensor is OK

    Ok, I found the example on the EasyPIC5 CD (I know it should of been the first place to look, but as it was called one-wire and not temp sensor you can understand why I missed it - I said I was rusty !)

    This works and gives a real temp of 25 degree C which rises when I touch the sensor. I apologise now as the following is not PBP - but hopefully someone can tell me why I was having so much trouble with PBP

    Code:
    ' *
    ' * Project name
    '     Onewire_Test (Interfacing the DS18x20 temperature sensor - all versions)
    ' * Copyright
    '     (c) MikroElektronika, 2005-2008
    ' * Description
    '     After reset, PIC reads temperature from the sensor and prints it on the Lcd.
    '     The display format of the temperature is 'xxx.xxxx°C'. To obtain correct
    '     results, the 18x20's temperature resolution has to be adjusted (constant
    '     TEMP_RESOLUTION)
    ' * Test configuration
    '     MCU             PIC16F887
    '     Dev.Board       EasyPIC5
    '     Oscillator      HS, 08.0000 MHz
    '     Ext. Modules    DS18x20 connected to RE2 pin, LCD_2x16
    '     SW              mikroC v8.0
    ' * NOTES
    '     - Pull up and turning off diode on pin used for one wire bus may be required.
    ' *
    
    program Onewire_Test
    
    '  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor
    '  18S20 9  (default setting can be 9,10,11,or 12)
    '  18B20 12
    const TEMP_RESOLUTION as byte = 9
    dim  text as string[8]
         temp as word
    
    sub procedure Display_Temperature(dim temp2write as Word)
    const RES_SHIFT as byte = TEMP_RESOLUTION - 8
    dim temp_whole as byte
        temp_fraction as Word
    
      text = "000.0000"
    
      ' check if temperature is negative
      if (temp2write and $8000) = 0x8000 then
    
         text[0] = "-"
         temp2write = not temp2write + 1
      end if
    
      ' extract temp_whole     0
      temp_whole = temp2write >> RES_SHIFT
    
      ' convert temp_whole to characters
      if (temp_whole/100) then
         text[0] = temp_whole/100  + 48
      end if
      text[1] = (temp_whole/10) mod 10 + 48             ' extract tens digit
      text[2] =  temp_whole mod 10     + 48             ' extract ones digit
    
      ' extract temp_fraction and convert it to unsigned int
      temp_fraction  = temp2write << (4-RES_SHIFT)
      temp_fraction  = temp_fraction and $000F
      temp_fraction = temp_fraction * 625
    
      ' convert temp_fraction to characters
      text[4] =  temp_fraction/1000    + 48         ' extract thousands digit
      text[5] = (temp_fraction/100) mod 10 + 48         ' extract hundreds digit
      text[6] = (temp_fraction/10) mod 10  + 48         ' extract tens digit
      text[7] =  temp_fraction mod 10      + 48         ' extract ones digit
    
      ' print temperature on LCD
      Lcd_Out(2, 5, text)
    end sub'~
    
    main:
      ANSEL  = 0                              ' Configure AN pins as digital I/O
    	ANSELH = 0
      Lcd_Config(PORTB,3,2,1,0,PORTB,4,7,5) ' Lcd_Init_EP5, see Autocomplete
      Lcd_Cmd(LCD_CURSOR_OFF)
      Lcd_Out(1, 1, " Temperature   ")
      ' Print degree character, 'C' for Centigrades
      Lcd_Chr(2,13,223)
      Lcd_Chr(2,14,"C")
    
      '--- main loop
      while TRUE
        '--- perform temperature reading
        Ow_Reset(PORTE,2)            ' Onewire reset signal
        Ow_Write(PORTE,2,$CC)       ' Issue command SKIP_ROM
        Ow_Write(PORTE,2,$44)       ' Issue command CONVERT_T
        Delay_us(120)
    
        Ow_Reset(PORTE,2)
        Ow_Write(PORTE,2,$CC)       ' Issue command SKIP_ROM
        Ow_Write(PORTE,2,$BE)       ' Issue command READ_SCRATCHPAD
    
        temp =  Ow_Read(PORTE,2)
        temp = (Ow_Read(PORTE,2) << 8) + temp
    
        '--- Format and display result on Lcd
        Display_Temperature(temp)
    
        Delay_ms(500)
      wend
    end.
    Maybe its time to learn a new language

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Just some food for thought. This is the code I use for reading the DS18S20.

    Code:
    ReadDS18S20:
        owout sensor,1,[$CC, $44]                       ' Send Start Temperature Conversion command
        owout sensor,1,[$CC, $BE]                       ' Send Read Temperature command
        owin sensor,0,[STR dq\9]                        ' Retrieve all 9 bytes of data
        RawTemp.Byte0 = dq[0]
        RawTemp.byte1 = dq[1]
        if RawTemp.8 = 1 then                           ' Check if temperature is a negative reading
            SignC = Negative
            RawTemp.lowbyte = RawTemp.lowbyte ^ 255     ' Invert data
            else
            SignC = Positive
    	endif
        dummy = RawTemp.0                               ' Store the half degree indicator bit
        TempC = ((RawTemp.lowbyte) >> 1) * 100          ' Divide raw data by 2 to give real temperature
        TempC = TempC + (dummy * 50)                    ' Add the half degree is present
        if SignC = Negative then                        ' Only proceed if temperature is negative
            if TempC => 1770 then   
                SignF = Negative
                TempF = (TempC + 5000) * 900
                TempF = div32 500
                TempF = TempF - 12200
                return 
                else
                SignF = Positive
                TempF = (TempC + 5000) * 900
                TempF = div32 500
                TempF = 12200 - TempF
                return 
            endif    
        endif
        SignF = Positive
        TempF = TempC * 18 / 10 + 3200
        return
    This is the code I use for the DS18B20. This one also includes the CRC calculation.

    Code:
    ReadDS18B20:
        low portb.7 : low portb.6 : low portb.5
        pause 10
        owout sensor,1,[$CC,$44]
        low RailPullUp
        pause 750
        high RailPullUp
        owout sensor,1,[$CC,$BE]
        owin sensor,0,[STR dq\9]
        rawtemp.Byte0 = dq[0]
        rawtemp.Byte1 = dq[1]
        gosub getcrc
        if SensorError[sensor] = 1 and y < 3 then ReadDS18B20
        gosub ConvertTemp
        if SensorError[sensor] = 1 then
            y = 0
            TempC = 0
            TempF = 3200
            Signc = Positive
            SignF = Positive
        endif
        high portb.7 : high portb.6 : high portb.5
        return
        
    ConvertTemp:
        if NegBit = 1 then BelowZero
        SignC = Positive
        SignF = Positive
        dummy = 625 * RawTemp
        TempC = DIV32 100
        dummy = 0
        dummy = 1125 * Rawtemp
        TempF = DIV32 100
        TempF = TempF + 3200
        return
    
    BelowZero:
        SignC = Negative
        dummy = 0
        RawTemp.byte0 = RawTemp.byte0 ^ 255
        RawTemp.Byte1 = RawTemp.byte1 ^ 255
        dummy = 625 * RawTemp + 1
       	TempC = DIV32 100
        TempF = (TempC + 5000) * 900
        TempF = DIV32 500
        if RawTemp >= 285 then
            TempF = TempF - 12199
            SignF = Negative
            else				
            TempF = 12199 - TempF
            SignF = Positive
    	endif
    	return
    
    '------------------------ Calculate the CRC from Byte 9 ------------------------
    
    GetCRC:
        for x = 0 to 7
        DataByte = dq[x]
        gosub CalcCRC
        next x
        if dq[8] <> CRCCalc then
            SensorError[sensor] = 1
            y = y + 1
            else
            SensorError[sensor] = 0
            y = 0
        endif
        CRCCalc = 0                
        return
    
    '--------------------- CRC Bit Calcuation Method -------------------------------
    
    CalcCRC:
        for i = 0 to 7
        DataBit = CRCCalc.0 ^ DataByte.0
        DataByte = DataByte >> 1
        if DataBit = 0 then Shift
        CRCCalc = CRCCalc ^ $18
    
    Shift:
        CRCCalc = CRCCalc >> 1
        CRCCalc.7 = DataBit
        next i
        return

  3. #3


    Did you find this post helpful? Yes | No

    Default

    "Maybe its time to learn a new language "

    He he, don't let Alain hear you say that :-)
    Pbp is somewhat dated, mE basic example is for the new DS 18S20, note the "S".
    It has slightly reduced resolution from the DS 1820, or DS18B20 as it's now known, therefore the code to interrogate the device, and change the data into "English" :-) is different.
    No, I don't have the solution, but at least I have a clear understanding of the problem, I think.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    The DS1820 and the DS18S20 were software interchangeable as we started out using the 1820 and then switched to the 18S20 when we could not get the other. We changed nothing I our code from my memory though.

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Copy'nPaste View Post
    "Maybe its time to learn a new language "

    He he, don't let Alain hear you say that :-)
    LOL - only kidding, it's taking me all these years to get my head pbp and I'm still getting it wrong - Alain, I'm not going nowhere

    @coke

    Many thanks for posting your code - I'll give it a try now that I know the hardware is sound.

Similar Threads

  1. Please help with 1-wire DS1820
    By hatuan291 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 9th March 2010, 13:51
  2. DS1820 with 16f688
    By jessey in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 23rd May 2009, 05:07
  3. DS1820 display with 7-seg 4 digits
    By chai98a in forum Code Examples
    Replies: 12
    Last Post: - 10th April 2008, 13:12
  4. PIC lcd ds1820
    By wchpikus in forum mel PIC BASIC
    Replies: 2
    Last Post: - 24th May 2007, 14:46
  5. DS1820 again
    By paxmowa in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th January 2006, 14:49

Members who have read this thread : 0

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