Professional Documents
Culture Documents
Author Message
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:
// 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
/************onewire_reset*************************************************/
/*This function initiates the 1wire bus */
/* */
/*PARAMETERS: */
/*RETURNS: */
/*********************************************************************/
int onewire_read()
{
int count, data;
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
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
jds-pic
Thanks.
you must carry forward the GPL license and as well as the copyright(s) of the original author(s).
jds-pic
you must carry forward the GPL license and as well as the copyright(s) of the original author(s).
jds-pic
Sorry.
thank you.
Code:
// onewire library, for dallas onewire devices
// currently includes:
// - generic onewire functions (init, readbyte, sendbyte, crc, etc.)
// - ds1822 dallas-semi econo-thermometer
#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);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
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 Class***********************/
/*Description: This class handles all communication */
/* between the processor and the 1wire */
/* sensors.
/*********************************************************/
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
int onewire_read()
{
int count, data;
return( data );
}
Last edited by scanan on Tue Apr 08, 2014 1:55 pm; edited 3 times in total
used :
signed int16 ds1820_read()
signed int16 result
result = temp3 * 5;
noobprogrammer ds18s20
Posted: Tue May 20, 2008 7:07 pm
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
Schematic?
Look this:
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
connect DS18S20 to this pin (or another defined)
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
Schematic?
Look this:
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
connect DS18S20 to this pin (or another defined)
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
Schematic?
Look this:
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
connect DS18S20 to this pin (or another defined)
will this work for the pic18f452? and which ports and pins are connected to the LCD..sorrie im a noob..thanks for your
help!
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