Autor Tema: Sensor temperatura DS18B20  (Leído 5373 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado vinroth

  • PIC10
  • *
  • Mensajes: 7
Sensor temperatura DS18B20
« en: 02 de Diciembre de 2008, 14:46:56 »
Hola a todos!

La verdad soy principiante con los Pic's. Estoy utilizando un sensor de temperatura DS18B20 para mostrar la temperatura en un LCD. El programa lo realizo en Proton. Ya he leído en los foros, y he empleado varios códigos que han funcionado a otras personas; sin embargo, no consigo que la simulación en Proteus me arroje valores en el LCD (se queda en -0.0. C el primer renglón y en 0.062 C el segundo). Es como si no estuviese leyendo o detectando el sensor. A pesar de esto, realicé el montaje con el mismo código de simulación, pero en este caso empieza a mostrarme valores completamente oscilantes...(no sé si es por causa del código, o si hay alg{un problema con la conversión de temperatura).

A continuación muestro el código que estoy utilizando, y adjunto un esquema del montaje en Proteus. Si alguien puede prestarme ayuda, le agradezco de antemano.

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

Device 16F877
XTAL 20

Declare LCD_TYPE ALPHA              ' Type of LCD Used is Alpha
Declare LCD_DTPIN PORTB.4           ' The control bits B0,B1,B2,B3
Declare LCD_RSPIN PORTB.2           ' RS pin on B4
Declare LCD_ENPIN PORTB.3           ' E pin on B5
Declare LCD_LINES 2                 ' Amount of LCD lines
Declare LCD_INTERFACE 4             ' Interface method is 4 bit

PORTB_PULLUPS = true
ALL_DIGITAL = TRUE

Symbol DQ = PORTB.0                 'Place the DS18B20 on bit 1 of PORTA
     
Dim Temp As Word                 'Temperature low/high bytes
Dim C As Byte                    'Status of I/O port bit
Dim NEGPOS As Byte                 'Temperature char "-" or "+"
Dim TempRd As Float

DelayMS 150            
Cls                                  'Clear the LCD screen
      
OWrite DQ, 3, [$CC,$4E,$FF,$FF,$7F]     'Set DS18B20 resolution byte 4; 1F=9bits ... 7F=12bits
      
Again:
      OWrite DQ, 1, [$CC,$44]              'Send calculate temperature command
      Repeat
      DelayMS 25                          'Waiting loop until reading complete
      ORead DQ, 4, [C]                 'Monitor for pin high transition ...
      Until C <> 0                     ' ... reading complete
      OWrite DQ, 1, [$CC, $BE]           'Send read scratchpad command
      ORead DQ, 2, [Temp.LowByte,Temp.HighByte]   
      'Read temperature register word ... ignore other bytes
      
      If Temp.11 = 1 Then
            NEGPOS = "-"
            Temp = ~Temp + 1
        Else
            NEGPOS = "+"
        EndIf
      'Determine sign by testing temperature word bit 11 (ignore similar bits 12...15)
      '... if negative prefix with "-" character & derive 2's complement temperature value
      
      Print At 1,1, NEGPOS, Dec Temp >> 4,".", Dec (Temp.LowByte & %00001111)* 100/16 Dig 1 ," ","C"," "
      'Display sign & lose 4 fractional bits (-1...-4) to display integer temperature ...
      '... right shifting introduces additional "0"s into high byte, so no impact on integer value
      '... similarly no need to manage shifted bits 11-15 as always "0" given 2's complement takes care of "-" temperatures
      'Display fractional temperature by masking low byte for bits -1...-4
      '... and turn 1/16 fractions into a decimal reading (most significant digit only)
      '... add an extra space at the end to overwrite hanging "C" for single digit integer readings
      'DS18B20 spec does not define status of fractional bits -2...-4 for lower resolution readings
      '... however experimentation shows unused bits default to "0"
      '... so code should correctly display decimalised fractions at all resolution levels
      
      TempRd = 0.0625 * Temp
      Print At 2,1, Dec TempRd, " C"
      
      GoTo Again
      
  'End

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

Desconectado dexmax07

  • PIC10
  • *
  • Mensajes: 4
Re: Sensor temperatura DS18B20
« Respuesta #1 en: 16 de Enero de 2010, 21:40:46 »
aqui te dejo un ejemplo que funciona a la perfeccion, espero que te sirva:

DEVICE = 16F877A
XTAL = 4

CONFIG HS_OSC , WDT_OFF , PWRTE_ON , BODEN_OFF , LVP_OFF
delayms 500
all_digital=true
declare lcd_dtpin portb.4


 
SYMBOL  DQ = PortA.0      ' Place the DS1820 on bit-1 of PORTA 
DIM  Temp  AS   WORD        ' Holds the temperature value
dim grad as word
DIM  C  AS   word          ' Holds the counts remaining value
DIM  CPerD  AS   word        ' Holds the Counts per degree C value   
CLS             ' Clear the LCD before we start
Again:
OWRITE  DQ, 1, [$CC, $44]     ' Send Calculate Temperature command
REPEAT
DELAYMS  25        ' Wait until conversion is complete
OREAD  DQ, 4, [C]        ' Keep reading low pulses until
UNTIL  C <> 0        ' the DS1820 is finished.
OWRITE  DQ, 1, [$CC, $BE]     ' Send Read ScratchPad command
OREAD  DQ, 2,[Temp.LOWBYTE,Temp.HIGHBYTE,C,C,C,C,C,CPerD]  ' Calculate the temperature in degrees Centigrade
Temp = (((Temp >> 1) * 100) - 25) + (((CPerD - C) * 100) / CPerD)
temp = temp/8
print at 1,3,"TEMPERATURA"
Print $FE,$40,$07,$05,$07,$00,$00,$00,$00,$00
PRINT AT 2,6, DEC Temp / 100, ".", DEC2 Temp," ", AT 2,10,0,"C"
GOTO Again

Desconectado alexysar

  • PIC16
  • ***
  • Mensajes: 206
    • e-Kit
Re: Sensor temperatura DS18B20
« Respuesta #2 en: 17 de Enero de 2010, 11:04:07 »
Te cuento mi experiencia cuando hice algo parecido.

Cuando trate de simular en proteus siempre me marcaba temperatura -1 en el display, que es lo mismo que marca si el sensor ds18b20 no este conectado. Investigando un poco más me dí cuenta que mi proteus no tenía la librería de simulación del sensor ds18b20 (aunque se que hay gente que tiene versiones de proteus más viejas y lo tiene). Para saber si lo tenes ó no, busca en la ventana Pick Devices de proteus y fijate si en la ventana derecha superior derecha dice VSM DLL model:



en vez el que dice shcematic model no funciona para la simulación



Si no tenes el modelo, se puede bajar del repositor de proteus.



Sobre el códiog no te puedo decir nada porque yo programo en C.

Espero te sirva. Saludos!