DS18B20

Rasprava o PIC mikrokontrolerima, PIC projekti i drugo vezano za PIC-eve...

Moderators: pedja089, stojke369, [eDo], trax

Post Reply
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

DS18B20

Post by febo26 »

Hi,
zabunom sam s Ebaya narucio DS18B20 umjesto DS1820 i sada mi ovaj program koji imam napisan za DS1820 ne radi.Koliko sam skuzio po datasheetu ds1820 ima rezoluciju 9 bita a DS18B20 ima rezoluciju 12 bita.
Sto treba promjenit u kodu da prepoznaje DS18B20.
zezam se cijelo popdne bez rezultata




Code: Select all

    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 29.11.2012                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
           define OSC 10
           TRISA = %0000000     'Postavi sve pinove porta A kao izlazni
           TRISB = %00000000  'Postavi sve pinove porta B kao izlazne
           TRISC = $00     'Postavi sve pinove porta C kao IZLAZNI
           TRISD = $00     'Postavi sve pinove porta D kao izlazne
           TRISE = %111    'Postavi sve pinove porta E kao ulazne
           ADCON1 = 7      ' PORTA i PORTE su digitalni  ISKLJUCI KOMPARATORE I ANALOGNE ULAZE
         'VARIJABLE ZA MULTIPLEXIRANJE

            BROJ VAR WORD
            BROJ1 VAR BYTE
            BROJ2 VAR BYTE
            BROJ3 VAR BYTE
            BROJ4 VAR BYTE
            I VAR BYTE
        'VARIJABLE MJERENJA TEMPERATURE   
        symbol  DQ = PORTE.2
        V VAR WORD
        T VAR WORD
        temperatura  Var Word         ' Promen.za cuvanje izmer. temp.
        count_remain Var Byte        ' Ostatak
        count_per_c  Var Byte        ' Ostatak po stepenu C
         porta=%0000000
     
     
        OWOut DQ, 1, [$CC, $44]      ' Start merenja temperature, obavezno pre glavne petlje
       
       
        Pocetak:
    ' POCETAK PROGRAMA
        gosub Merenje ' ZOVI SUBRUTINU ZA MJERENJE TEMPERATURE
    'IZDVAJANJE  BROJEVA IZ TEMPERATURE ZA PRIKAZ NA DISPLEJU   
         BROJ1 = T  DIG 3
         BROJ2 = T  DIG 2
         BROJ3 = T  DIG 1
         BROJ4 = t  dig 0
    'MULTIPLEKSIRANJE DISPLEJA I PRIKAZ
            PORTB=BROJ1
            HIGH PORTB.7
            PAUSE 2
            LOW PORTB.7

            PORTB=BROJ2
            HIGH PORTB.6
            PAUSE 2
            LOW PORTB.6

            PORTB=BROJ3
            HIGH PORTB.5
            PAUSE 2
            LOW PORTB.5
             
            PORTB=BROJ4
            HIGH PORTB.4
            PAUSE 2
            LOW PORTB.4
            'NEXT I     
       
      'PALJENJE MINUS PREDZNAKA     
        If v < 20000 then  'AKO JE TEMP VECA OD NULE NE SVIJETLI MINUS                 
        portA.0 = 0                           
        Else
        HIGH PORTA.0  'AKO JE TEMP MANJA OD NULE SVIJETLI MINUS                       
        endif
     
        GOTO Pocetak


        Merenje: 'SUBRUTINA ZA MJERENJE TEMPERATURE
           
                   OWOut DQ, 1, [$CC, $BE]     ' Procitaj izmerenu temperaturu
                   OWIn DQ, 0, [temperatura.LOWBYTE, temperatura.HIGHBYTE, Skip 4, count_remain, count_per_c]
    ' Prikazi temperaturu u DEC obliku

              temperatura = (((temperatura >> 1) * 100) - 25) + (((count_per_c - count_remain) * 100) / count_per_c)
               v=20000-temperatura
            If V >= 20000 then    ' nad 0 C.     
            Temperatura=V-20000
         Else                                   
            Temperatura=20000-V ' pod 0 C
            EndIf
           
           
    'VARIJABLA T ZA IZDVAJANJE BROJEVA IMA VRIJEDNOST TEMPERATURA             
            t=temperatura
           
       
    ' Malopre procitana temperatura, pa je meri opet...
                   OWOut DQ, 1, [$CC, $44]
        Return
       
    end
