Autor Tema: Sensor digital de temperatura DS18B20 y PIC16F88  (Leído 3004 veces)

0 Usuarios y 3 Visitantes están viendo este tema.

Desconectado aitorsp

  • PIC18
  • ****
  • Mensajes: 296
Sensor digital de temperatura DS18B20 y PIC16F88
« en: 12 de Octubre de 2011, 12:46:11 »
Hola:

Les dejo un código para el sensor digital DS18B20 que muestra la temperatura así como una barra que aumenta en función del aumento de temperatura. Si le echais un vistazo y encontrais una manera mejor de hacer lo mismo genial. Asi y todo el código parece funcionar bien.

La lcd que he usado es la 4x20

Este es el main:

Código: C
  1. #include <16f88.h>
  2. #fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,NOWRT,NODEBUG,NOMCLR
  3. #use delay(internal=8M)
  4.  
  5. #define LCD_DB4 PIN_A0
  6. #define LCD_DB5 PIN_A1
  7. #define LCD_DB6 PIN_A7
  8. #define LCD_DB7 PIN_A6
  9.  
  10. #define LCD_RS PIN_B7
  11. #define LCD_E PIN_B6
  12.  
  13. #include "flex_lcd420.c"
  14.  
  15. #include "1wire.c"
  16. #include "DS1820.c"
  17.  
  18. void main()
  19. {
  20. float temperature;
  21.  
  22. setup_adc_ports(NO_ANALOGS);
  23.  
  24. lcd_init();
  25.  
  26. while (1)
  27. {
  28.   temperature = ds1820_read();
  29.   lcd_gotoxy(1,1);
  30.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  31.   lcd_putc(223);
  32.   lcd_putc("C    ");
  33.   lcd_gotoxy(1,2);
  34.   if(temperature > 20.0 && temperature < 22.0){
  35.    lcd_putc('\f');
  36.    lcd_gotoxy(1,1);
  37.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  38.   lcd_putc(223);
  39.   lcd_putc("C    ");
  40.   lcd_gotoxy(1,2);
  41.   printf(lcd_putc,"Temp Ambiente    ");
  42.    lcd_gotoxy(1,4);
  43.    lcd_putc(223);
  44.    delay_ms(100);
  45.   }
  46.   else if(temperature > 22.0 && temperature < 24.0){
  47.    lcd_putc('\f');
  48.    lcd_gotoxy(1,1);
  49.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  50.   lcd_putc(223);
  51.   lcd_putc("C    ");
  52.   lcd_gotoxy(1,2);
  53.   printf(lcd_putc,"Temp Ambiente    ");
  54.    lcd_gotoxy(1,4);
  55.    lcd_putc(223);
  56.    delay_ms(100);
  57.    lcd_putc(223);
  58.    delay_ms(100);
  59.    }
  60.    else if(temperature > 24.0 && temperature < 26.0){
  61.    lcd_putc('\f');
  62.    lcd_gotoxy(1,1);
  63.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  64.   lcd_putc(223);
  65.   lcd_putc("C    ");
  66.   lcd_gotoxy(1,2);
  67.   printf(lcd_putc,"Temp Ambiente    ");
  68.    lcd_gotoxy(1,4);
  69.    lcd_putc(223);
  70.    delay_ms(100);
  71.    lcd_putc(223);
  72.    delay_ms(100);
  73.    lcd_putc(223);
  74.    delay_ms(100);
  75.    }
  76.    else if(temperature > 26.0 && temperature < 28.0){
  77.    lcd_putc('\f');
  78.    lcd_gotoxy(1,1);
  79.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  80.   lcd_putc(223);
  81.   lcd_putc("C    ");
  82.   lcd_gotoxy(1,2);
  83.   printf(lcd_putc,"Temp Ambiente    ");
  84.    lcd_gotoxy(1,4);
  85.    lcd_putc(223);
  86.    delay_ms(100);
  87.    lcd_putc(223);
  88.    delay_ms(100);
  89.    lcd_putc(223);
  90.    delay_ms(100);
  91.    lcd_putc(223);
  92.    delay_ms(100);
  93.  }
  94.  else if(temperature > 28.0 && temperature < 30.0){
  95.    lcd_putc('\f');
  96.    lcd_gotoxy(1,1);
  97.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  98.   lcd_putc(223);
  99.   lcd_putc("C    ");
  100.   lcd_gotoxy(1,2);
  101.   printf(lcd_putc,"Temp Ambiente    ");
  102.    lcd_gotoxy(1,4);
  103.    lcd_putc(223);
  104.    delay_ms(100);
  105.    lcd_putc(223);
  106.    delay_ms(100);
  107.    lcd_putc(223);
  108.    delay_ms(100);
  109.    lcd_putc(223);
  110.    delay_ms(100);
  111.    lcd_putc(223);
  112.    delay_ms(100);
  113. }
  114. else if(temperature > 30.0 && temperature < 32.0){
  115.    lcd_putc('\f');
  116.    lcd_gotoxy(1,1);
  117.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  118.   lcd_putc(223);
  119.   lcd_putc("C    ");
  120.   lcd_gotoxy(1,2);
  121.   printf(lcd_putc,"Temp Ambiente    ");
  122.    lcd_gotoxy(1,4);
  123.    lcd_putc(223);
  124.    delay_ms(100);
  125.    lcd_putc(223);
  126.    delay_ms(100);
  127.    lcd_putc(223);
  128.    delay_ms(100);
  129.    lcd_putc(223);
  130.    delay_ms(100);
  131.    lcd_putc(223);
  132.    delay_ms(100);
  133.    lcd_putc(223);
  134.    delay_ms(100);
  135. }
  136. else if(temperature > 32.0 && temperature < 34.0){
  137.    lcd_putc('\f');
  138.    lcd_gotoxy(1,1);
  139.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  140.   lcd_putc(223);
  141.   lcd_putc("C    ");
  142.   lcd_gotoxy(1,2);
  143.   printf(lcd_putc,"Temp Ambiente    ");
  144.    lcd_gotoxy(1,4);
  145.    lcd_putc(223);
  146.    delay_ms(100);
  147.    lcd_putc(223);
  148.    delay_ms(100);
  149.    lcd_putc(223);
  150.    delay_ms(100);
  151.    lcd_putc(223);
  152.    delay_ms(100);
  153.    lcd_putc(223);
  154.    delay_ms(100);
  155.    lcd_putc(223);
  156.    delay_ms(100);
  157.    lcd_putc(223);
  158.    delay_ms(100);
  159. }
  160. else if(temperature > 34.0 && temperature < 36.0){
  161.    lcd_putc('\f');
  162.    lcd_gotoxy(1,1);
  163.   printf(lcd_putc,"TEMP: %3.1f ", temperature);
  164.   lcd_putc(223);
  165.   lcd_putc("C    ");
  166.   lcd_gotoxy(1,2);
  167.   printf(lcd_putc,"Temp Ambiente    ");
  168.    lcd_gotoxy(1,4);
  169.    lcd_putc(223);
  170.    delay_ms(100);
  171.    lcd_putc(223);
  172.    delay_ms(100);
  173.    lcd_putc(223);
  174.    delay_ms(100);
  175.    lcd_putc(223);
  176.    delay_ms(100);
  177.    lcd_putc(223);
  178.    delay_ms(100);
  179.    lcd_putc(223);
  180.    delay_ms(100);
  181.    lcd_putc(223);
  182.    delay_ms(100);
  183.    lcd_putc(223);
  184.    delay_ms(100);
  185. }
  186. }
  187. }

