OneWire Library

 

History

The OneWire protocol which in fact needs two wires (data and ground) is fantastic for many reasons: you can have a very long cable (250 feets), you can have multiples of devices on this cable and with a simple code you can poll those devices to get temperature, voltage, switch position, wind speed, etc.

This protocol was developed by Dallas Semiconductor (now Maxim-IC).  Many flavors of devices can be connected to an OneWire port. You can see them at Maxim (http://www.maxim-ic.com/1-Wire.cfm) web site!

 

 

OneWire Features

puce Cable can be long
puce Devices take their power from the data line
puce Multiples devices can be used on the same cable
puce Each device has is own ID (48 bits serial number)

 

Source Code

//**************************************
// OneWire
// Version 1.0 Sept 2000
// Sylvain Bissonnette
//
//**************************************

//**************************************

//**************************************
//            I N C L U D E
//**************************************

#include <io2313.h>

//**************************************
//            D E F I N E
//**************************************
// User dependent BEGIN

#define ONEWIRE      0x01                    // BIT
#define ONEPIN       (*(volatile unsigned char *)0x36)   // PIN register
#define ONEDDR       (*(volatile unsigned char *)0x37)   // Data Direction Register
#define ONEPORT      (*(volatile unsigned char *)0x38)   // PORT
// User dependent END


#define TRUE      1
#define FALSE     0


//**************************************
//          P R O T O T Y P E
//**************************************

unsigned char ConvertTemp(unsigned char);
int
GetTemp(unsigned char);
void
OneWireStrong(unsigned char mask,unsigned char STAT);
unsigned char
OneWireReset(unsigned char);
void
OneWireWriteByte(unsigned char,unsigned char);
unsigned char
OneWireReadByte(unsigned char);
void
Delay_500us(void);
void
Delay_70us(void);
void
Delay_7us(void);

//**************************************
//            M A I N
//**************************************

void main(void)
{
while(
1);
}


//*********************************************************
// OneWire Code
//*********************************************************
/**********************************************************

Name:       unsigned char ConvertTemp(unsigned char mask)

Description:   Convert temperature of the sensor in mask,

Input:         mask : Where the device are on the port

Output:        0 -> no device present
            1 -> device present

Misc:      

**********************************************************/

unsigned char ConvertTemp(unsigned char mask)
{
if (!
OneWireReset(mask)) return 0;
OneWireWriteByte(mask,0xcc);
OneWireWriteByte(mask,0x44);
return
1;
}


/**********************************************************

Name:       unsigned char GetTemp(unsigned char mask)

Description:   Get temperature of the sensor in mask,

Input:         mask : Where the device are on the port

Output:        Temp in C * 10 or
            9999 if no device present

Misc:       for DS18B20 NOT DS1820 

**********************************************************/

int GetTemp(unsigned char mask)
{
unsigned char
msb,lsb;
unsigned char
negative;
int
i;

if (!
OneWireReset(mask)) return 9999;
OneWireWriteByte(mask,0xCC);
OneWireWriteByte(mask,0xbe);
lsb = OneWireReadByte(mask);
msb = OneWireReadByte(mask);
OneWireReset(mask);

negative = FALSE;
i = (msb << 8) + lsb;  //  temp contient la temperature reelle x 16
if (i < 0)
   {

   negative = TRUE;
   i = -i;
   }

i = (i * 10) / 16;
if (
negative == TRUE) i = -i;  //  Restore sign.
return i;
}


/**********************************************************

Name:       OneWireStrong(unsigned char mask,unsigned char STAT)

Description:   Set if the strong pull up is on or off

Input:         mask : Where the device are on the port
            STAT : 0 -> strong pull up disable
                  1 -> strong pull up enable

Output:        none

Misc:      

**********************************************************/

void OneWireStrong(unsigned char mask,unsigned char STAT)
{
if (
STAT == TRUE)
   {

   ONEPORT |= mask;
   ONEDDR |= mask;
   }
else
   {

   ONEDDR &= ~(mask);
   }
}


/**********************************************************

Name:       unsigned char OneWireReset(unsigned char mask)

Description:   Send a reset on the buss,

Input:         mask : Where the device are on the port

Output:        0 -> Device not present
            1 -> Device present

Misc:      

**********************************************************/

unsigned char OneWireReset(unsigned char mask)
{

ONEPORT &= ~(mask);                 // Normal input no pull up
if (!(ONEPIN & mask)) return 0;        // detect 0V on buss error
ONEDDR |= mask;                        // out at 0
Delay_500us();
ONEDDR &= ~(mask);                  // Set to input
Delay_70us();
if ((
ONEPIN & mask) == 0)
   {

   Delay_500us();
   return(
1);
   }

Delay_500us();
return(
0);
}


/**********************************************************

Name:       void OneWireWriteByte(unsigned char mask,unsigned char data)

Description:   Write a byte on the OneWire buss,

Input:         mask : Where the device are on the port,
            data : Data to write on the port

Output:        none

Misc:      

**********************************************************/

void OneWireWriteByte(unsigned char mask, unsigned char data)
{
unsigned char
i;

ONEPORT &= ~(mask);
for (
i=0;i<=7;i++)
   {

   ONEDDR |= mask;
   if (
data & 0x01)
       {

      Delay_7us();           // Send 1
      ONEDDR &= ~(mask);
      Delay_70us();
       }
   else
      {

      Delay_70us();         // Send 0
      ONEDDR &= ~(mask);
      Delay_7us();
      }

   data>>=1;
   }
}


/**********************************************************

Name:       unsigned char OneWireReadByte(unsigned char mask)

Description:   Read a byte on the OneWire buss,

Input:         mask : Where the device are on the port

Output:        data that had been read

Misc:      

**********************************************************/

unsigned char OneWireReadByte(unsigned char mask)
{
unsigned char
data = 0;
unsigned char
i;

ONEPORT &= ~(mask);                       // Output '0' or input without pullup
for (i=0;i<=7;i++)
   {

   ONEDDR |= mask;                  // Set output to '0'
   Delay_7us();
   ONEDDR &= ~(mask);               // Set to input
   Delay_7us();
   data >>=1;
   if (
ONEPIN & mask) data |= 0x80;
   else
data &= 0x7f;
   Delay_70us();
   }
return
data;
}


/**********************************************************

Name:       void Delay_500us(void)

Description:   Delay of 500 us with 4Mhz resonator

Input:         none

Output:        none

Misc:      

**********************************************************/

void Delay_500us(void)
{
int
i;

i = 280;
while(
i--);
}


/**********************************************************

Name:       void Delay_70us(void)

Description:   Delay of 70 us with 4Mhz resonator

Input:         none

Output:        none

Misc:      

**********************************************************/

void Delay_70us(void)
{
char
i;

i = 43;
while(
i--);
}


/**********************************************************

Name:       void Delay_7us(void)

Description:   Delay of 7 us with 4Mhz resonator

Input:         none

Output:        none

Misc:      

**********************************************************/

void Delay_7us(void)
{
char
i;

i = 1;
while(
i--);
}