User avatar
MHN
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 350
Joined: 11-03-2005, 22:02
Location: Sarajevo

Re: DS18B20

Post by MHN »

O tome je Feko pisao i radio citav termostat, a to imas ovdije http://www.elektronika.ba/forum/viewtop ... =30&t=5005
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

Re: DS18B20

Post by febo26 »

Evo ga,
kod za citanje temperature s DS18B20 senzora i prikaz na led displayu u multiplexu

Code: Select all

'****************************************************************
'*  Name    : termometer i termostat                                      *
'*  Author  : [fric zeljko]                    *
'*  Notice  : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 29.11.2012                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                  *
'****************************************************************

'#config
'_config _HS_OSC & _wdt_on & _pwrte_off & _cp_off & _borren_off
'#endconfig

       define OSC 10 ' u programatoru postavi oscilator na HS 
       TRISA = %111110     
       TRISB = %00000000  'Postavi sve pinove porta B kao izlazne
       CMCON = 7      ' PORTA i PORTE su digitalni  ISKLJUCI KOMPARATORE I ANALOGNE ULAZE



     'VARIJABLE ZA MULTIPLEXIRANJE

        BROJ VAR WORD
        BROJ1 VAR BYTE
        BROJ2 VAR BYTE
        BROJ3 VAR BYTE
        BROJ4 VAR BYTE
        I VAR WORD
'konstante za rezoluciju ds18B20 senzora
DS18B20_9bit  	CON %00011111   	' 93.75ms, 0.5°C
DS18B20_10bit 	CON %00111111   	' 187.5ms, 0.25°C  <-- My favorite
DS18B20_11bit 	CON %01011111   	' 375ms,   0.125°C
DS18B20_12bit 	CON %01111111   	' 750ms,   0.0625°C  (default)
  
    'VARIJABLE MJERENJA TEMPERATURE   
    symbol  DQ = PORTA.4
    V VAR WORD
    T VAR WORD
    temperature  Var Word         ' Promen.za cuvanje izmer. temp.
     TempC			Var Word
     Float			Var Word
    porta=%000000  'postavi a port na 0 
    
    
    
    
'postavi senzor u mod za citanje temperature
 OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_12bit]    'Skip ROM search and write N_bits
 pause 100                                               '  resolution to scratch pad
 OWOut DQ, 1, [$CC, $44]      ' Start merenja temperature, obavezno pre glavne petlje
 pause 100
    

 Pocetak:

    gosub Merenje ' ZOVI SUBRUTINU ZA MJERENJE TEMPERATURE
'IZDVAJANJE  BROJEVA IZ TEMPERATURE ZA PRIKAZ NA DISPLEJU   
     BROJ1 = T  DIG 3
     BROJ2 = T  DIG 2
     BROJ3 = T  DIG 1
     BROJ4 = t  dig 0
   
'MULTIPLEKSIRANJE DISPLEJA I PRIKAZ
FOR I = 1 TO 1000
        PORTB=BROJ1
        if broj1 = 0 then 
        low portb.7
        else
        HIGH PORTB.7
        endif
        PAUSE 5
        LOW PORTB.7

        PORTB=BROJ2
        HIGH PORTB.6
        PAUSE 5
        LOW PORTB.6

        PORTB=BROJ3
        HIGH PORTB.5
        PAUSE 5
        LOW PORTB.5
         
        PORTB=BROJ4
        HIGH PORTB.4
        PAUSE 5
        LOW PORTB.4
NEXT I
              
GOTO Pocetak




Merenje: 
    OWOUT   DQ, 1, [$CC, $BE ]                       ' Skip ROM search & read scratchpad 														memory
    OWIN    DQ, 2, [Temperature.Lowbyte, Temperature.Highbyte]' Read two bytes / end comms
                                      
IF Temperature.15 THEN	
Temperature = ~Temperature + 1
ENDIF
   											
    TempC =  ( Temperature & $7FF ) >> 4   				' Partie Entičre '4
                  
    Float = ((Temperature.Lowbyte & $0F ) * 25 )>> 2    ' Partie décimale 100čmes
    
    Temperature = TempC*100 + Float						'Préparation 1/100 degrés
    


        
        
'VARIJABLA T ZA IZDVAJANJE BROJEVA IMA VRIJEDNOST TEMPERATURA             
        t=temperature
       
    
' Malopre procitana temperatura, pa je meri opet...
         
               OWOut DQ, 1, [$CC, $44 ]  
    Return


end
User avatar
MHN
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 350
Joined: 11-03-2005, 22:02
Location: Sarajevo

Re: DS18B20

Post by MHN »

Koji si PIC koristio, koji kristal koristis jel od 10MHz?
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

Re: DS18B20

Post by febo26 »

10 mhz kristal,PIC16F628A,bcd dekoder cd4511 za prikaz multiplexa.
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

Re: DS18B20

Post by febo26 »

Napravio sam termometar s DS1820 chipom s prikazom na LCD displeju i stvar radi Ok.
Sada me zanima na koji nacin da ispisem na LCD-u "No Sensor" kada nije prikljucen DS1820.
Na koji nacin da prepoznam da nema prikljucenog senzora.

i jos jedno pitanje,kako napisati na pocetku programa konfiguraciju procesora tako da svaki pute ne moram postelavati FUSE bitove u programatoru.
Probao sam ovu i ne radi kompajler javi gresku

Code: Select all

'@ DEVICE pic16F877, HS_OSC ' System Clock Options
'@ DEVICE pic16F877, WDT_ON ' Watchdog Timer
'@ DEVICE pic16F877, PWRT_ON ' Power-On Timer
'@ DEVICE pic16F877, BOD_FF ' Brown-Out Detect
'@ DEVICE pic16F877, LVP_OFF ' Low-Voltage Programming
'@ DEVICE pic16F877, CPD_OFF ' Data Memory Code Protect
'@ DEVICE pic16F877, PROTECT_OFF
' Program Code Protection
'@ DEVICE pic16F877A, WRT_OFF ' Flash Memory Word Enable
yt1nvs
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 286
Joined: 30-03-2009, 16:54

Re: DS18B20

Post by yt1nvs »

Ako je PBP3 onda nesto ovako primer na brzinu.

#CONFIG
__config _XT_OSC & _WDT_OFF & _PWRTE_ON &_CP_ON & _MCLRE_OFF & _BODEN_OFF & _LVP_OFF

#ENDCONFIG
yt1nvs
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 286
Joined: 30-03-2009, 16:54

Re: DS18B20

Post by yt1nvs »

Sto se tice prikaza na LCD da senzor nije OK moze ovako ja sam koristio 18B20 ali mislim da moze i kod 1820

main:

Output DQ ' Make Pin Output
DQ=0 ' OneWire line Low
PauseUs 480 ' Keep down for 480 µS
Input DQ ' Make Pin Input
PauseUs 70 ' Wait 70 µS
If DQ=1 then ' No presence pulse from DS1820
LcdOut $FE, $1,"SNZ GRESKA!" ' Show message
Pause 500 ' Wait 0.5 Sec.
Goto main ' Check again
EndIf

Ako prodje ovo gore onda komuniciras SNZ klasicno sa OWIN OWOUT
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

Re: DS18B20

Post by febo26 »

Hvala puno na pomoci
radi bez greske
User avatar
electron
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 225
Joined: 28-02-2009, 19:20
Location: Serbia

Re: DS18B20

Post by electron »