1wire

Código: C
  1. // (C) copyright 2003 j.d.sandoz / jds-pic !at! losdos.dyndns.org
  2.  
  3. // released under the GNU GENERAL PUBLIC LICENSE (GPL)
  4. // refer to http://www.gnu.org/licenses/gpl.txt
  5.  
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 2 of the License, or
  9. // (at your option) any later version.
  10.  
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15.  
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19.  
  20. /***********************1Wire Class***********************/
  21. /*Description: This class handles all communication */
  22. /* between the processor and the 1wire */
  23. /* sensors.
  24. /*********************************************************/
  25.  
  26. /*-------1-wire definitions-------*/
  27. #define ONE_WIRE_PIN PIN_A2
  28.  
  29. /*******************1-wire communication functions********************/
  30.  
  31. /************onewire_reset*************************************************/
  32. /*This function initiates the 1wire bus */
  33. /* */
  34. /*PARAMETERS: */
  35. /*RETURNS: */
  36. /*********************************************************************/
  37.  
  38. void onewire_reset()  // OK if just using a single permanently connected device
  39. {
  40.  output_low(ONE_WIRE_PIN);
  41.  delay_us( 500 ); // pull 1-wire low for reset pulse
  42.  output_float(ONE_WIRE_PIN); // float 1-wire high
  43.  delay_us( 500 ); // wait-out remaining initialisation window.
  44.  output_float(ONE_WIRE_PIN);
  45. }
  46.  
  47. /*********************** onewire_write() ********************************/
  48. /*This function writes a byte to the sensor.*/
  49. /* */
  50. /*Parameters: byte - the byte to be written to the 1-wire */
  51. /*Returns: */
  52. /*********************************************************************/
  53.  
  54. void onewire_write(int data)
  55. {
  56.  int count;
  57.  
  58.  for (count=0; count<8; ++count)
  59.  {
  60.   output_low(ONE_WIRE_PIN);
  61.   delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
  62.   output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire
  63.   delay_us( 60 ); // wait until end of write slot.
  64.   output_float(ONE_WIRE_PIN); // set 1-wire high again,
  65.   delay_us( 2 ); // for more than 1us minimum.
  66.  }
  67. }
  68.  
  69. /*********************** read1wire() *********************************/
  70. /*This function reads the 8 -bit data via the 1-wire sensor. */
  71. /* */
  72. /*Parameters: */
  73. /*Returns: 8-bit (1-byte) data from sensor */
  74. /*********************************************************************/
  75.  
  76. int onewire_read()
  77. {
  78.  int count, data;
  79.  
  80.  for (count=0; count<8; ++count)
  81.  {
  82.   output_low(ONE_WIRE_PIN);
  83.   delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
  84.   output_float(ONE_WIRE_PIN); // now let 1-wire float high,
  85.   delay_us( 8 ); // let device state stabilise,
  86.   shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result.
  87.   delay_us( 120 ); // wait until end of read slot.
  88.  }
  89.  
  90.  return( data );
  91. }

