Interface for Dallas DS18B20 digital thermometer device.

Dependents:   WIZ550io_Xively_Demo ESP8266-WEB-Mbed-Controller BrewCtrl Brew_Keg ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS18B20.h Source File

DS18B20.h

00001 #ifndef DS18B20_H
00002 #define DS18B20_H
00003 
00004 #include "mbed.h"
00005 #include <stdint.h>
00006 
00007 /** A DS18B20 Dallas 1-wire digital thermometer interface.
00008  *
00009  * Currently supports 9, 10, 11 or 12-bit conversions. Fewer bits
00010  * require less conversion time from 93.75ms to 750ms. Also supports
00011  * reading back the 8-byte internal ROM. Orignal code copied from
00012  * DS18B20 C program by Niall Cooling (thanks!) and wrapped in C++
00013  * class by this library.
00014  *
00015  * @code
00016  * #include "mbed.h"
00017  * #include <stdint.h>
00018  * #include "DS18B20.h"
00019  *
00020  * Serial pc(USBTX, USBRX);     // serial comms over usb back to console
00021  * DS18B20 thermom(p16, DS18B20::RES_12_BIT); // Dallas 1-wire
00022  *
00023  * int main() {
00024  *   pc.printf("DS18B20 Configuration\n\r");
00025  *   
00026  *   DS18B20::ROM_Code_t ROM_Code;
00027  *   thermom.ReadROM(&ROM_Code);
00028  *   pc.printf("Family code: 0x%X\n\r", ROM_Code.BYTES.familyCode);
00029  *   pc.printf("Serial Number: ");
00030  *   for (unsigned i = 6; i != 0; --i) {
00031  *       pc.printf("%02X%s", ROM_Code.BYTES.serialNo[i-1], (i != 1)?":":"\r\n");
00032  *   }
00033  *   pc.printf("CRC: 0x%X\r\n", ROM_Code.BYTES.CRC);
00034  *   
00035  *   pc.printf("\n\rRunning temperature conversion...\n\r");
00036  *   while (1) {
00037  *       pc.printf("Temperature is: %.4fC\n\r", thermom.GetTemperature());
00038  *       wait(10);
00039  *   }
00040  * }
00041  * @endcode
00042  */
00043 
00044 class DS18B20
00045 {
00046 public:
00047     /** Value to return when Reset() fails */
00048     enum {INVALID_TEMPERATURE = -10000};
00049     
00050     /** Temperature conversion dit width resolutions */
00051     enum RESOLUTION { RES_9_BIT=0x1f,    /**< 93.75ms */
00052                       RES_10_BIT=0x3f,   /**< 187.5ms */
00053                       RES_11_BIT=0x5f,   /**< 375ms */
00054                       RES_12_BIT=0x7f    /**< 750ms */
00055     };
00056     
00057     /** Holds 8-byte internal ROM */
00058     typedef union {
00059         uint8_t rom[8];
00060         struct {
00061             uint8_t familyCode;  /**< Family Code */
00062             uint8_t serialNo[6]; /**< Serial Number */
00063             uint8_t CRC;         /**< CRC check byte */
00064         } BYTES;
00065     } ROM_Code_t;
00066     
00067     /** Device onboard register layout (for reference only, not currently used) */
00068     typedef struct {
00069         uint8_t    LSB; /**< LSB of converted temperature */
00070         uint8_t    MSB; /**< MSB of converted temperature */
00071         uint8_t    Th; /**< Threshold for high alarm */
00072         uint8_t    Tl; /**< Threshold for low alarm */
00073         uint8_t    config; /**< Conversion resultion */
00074         uint8_t    reserved0xFF;
00075         uint8_t    reserved0xCH;
00076         uint8_t    reserved0x10;
00077         uint8_t    CRC; /**< CRC check byte */
00078     } ScratchPad_t;
00079     
00080     /** Create a Dallas DS18B20 1-wire interface
00081      *
00082      * @param pin         Pin to use for 1-wire interface (bidirectional I/O)
00083      * @param resolution  Sets the conversion bit width (using RESOLUTION enum)
00084      */
00085     DS18B20(PinName pin, unsigned resolution);
00086     
00087     /** Destructor */
00088     ~DS18B20();
00089 
00090     /** Performs conversion in DS18B20 and then reads and converts returned temperature
00091      *  to floating point. For many applications this is the only required method that
00092      *  needs to be used.
00093      */
00094     float GetTemperature();
00095 
00096     /** Performs conversion but does not read back temperature. Not needed if
00097      *  GetTemperature() is used as this calls DoConversion() itself. */
00098     unsigned DoConversion();
00099     
00100     /** The method that GetTemperature() calls to do all the conversion and reading
00101      *  but this method returns a 32-bit signed integer. The integer contains 4
00102      *  fractional LSBs. Sometimes referred to as s28.4 format. */
00103     int RawTemperature();
00104     
00105     /** Reads and returns the 8-byte internal ROM */
00106     int ReadROM(ROM_Code_t *ROM_Code);
00107     
00108     /** Sets the conversion resolution with RESOLUTION enum (9-12 bits signed) */
00109     unsigned SetResolution(unsigned resolution);
00110 
00111 protected:
00112 
00113     // Timing delay for 1-wire serial standard option
00114     enum DELAY { A = 6, B = 64, C = 60, D = 10, E = 9, F = 55, G = 0, H = 480, I = 70, J = 410 };
00115 
00116     // Device byte commands over 1-wire serial
00117     enum COMMANDS { READ_ROM = 0x33, CONVERT = 0x44, READ_SCRATCHPAD = 0xBE,  WRITE_SCRATCHPAD = 0x4E, SKIP_ROM = 0xCC };
00118     
00119     // Methods from DS1Wire.h
00120     unsigned Reset();
00121     void WriteBit(unsigned bit);
00122     unsigned ReadBit();
00123     void WriteByte(unsigned byte);
00124     unsigned ReadByte();
00125 
00126     // The pin used for the Dallas 1-wire interface
00127     DigitalInOut _pin;
00128 };
00129 
00130 #endif