The DS18B20 Digital Thermometer and PIC16F877A microcontroller on PIC-01 Development Board.
1. Introduction.
It is an example of the DS18B20 digital thermometer. The application was tested on the PIC-01 Development Board with the PIC16F877A microcontroller installed. All of the routines were written in the CCS C.
2. DS18B20.
The DS18B20 is the digital thermometer from Dallas Semicondutor (Maxim). It communicates with the microcontroller by the 1-Wire® interface. The DS18B20 can measure temperatures from -55°C to 125°C with an accuracy of +/- 0.5°C. Each device has a unique 64-bit serial number and the resolution is user-selectable from 9 to 12 bits. The device can be used in thermostatic control applications.
3. Circuit.
The DS18B20′s data pin is connected to the RB0 pin of the PIC16F877A microcontroller. The data pin should be connected to power supply through 4.7k pull-up resistor. The LED jumper of the development board should be disconnected.
4. Programming.
All of the routines were written in the CCS C Compiler language.
- 1wire.h is the library which contains three functions: init_1wire(), read_1wire() and write_1wire(). The init_1wire() function resets 1-Wire® line. The read_1wire() and write_1wire() functions read and write one byte into 1-Wire® device. The library was tested only with the DS18B20 and might not work with different 1-Wire® devices.
//-----------------------------------------------------
// 1wire.h
//-----------------------------------------------------
//-------------------------data pin definition-----------
#define DQ pin_b0
//--------------------------1wire reset-----------------
init_1wire()
{
int a;
output_low(DQ);
delay_us(480);
output_float(DQ);
delay_us(65);
a=!input(DQ);
delay_us(240);
if(a)
return(1);
else
return(0);
}
//---------------------------read byte---------------
byte read_1wire()
{
byte a,data;
for(a=0;a<8;a++)
{
output_low(DQ);
delay_us(15);
output_float(DQ);
delay_us(5);
shift_right(&data,1,input(DQ));
delay_us(100);
}
return(data);
}
//------------------------write byte--------------------
byte write_1wire(byte data)
{
byte a;
for(a=0;a<8;a++)
{
output_low(DQ);
delay_us(10);
if(shift_right(&data,1,0))
output_high(DQ);
else
output_low(DQ);
delay_us(50);
output_high(DQ);
delay_us(50);
}
return(1);
}
- There is a small change in the llcd_new.h library. The degree sign (°) is not a standard sign stored in the LCD memory so it has to be initiated.
LCD_CUSTOM_CHARS[64] = {0x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00,
0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,
0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00,
0x00,0x04,0x04,0x04,0x04,0x04,0x00,0x00,
0x11,0x11,0x15,0x15,0x0A,0x00,0x00,0x00,
0x0E,0x01,0x0F,0x11,0x0F,0x00,0x00,0x00,
0x11,0x11,0x0F,0x01,0x0E,0x00,0x00,0x00,
0x06,0x09,0x09,0x06,0x00,0x00,0x00,0x00}; //degree sign
- The main code. The temperature is displayed on the PIC Development Board’s LCD in the format “Temp= XX.X°C”.
//----------------------------------------------------
// ds18b20.c
//----------------------------------------------------#include<16f877a.h> //Device
#fuses xt,nowdt,protect,noput,nolvp //Fuses
#use delay (clock=4000000) //4.00 MHz
#use rs232 (Baud=9600) //RS-232 parametres
//(only for printf function)
#include"llcd_new.h" //LCD driver
#include"1wire.h"int i,read_ok;
byte buffer[8];
signed int16 t,t1;
main()
{
lcd_init(); //LCD start
lcd_init_custom_chars(); //write custom characters
while(1)
{
if(init_1wire())
{
write_1wire(0xcc); //skip ROM
write_1wire(0x44); //convert T
}
if(init_1wire())
{
write_1wire(0xcc); //skip ROM
write_1wire(0xbe); //read scratchpad
for(i=0;i<8;i++)
buffer[i]=read_1wire();
read_ok=1;
}
if(read_ok)
{
t=make16(buffer[1],buffer[0]); //calculate temperature
t=(float)t/16.0; //Calculation 0.1 deg C resolution
}
lcd_gotoxy(1,1);
printf(lcd_putc,"Temp= %3.1f \uC", t);
//\u displays degree sign
delay_ms(500);
}
}
All files are available here:
ds18b20.c (.zip)
ds18b20.hex (.zip)
1wire.h (.zip)
DS18B20.pdf The DS18B20 data sheet.
The APP162 application note – “Interfacing the DS18X20/DS1822 1-Wire Temperature Sensor in a Microcontroller Environment”

