1-Wire DS18B20 Thermometer

Measuring room or object temperatures with the DS18B20. It is a digital component wich does all the work for you. You only need to COMMAND it do the measurement. A short moment later you can request 2 bytes containing the temperature.

Of course there is a protocal wich says how to communicate. See 1-Wire and the DS18B20 datasheet.

How 1-Wire works (by Maxim): http://www.maxim-ic.com/products/1-wire/flash/overview/index.cfm

Maxim 1-Wire

Maxim 1-Wire, how to communicate

Used parts:

  • PIC16F689
    1. FLASH: 4096 Words
    2. RAM: 256 Bytes
    3. IO Pins: 18
    4. Clock internal: 8MHz internal, 4MHz default config
    5. Clock external:up to 20MHz external
  • LCD 2×16, HD44780 Compatible
  • DS18B20

All codings are done with Microchip’s MPLAB X IDE and code is compiled by HI TECH’s (lite) tools. This software can be downloaded at Microchip’s website.

HEX files generated by the compiler will be uploaded to the microcontroller with XWisp (software) and Wisp628/648 (hardware). In-Circuit-Serial-Programming ICSP.

For 1-wire code see: PIC16 C Sample 1-Wire

unsigned int address[8];  //For storing 64bit 1-wire address
bit devicePresent = 0;
void main(void)
{
   if(owReset())  //Device present
   {
      owWriteByte(0x33);  //Read ROM command (request address)
      for(int i = 0; i < 8; i++)
      {
         int b = owReadByte();
         address[i] = b;
      }
      devicePresent = 1; //Flag for having found the address
   }

   while(1)//Infinite loop
   {
      owMatchRom(address); //First send device address
      owWriteByte(0x44);   //0x44, send command for start converting temp

      //wait 2 seconds, device is calculating temp
      __delay_ms(2000);

      //Now request the result by sending the address again 
      owMatchRom(address);
      //followed by the read command 0xBE
      owWriteByte(0xBE);
      
      //9 bytes are waiting to be read
      //see DS18B20 datasheet
      int answer[9];
      for(int i = 0; i < 9; i++)
      {
         answer[i] = owReadByte();
      }

      //First 2 bytes contain temp (celsius)
      //LSB and MSB, google it......
      int Temp = (answer[1] << 8) + answer[0]; Temp = Temp >> 4; //Remove data behind . or , 
      //This will give 18 | 19 and not 18.45 or 19.5

      //Temp will now contains temperature in celsius

      char text[3]; //space for three characters
      sprintf(text,"%3d",Temp); //Convert Temp to ASCII characters

      lcdtxt(text); //show result on you lcd
      serialwrite(text); //write result to your computer?

      __delay_ms(5000); //Repeat every 5 seconds
   }
}

ow functions

Update: 2016-07-07

void owMatchRom(unsigned int * id)
{
    owReset();
    owWriteByte(0x55);  //Match ROM cmd
    //Now write device address/id
    for(int i = 0;i<8;i++)
    {
        owWriteByte(id[i]);
    }
}

int owReset(void)
{
    char state = 0;
    OW_TRIS = 0;    //Set as output
    OW_PORT = 0;    //Drive Low
    __delay_us(480);

    OW_TRIS = 1;    //Release, Set back as input
    __delay_us(70);
    state = !OW_PORT;
    __delay_us(410);
    return state;
}
void owWriteByte(unsigned int data)
{
    int loop;
    for(loop = 0; loop < 8; loop++) { owWriteBit(data &0x01); data >>= 1;
    }
}
void owWriteBit(unsigned int b)
{
    OW_TRIS = 0;    //Set as output
    OW_PORT = 0;    //Drive low

    if(b==1)
    {
        __delay_us(3);
        OW_TRIS = 1;    //Release, set as input
        __delay_us(62);
    }
    else
    {
        __delay_us(57);
        OW_TRIS = 1;    //Release, set as input
        __delay_us(7);
    }
}
int owReadByte(void)
{
    int loop, result = 0;
    for(loop = 0; loop < 8; loop++) { result >>= 1;
        if(owReadBit())
            result |= 0x80;
    }
    return result;
}
int owReadBit(void)
{
    unsigned int iReadState = 0;
    OW_TRIS = 0;    //Set as output
    OW_PORT = 0;    //Drive low
    __delay_us(4);
    OW_TRIS = 1;    //Release, set as input
    __delay_us(8);
    iReadState = OW_PORT;
    return iReadState;
}

 

Pic16f689 1wire Test
Pic16f689 1wire Test
pic16f689_1wire_test.c
8.6 KiB
1300 Downloads
Details...

3 gedachten over “1-Wire DS18B20 Thermometer

  1. LD

    hi!

    Where is prototype this function?

    owMatchRom(address)

    main.c:95: warning: (1464) number of arguments passed to function “_owMatchRom” does not match function’s prototype

    1. Robin Bericht auteur

      Hi Ledo,

      Please see the update post.
      I put the extra functions in it, and attached an demo .c file.

      I hope you get it working now.

      Regards,
      Robin

Geef een antwoord

Het e-mailadres wordt niet gepubliceerd.

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.