estoy creando un programa en c para recibir la temperatura de un sensor ds18b20

Upload: leonardo-grafia

Post on 13-Jul-2015

871 views

Category:

Documents


0 download

TRANSCRIPT

Estoy creando un programa en C para recibir la temperatura de un sensor DS18B20, y mostrarlo en un glcd mediante un PIC 16F887. La simulacion corre perfecta, pero a la hora de probarlo fisicamente, el glcd no hace mas que mostrarme 85.0 C, y no cambia. Alguien sabe porque puede ser? A continuacion adjunto el programa: Citar #include #include #include #include #include #include

int8 Pro[]= "PROYECTO"; int8 Ter[]= "TERMOMETRO"; float temperatura_antigua ; float temperatura_nueva ; char palabra [16],palabra2 [16]; int8 guar[]= "Guardando"; int8 tempe[]= "Temperatura"; int8 trans[]= "Transmitiendo..."; float resultado;

void main() { setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard

glcd_init(ON); delay_ms(20); glcd_rect(10,10,110,58,no,on); glcd_rect(18,18,68,28,no,on); glcd_rect(38,38,100,48,no,on); glcd_text57(20,20,Pro,1,on); glcd_text57(40,40,Ter,1,on); delay_ms(1500); glcd_fillScreen(0); glcd_rect(10,25,110,42,no,on);

while (1) {

temperatura_nueva = ds1820_read ();

// Se vuelve a leer el sensor y este valor se escribe // en una nueva variable

if (temperatura_nueva != temperatura_antigua){ glcd_rect(50,30,105,40,yes,OFF); sprintf(palabra,"TEMP = %3.1f &C",temperatura_nueva); glcd_text57(15, 30,palabra, 1, ON); temperatura_antigua = temperatura_nueva ; }

// Se compara la temperatura vieja con la nueva // Se borra la temperatura vieja // Se vuelve a crear un array de valores

if (input(PIN_A0)){ glcd_text57(2, 10,guar, 1, ON); glcd_text57(60, 10,tempe, 1, ON); guardar(temperatura_nueva,0); delay_ms(1500); glcd_rect(2,10,126,20,yes,off); } if (input(PIN_A1)){ glcd_text57(10,50,trans,1,on); delay_ms(1500); leer(resultado,0); sprintf(palabra2,"Temperatura = %f",resultado); printf (palabra2); glcd_rect(10,50,110,60,yes,off); } } }

Libreria DS18B20:float ds1820_read() { int8 busy=0, temp1, temp2; signed int16 temp3; float result;

onewire_reset(); onewire_write(0xCC); onewire_write(0x44);

while (busy == 0) busy = onewire_read();

onewire_reset(); onewire_write(0xCC); onewire_write(0xBE); temp1 = onewire_read(); temp2 = onewire_read(); temp3 = make16(temp2, temp1);

//result = (float) temp3 / 2.0;

//Calculation for DS18S20 with 0.5 deg C resolution

result = (float) temp3 / 16.0; //Calculation for DS18B20 with 0.1 deg C resolution

delay_ms(200); return(result); }

Libreria 1wire:// (C) copyright 2003 j.d.sandoz / jds-pic !at! losdos.dyndns.org // released under the GNU GENERAL PUBLIC LICENSE (GPL) // refer to http://www.gnu.org/licenses/gpl.txt // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /***********************1Wire Class***********************/ /*Description: This class handles all communication */ /* between the processor and the 1wire */ /* sensors. /*********************************************************/ /*-------1-wire definitions-------*/ #define ONE_WIRE_PIN PIN_C2 /*******************1-wire communication functions********************/ /************onewire_reset*************************************************/ /*This function initiates the 1wire bus */ /* */ /*PARAMETERS: */ /*RETURNS: */ /*********************************************************************/ void onewire_reset() // OK if just using a single permanently connected device { output_low(ONE_WIRE_PIN); delay_us( 500 ); // pull 1-wire low for reset pulse output_float(ONE_WIRE_PIN); // float 1-wire high delay_us( 500 ); // wait-out remaining initialisation window. output_float(ONE_WIRE_PIN); } /*********************** onewire_write() ********************************/ /*This function writes a byte to the sensor.*/ /* */ /*Parameters: byte - the byte to be written to the 1-wire */ /*Returns: */ /*********************************************************************/ void onewire_write(int data)

{ int count; for (count=0; count to RX on Serial LCD copyright, Peter H. Anderson, Baltimore, MD, Apr, 99

#case #include #include #include void display_lcd(int *d, int num_vals); void get_16f84_eeprom(int adr, int *d, int num_vals); void put_16f84_eeprom(int adr, int *d, int num_vals); void write_16f84_eeprom(int adr, int d); int read_16f84_eeprom(int adr); void ds1820_read_rom(int *d, int sensor); void make_temperature_meas(int *adr, int *d, int sensor); // 1-wire prototypes void _1w_init(int sensor); int _1w_in_byte(int sensor); void _1w_out_byte(int d, int sensor); void _1w_pin_hi(int sensor); void _1w_pin_low(int sensor); void _1w_strong_pull_up(int sensor); // not used in this routine // delay routines void delay_ms(long t); void delay_10us(int t); // LCD routines void lcd_init(void); void out_RAM_str(int *s); void lcd_hex_byte(int val); void lcd_dec_byte(int val, int digits); int num_to_char(int val); void lcd_char(int ch); void lcd_new_line(void); #define TxData 0 void main(void) { int dev_adr[8], t_dat[9]; lcd_init(); ds1820_read_rom(dev_adr, 0); // read serial number from DS1820 display_lcd(dev_adr, 8); // display the result on LCD put_16f84_eeprom(0x00, dev_adr, 8); // save to eeprom, beginning at adr 0x00, 8 bytes

// temporary storage

// now fetch the serial number, address and perform a temperature // measurment get_16f84_eeprom(0x00, dev_adr, 8); // fetch from 16F84, 8 bytes and return in array d make_temperature_meas(dev_adr, t_dat, 0); display_lcd(t_dat, 9); // display the 9 byte result #asm DONE: clrwdt GOTO DONE #endasm } void display_lcd(int *d, int num_vals) { int n; for (n=0; n