How come my DS18S20 code still works?


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    15

    Default How come my DS18S20 code still works?

    I have been trying for 2 weeks to get my project working properly and now I am at my wits end.
    Part of my project is to read a DS18S20 and display the correct termperature on a 2x16 lcd display. I have searched the forum and all the examples for reading the device are the same so I just used it and it worked great.
    Problem is I cannot understand what the count_remain variable actually does. I am also confused about the count_per_c variable. So I did what any older person would do and just remarked them out of the code to see what would happen. But nothing did. Everything worked the same.

    the code is as follows:

    Hope someone can help

    Wilson

    @ DEVICE pic16F628a, INTRC_OSC_NOCLKOUT,LVP_OFF,WDT_OFF,MCLR_OFF,PROTEC T_OFF
    @ DEVICE pic16F628a, CPD_OFF
    trisa =%00100000
    trisb =%00000000
    cmcon =%00000111 'Comparators Off
    Define LCD_BITS 4
    Define LCD_DREG PORTB
    Define LCD_DBIT 0
    Define LCD_RWREG PORTA
    Define LCD_RWBIT 4
    Define LCD_RSREG PORTA
    Define LCD_RSBIT 3
    Define LCD_EREG PORTA
    Define LCD_EBIT 2
    Define LCD_COMMANDUS 5000
    Define LCD_DATAUS 100
    Define LCD_INITMS 2

    pause 100

    DQ VAR PORTB.5 ' One-wire data pin
    temperature VAR WORD ' Temperature storage
    'I removed the next two lines
    'count_remain VAR BYTE ' Count remaining
    'count_per_c VAR BYTE ' Count per degree C

    loop:
    LCDOUT $FE, 1
    LCDOut $FE, $C0, "Temp ", dec(temperature / 100)," C "

    OWOut DQ, 1, [$CC, $44] ' Start temperature conversion
    pause 750 'NEEDED FOR TEMP STABILIZING

    'and the following three lines
    'waitloop:
    'OWIn DQ, 4, [count_remain] ' Check for still busy converting
    'IF count_remain = 0 Then waitloop

    OWOut DQ, 1, [$CC, $BE] ' Read the temperature

    'as well as the last bits of the next two lines
    OWIn DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]', Skip 4, count_remain, count_per_c]

    temperature = (((temperature >> 1) * 100) - 25)' + (((count_per_c - count_remain) * 100) / count_per_c)

    goto loop

  2. #2
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    I have studied (but not fully understood) the 18S20 data sheet and concluded that all that needs to happen is...

    1: Initiate the device (start the conversion)
    2: Wait until the conversion is complete (pause for at least 750ms)
    3: Read what is in the register.

    I have it running okay. The LCD displays the raw temp i.e. the register value in decimal as well as hex and whether bit 0 is set or not.

    It seems that the value in the 18S20 register is actually 0.5 degrees centigrade units
    so I figured out why not just divide it by 2 to get the temperature.


    Code:
    @ DEVICE pic16F628a, INTRC_OSC_NOCLKOUT,LVP_OFF,WDT_OFF,MCLR_OFF,PROTECT_OFF
    @ DEVICE pic16F628a, CPD_OFF
    trisa =%00100000
    trisb =%00000000
    cmcon =%00000111 'Comparators Off
    Define LCD_BITS 4
    Define LCD_DREG PORTB
    Define LCD_DBIT 0
    Define LCD_RWREG PORTA
    Define LCD_RWBIT 4
    Define LCD_RSREG PORTA
    Define LCD_RSBIT 3
    Define LCD_EREG PORTA
    Define LCD_EBIT 2
    Define LCD_COMMANDUS 5000
    Define LCD_DATAUS 100
    Define LCD_INITMS 2
    
    pause 100
    
    DQ VAR PORTB.5 ' One-wire data pin
    temperature VAR WORD ' Temperature storage
    decimal var byte
    loop:
    	 
    OWOut DQ, 1, [$CC, $44] ' Start temperature conversion
    pause 750 'Needed to complete conversion
    OWOut DQ, 1, [$CC, $BE] ' Read the temperature
    OWIn DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]
    
    'just check if bit0 is set
    decimal = temperature mod 2
    if decimal = 1 then 'if set, it must be .5
    decimal =5
    else
    decimal = 0
    endif
    
    ' raw value is the register value
    lcdout $FE,1, "Raw ", dec(temperature)," ", hex(temperature), " ", dec(decimal)
    
    LCDOut $FE, $C0, "Temp ", dec(temperature /2), ".", dec(decimal), " C"
    
    goto loop

    My LCD displays
    Raw 44 2C 0
    Temp 22.0 C

    When I touch the 18S20 for five seconds I get

    Raw 55 37 5
    Temp 27.5 C

    I am not sure how accurate it is because I do not have a reference temperature.
    I tried putting the 18S20 under my tongue but I got a shock.
    It seems to work okay but I am not convinced it is the correct way to go about it.

    Am I on the right track or just plain lucky that it works?

    Wilson

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


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi,

    At this time, there's another thread running about the 18S20 or 18B20 ...

    just have a look to the methods used ...

    http://www.picbasic.co.uk/forum/showthread.php?t=7079

    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 " !!!
    *****************************************

  4. #4
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    Thanks Alain

    As I mentioned when I started the thread, I have already searched the forum read all the posts about the 18S20.
    The only thing I learned was that someone wrote a piece of code a long time ago and it seems to be the defacto standard- everyone uses it. The samples are the same, comments and all. Thats okay. Cutting and pasting is easy but it doesn't help me learn.

    Wilson

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,651


    Did you find this post helpful? Yes | No

    Talking

    Hi, Wilson

    Learning is on your own whatever the book ...

    so just trying to understand what happens is ... enough !!!

    Why not cut and paste ... if you know what you're pasting ...

    LOL ...

    Following you, ... you also can make it work without Owin ansd Owout ... just toggling pins on the adequate moment ...

    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 " !!!
    *****************************************

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


    Did you find this post helpful? Yes | No

    Default

    Have you looked at Bruce's website? His Rentron Electronics site has some tutorials on using one wire devices, and explains it very well. He used the 18b20 in his examples, but I was able to get an 18s20 working after reading his explanations. I'm not where I can look it up, but I think the count remaining is a way of checking to see if the temperature conversion is completed or not, instead of just waiting a set amount of time like the 'pause' command does. So just pausing 750ms eliminates the need to check if the conversion is completed. Bruce also has an example that uses DIV32 to get better precision, but for my application anything within three degrees was close enough. Here is the link to the first example.

    http://www.rentron.com/PicBasic/PBP1-wire.htm

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

Similar Threads

  1. Manchester coding/decoding Code help
    By financecatalyst in forum Code Examples
    Replies: 0
    Last Post: - 25th August 2009, 19:05
  2. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  3. Why doesn't my code for 18f452 work on 18f252?
    By senojlr in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd December 2005, 02:42
  4. Timer PIC16F57
    By leonel in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 1st July 2005, 08:14
  5. Port control code Yelp!!
    By andyf in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 8th May 2005, 22:36

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