Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS18B20.hpp Source File

DS18B20.hpp

00001 /*******************************************************************************
00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterface_DS18B20
00034 #define MaximInterface_DS18B20
00035 
00036 #include <MaximInterface/Links/SelectRom.hpp>
00037 #include <MaximInterface/Links/Sleep.hpp>
00038 #include <MaximInterface/Utilities/array_span.hpp>
00039 #include <MaximInterface/Utilities/Export.h>
00040 
00041 namespace MaximInterface {
00042 
00043 /// @brief DS18B20 Programmable Resolution 1-Wire Digital Thermometer
00044 /// @details The DS18B20 digital thermometer provides 9-bit to 12-bit
00045 /// Celsius temperature measurements and has an alarm function with
00046 /// nonvolatile user-programmable upper and lower trigger points. The
00047 /// DS18B20 communicates over a 1-Wire bus that by definition requires
00048 /// only one data line (and ground) for communication with a central
00049 /// microprocessor. In addition, the DS18B20 can derive power directly
00050 /// from the data line ("parasite power"), eliminating the need for an
00051 /// external power supply.
00052 class DS18B20 {
00053 public:
00054   enum ErrorValue { CrcError = 1, DataError };
00055 
00056   static const uint_least8_t nineBitResolution = 0x1F;
00057   static const uint_least8_t tenBitResolution = 0x3F;
00058   static const uint_least8_t elevenBitResolution = 0x5F;
00059   static const uint_least8_t twelveBitResolution = 0x7F;
00060 
00061   /// Holds the contents of the device scratchpad.
00062   typedef array_span<uint_least8_t, 8> Scratchpad;
00063 
00064   DS18B20(Sleep & sleep, OneWireMaster & master, const SelectRom & selectRom)
00065       : selectRom(selectRom), master(&master), sleep(&sleep), resolution(0) {}
00066 
00067   void setSleep(Sleep & sleep) { this->sleep = &sleep; }
00068   
00069   void setMaster(OneWireMaster & master) { this->master = &master; }
00070   
00071   void setSelectRom(const SelectRom & selectRom) {
00072     this->selectRom = selectRom;
00073   }
00074 
00075   /// Initializes the device for first time use.
00076   MaximInterface_EXPORT error_code initialize();
00077 
00078   /// Write Scratchpad Command
00079   /// @details If the result of a temperature measurement is higher
00080   /// than TH or lower than TL, an alarm flag inside the device is
00081   /// set. This flag is updated with every temperature measurement.
00082   /// As long as the alarm flag is set, the DS1920 will respond to
00083   /// the alarm search command.
00084   /// @param[in] th 8-bit upper temperature threshold, MSB indicates sign.
00085   /// @param[in] tl 8-bit lower temperature threshold, LSB indicates sign.
00086   /// @param[in] res Resolution of the DS18B20.
00087   MaximInterface_EXPORT error_code writeScratchpad(uint_least8_t th,
00088                                                    uint_least8_t tl,
00089                                                    uint_least8_t res);
00090 
00091   /// Read Scratchpad Command
00092   /// @param[out] scratchpad Contents of scratchpad.
00093   MaximInterface_EXPORT error_code readScratchpad(Scratchpad::span scratchpad);
00094 
00095   /// Copy Scratchpad Command
00096   /// @details This command copies from the scratchpad into the
00097   /// EEPROM of the DS18B20, storing the temperature trigger bytes
00098   /// and resolution in nonvolatile memory.
00099   MaximInterface_EXPORT error_code copyScratchpad();
00100 
00101   /// Read Power Supply command
00102   /// @details This command determines if the DS18B20 is parasite
00103   /// powered or has a local supply
00104   /// @param[out] localPower
00105   /// True if the device is powered by a local power supply, or false if the
00106   /// device is parasitically powered.
00107   MaximInterface_EXPORT error_code readPowerSupply(bool & localPower);
00108 
00109   /// Convert Temperature Command
00110   /// @details This command begins a temperature conversion.
00111   MaximInterface_EXPORT error_code convertTemperature();
00112 
00113   /// Recall Command
00114   /// @details This command recalls the temperature trigger values
00115   /// and resolution stored in EEPROM to the scratchpad.
00116   MaximInterface_EXPORT error_code recallEeprom();
00117 
00118   MaximInterface_EXPORT static const error_category & errorCategory();
00119 
00120 private:
00121   SelectRom selectRom;
00122   OneWireMaster * master;
00123   const Sleep * sleep;
00124   uint_least8_t resolution;
00125 };
00126 
00127 /// Reads the current temperature as an integer value with decimal.
00128 /// @param ds18b20 Device to read.
00129 /// @param[out] temperature Temperature in degrees Celsius multiplied by 16.
00130 MaximInterface_EXPORT error_code readTemperature(DS18B20 & ds18b20,
00131                                                  int & temperature);
00132 
00133 inline error_code make_error_code(DS18B20::ErrorValue e) {
00134   return error_code(e, DS18B20::errorCategory());
00135 }
00136 
00137 } // namespace MaximInterface
00138 
00139 #endif