Fork of DS1820 that can function with the data and parasite power pin being the same.

Dependents:   AutonomousDAQ AutonomousDAQ

Fork of DS1820 by David Pairman

Committer:
uci1
Date:
Thu Oct 30 05:55:15 2014 +0000
Revision:
7:aceafdd78b28
Parent:
6:c78fb487c397
allow use of parasite power to be switched on and off

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
Michael_ 2:ee820a991b95 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 */
Michael_ 2:ee820a991b95 22
Michael_ 2:ee820a991b95 23 #ifndef MBED_DS1820_H
Michael_ 2:ee820a991b95 24 #define MBED_DS1820_H
Michael_ 2:ee820a991b95 25
Michael_ 2:ee820a991b95 26 #include "mbed.h"
Michael_ 2:ee820a991b95 27
Michael_ 2:ee820a991b95 28 // ****** THIS GLOBAL VARIABLES MUST BE DEFINED IN main.cpp
Michael_ 2:ee820a991b95 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;
Michael_ 2:ee820a991b95 33 //char DS1820_search_ROM[8];
Michael_ 2:ee820a991b95 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
Michael_ 2:ee820a991b95 56 * probe[0]->search_ROM_setup();
Michael_ 2:ee820a991b95 57 * // Loop to find all devices on the data line
Michael_ 2:ee820a991b95 58 * while (probe[devices_found]->search_ROM() and devices_found<MAX_PROBES-1)
Michael_ 2:ee820a991b95 59 * devices_found++;
Michael_ 2:ee820a991b95 60 * // If maximum number of probes are found,
Michael_ 2:ee820a991b95 61 * // bump the counter to include the last array entry
Michael_ 2:ee820a991b95 62 * if (probe[devices_found]->ROM[0] != 0xFF)
Michael_ 2:ee820a991b95 63 * devices_found++;
Michael_ 2:ee820a991b95 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) {
pairmand 3:8f2b7f4940b5 70 * probe[0]->convert_temperature(true, 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
Michael_ 2:ee820a991b95 81 class DS1820 {
Michael_ 2:ee820a991b95 82 public:
Michael_ 2:ee820a991b95 83 enum devices{
Michael_ 2:ee820a991b95 84 this_device, // command applies to only this device
Michael_ 2:ee820a991b95 85 all_devices }; // command applies to all devices
Michael_ 2:ee820a991b95 86
Michael_ 2:ee820a991b95 87 /** Create a probe object connected to the specified pins
Michael_ 2:ee820a991b95 88 *
Michael_ 2:ee820a991b95 89 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 90 * @param power_pin DigitalOut pin to control the power MOSFET
Michael_ 2:ee820a991b95 91 */
uci1 7:aceafdd78b28 92 DS1820(PinName data_pin, PinName power_pin,
uci1 7:aceafdd78b28 93 const bool useParasitePwr=true); // Constructor with parasite power pin
Michael_ 2:ee820a991b95 94
Michael_ 2:ee820a991b95 95 /** Create a probe object connected to the specified pin
Michael_ 2:ee820a991b95 96 * this is used when all probes are externally powered
Michael_ 2:ee820a991b95 97 *
Michael_ 2:ee820a991b95 98 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 99 */
Michael_ 2:ee820a991b95 100 DS1820(PinName data_pin);
Michael_ 2:ee820a991b95 101
Michael_ 2:ee820a991b95 102 /** ROM is a copy of the internal DS1820's ROM
Michael_ 2:ee820a991b95 103 * It is created during the search_ROM() or search_alarm() commands
Michael_ 2:ee820a991b95 104 *
Michael_ 2:ee820a991b95 105 * ROM[0] is the Dallas Family Code
Michael_ 2:ee820a991b95 106 * ROM[1] thru ROM[6] is the 48-bit unique serial number
Michael_ 2:ee820a991b95 107 * ROM[7] is the device CRC
Michael_ 2:ee820a991b95 108 */
Michael_ 2:ee820a991b95 109 char ROM[8];
Michael_ 2:ee820a991b95 110 #define FAMILY_CODE ROM[0]
Michael_ 2:ee820a991b95 111 #define FAMILY_CODE_DS1820 0x10
Michael_ 2:ee820a991b95 112 #define FAMILY_CODE_DS18S20 0x10
Michael_ 2:ee820a991b95 113 #define FAMILY_CODE_DS18B20 0x28
Michael_ 2:ee820a991b95 114
Michael_ 2:ee820a991b95 115 /** RAM is a copy of the internal DS1820's RAM
Michael_ 2:ee820a991b95 116 * It's updated during the read_RAM() command
Michael_ 2:ee820a991b95 117 * which is automaticaly called from any function
pairmand 3:8f2b7f4940b5 118 * using the RAM values except RAM_checksum_error.
Michael_ 2:ee820a991b95 119 */
Michael_ 2:ee820a991b95 120 char RAM[9];
Michael_ 2:ee820a991b95 121
pairmand 3:8f2b7f4940b5 122 /** This function copies the DS1820's RAM into the object's
pairmand 3:8f2b7f4940b5 123 * RAM[]. It is automaticaly called by temperature(),
pairmand 3:8f2b7f4940b5 124 * read_scratchpad(), and recall_scratchpad() of a single probe.
pairmand 3:8f2b7f4940b5 125 */
Michael_ 2:ee820a991b95 126 void read_RAM();
Michael_ 2:ee820a991b95 127
Michael_ 2:ee820a991b95 128 /** This routine initializes the global variables used in
Michael_ 2:ee820a991b95 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 */
Michael_ 2:ee820a991b95 132 void search_ROM_setup();
Michael_ 2:ee820a991b95 133
Michael_ 2:ee820a991b95 134 /** This routine will search for an unidentified device
Michael_ 2:ee820a991b95 135 * on the bus. It uses the variables in search_ROM_setup
Michael_ 2:ee820a991b95 136 * to remember the pervious ROM 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 */
Michael_ 2:ee820a991b95 140 bool search_ROM();
Michael_ 2:ee820a991b95 141
Michael_ 2:ee820a991b95 142 /** This routine will search for an unidentified device
Michael_ 2:ee820a991b95 143 * which has the temperature alarm bit set. It uses the
Michael_ 2:ee820a991b95 144 * variables in search_ROM_setup to remember the pervious
Michael_ 2:ee820a991b95 145 * ROM 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();
Michael_ 2:ee820a991b95 149
Michael_ 2:ee820a991b95 150 /** This routine will read the ROM (Family code, serial number
Michael_ 2:ee820a991b95 151 * and Checksum) from a dedicated device on the bus.
Michael_ 2:ee820a991b95 152 *
Michael_ 2:ee820a991b95 153 * NOTE: This command can only be used when there is only one
Michael_ 2:ee820a991b95 154 * DS1820 on the bus. If this command is used when there
Michael_ 2:ee820a991b95 155 * is more than one slave present on the bus, a data
Michael_ 2:ee820a991b95 156 * collision will occur when all the DS1820s attempt to
Michael_ 2:ee820a991b95 157 * respond at the same time.
Michael_ 2:ee820a991b95 158 */
Michael_ 2:ee820a991b95 159 void read_ROM();
Michael_ 2:ee820a991b95 160
Michael_ 2:ee820a991b95 161 /** This routine will initiate the temperature conversion within
pairmand 3:8f2b7f4940b5 162 * one or all DS1820 probes. There is an optional built in delay
pairmand 3:8f2b7f4940b5 163 * (up to 750ms) to allow the conversion to complete.
Michael_ 2:ee820a991b95 164 *
Michael_ 2:ee820a991b95 165 * To update all probes on the bus, use a statement such as this:
pairmand 3:8f2b7f4940b5 166 * probe[0]->convert_temperature(true, DS1820::all_devices);
Michael_ 2:ee820a991b95 167 *
pairmand 3:8f2b7f4940b5 168 * @param wait if true or parisitic power is used, waits up to 750 ms for
pairmand 3:8f2b7f4940b5 169 * conversion otherwise returns immediatly.
pairmand 3:8f2b7f4940b5 170 * @param device allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 171 * to all devices on the 1-Wire bus.
pairmand 3:8f2b7f4940b5 172 * @returns milliseconds untill conversion will complete.
Michael_ 2:ee820a991b95 173 */
pairmand 3:8f2b7f4940b5 174 int convert_temperature(bool wait, devices device=this_device);
Michael_ 2:ee820a991b95 175
pairmand 3:8f2b7f4940b5 176 /** This function will return the probe temperature. Approximately 10ms per
pairmand 3:8f2b7f4940b5 177 * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
pairmand 3:8f2b7f4940b5 178 * This function uses the count remainding values to interpolate the temperature
Michael_ 2:ee820a991b95 179 * to about 1/150th of a degree. Whereas the probe is not spec to
Michael_ 2:ee820a991b95 180 * that precision. It does seem to give a smooth reading to the
Michael_ 2:ee820a991b95 181 * tenth of a degree.
Michael_ 2:ee820a991b95 182 *
Michael_ 2:ee820a991b95 183 * @param scale, may be either 'c' or 'f'
pairmand 3:8f2b7f4940b5 184 * @returns temperature for that scale, or -1000.0 if CRC error detected.
Michael_ 2:ee820a991b95 185 */
Michael_ 2:ee820a991b95 186 float temperature(char scale='c');
Michael_ 2:ee820a991b95 187
Michael_ 2:ee820a991b95 188 /** This function calculates the ROM checksum and compares it to the
Michael_ 2:ee820a991b95 189 * CRC value stored in ROM[7].
Michael_ 2:ee820a991b95 190 *
pairmand 3:8f2b7f4940b5 191 * @returns true if the checksums mis-match, otherwise false.
Michael_ 2:ee820a991b95 192 */
Michael_ 2:ee820a991b95 193 bool ROM_checksum_error();
Michael_ 2:ee820a991b95 194
Michael_ 2:ee820a991b95 195 /** This function calculates the RAM checksum and compares it to the
pairmand 3:8f2b7f4940b5 196 * CRC value stored in RAM[8]. Approx 10 us on LPC1768.
Michael_ 2:ee820a991b95 197 *
pairmand 3:8f2b7f4940b5 198 * @returns true if the checksums mis-matche, otherwise false.
Michael_ 2:ee820a991b95 199 */
Michael_ 2:ee820a991b95 200 bool RAM_checksum_error();
Michael_ 2:ee820a991b95 201
pairmand 3:8f2b7f4940b5 202 /** This function sets the temperature resolution for the DS18B20
pairmand 3:8f2b7f4940b5 203 * in the configuration register.
pairmand 3:8f2b7f4940b5 204 *
pairmand 3:8f2b7f4940b5 205 * @param a number between 9 and 12 to specify resolution
pairmand 3:8f2b7f4940b5 206 * @returns true if successful
pairmand 3:8f2b7f4940b5 207 */
pairmand 3:8f2b7f4940b5 208 bool set_configuration_bits(unsigned int resolution);
pairmand 3:8f2b7f4940b5 209
Michael_ 2:ee820a991b95 210 /** This function returns the values stored in the temperature
Michael_ 2:ee820a991b95 211 * alarm registers.
Michael_ 2:ee820a991b95 212 *
Michael_ 2:ee820a991b95 213 * @returns a 16 bit integer of TH (upper byte) and TL (lower byte).
Michael_ 2:ee820a991b95 214 */
Michael_ 2:ee820a991b95 215 int read_scratchpad();
Michael_ 2:ee820a991b95 216
Michael_ 2:ee820a991b95 217 /** This function will store the passed data into the DS1820's RAM.
Michael_ 2:ee820a991b95 218 * Note: It does NOT save the data to the EEPROM for retention
Michael_ 2:ee820a991b95 219 * during cycling the power off and on.
Michael_ 2:ee820a991b95 220 *
Michael_ 2:ee820a991b95 221 * @param a 16 bit integer of TH (upper byte) and TL (lower byte).
Michael_ 2:ee820a991b95 222 */
Michael_ 2:ee820a991b95 223 void write_scratchpad(int data);
Michael_ 2:ee820a991b95 224
Michael_ 2:ee820a991b95 225 /** This function will transfer the TH and TL registers from the
Michael_ 2:ee820a991b95 226 * DS1820's RAM into the EEPROM.
Michael_ 2:ee820a991b95 227 * Note: There is a built in 10ms delay to allow for the
Michael_ 2:ee820a991b95 228 * completion of the EEPROM write cycle.
Michael_ 2:ee820a991b95 229 *
Michael_ 2:ee820a991b95 230 * @param allows the fnction to apply to a specific device or
Michael_ 2:ee820a991b95 231 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 232 */
Michael_ 2:ee820a991b95 233 void store_scratchpad(devices device=this_device);
Michael_ 2:ee820a991b95 234
Michael_ 2:ee820a991b95 235 /** This function will copy the stored values from the EEPROM
Michael_ 2:ee820a991b95 236 * into the DS1820's RAM locations for TH and TL.
Michael_ 2:ee820a991b95 237 *
Michael_ 2:ee820a991b95 238 * @param allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 239 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 240 */
Michael_ 2:ee820a991b95 241 int recall_scratchpad(devices device=this_device);
Michael_ 2:ee820a991b95 242
Michael_ 2:ee820a991b95 243 /** This function will return the type of power supply for
Michael_ 2:ee820a991b95 244 * a specific device. It can also be used to query all devices
Michael_ 2:ee820a991b95 245 * looking for any device that is parasite powered.
Michael_ 2:ee820a991b95 246 *
Michael_ 2:ee820a991b95 247 * @returns true if the device (or all devices) are Vcc powered,
Michael_ 2:ee820a991b95 248 * returns false if the device (or ANY device) is parasite powered.
Michael_ 2:ee820a991b95 249 */
Michael_ 2:ee820a991b95 250 bool read_power_supply(devices device=this_device);
uci1 6:c78fb487c397 251
uci1 6:c78fb487c397 252 /** Check if parasite power is being used.
uci1 6:c78fb487c397 253 *
uci1 6:c78fb487c397 254 * @returns true if using parasite power
uci1 6:c78fb487c397 255 * false if using external power
uci1 6:c78fb487c397 256 */
uci1 6:c78fb487c397 257 bool is_on_parasite_power() const { return _parasite_power; }
uci1 6:c78fb487c397 258
uci1 7:aceafdd78b28 259 /** Allow use of parasite power to be switched on or off
uci1 7:aceafdd78b28 260 *
uci1 7:aceafdd78b28 261 * @param pwr Specify whether to use the parasite pin for power (true)
uci1 7:aceafdd78b28 262 * or not (false).
uci1 7:aceafdd78b28 263 * @return true if using parasite power
uci1 7:aceafdd78b28 264 * false if not (i.e. parasite mode requested, but no parasite pin)
uci1 7:aceafdd78b28 265 */
uci1 7:aceafdd78b28 266 bool set_use_parasite_power(const bool pwr) {
uci1 7:aceafdd78b28 267 if (pwr && (_parasite_power_pn!=NC)) {
uci1 7:aceafdd78b28 268 _parasite_power = true;
uci1 7:aceafdd78b28 269 } else {
uci1 7:aceafdd78b28 270 _parasite_power = false;
uci1 7:aceafdd78b28 271 }
uci1 7:aceafdd78b28 272 return _parasite_power;
uci1 7:aceafdd78b28 273 }
uci1 7:aceafdd78b28 274
Michael_ 2:ee820a991b95 275 private:
Michael_ 2:ee820a991b95 276 bool _parasite_power;
uci1 7:aceafdd78b28 277 PinName _parasite_power_pn;
uci1 7:aceafdd78b28 278
Michael_ 2:ee820a991b95 279 char CRC_byte (char CRC, char byte );
Michael_ 2:ee820a991b95 280 bool onewire_reset();
Michael_ 2:ee820a991b95 281 void match_ROM();
Michael_ 2:ee820a991b95 282 void skip_ROM();
Michael_ 2:ee820a991b95 283 bool search_ROM_routine(char command);
Michael_ 2:ee820a991b95 284 void onewire_bit_out (bool bit_data);
Michael_ 2:ee820a991b95 285 void onewire_byte_out(char data);
Michael_ 2:ee820a991b95 286 bool onewire_bit_in();
Michael_ 2:ee820a991b95 287 char onewire_byte_in();
Michael_ 2:ee820a991b95 288
Michael_ 2:ee820a991b95 289 protected:
Michael_ 2:ee820a991b95 290 DigitalInOut _datapin;
uci1 5:00539fbc6d79 291 DigitalInOut _parasitepin;
Michael_ 2:ee820a991b95 292 };
Michael_ 2:ee820a991b95 293
Michael_ 2:ee820a991b95 294
Michael_ 2:ee820a991b95 295 #endif