Dallas / Maxim DS1820 1-Wire library. For communication with multiple DS1820 on a single 1-Wire bus. Also supports DS18S20 and DS18B20.

Committer:
lamell
Date:
Sun Feb 17 22:21:24 2019 +0000
Revision:
5:31e245719610
Parent:
3:3e89eafb60c2
No changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael_ 2:ee820a991b95 1 /* mbed DS1820 Library, for the Dallas (Maxim) 1-Wire Digital Thermometer
Michael_ 2:ee820a991b95 2 * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com
Michael_ 2:ee820a991b95 3 *
Michael_ 2:ee820a991b95 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Michael_ 2:ee820a991b95 5 * of this software and associated documentation files (the "Software"), to deal
Michael_ 2:ee820a991b95 6 * in the Software without restriction, including without limitation the rights
Michael_ 2:ee820a991b95 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Michael_ 2:ee820a991b95 8 * copies of the Software, and to permit persons to whom the Software is
Michael_ 2:ee820a991b95 9 * furnished to do so, subject to the following conditions:
Michael_ 2:ee820a991b95 10 *
Michael_ 2:ee820a991b95 11 * The above copyright notice and this permission notice shall be included in
Michael_ 2:ee820a991b95 12 * all copies or substantial portions of the Software.
Michael_ 2:ee820a991b95 13 *
Michael_ 2:ee820a991b95 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Michael_ 2:ee820a991b95 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michael_ 2:ee820a991b95 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michael_ 2:ee820a991b95 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
lamell 3:3e89eafb60c2 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FRom,
Michael_ 2:ee820a991b95 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Michael_ 2:ee820a991b95 20 * THE SOFTWARE.
Michael_ 2:ee820a991b95 21 */
lamell 3:3e89eafb60c2 22
Michael_ 2:ee820a991b95 23 #ifndef MBED_DS1820_H
Michael_ 2:ee820a991b95 24 #define MBED_DS1820_H
lamell 3:3e89eafb60c2 25
Michael_ 2:ee820a991b95 26 #include "mbed.h"
lamell 3:3e89eafb60c2 27
Michael_ 2:ee820a991b95 28 // ****** THIS GLOBAL VARIABLES MUST BE DEFINED IN main.cpp
lamell 3:3e89eafb60c2 29
Michael_ 2:ee820a991b95 30 // Global variables shared between all DS1820 objects
Michael_ 2:ee820a991b95 31 //bool DS1820_done_flag;
Michael_ 2:ee820a991b95 32 //int DS1820_last_descrepancy;
lamell 3:3e89eafb60c2 33 //char DS1820_search_RomM[8];
lamell 3:3e89eafb60c2 34
Michael_ 2:ee820a991b95 35 /** DS1820 Dallas 1-Wire Temperature Probe
Michael_ 2:ee820a991b95 36 *
Michael_ 2:ee820a991b95 37 * Example:
Michael_ 2:ee820a991b95 38 * @code
Michael_ 2:ee820a991b95 39 * #include "mbed.h"
Michael_ 2:ee820a991b95 40 *
Michael_ 2:ee820a991b95 41 * #include "TextLCD.h"
Michael_ 2:ee820a991b95 42 * #include "DS1820.h"
Michael_ 2:ee820a991b95 43 *
Michael_ 2:ee820a991b95 44 * TextLCD lcd(p25, p26, p21, p22, p23, p24, TextLCD::LCD16x2); // rs, e, d0-d3, layout
Michael_ 2:ee820a991b95 45 *
Michael_ 2:ee820a991b95 46 * const int MAX_PROBES = 16;
Michael_ 2:ee820a991b95 47 * DS1820* probe[MAX_PROBES];
Michael_ 2:ee820a991b95 48 *
Michael_ 2:ee820a991b95 49 * int main() {
Michael_ 2:ee820a991b95 50 * int i;
Michael_ 2:ee820a991b95 51 * int devices_found=0;
Michael_ 2:ee820a991b95 52 * // Initialize the probe array to DS1820 objects
Michael_ 2:ee820a991b95 53 * for (i = 0; i < MAX_PROBES; i++)
Michael_ 2:ee820a991b95 54 * probe[i] = new DS1820(p27);
Michael_ 2:ee820a991b95 55 * // Initialize global state variables
lamell 3:3e89eafb60c2 56 * probe[0]->search_RomMM_setup();
Michael_ 2:ee820a991b95 57 * // Loop to find all devices on the data line
lamell 3:3e89eafb60c2 58 * while (probe[devices_found]->search_RomM() and devices_found<MAX_PROBES-1)
Michael_ 2:ee820a991b95 59 * devices_found++;
lamell 3:3e89eafb60c2 60 * // If maximum number of probes are found,
Michael_ 2:ee820a991b95 61 * // bump the counter to include the last array entry
lamell 3:3e89eafb60c2 62 * if (probe[devices_found]->RomMMMM[0] != 0xFF)
Michael_ 2:ee820a991b95 63 * devices_found++;
lamell 3:3e89eafb60c2 64 *
Michael_ 2:ee820a991b95 65 * lcd.cls();
Michael_ 2:ee820a991b95 66 * if (devices_found==0)
Michael_ 2:ee820a991b95 67 * lcd.printf("No devices found");
Michael_ 2:ee820a991b95 68 * else {
Michael_ 2:ee820a991b95 69 * while (true) {
Michael_ 2:ee820a991b95 70 * probe[0]->convert_temperature(DS1820::all_devices);
Michael_ 2:ee820a991b95 71 * lcd.cls();
Michael_ 2:ee820a991b95 72 * for (i=0; i<devices_found; i++) {
Michael_ 2:ee820a991b95 73 * lcd.printf("%3.1f ",probe[i]->temperature('f'));
Michael_ 2:ee820a991b95 74 * }
Michael_ 2:ee820a991b95 75 * }
Michael_ 2:ee820a991b95 76 * }
Michael_ 2:ee820a991b95 77 * }
Michael_ 2:ee820a991b95 78 * @endcode
Michael_ 2:ee820a991b95 79 */
Michael_ 2:ee820a991b95 80
lamell 3:3e89eafb60c2 81 class DS1820
lamell 3:3e89eafb60c2 82 {
Michael_ 2:ee820a991b95 83 public:
lamell 3:3e89eafb60c2 84 enum devices {
Michael_ 2:ee820a991b95 85 this_device, // command applies to only this device
lamell 3:3e89eafb60c2 86 all_devices
lamell 3:3e89eafb60c2 87 }; // command applies to all devices
lamell 3:3e89eafb60c2 88
Michael_ 2:ee820a991b95 89 /** Create a probe object connected to the specified pins
Michael_ 2:ee820a991b95 90 *
Michael_ 2:ee820a991b95 91 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 92 * @param power_pin DigitalOut pin to control the power MOSFET
Michael_ 2:ee820a991b95 93 */
Michael_ 2:ee820a991b95 94 DS1820(PinName data_pin, PinName power_pin); // Constructor with parasite power pin
lamell 3:3e89eafb60c2 95
Michael_ 2:ee820a991b95 96 /** Create a probe object connected to the specified pin
Michael_ 2:ee820a991b95 97 * this is used when all probes are externally powered
Michael_ 2:ee820a991b95 98 *
Michael_ 2:ee820a991b95 99 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 100 */
Michael_ 2:ee820a991b95 101 DS1820(PinName data_pin);
lamell 3:3e89eafb60c2 102
lamell 3:3e89eafb60c2 103 /** RomMMM is a copy of the internal DS1820's Rom
lamell 3:3e89eafb60c2 104 * It is created during the search_RomM() or search_alarm() commands
Michael_ 2:ee820a991b95 105 *
lamell 3:3e89eafb60c2 106 * RomMMM[0] is the Dallas Family Code
lamell 3:3e89eafb60c2 107 * RomMMM[1] thru RomMMM[6] is the 48-bit unique serial number
lamell 3:3e89eafb60c2 108 * RomMMM[7] is the device ow_CRC
Michael_ 2:ee820a991b95 109 */
lamell 3:3e89eafb60c2 110 char Rom[8];
lamell 3:3e89eafb60c2 111 #define FAMILY_CODE Rom[0]
lamell 3:3e89eafb60c2 112 #define FAMILY_CODE_DS1820 0x10
lamell 3:3e89eafb60c2 113 #define FAMILY_CODE_DS18S20 0x10
lamell 3:3e89eafb60c2 114 #define FAMILY_CODE_DS18B20 0x28
lamell 3:3e89eafb60c2 115
Michael_ 2:ee820a991b95 116 /** RAM is a copy of the internal DS1820's RAM
Michael_ 2:ee820a991b95 117 * It's updated during the read_RAM() command
lamell 3:3e89eafb60c2 118 * which is automaticaly called fRom any function
Michael_ 2:ee820a991b95 119 * using the RAM values.
Michael_ 2:ee820a991b95 120 */
Michael_ 2:ee820a991b95 121 char RAM[9];
lamell 3:3e89eafb60c2 122
Michael_ 2:ee820a991b95 123 /* This function copies the DS1820's RAM into the object's
Michael_ 2:ee820a991b95 124 * RAM[].
Michael_ 2:ee820a991b95 125 */
Michael_ 2:ee820a991b95 126 void read_RAM();
lamell 3:3e89eafb60c2 127
Michael_ 2:ee820a991b95 128 /** This routine initializes the global variables used in
lamell 3:3e89eafb60c2 129 * the search_Rom() and search_alarm() funtions. It should
Michael_ 2:ee820a991b95 130 * be called once before looping to find devices.
Michael_ 2:ee820a991b95 131 */
lamell 3:3e89eafb60c2 132 void search_Rom_setup();
lamell 3:3e89eafb60c2 133
Michael_ 2:ee820a991b95 134 /** This routine will search for an unidentified device
lamell 3:3e89eafb60c2 135 * on the bus. It uses the variables in search_RomMMMM_setup
lamell 3:3e89eafb60c2 136 * to remember the pervious RomMMMM address found.
Michael_ 2:ee820a991b95 137 * It will return FALSE if there were no new devices
Michael_ 2:ee820a991b95 138 * discovered on the bus.
Michael_ 2:ee820a991b95 139 */
lamell 3:3e89eafb60c2 140 bool search_Rom();
lamell 3:3e89eafb60c2 141
Michael_ 2:ee820a991b95 142 /** This routine will search for an unidentified device
lamell 3:3e89eafb60c2 143 * which has the temperature alarm bit set. It uses the
lamell 3:3e89eafb60c2 144 * variables in search_RomMMM_setup to remember the pervious
lamell 3:3e89eafb60c2 145 * RomMMM address found. It will return FALSE if there were
Michael_ 2:ee820a991b95 146 * no new devices with alarms discovered on the bus.
Michael_ 2:ee820a991b95 147 */
Michael_ 2:ee820a991b95 148 bool search_alarm();
lamell 3:3e89eafb60c2 149
lamell 3:3e89eafb60c2 150 /** This routine will read the RomMMMM (Family code, serial number
lamell 3:3e89eafb60c2 151 * and Checksum) fRomM a dedicated device on the bus.
Michael_ 2:ee820a991b95 152 *
lamell 3:3e89eafb60c2 153 * NOTE: This command can only be used when there is only one
lamell 3:3e89eafb60c2 154 * DS1820 on the bus. If this command is used when there
lamell 3:3e89eafb60c2 155 * is more than one slave present on the bus, a data
lamell 3:3e89eafb60c2 156 * collision will occur when all the DS1820s attempt to
Michael_ 2:ee820a991b95 157 * respond at the same time.
Michael_ 2:ee820a991b95 158 */
lamell 3:3e89eafb60c2 159 void read_Rom();
lamell 3:3e89eafb60c2 160
Michael_ 2:ee820a991b95 161 /** This routine will initiate the temperature conversion within
lamell 3:3e89eafb60c2 162 * a DS1820. There is a built in 750ms delay to allow the
Michael_ 2:ee820a991b95 163 * conversion to complete.
Michael_ 2:ee820a991b95 164 *
Michael_ 2:ee820a991b95 165 * To update all probes on the bus, use a statement such as this:
Michael_ 2:ee820a991b95 166 * probe[0]->convert_temperature(DS1820::all_devices);
Michael_ 2:ee820a991b95 167 *
Michael_ 2:ee820a991b95 168 * @param allows the fnction to apply to a specific device or
Michael_ 2:ee820a991b95 169 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 170 */
Michael_ 2:ee820a991b95 171 void convert_temperature(devices device=this_device);
lamell 3:3e89eafb60c2 172
Michael_ 2:ee820a991b95 173 /** This function will return the probe temperature. This function
Michael_ 2:ee820a991b95 174 * uses the count remainding values to interpolate the temperature
Michael_ 2:ee820a991b95 175 * to about 1/150th of a degree. Whereas the probe is not spec to
Michael_ 2:ee820a991b95 176 * that precision. It does seem to give a smooth reading to the
Michael_ 2:ee820a991b95 177 * tenth of a degree.
Michael_ 2:ee820a991b95 178 *
Michael_ 2:ee820a991b95 179 * @param scale, may be either 'c' or 'f'
Michael_ 2:ee820a991b95 180 * @returns temperature for that scale
Michael_ 2:ee820a991b95 181 */
Michael_ 2:ee820a991b95 182 float temperature(char scale='c');
lamell 3:3e89eafb60c2 183
lamell 3:3e89eafb60c2 184 /** This function calculates the Rom checksum and compares it to the
lamell 3:3e89eafb60c2 185 * ow_CRC value stored in RomMMM[7].
Michael_ 2:ee820a991b95 186 *
Michael_ 2:ee820a991b95 187 * @returns true if the checksum matches, otherwise false.
Michael_ 2:ee820a991b95 188 */
lamell 3:3e89eafb60c2 189 bool Rom_checksum_error();
lamell 3:3e89eafb60c2 190
Michael_ 2:ee820a991b95 191 /** This function calculates the RAM checksum and compares it to the
lamell 3:3e89eafb60c2 192 * ow_CRC value stored in RAM[8].
Michael_ 2:ee820a991b95 193 *
Michael_ 2:ee820a991b95 194 * @returns true if the checksum matches, otherwise false.
Michael_ 2:ee820a991b95 195 */
Michael_ 2:ee820a991b95 196 bool RAM_checksum_error();
lamell 3:3e89eafb60c2 197
Michael_ 2:ee820a991b95 198 /** This function returns the values stored in the temperature
lamell 3:3e89eafb60c2 199 * alarm registers.
Michael_ 2:ee820a991b95 200 *
Michael_ 2:ee820a991b95 201 * @returns a 16 bit integer of TH (upper byte) and TL (lower byte).
Michael_ 2:ee820a991b95 202 */
Michael_ 2:ee820a991b95 203 bool set_configuration_bits(unsigned int resolution);
lamell 3:3e89eafb60c2 204
Michael_ 2:ee820a991b95 205 /** This function sets the temperature resolution for the DS18B20
Michael_ 2:ee820a991b95 206 * in the configuration register.
Michael_ 2:ee820a991b95 207 *
Michael_ 2:ee820a991b95 208 * @param a number between 9 and 12 to specify the resolution
Michael_ 2:ee820a991b95 209 * @returns true if successful
lamell 3:3e89eafb60c2 210 */
Michael_ 2:ee820a991b95 211 int read_scratchpad();
lamell 3:3e89eafb60c2 212
Michael_ 2:ee820a991b95 213 /** This function will store the passed data into the DS1820's RAM.
lamell 3:3e89eafb60c2 214 * Note: It does NOT save the data to the EEPRomMMM for retention
Michael_ 2:ee820a991b95 215 * during cycling the power off and on.
Michael_ 2:ee820a991b95 216 *
Michael_ 2:ee820a991b95 217 * @param a 16 bit integer of TH (upper byte) and TL (lower byte).
lamell 3:3e89eafb60c2 218 */
Michael_ 2:ee820a991b95 219 void write_scratchpad(int data);
lamell 3:3e89eafb60c2 220
lamell 3:3e89eafb60c2 221 /** This function will transfer the TH and TL registers fRomM the
lamell 3:3e89eafb60c2 222 * DS1820's RAM into the EEPRomMMMM.
Michael_ 2:ee820a991b95 223 * Note: There is a built in 10ms delay to allow for the
lamell 3:3e89eafb60c2 224 * completion of the EEPRomMMM write cycle.
Michael_ 2:ee820a991b95 225 *
Michael_ 2:ee820a991b95 226 * @param allows the fnction to apply to a specific device or
Michael_ 2:ee820a991b95 227 * to all devices on the 1-Wire bus.
lamell 3:3e89eafb60c2 228 */
Michael_ 2:ee820a991b95 229 void store_scratchpad(devices device=this_device);
lamell 3:3e89eafb60c2 230
lamell 3:3e89eafb60c2 231 /** This function will copy the stored values fRomMMM the EEPRomM
Michael_ 2:ee820a991b95 232 * into the DS1820's RAM locations for TH and TL.
Michael_ 2:ee820a991b95 233 *
Michael_ 2:ee820a991b95 234 * @param allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 235 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 236 */
Michael_ 2:ee820a991b95 237 int recall_scratchpad(devices device=this_device);
lamell 3:3e89eafb60c2 238
Michael_ 2:ee820a991b95 239 /** This function will return the type of power supply for
Michael_ 2:ee820a991b95 240 * a specific device. It can also be used to query all devices
Michael_ 2:ee820a991b95 241 * looking for any device that is parasite powered.
Michael_ 2:ee820a991b95 242 *
Michael_ 2:ee820a991b95 243 * @returns true if the device (or all devices) are Vcc powered,
Michael_ 2:ee820a991b95 244 * returns false if the device (or ANY device) is parasite powered.
Michael_ 2:ee820a991b95 245 */
Michael_ 2:ee820a991b95 246 bool read_power_supply(devices device=this_device);
lamell 3:3e89eafb60c2 247
Michael_ 2:ee820a991b95 248 private:
Michael_ 2:ee820a991b95 249 bool _parasite_power;
lamell 3:3e89eafb60c2 250 char ow_CRC_byte (char ow_CRC, char byte );
Michael_ 2:ee820a991b95 251 bool onewire_reset();
lamell 3:3e89eafb60c2 252 void match_Rom();
lamell 3:3e89eafb60c2 253 void skip_Rom();
lamell 3:3e89eafb60c2 254 bool search_Rom_routine(char command);
Michael_ 2:ee820a991b95 255 void onewire_bit_out (bool bit_data);
Michael_ 2:ee820a991b95 256 void onewire_byte_out(char data);
Michael_ 2:ee820a991b95 257 bool onewire_bit_in();
Michael_ 2:ee820a991b95 258 char onewire_byte_in();
lamell 3:3e89eafb60c2 259
Michael_ 2:ee820a991b95 260 protected:
Michael_ 2:ee820a991b95 261 DigitalInOut _datapin;
Michael_ 2:ee820a991b95 262 DigitalOut _parasitepin;
Michael_ 2:ee820a991b95 263 };
lamell 3:3e89eafb60c2 264
lamell 3:3e89eafb60c2 265
lamell 3:3e89eafb60c2 266 #endif