CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

DS18B20 with PIC16F876A, need help!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
milkman41



Joined: 16 Jun 2008
Posts: 30

View user's profile Send private message

DS18B20 with PIC16F876A, need help!
PostPosted: Fri Mar 12, 2010 1:47 pm     Reply with quote

Hey all,

I'm trying to use a DS18B20 1-wire temperature sensor with a PIC16F876A, and I'm having some trouble with it. I'm using a board built by olimex, and then subsequently programmed by Paul H. Anderson, though I'm programming over this. I'm using the DS18B20 temperature sensors in parasite power mode, and they work with Anderson's code, but when I use the code provided by hansolo and scanan in the Code Library (http://ccsinfo.com/forum/viewtopic.php?t=28425&highlight=dallas+18b20), the only temperature value returned is 85 degrees C. I don't know how to fix this, although I understand that it is the startup value of the sensor.

Here is my code:

temptest.c
Code:
#include <16f876A.h>
#device *=16
#device adc=8

#FUSES NOWDT, HS, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT     
#use delay(clock=20000000)

#include <1wire.c>
#include <Flex_lcd_16x1.c>
#include <DS18B20.c>

#define LED PIN_B4

void main()
{
   float temperature;
/*
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(VREF_LOW|-2);
*/
   lcd_init();
   delay_ms(100);
   
   //for  10 bit resolution mod
   onewire_write(0xCC);
   onewire_write(0x4E);

   onewire_write(125);
   onewire_write(-55); //this should be done for proper working of DS18B20
   onewire_write(127);

   onewire_reset();
   onewire_write(0xCC);
   onewire_write(0x48);
   delay_ms(15);

   delay_ms(500);
   output_low(LED);

   printf(lcd_putc, "\fWelcome");
   delay_ms(500);

   lcd_putc("\f");

   while (TRUE) {
   
      printf(lcd_putc,"\f ");
      delay_ms(500);
      temperature = ds1820_read();

      printf(lcd_putc,"TEMP: %d ", (signed int)temperature);
      delay_ms(500);

      if(temperature >= 29.0)
         printf(lcd_putc,"\nHot!    ");
      else if( temperature >= 20 && temperature < 29.0)
         printf(lcd_putc,"\nComfort!");
      else
         printf(lcd_putc,"\nCold!   ");
      delay_ms(500);
   }
return;
}



DS18B20.c
Code:
float ds1820_read()
{
 int8 tH,tL,Conf;
 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();
 
 tH=onewire_read();
 tL=onewire_read();
 Conf=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);
}



1wire.c
Code:
// (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_B5

/*******************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<8; ++count)
 {
  output_low(ONE_WIRE_PIN);
  delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
  output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire
  delay_us( 60 ); // wait until end of write slot.
  output_float(ONE_WIRE_PIN); // set 1-wire high again,
  delay_us( 2 ); // for more than 1us minimum.
 }
}

/*********************** read1wire() *********************************/
/*This function reads the 8 -bit data via the 1-wire sensor. */
/* */
/*Parameters: */
/*Returns: 8-bit (1-byte) data from sensor */
/*********************************************************************/

int onewire_read()
{
 int count, data;

 for (count=0; count<8; ++count)
 {
  output_low(ONE_WIRE_PIN);
  delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
  output_float(ONE_WIRE_PIN); // now let 1-wire float high,
  delay_us( 8 ); // let device state stabilise,
  shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result.
  delay_us( 120 ); // wait until end of read slot.
 }

 return( data );
}


Any help would be greatly, greatly appreciated.

Thanks,
-Nick
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 12, 2010 1:58 pm     Reply with quote

See this thread for help with running a ds18B20 in parasite mode.
This is a 2-page thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=40106
milkman41



Joined: 16 Jun 2008
Posts: 30

View user's profile Send private message

PostPosted: Fri Mar 12, 2010 8:26 pm     Reply with quote

I looked at it before, and I wasn't sure quite how to change my code. However, now, upon further review, it looks like I should write my onewire_write function to imitate the write_byte function you posted on the second page (and make the relevant changes to my main function). Would this work?

Thanks,
-Nick
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 14, 2010 1:27 pm     Reply with quote

I have posted the full program at the end of the original thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=40106&start=28
milkman41



Joined: 16 Jun 2008
Posts: 30

View user's profile Send private message

PostPosted: Fri Mar 19, 2010 2:14 pm     Reply with quote

Thanks alot, I'll check it out next week when I'm off break.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group