Version of Richard Lane's library, with #define CABLE_EXT_DELAYS to extend reset() settling delays for extension cable

Fork of DS18B20_1wire by Richard Lane

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