Ima li neko praktičnog iskustva na koje dužine vodovi mogu najduže ići kod DS18B20, a da radi stabilno, tj da radi, bilo da je spojen dvožično ili trožično kao i više njih u paraleli, na jednom pinu PIC-a?
User avatar
electron
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 225
Joined: 28-02-2009, 19:20
Location: Serbia

Re: DS18B20

Post by electron »

Evo da odgovorim na pitanje ... Prakticno sam probao sa kablom 4x0,35 LIFYY11Y, bez širma (ne oklopljen) i radi na 25m duzine i Pull UP 4,7K kao i 2x0,75 So-LiYCY oklopljen 15m, DS18B20 spojen trozicno radi sasvim O.K.
User avatar
electron
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 225
Joined: 28-02-2009, 19:20
Location: Serbia

Re: DS18B20

Post by electron »

U kod termostata sam ubacio da mi uz prikaz temperature u donjem redu LCD odbrojava neko vreme, u gornjem redu LCD, evo primer deo koda:

Code: Select all

Pauza:
 
 read 9,tmp1
 read 10,tmp2
 I.lowbyte =tmp1
 I.highbyte=tmp2 
 
 
 DOZ=0
 GR=0
 ve=1
 for j=0 to I
 if ST=0  then Blokada
 if j=I then  I=0

Output DQ                      ' Make Pin Output
DQ=0               ' OneWire line Low
PauseUs 480                            ' Keep down for 480 µS  
Input DQ                               ' Make Pin Input
PauseUs 70                             ' Wait 70 µS
If DQ=1 then                           ' No presence pulse from DS1820   
  LcdOut $FE, $1, "** No Sensor! **"    ' Show message
  Pause 500                             ' Wait 0.5 Sec. 
  Goto Pauza                         ' Check again
EndIf

'*****************************************************************************
' Temp reading
'*****************************************************************************

OWOut DQ, 1, [$CC, $44 ]             ' Start temperature conversion

waitloop1: 

   WHILE NOT DQ
   WEND
      
    OWOUT   DQ, 1, [$CC, $BE ]                       ' Skip ROM search & read scratchpad                                           memory
    OWIN    DQ, 2, [Temperature.Lowbyte, Temperature.Highbyte]' Read two bytes / end comms
      
Convert_Temp1:                                       

   IF Temperature.15 THEN
   
       Temperature = ~Temperature + 1
      Twist = 1
      
   ENDIF
                                    
    TempC =  ( Temperature & $7FF ) >> 4               ' Partie Entiere '4
                  
    Float = ((Temperature.Lowbyte & $0F ) * 25 )>> 2    ' Partie décimale 100emes
    
    Temperature = TempC*100 + Float                  'Préparation 1/100 degrés
    
IF Twist THEN
   
  V= 10000 - Temperature                ' 25 C=12500  0 C=10000  -10 C=9000
  Twist = 0 
else
  V= 10000 + Temperature
EndIf
'----------------------- Upravljanje relejima ----------------------------------
Read 0, B1                              ' Read TargetTemp MSB
Read 1, B2                              ' Read TargetTemp LSB
TargetTemp=B1*256+B2                    ' Calculate TargetTemp value (Default=20.0 C.)

If V > TargetTemp + Hyst then Mainloop2          ' Above Target temperature + Hysteresis
'-------------------------------------------------------------------------------
GoSub SelectSign                       ' +/blank/- Sign 
GoSub DisplayTemp                      ' Temperature to LCD
LCDOUT $FE,$80, "Pauza   :   "   ,dec3 j
pause 1000
next j
goto Pauza
Medjutim, temperaturu osvezava svake sekunde prilikom odbrojavanja brojaca, kako to resiti drugacije?
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

Re: DS18B20

Post by febo26 »

Da li se moze na neki nacin zastiti ulaz od prenapona na PIC procesoru na koji je spojen DQ pin od DS18b20 cipa
Duzina kabla na koji su spojeni senzori je oko 30 metara a kabel nije oklopljen vec obicni industrijski trozilni kabel 3x0,75mm.Senzori se nalaze na solarnim kolektorima pa u slucaju grmljavine da mi ne izgori ulaz na PICu, da se ipak neki manji prenaponi ublaze.
User avatar
pedja089
Administrator sajta
Administrator sajta
Posts: 7876
Joined: 20-02-2007, 14:50
Location: Beočin -Srbija

