Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Devices/DS18B20.hpp@7:471901a04573, 2019-03-04 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Mar 04 08:10:00 2019 -0600
- Revision:
- 7:471901a04573
- Parent:
- 6:a8c83a2e6fa4
Updated to version 1.7.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 0:f77ad7f72d04 | 1 | /******************************************************************************* |
IanBenzMaxim | 0:f77ad7f72d04 | 2 | * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 0:f77ad7f72d04 | 3 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 0:f77ad7f72d04 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 0:f77ad7f72d04 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 0:f77ad7f72d04 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 0:f77ad7f72d04 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 0:f77ad7f72d04 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 0:f77ad7f72d04 | 10 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 0:f77ad7f72d04 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 0:f77ad7f72d04 | 13 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 0:f77ad7f72d04 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 0:f77ad7f72d04 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 0:f77ad7f72d04 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 0:f77ad7f72d04 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 0:f77ad7f72d04 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 0:f77ad7f72d04 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 0:f77ad7f72d04 | 21 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 0:f77ad7f72d04 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 0:f77ad7f72d04 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 0:f77ad7f72d04 | 25 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 0:f77ad7f72d04 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 0:f77ad7f72d04 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 0:f77ad7f72d04 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 0:f77ad7f72d04 | 30 | * ownership rights. |
IanBenzMaxim | 0:f77ad7f72d04 | 31 | *******************************************************************************/ |
IanBenzMaxim | 0:f77ad7f72d04 | 32 | |
IanBenzMaxim | 0:f77ad7f72d04 | 33 | #ifndef MaximInterface_DS18B20 |
IanBenzMaxim | 0:f77ad7f72d04 | 34 | #define MaximInterface_DS18B20 |
IanBenzMaxim | 0:f77ad7f72d04 | 35 | |
IanBenzMaxim | 0:f77ad7f72d04 | 36 | #include <MaximInterface/Links/SelectRom.hpp> |
IanBenzMaxim | 0:f77ad7f72d04 | 37 | #include <MaximInterface/Links/Sleep.hpp> |
IanBenzMaxim | 6:a8c83a2e6fa4 | 38 | #include <MaximInterface/Utilities/array_span.hpp> |
IanBenzMaxim | 0:f77ad7f72d04 | 39 | #include <MaximInterface/Utilities/Export.h> |
IanBenzMaxim | 0:f77ad7f72d04 | 40 | |
IanBenzMaxim | 0:f77ad7f72d04 | 41 | namespace MaximInterface { |
IanBenzMaxim | 0:f77ad7f72d04 | 42 | |
IanBenzMaxim | 7:471901a04573 | 43 | /// @brief DS18B20 Programmable Resolution 1-Wire Digital Thermometer |
IanBenzMaxim | 0:f77ad7f72d04 | 44 | /// @details The DS18B20 digital thermometer provides 9-bit to 12-bit |
IanBenzMaxim | 0:f77ad7f72d04 | 45 | /// Celsius temperature measurements and has an alarm function with |
IanBenzMaxim | 0:f77ad7f72d04 | 46 | /// nonvolatile user-programmable upper and lower trigger points. The |
IanBenzMaxim | 0:f77ad7f72d04 | 47 | /// DS18B20 communicates over a 1-Wire bus that by definition requires |
IanBenzMaxim | 0:f77ad7f72d04 | 48 | /// only one data line (and ground) for communication with a central |
IanBenzMaxim | 0:f77ad7f72d04 | 49 | /// microprocessor. In addition, the DS18B20 can derive power directly |
IanBenzMaxim | 0:f77ad7f72d04 | 50 | /// from the data line ("parasite power"), eliminating the need for an |
IanBenzMaxim | 0:f77ad7f72d04 | 51 | /// external power supply. |
IanBenzMaxim | 0:f77ad7f72d04 | 52 | class DS18B20 { |
IanBenzMaxim | 0:f77ad7f72d04 | 53 | public: |
IanBenzMaxim | 0:f77ad7f72d04 | 54 | enum ErrorValue { CrcError = 1, DataError }; |
IanBenzMaxim | 0:f77ad7f72d04 | 55 | |
IanBenzMaxim | 0:f77ad7f72d04 | 56 | static const uint_least8_t nineBitResolution = 0x1F; |
IanBenzMaxim | 0:f77ad7f72d04 | 57 | static const uint_least8_t tenBitResolution = 0x3F; |
IanBenzMaxim | 0:f77ad7f72d04 | 58 | static const uint_least8_t elevenBitResolution = 0x5F; |
IanBenzMaxim | 0:f77ad7f72d04 | 59 | static const uint_least8_t twelveBitResolution = 0x7F; |
IanBenzMaxim | 0:f77ad7f72d04 | 60 | |
IanBenzMaxim | 0:f77ad7f72d04 | 61 | /// Holds the contents of the device scratchpad. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 62 | typedef array_span<uint_least8_t, 8> Scratchpad; |
IanBenzMaxim | 0:f77ad7f72d04 | 63 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 64 | DS18B20(Sleep & sleep, OneWireMaster & master, const SelectRom & selectRom) |
IanBenzMaxim | 0:f77ad7f72d04 | 65 | : selectRom(selectRom), master(&master), sleep(&sleep), resolution(0) {} |
IanBenzMaxim | 0:f77ad7f72d04 | 66 | |
IanBenzMaxim | 6:a8c83a2e6fa4 | 67 | void setSleep(Sleep & sleep) { this->sleep = &sleep; } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 68 | |
IanBenzMaxim | 0:f77ad7f72d04 | 69 | void setMaster(OneWireMaster & master) { this->master = &master; } |
IanBenzMaxim | 6:a8c83a2e6fa4 | 70 | |
IanBenzMaxim | 0:f77ad7f72d04 | 71 | void setSelectRom(const SelectRom & selectRom) { |
IanBenzMaxim | 0:f77ad7f72d04 | 72 | this->selectRom = selectRom; |
IanBenzMaxim | 0:f77ad7f72d04 | 73 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 74 | |
IanBenzMaxim | 0:f77ad7f72d04 | 75 | /// Initializes the device for first time use. |
IanBenzMaxim | 0:f77ad7f72d04 | 76 | MaximInterface_EXPORT error_code initialize(); |
IanBenzMaxim | 0:f77ad7f72d04 | 77 | |
IanBenzMaxim | 0:f77ad7f72d04 | 78 | /// Write Scratchpad Command |
IanBenzMaxim | 0:f77ad7f72d04 | 79 | /// @details If the result of a temperature measurement is higher |
IanBenzMaxim | 0:f77ad7f72d04 | 80 | /// than TH or lower than TL, an alarm flag inside the device is |
IanBenzMaxim | 0:f77ad7f72d04 | 81 | /// set. This flag is updated with every temperature measurement. |
IanBenzMaxim | 0:f77ad7f72d04 | 82 | /// As long as the alarm flag is set, the DS1920 will respond to |
IanBenzMaxim | 0:f77ad7f72d04 | 83 | /// the alarm search command. |
IanBenzMaxim | 0:f77ad7f72d04 | 84 | /// @param[in] th 8-bit upper temperature threshold, MSB indicates sign. |
IanBenzMaxim | 0:f77ad7f72d04 | 85 | /// @param[in] tl 8-bit lower temperature threshold, LSB indicates sign. |
IanBenzMaxim | 0:f77ad7f72d04 | 86 | /// @param[in] res Resolution of the DS18B20. |
IanBenzMaxim | 0:f77ad7f72d04 | 87 | MaximInterface_EXPORT error_code writeScratchpad(uint_least8_t th, |
IanBenzMaxim | 0:f77ad7f72d04 | 88 | uint_least8_t tl, |
IanBenzMaxim | 0:f77ad7f72d04 | 89 | uint_least8_t res); |
IanBenzMaxim | 0:f77ad7f72d04 | 90 | |
IanBenzMaxim | 0:f77ad7f72d04 | 91 | /// Read Scratchpad Command |
IanBenzMaxim | 0:f77ad7f72d04 | 92 | /// @param[out] scratchpad Contents of scratchpad. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 93 | MaximInterface_EXPORT error_code readScratchpad(Scratchpad::span scratchpad); |
IanBenzMaxim | 0:f77ad7f72d04 | 94 | |
IanBenzMaxim | 0:f77ad7f72d04 | 95 | /// Copy Scratchpad Command |
IanBenzMaxim | 0:f77ad7f72d04 | 96 | /// @details This command copies from the scratchpad into the |
IanBenzMaxim | 0:f77ad7f72d04 | 97 | /// EEPROM of the DS18B20, storing the temperature trigger bytes |
IanBenzMaxim | 0:f77ad7f72d04 | 98 | /// and resolution in nonvolatile memory. |
IanBenzMaxim | 0:f77ad7f72d04 | 99 | MaximInterface_EXPORT error_code copyScratchpad(); |
IanBenzMaxim | 0:f77ad7f72d04 | 100 | |
IanBenzMaxim | 0:f77ad7f72d04 | 101 | /// Read Power Supply command |
IanBenzMaxim | 0:f77ad7f72d04 | 102 | /// @details This command determines if the DS18B20 is parasite |
IanBenzMaxim | 0:f77ad7f72d04 | 103 | /// powered or has a local supply |
IanBenzMaxim | 0:f77ad7f72d04 | 104 | /// @param[out] localPower |
IanBenzMaxim | 0:f77ad7f72d04 | 105 | /// True if the device is powered by a local power supply, or false if the |
IanBenzMaxim | 0:f77ad7f72d04 | 106 | /// device is parasitically powered. |
IanBenzMaxim | 0:f77ad7f72d04 | 107 | MaximInterface_EXPORT error_code readPowerSupply(bool & localPower); |
IanBenzMaxim | 0:f77ad7f72d04 | 108 | |
IanBenzMaxim | 0:f77ad7f72d04 | 109 | /// Convert Temperature Command |
IanBenzMaxim | 0:f77ad7f72d04 | 110 | /// @details This command begins a temperature conversion. |
IanBenzMaxim | 0:f77ad7f72d04 | 111 | MaximInterface_EXPORT error_code convertTemperature(); |
IanBenzMaxim | 0:f77ad7f72d04 | 112 | |
IanBenzMaxim | 0:f77ad7f72d04 | 113 | /// Recall Command |
IanBenzMaxim | 0:f77ad7f72d04 | 114 | /// @details This command recalls the temperature trigger values |
IanBenzMaxim | 0:f77ad7f72d04 | 115 | /// and resolution stored in EEPROM to the scratchpad. |
IanBenzMaxim | 0:f77ad7f72d04 | 116 | MaximInterface_EXPORT error_code recallEeprom(); |
IanBenzMaxim | 0:f77ad7f72d04 | 117 | |
IanBenzMaxim | 0:f77ad7f72d04 | 118 | MaximInterface_EXPORT static const error_category & errorCategory(); |
IanBenzMaxim | 0:f77ad7f72d04 | 119 | |
IanBenzMaxim | 0:f77ad7f72d04 | 120 | private: |
IanBenzMaxim | 0:f77ad7f72d04 | 121 | SelectRom selectRom; |
IanBenzMaxim | 0:f77ad7f72d04 | 122 | OneWireMaster * master; |
IanBenzMaxim | 0:f77ad7f72d04 | 123 | const Sleep * sleep; |
IanBenzMaxim | 0:f77ad7f72d04 | 124 | uint_least8_t resolution; |
IanBenzMaxim | 0:f77ad7f72d04 | 125 | }; |
IanBenzMaxim | 0:f77ad7f72d04 | 126 | |
IanBenzMaxim | 0:f77ad7f72d04 | 127 | /// Reads the current temperature as an integer value with decimal. |
IanBenzMaxim | 7:471901a04573 | 128 | /// @param ds18b20 Device to read. |
IanBenzMaxim | 6:a8c83a2e6fa4 | 129 | /// @param[out] temperature Temperature in degrees Celsius multiplied by 16. |
IanBenzMaxim | 0:f77ad7f72d04 | 130 | MaximInterface_EXPORT error_code readTemperature(DS18B20 & ds18b20, |
IanBenzMaxim | 0:f77ad7f72d04 | 131 | int & temperature); |
IanBenzMaxim | 0:f77ad7f72d04 | 132 | |
IanBenzMaxim | 0:f77ad7f72d04 | 133 | inline error_code make_error_code(DS18B20::ErrorValue e) { |
IanBenzMaxim | 0:f77ad7f72d04 | 134 | return error_code(e, DS18B20::errorCategory()); |
IanBenzMaxim | 0:f77ad7f72d04 | 135 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 136 | |
IanBenzMaxim | 0:f77ad7f72d04 | 137 | } // namespace MaximInterface |
IanBenzMaxim | 0:f77ad7f72d04 | 138 | |
IanBenzMaxim | 0:f77ad7f72d04 | 139 | #endif |