You are on page 1of 8

CCS does not monitor this forum on a regular basis.

FAQ    Forum Help    Official CCS Support    Search  


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

Profile    Log in to check your private messages    Log in 

DS18S20 & DS18B20 Codes


Goto page 1, 2, 3, 4  Next
 

       CCS Forum Index -> Code Library

View previous topic :: View next topic  

Author Message

hansolo DS18S20 & DS18B20 Codes


Posted: Fri Oct 06, 2006 6:56 pm    

Joined: 06 Oct 2006


Posts: 20 I find that the codes available in this site for DS18S20 & DS18B20 is a bit messy and complicated.

Below is the simple version of the codes which works. It also take care of negative temperature.

However, due to its simplicity, this will only work with one DS1820 connected to the 1-wire bus of the PIC. This code
will not work with 2 or more DS1820 sharing the same 1-wire bus.

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_A0

/*******************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 );
}

ds1820.c
Code:

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);
}

Main code
Code:

#include <16F877A.h>
#device *=16
#device adc=8

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

#include<1wire.c>
#include<lcd.c>
#include<ds1820.c>

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();
lcd_putc("\f");

while (1)
{
  temperature = ds1820_read();

  lcd_gotoxy(1,1);
  printf(lcd_putc,"TEMP: %3.1f ", temperature);
  lcd_putc(223);
  lcd_putc("C    ");

  lcd_gotoxy(1,2);
  if(temperature >= 29.0)
   printf(lcd_putc,"Hot!    ");
  else if( temperature >= 20 && temperature < 29.0)
   printf(lcd_putc,"Comfort!");
  else
   printf(lcd_putc,"Cold!   ");
 }

Last edited by hansolo on Sun Oct 15, 2006 5:56 pm; edited 1 time in total

jds-pic Re: DS18S20 & DS18B20 Codes


Posted: Wed Oct 11, 2006 3:02 pm    

Joined: 17 Sep 2003


Posts: 205 hansolo wrote:
I find that the codes available in this site for DS18S20 & DS18B20 is a bit messy and complicated.

Below is the simple version of the codes which works. It also take care of negative temperature.

the entirety of the onewire driver code is copied directly from my post of Fri Jun 04, 2004 9:54 am EST -- a post which
noted that the attached code is covered under the GPL.
http://ccsinfo.com/forum/viewtopic.php?t=19520

you can read about the GPL here


http://www.gnu.org/licenses/gpl.txt

jds-pic

hansolo Posted: Wed Oct 11, 2006 3:43 pm    

Joined: 06 Oct 2006


I posted the codes for sharing and I am not selling it.
Posts: 20
I hope you do not mind.

Thanks.

jds-pic Posted: Sun Oct 15, 2006 1:34 pm    

Joined: 17 Sep 2003


hansolo wrote:
Posts: 205
I posted the codes for sharing and I am not selling it.
I hope you do not mind.
Thanks.

you must carry forward the GPL license and as well as the copyright(s) of the original author(s).

jds-pic

hansolo Posted: Sun Oct 15, 2006 5:52 pm    

Joined: 06 Oct 2006 jds-pic wrote:


Posts: 20
hansolo wrote:
I posted the codes for sharing and I am not selling it.
I hope you do not mind.
Thanks.

you must carry forward the GPL license and as well as the copyright(s) of the original author(s).

jds-pic

My mistake. I did not read the fine print.

I will amend it.

Sorry.

ulaska Posted: Mon Jul 16, 2007 3:08 pm    

Joined: 16 Jul 2007


i tried this codes. it works OK.
Posts: 3 i use 2x16 standard LCD, DS1280 and pic16f876a.

but it shows the temperatures like, 25.4 , 25.9, 27.4 ,


it increases or decreases 0.5 degree.
the resolution is 0.5 degree, but it isnt whole numbers or half (25.0, 25.5 etc.)

i think there are some problems with data types in ds1820_read() ?


but i cannot see any mistake, also i tried to add 0.1 to reach whole numbers but doesnt works.

thank you.

p.s. i am new in PICs

scanan 0.1 degree resolution working code


Posted: Wed Jan 16, 2008 4:29 pm    

Joined: 13 Aug 2004


Posts: 58 A working modification of hansolo - jds-pic codes.
Location: Turkey Now it works fine.

Code:
// onewire library, for dallas onewire devices
// currently includes:
//   - generic onewire functions (init, readbyte, sendbyte, crc, etc.)
//   - ds1822 dallas-semi econo-thermometer

// (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

//Previously Posted in CCS thread from jds-pic


//Revision Made By SCanan Date: 26.06.2007
#include <16f628.h>
#device *=16
// internal osilator used
#FUSES NOWDT,INTRC, NOPUT, NOPROTECT, NOBROWNOUT, NOMCLR, NOLVP, NOCPD//for 16f628
#use delay(clock=4000000)
#USE RS232(BAUD=9600, XMIT=PIN_B2, RCV=PIN_B1)

#include "1wire.c"

int8 tH,tL,Conf;

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

onewire_reset();
onewire_write(0xCC);
onewire_write(0x44);
delay_ms(200);
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;   // 0.5 deg C resolution


result = (float) temp3 / 16.0;  //0.1 deg C resolution
delay_ms(200);
return(result);
}
void main()
{
float temperature;

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

//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);

while (1)
{
  temperature = ds1820_read();

printf("Temperature= %3.1f\n\r",temperature);
}

////// 1wire.c fontion //////


///// DS18b20 data pin connected to  pic16f628 B0 pin //////

/***********************1Wire Class***********************/
/*Description: This class handles all communication */
/* between the processor and the 1wire */
/* sensors.
/*********************************************************/