codigo del ds1820

Código: C
  1. float ds1820_read()
  2. {
  3.  int8 busy=0, temp1, temp2;
  4.  signed int16 temp3;
  5.  float result;
  6.  
  7.  onewire_reset();
  8.  onewire_write(0xCC);
  9.  onewire_write(0x44);
  10.  
  11.  while (busy == 0)
  12.   busy = onewire_read();
  13.  
  14.  onewire_reset();
  15.  onewire_write(0xCC);
  16.  onewire_write(0xBE);
  17.  temp1 = onewire_read();
  18.  temp2 = onewire_read();
  19.  temp3 = make16(temp2, temp1);
  20.  
  21.  result = (float) temp3 / 20.0;   //Calculation for DS18S20 with 0.5 deg C resolution
  22. // result = (float) temp3 / 16.0;  //Calculation for DS18B20 with 0.1 deg C resolution
  23.  
  24.  delay_ms(200);
  25.  return(result);
  26. }

Desconectado fabianjsm

  • PIC18
  • ****
  • Mensajes: 255
    • fabianjsm is on twitter
Re: Sensor digital de temperatura DS18B20 y PIC16F88
« Respuesta #1 en: 13 de Octubre de 2011, 03:07:53 »
Para compartirlo podes adjuntar el fuente al post para que resulte fácil bajarlo, en mi caso facilitaría la lectura del código y hacer modificaciones.
Para no repetir N veces lcd_putc y luego delay_ms, podrías utilizar un bucle:
Código: C
  1. for(i=0; i<N; i++) {
  2.     lcd_putc(223);
  3.     delay_ms(100);
  4. }
Las instrucciones que aparecen repetidas veces al inicio de cada IF, ELSE-IF, podrían moverse antes de entrar al IF.
@fabianjsm is on twitter

Desconectado aitorsp

  • PIC18
  • ****
  • Mensajes: 296
Re: Sensor digital de temperatura DS18B20 y PIC16F88
« Respuesta #2 en: 13 de Octubre de 2011, 05:47:05 »
Para compartirlo podes adjuntar el fuente al post para que resulte fácil bajarlo, en mi caso facilitaría la lectura del código y hacer modificaciones.
Para no repetir N veces lcd_putc y luego delay_ms, podrías utilizar un bucle:
Código: C
  1. for(i=0; i<N; i++) {
  2.     lcd_putc(223);
  3.     delay_ms(100);
  4. }
Las instrucciones que aparecen repetidas veces al inicio de cada IF, ELSE-IF, podrían moverse antes de entrar al IF.

Ok si, es cierto. La otra cuestion seria si hay otra manera de generar caraceteres en la LCD sin escribir mediante lcd_putc. Me refiero a construir mis propios dibujos pero para eso supongo que habria que acceder a la memoria de la LCD no ?

Desconectado Edwin16

  • PIC10
  • *
  • Mensajes: 2
Re: Sensor digital de temperatura DS18B20 y PIC16F88
« Respuesta #3 en: 26 de Marzo de 2015, 14:23:58 »
hola fijate que estoy tratando de realizar algo similar pero tengo un problema al llamar las librerias 1wire.c y ds1820.c me da muchos errores prodrias ayudarme. de antemano gracias

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re: Sensor digital de temperatura DS18B20 y PIC16F88
« Respuesta #4 en: 26 de Marzo de 2015, 17:10:00 »
hola fijate que estoy tratando de realizar algo similar pero tengo un problema al llamar las librerias 1wire.c y ds1820.c me da muchos errores prodrias ayudarme. de antemano gracias

Cuales son los errores ?