Re: DS18B20

Post by pedja089 »

Otpor prema DS, TVS dioda, i pullup otpor prema picu i to bi trebalo da je to.
Otpori reda 30-50 oma mislim da bi bili ok.
Nisam probao, ali ne zaboravi da i vcc treba da zastitis.
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

Re: DS18B20

Post by febo26 »

Ovo je kod s kojim citam temperaturu s 16 komada senzora

Code: Select all

DQ      var PORTE.2
count_remain    var byte
R_Temp      VAR    WORD     ' RAW Temperature readings
TempC       VAR WORD        ' Temp in deg C
Float       VAR WORD        ' Holds remainder for + temp C display
Cold_Bit    VAR R_Temp.Bit11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Real_Cold   CON 1           ' Define Real_Cold = 1
Deg         CON 223         ' Data to display Deg ° symbol
Sign        VAR BYTE        ' +/- sign for temp display
Dummy       VAR BYTE        ' Dummy for Div32

'lcdout $fe,1
OWOut DQ, 1, [$CC,$4E, $00,$00,$3F] ;set DS18B20 at 10bit 

main11: OWOut DQ, 1, [$CC, $44]       ' Start temperature conversion

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
    OWIn DQ, 2, [R_temp.LOWBYTE, R_temp.HIGHBYTE]', Skip 4, count_remain, count_per_c]
    gosub convert_temp
    
goto CIKLUS
    
Convert_Temp:                 ' +32.0 to +257 F 
    IF Cold_Bit = Real_Cold THEN COLD  ' If Cold_Bit = 1, it's below "0" deg C
    Sign  = "+"
    Dummy = 625 * R_Temp      ' Multiply to load internal registers with 32-bit value
    TempC = DIV32 10          ' Use Div32 value to calculate precise deg C
    Dummy = 1125 * R_Temp
    TempC  = (R_Temp & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
    Float = ((R_Temp.Lowbyte & $0F) * 625)/100 ' Lower 4-bits of result * 625
    lcdout $fe,$94, " TempC = ",Sign,DEC TempC,".",DEC2 Float,Deg,"C "
    RETURN

COLD:                      ' Display full range -C to -F conversion
    Sign   = "-"            ' Display - symbol for negative temp
    Dummy  = 625 * ~R_Temp+1' Multiply to load internal registers with 32-bit value
    TempC  = DIV32 10       ' Use Div32 value to calculate precise deg C
    lcdout $fe,$94, " TempC = ",Sign,DEC TempC DIG 4,DEC TempC DIG 3,".",DEC TempC,Deg,"C "
    RETURN
    
END
imam problem u tome sto moram da usporedim temperature s pojedinih senzora i to mi nije problem ako su temperature pozitivne



Primjer,
trebaju mi samo cijeli brojevi temperature ,decimalna mjesta me ne zanimaju
neznam kako da drugacije objasnim

Code: Select all

ako je temp pozitivna, 
temperatura1 = TempC    'temp s prvog senzora
temperatura2 = TempC   'temp s drugog senzora

if temperatura1 < temperatura2 then pali relej 
to funkcionira u pozitivnim temp

-------------------------------------------------------------------------------------------------------------------------------
e sad ako je temp negativna onda imam problem jer neznam kako da napravim cijeli broj iz ovoga ,jer se na ovaj nacim temp prikazuje na LCD displeju

Code: Select all

lcdout $fe,$94, " TempC = ",Sign,DEC TempC DIG 4,DEC TempC DIG 3,".",DEC TempC,Deg,"C "
TempC dig 4 i TempC dig 3 su cijeli brojevi za prikaz temperature i kako da njih smjestim u neku promjenjivu s kojom mogu dalje radit sto hocu.
Post Reply