/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0

/*******************1-wire communication functions********************/

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 );
}

Last edited by scanan on Tue Apr 08, 2014 1:55 pm; edited 3 times in total

mthornton Posted: Thu Feb 21, 2008 3:56 pm    

Joined: 22 Jan 2008


The above ds18s20 code is nice & light & works, but I avoided the floating variables by integer-multiplying the result by
Posts: 4 5 instead of dividing by 2 or 16. So my result is in DegC x 10. No problem, makes it even lighter. Just have to
Location: Canal Flats BC
remember where to place the decimal-point in the display routine.

instead of the lines:


float ds1820_read()
float result
result = (float) temp3 / 2.0;

used :
signed int16 ds1820_read()
signed int16 result
result = temp3 * 5;

40inD Posted: Sat Apr 26, 2008 1:08 pm    

Joined: 30 Jul 2007


Why i get 0.5 degree resolution in 18B20 when i do this:
Posts: 106 Code:
Location: Moscow, Russia
result = (float) temp3 / 16.0;

meereck Posted: Tue Apr 29, 2008 4:42 am    

Joined: 09 Nov 2006


hello, thanx for the source code. Works like a charm with both DS18S20 and B. Can any1 tell me what the maximal
Posts: 173 length of a cable between a PIC and the sensor can be?
cheers meereck

noobprogrammer ds18s20
Posted: Tue May 20, 2008 7:07 pm    

Joined: 20 May 2008


Posts: 5 meereck wrote:
hello, thanx for the source code. Works like a charm with both DS18S20 and B. Can any1 tell me what the maximal
length of a cable between a PIC and the sensor can be?
cheers meereck

anybody has the schematic for this? thanks

muerte Re: ds18s20


Posted: Wed May 21, 2008 3:40 am    

Joined: 19 May 2008


Posts: 8 Hi all,
Location: Poland noobprogrammer wrote:

meereck wrote:
hello, thanx for the source code. Works like a charm with both DS18S20 and B. Can any1 tell me
what the maximal length of a cable between a PIC and the sensor can be?
cheers meereck

anybody has the schematic for this? thanks

Schematic?
Look this:
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
connect DS18S20 to this pin (or another defined)

and DS18S20 data sheet from Maxim http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf


_________________
Best regards,
muerte

meereck Re: ds18s20


Posted: Wed May 21, 2008 7:08 am    

Joined: 09 Nov 2006


Posts: 173 muerte wrote:
Hi all,
noobprogrammer wrote:

meereck wrote:
hello, thanx for the source code. Works like a charm with both DS18S20 and B. Can any1
tell me what the maximal length of a cable between a PIC and the sensor can be?
cheers meereck

anybody has the schematic for this? thanks

Schematic?
Look this:
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
connect DS18S20 to this pin (or another defined)

and DS18S20 data sheet from Maxim http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf

In addition, do not forget the pull-up resistor on the data line

ckielstra Posted: Wed May 21, 2008 10:11 am    

Joined: 18 Mar 2004


meereck wrote:
Posts: 3680 Can any1 tell me what the maximal length of a cable between a PIC and the sensor can be?
Location: The Netherlands
http://www.ccsinfo.com/forum/viewtopic.php?t=34676

noobprogrammer Re: ds18s20


Posted: Wed May 21, 2008 7:35 pm    

Joined: 20 May 2008


Posts: 5 muerte wrote:
Hi all,
noobprogrammer wrote:

meereck wrote:
hello, thanx for the source code. Works like a charm with both DS18S20 and B. Can any1
tell me what the maximal length of a cable between a PIC and the sensor can be?
cheers meereck

anybody has the schematic for this? thanks

Schematic?
Look this:
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
connect DS18S20 to this pin (or another defined)

and DS18S20 data sheet from Maxim http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf

will this work for the pic18f452? and which ports and pins are connected to the LCD..sorrie im a noob..thanks for your
help!

Display posts from previous: All Posts   Oldest First   Go

    All times are GMT - 6 Hours


   CCS Forum Index -> Code Library
Goto page 1, 2, 3, 4  Next
Page 1 of 4

Jump to:  Code Library   Go

 
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

You might also like