1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

One Wire Temperature sensor (DS1820) to PC interface.

Discussion in 'Electronic Projects' started by Pommie, Jan 10, 2009.

Thread Status:
Not open for further replies.
  1. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,641
    Likes:
    109
    Location:
    Michigan, USA
    Nice work Mike. I like it. I see how TRIS is setup for us during the first OwReset call. That's an excellent find for saving 2 words.

    You can reclaim one word in the section between PutDigit and PutOnes in your example. Just change the skpnz to skpz and remove the goto PutIt instruction on the next line (just before the PutOnes label).

    I reworked that Bin2packedBcd routine to make it a little more general purpose (below). It now works correctly for 8 bit input values of 0..255 using only 12 words and 2 variables. I should probably pass it along to James Newton over at PICLIST.

    I setup my temperature variables a bit differently and saved about 4 words (the BinPrep code segment below). I didn't see the need to copy the four fraction bits in TempLo into a new variable. I don't clear the bits in the upper nybble because the AND 0x0F sequence in your FracDigit routine (called PutFraction in my program) does that for us. I use TempHi to hold the 7 bit temperature value (0..125). The AbsFunc code leaves zeros in TempHi bits 7 through 3 so we don't need to clear those bits before copying the low order temperature bits from TempLo into TempHi.

    More later. Mike, K8LH

    Code (text):
            Send232 " "             ; print <space>
    AbsFunc
            movlw   " "             ;
            btfss   TempHi,7        ; negative?
            goto    PutSign         ; no, branch, else
            comf    TempLo,F        ; two's compliment it
            comf    TempHi,F        ;
            incf    TempLo,F        ;
            skpnz                   ;
            incf    TempHi,F        ;
            movlw   "-"             ;
    PutSign
            call    Put232          ; print " " or "-"
    [COLOR=Blue]BinPrep
            movlw   0xF0            ; W = 11110000
            andwf   TempLo,W        ; W = LLLL0000, F = LLLLFFFF
            iorwf   TempHi,F        ; F = LLLL0HHH
            swapf   TempHi,W        ; W = 0HHHLLLL, 0..127
    [/COLOR][COLOR=DarkOrchid]Bin2Bcd
            clrf    tens            ; tens equates to 'OwByte'
            decf    tens,F          ; preset 'tens' to -1
    div10   movwf   ones            ; ones equates to 'TempHi'
            incf    tens,F          ; bump 'tens', 0x00..0x25
            movlw   6               ; using "packed bcd" format
            addwf   tens,W          ; bcd "digit carry"?
            skpndc                  ; no, skip, else
            movwf   tens            ; adjust 'tens'
            movlw   10              ; ones = ones - 10
            subwf   ones,W          ; borrow (negative)?
            bc      div10           ; no, branch, else[/COLOR]
    PrintIt
     
     
    Last edited: Feb 1, 2009
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    Thanks Mike, that got me the words I needed to implement the space before minus output. I also managed to reinstate the space between the ROMid and the scratchpad and turn the WDT back on. I decided the WDT was more important than printing °C (just noticed you dropped that). I did find one more word, moving clr GPIO to the reset routine got rid of the need to clear the data bit after the conversion.

    That Bin2BCD has got to be the most efficient ever. You definitely should post it on the PicList.

    Mike.
     
  3. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,641
    Likes:
    109
    Location:
    Michigan, USA
    Hi Mike,

    Moving the clrf GPIO instruction into OwReset actually seems very natural and intuitive and a perfect fit for the evolving program structure.

    I decided to print the "-" minus sign in the hundreds column since there's no hundreds digit in the negative temperature range of -0.0625° to -55.0000°. I attached my version of the program below (242 words) just in case you or anyone else would like to take a peek.

    On another subject, do you think the TMR0 register could be used as a GPR in a pinch on a future project by setting up Timer 0 in "counter" mode? On the 14 bit core devices you can continue to use the T0CKI pin as an output without affecting the TMR0 register but I'm not sure that the 12 bit core products function the same way.

    Thank you for letting me participate (or should I say "butt in") on your very cool project. I had no idea there were so many challenges when working with a 12 bit core product (missing instructions, only 2 stack levels, etc.) with only 16 bytes RAM. It's been an extremely fun exercise and I learned a lot, as always, by studying your excellent code.

    Regards, Mike

    <added>

    James added my Bin2Bcd routine to the PICLIST source code library. It can be found on the Base Conversions page in the Binary to BCD section.
     

    Attached Files:

    Last edited by a moderator: Feb 12, 2009
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,641
    Likes:
    109
    Location:
    Michigan, USA

    Hi again Mike,

    I started to prototype a board last weekend for my 10F206. Just need to add an ICSP header to it since I don't have a programming adapter for that particular PIC.

    More later...
     
  6. mvs sarma

    mvs sarma Well-Known Member

    Joined:
    Oct 29, 2006
    Messages:
    3,512
    Likes:
    76
    Location:
    Hyderabad, India.
    Interesting Pommie and Mike K8LH !
    I seek help in porting the software into 12F510 as I have them ready. Is it possible?
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    It is definitely possible on the 12F510 as it has 1024 words for the program. We were trying to keep the code to 256 bytes and so it was a little tricky.

    It's late here now and I don't have time to check if any additional code is required. Just try it and if you have any problems I'm sure we can solve them.

    Mike.
     
  8. webbyone

    webbyone New Member

    Joined:
    Jan 27, 2009
    Messages:
    1
    Likes:
    0
    Kind of a noob question, what program are you guys using to compile your assembly code to hex? For the PIC10. Better yet do you plan on posting some of the newer .hex files?
     
  9. mvs sarma

    mvs sarma Well-Known Member

    Joined:
    Oct 29, 2006
    Messages:
    3,512
    Likes:
    76
    Location:
    Hyderabad, India.
    Perhaps it is parasite mode of powering a sensor. but it is with Vdd pin connected to ground. DQ pin should however be only for pull up and data.
     
  10. liucw99

    liucw99 New Member

    Joined:
    Nov 1, 2007
    Messages:
    4
    Likes:
    0
    Hi, Mike,
    I am a begginner.
    I just have one ds18b20, and want to read temp. from it. What should I do ?

    Thanks a lot.
     
  11. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    What microcontroller are you using?
     
  12. liucw99

    liucw99 New Member

    Joined:
    Nov 1, 2007
    Messages:
    4
    Likes:
    0
    I am using PIC 10F200.

    I have only one DS18B20 in my project. I just want to read the temp. from DS18B20, and then action some judgement according to the value of temp.

    Thanks.
     
  13. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Lots of code snippets all over for the 1-wire stuff including this thread.
     
  14. rana jordan

    rana jordan New Member

    Joined:
    May 16, 2009
    Messages:
    4
    Likes:
    0
    thnx alot,,,its very useful
     
  15. bdgtrader

    bdgtrader New Member

    Joined:
    Aug 31, 2009
    Messages:
    4
    Likes:
    0
    Dear,

    I'm very happy with your system.
    I would like to have 8 sensors on 1 bus. can we extend the wires? what is the maximum lenght between the IC's and the controller board? Can we use an external voltage? Do you have a small scheme with the modification of the external voltage?
     
  16. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    You should be able to have 8 sensors on the bus. I've only tried it with 3 and the only reason it wouldn't work is the parasitic power supply, adding an external supply would solve any problems. To use an external supply, remove the diode and connect a 9VDC supply across C2. The cable length should be kept below 100M.

    Mike.
     
  17. bdgtrader

    bdgtrader New Member

    Joined:
    Aug 31, 2009
    Messages:
    4
    Likes:
    0
    pic program

    Hi,

    I have some 10F200 programmed to sell with the apllication above.

    If someone interested i sell them for 2€ / piece programmed.
    I have also 50x DS18S20 for 2.0€ /piece

    I'm from Belgium Europe

    Regards
    Ben
     
  18. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    Hi Ben,

    Did you try putting 8 sensors on a parasitically powered module? Did you manage more? It would be useful to know how many can be added before problems appear. Also, did an external power supply work as described/expected above?

    Mike.
     
  19. bdgtrader

    bdgtrader New Member

    Joined:
    Aug 31, 2009
    Messages:
    4
    Likes:
    0
    Ascii output

    sometimes i'm not getting a fully clearly ASCII output in Hyperterminal or in the VB program. I only connected 1 sensor right now to test.
    Anyone else have this problem? I changed the baudrate but was unsuccesfull.
    Any tip?

    @Mike i will do it when i get a clear line. Everything is ready.
    Maybe the voltage is to low that is also a reason maybe for the garbage output? Regards Ben
     
  20. bdgtrader

    bdgtrader New Member

    Joined:
    Aug 31, 2009
    Messages:
    4
    Likes:
    0
    chinese

    I changed the 10F200 and the other component but i'm still getting garbage in Hyperterminal and in VB.
    It seems that there's a baudrate problem?

    Anyone else have this porblem? under is the output of the one wire sensor in Hyper terminal...

    ￾￾”¢”†¦††¦￾¡‘￾¤˜￾Š“￾￾￾€˜￾‘“™—¡——‘￾€”—￾€”‚”–¦¦¦¦￾’￾￾•”￾Š“￾￾￾￾˜€‘“™—￾——‘€€”–￾￾”¢”–
    ¦†¦¦￾¢‘￾¡…￾Š“￾￾￾￾˜￾￾“™—¡——‘￾€”•￾€”‚”–¦¦¦¦￾”‘￾—˜￾Š“￾￾￾￾˜￾￾“‰—¡——‘￾€””￾￾”¢”–¦¦¦†€¢
    ‘￾’“￾Š“￾￾￾€˜￾‘“™—￾—‡‘￾€”“￾￾„¢”–¦¦¦¦￾“‘￾˜•￾Š“￾€￾￾˜￾‘“‰—¡——‘￾€”’￾￾”‚„–†¦¦¦￾¡‘￾—”￾Š
    “￾€￾￾˜￾‘“™—¡——‘￾€”’￾￾„¢”–¦¦¦¦‘￾￾￾—¦￾Š“￾￾￾￾˜￾￾“™—¡——‘￾€”‘￾￾”‚„–¦¦¦¦￾†‘￾†£￾Š“￾€￾￾˜
    ￾‘“‰—¡——‘￾€”￾￾￾”‚„–†¦¦¦￾£‘￾…˜￾Š“￾￾￾￾ˆ￾‘“™—¡——‘￾€“¦￾￾”¢”–¦¦†¦￾“‘￾￾†￾Š“￾￾￾￾˜￾‘“™‡¡
    ——‘￾€“…€￾”¢”–¦¦¦†￾¢‘￾“¡￾Š“￾￾￾￾˜￾‘“™‡¡‡—‘￾€“…￾￾”¢”–¦†¦¦‘￾‘￾¦•￾Š“￾￾￾￾˜￾‘ƒ™—￾——‘€€“
    ¤￾￾”¢”–¦¦¦¦￾–‘￾—–￾Š“￾€￾￾˜￾‘“‰—¡——‘￾€“£￾￾”‚„–†¦¦¦￾¢‘￾¢£￾Šƒ￾￾￾￾˜￾￾“™—¡——‘￾€“£￾￾”‚„
    –†¦¦¦￾¦‘￾˜—￾Šƒ￾￾￾￾˜￾‘“™—¡——‘￾€“¢￾￾”‚„–†¦¦¦￾”‘￾‡”￾Š“￾￾￾￾˜￾￾“™—¡——‘￾€“¢￾￾”‚„–†¦¦¦￾
    —‘￾’‘￾Šƒ￾￾￾￾˜￾￾“‰—¡——‘￾€“¡￾￾”‚”–¦¦¦¦￾‚￾￾’¦￾Šƒ￾￾￾￾˜￾￾“™—¡——‘￾€“¡￾￾”¢”–†¦¦¦￾¦‘€￾”￾
    Š“￾￾￾€˜￾‘“™—￾—‡‘￾€“™￾€”‚”–†¦¦¦￾’‘￾…˜￾Š“￾￾￾￾ˆ￾‘“™—¡——‘￾€“™￾￾”¢”†¦††¦￾•‘￾“†￾Š“￾￾￾€
    ˜￾‘“™—¡——‘￾€“™￾€”‚”–¦¦¦¦￾ˆ‘￾‚¦￾Š“￾￾￾￾˜￾‘ƒ™—￾——‘€€“ˆ￾￾”¢”–¦¦¦¦￾‚‘￾¡™￾Š“￾￾€￾˜￾‘“™—
    ¡——￾￾€“˜￾￾”¢”†¦††¦￾¥‘￾•†￾Š“￾￾￾￾ˆ￾‘“™—¡——‘￾€“—￾￾”¢”–¦¦†¦￾‘‘￾¢”￾Š“￾￾€￾˜￾‘“™—¡——￾￾€
    “—￾￾”¢”–¦¦¦†￾“‘￾’•￾Š“￾￾￾￾˜￾‘“™—¡——￾￾€ƒ—￾￾”¢”–¦¦¦†€–‘￾¤¡￾Š“￾￾￾￾˜￾‘“™—￾——‘€€“†￾￾”¢
    ”–¦¦¦¦￾™‘￾˜‘￾Š“￾￾￾￾˜￾‘“™‡¡——‘￾€“–€￾”¢”–¦¦¦†￾¢‘￾‘￾￾Š“￾￾€￾˜￾‘“™—¡——￾￾€“–￾￾”¢”–¦¦¦†
    €¥‘￾¥¦￾Š“￾￾￾￾˜￾‘ƒ™—￾——‘￾€“–￾€”‚”–¦¦¦¦‘€‘￾„¦￾Š“￾￾￾￾˜￾‘ƒ™—￾——‘€€“…￾￾”¢”–¦¦¦¦￾“￾￾￾“
    ￾Š“￾￾￾￾˜￾‘“™—¡——￾￾€“•￾￾”¢”–¦¦¦†€•‘￾￾™￾Š“￾￾￾￾˜€‘“™—￾—‡‘￾€“•￾€”‚”–†¦¦¦￾˜‘￾ˆ￾￾Š“￾￾￾
    ￾˜€‘“™—￾——‘￾€“”￾￾”‚„–†¦¦¦￾¡‘€•’￾Š“￾￾￾￾˜￾‘ƒ™—￾——‘￾€“”￾￾„¢”–¦¦¦¦￾ƒ￾￾†˜￾Š“￾￾￾€˜￾‘“™
    —¡—‡‘￾€“”￾￾„¢”–¦¦¦¦￾„￾￾“£￾Š“￾€￾￾˜￾‘“‰—¡——‘￾€“”￾￾”¢”–†¦¦¦‘￾‘￾•™￾Š“￾￾￾€˜￾‘“™—¡——‘￾
    €““￾￾”¢”–¦¦†¦￾‘‘￾¡‘￾Š“￾￾￾￾˜￾￾“™—¡——‘￾€““￾￾”¢”–¦¦¦†￾“‘￾“￾￾Š“￾￾￾￾˜￾‘ƒ™—￾——‘￾€““￾￾”
    ‚”–¦¦¦¦￾•‘￾‰¡￾Š“￾￾￾￾˜€‘“™—¡——‘￾€““￾￾”¢”–¦¦†¦￾—‘￾￾¢￾Š“￾￾€￾˜￾‘“™—¡——￾￾€““￾￾”¢”–¦¦¦
    †￾˜‘￾‘“￾Š“€￾￾￾˜￾‘“™—¡‡—‘￾€“’€￾”¢”–¦¦¦¦￾￾‘￾ƒ‘￾Š“￾￾€￾˜￾‘“™—¡‡—‘￾€“’￾￾„¢”–¦¦¦¦￾¢‘￾￾
    •￾Š“￾￾€￾˜￾‘“™—¡——￾￾€“’€￾”¢”–¦¦¦¦￾„￾￾￾¦￾Š“￾￾￾￾˜￾‘ƒ™—￾——‘￾€“’￾￾”‚„–†¦¦¦￾¥‘￾¦￾￾Š“￾￾
    ￾￾˜￾‘“™—¡——‘￾€“’￾￾”¢”†¦†¦¦￾¦‘￾“¥￾Š“￾￾￾￾˜€‘“™—¡——‘￾€“’￾￾”¢”–¦¦†¦‘￾‘￾£￾￾Šƒ￾￾￾￾ˆ￾‘“
    ™—¡——‘￾€“‘￾￾”¢”–¦¦¦¦￾‘‘￾’—￾Šƒ￾￾￾￾˜￾‘“™—¡——‘￾€ƒ‘￾￾”¢”–¦¦¦†￾“‘￾¢–￾Šƒ￾￾￾￾˜￾‘“™—¡——‘
    ￾€“‘￾￾”¢”–¦¦¦¦￾”‘￾„˜￾Š“€￾￾￾˜￾‘“™‡¡——‘€€“‘￾￾”‚”–†¦†¦￾•‘￾‘£￾Š
     
  21. rajbex

    rajbex Member

    Joined:
    Sep 17, 2009
    Messages:
    174
    Likes:
    5
    Location:
    Williamsburg, VA
    Nice one Mike!
    I have made a temperature meter using DS1820 with PIC16F628A. The result is displayed on LCD though. Check out.
    Experiments with PIC16F628A
    - Raj
     
Thread Status:
Not open for further replies.

Share This Page