Monday, November 23, 2009

Interfacing with DS 18 B 20 Digital Thermometer



DS18B20PAR is 3 pin digital thermometer with all necessary features to measure temperature upto 12 bit resolution. This use one wire protocol, so it is easy to wire. But, bit difficult to interface. This IC operates with Parasite-Power(bus powered) and so we need only two wires. For more information and free samples, refer

http://www.maxim-ic.com/quick_view2.cfm?qv_pk=2813

This is the mcu code to read and write data from this chip.

Hardware over view.....

** Use PIC18F452 with 12MHz oscillator.
** Refer the top image for hardware connections
** Use Mikro Electronika C compiler
----------------------------------------------------------------------------------
#define set_A(var,bitnum) (var)|=(1<<(bitnum))
#define res_A(var,bitnum) (var)&=~(1<<(bitnum))

short reset_ow()
{
short r;
set_A(TRISA,5); // pull up disable
res_A(TRISA,4); // data pin as o/p
res_A(PORTA,4); // put bus to low
Delay_us(500);
set_A(TRISA,4); // i/p
Delay_us(70); // wait for response
if(PORTA & 0x10) // if bus is still high,
{
r=0;
}
else
{
r=1;
}
Delay_us(500);
return r;
}
/////////////////////////////////////////////////////////////////////////////////////
void write_ow(unsigned short data)
{
short x;
set_A(TRISA,5); // pull up disable
//set_A(TRISA,4); // pull up disable
res_A(TRISA,4); // data pin as o/p
for(x=0;x<8;x++)
{
if(data & 0x01) // write 1 time slot
{
res_A(PORTA,4); // put bus to low
Delay_us(10);
set_A(PORTA,4); //release the bus to high
Delay_us(60);
}
else // write 0 time slot
{
res_A(PORTA,4); // put bus to low
Delay_us(70);
set_A(PORTA,4);
}
Delay_us(2); // recovery time
data=data>>1; // next data bit
}
}
/////////////////////////////////////////////////////////////////////////////////////
unsigned short read_ow()
{
unsigned short res=0,x=0,y=0;
set_A(TRISA,5); // pull up disable
for(x=0;x<8;x++)
{
res_A(PORTA,4); // put bus to low
res_A(TRISA,4); // data pin as o/p
Delay_us(2);
set_A(TRISA,4); //release the bus to high & data pin as i/p
Delay_us(10);
if(PORTA & 0x10) // if bus is high,
{
y=1< }
else
{
y=0;
}
res=res | y;
Delay_us(50);
}
return res;
}
/////////////////////////////////////////////////////////////////////////////////////
void pullup()
{
set_A(PORTA,5);
res_A(TRISA,5); // RA 5 as o/p
}
////////////////////////////////////////////////////////////////////////////////////
void main()
{
unsigned short j=0;

unsigned int temp=0,f=0;

ADCON1 = 0xFF;

while(1)
{
reset_ow();
write_ow(0xCC); //skip ROM
write_ow(0x44); // convert
pullup();
Delay_ms(800);

reset_ow();
write_ow(0xCC); //skip ROM
write_ow(0xBE); // read search pad
j=read_ow();
temp=read_ow();

temp <<= 8;
temp = temp + j; // Form the result you can get this as temparature

Delay_ms(200);
}
}
/////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment