Fork to fix compiler warnings.

Dependencies:   LinkedList

Fork of DS1820 by Erik -

Committer:
pairmand
Date:
Thu Aug 01 10:18:17 2013 +0000
Revision:
3:8f2b7f4940b5
Parent:
2:ee820a991b95
Child:
5:2cd4928e8147
Fixed to work with latest library version, allowed immediate return from convert_temperature(), and tidied up a few other things.

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 */
Michael_ 2:ee820a991b95 92 DS1820(PinName data_pin, PinName power_pin); // Constructor with parasite power pin
Michael_ 2:ee820a991b95 93
Michael_ 2:ee820a991b95 94 /** Create a probe object connected to the specified pin
Michael_ 2:ee820a991b95 95 * this is used when all probes are externally powered
Michael_ 2:ee820a991b95 96 *
Michael_ 2:ee820a991b95 97 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 98 */
Michael_ 2:ee820a991b95 99 DS1820(PinName data_pin);
Michael_ 2:ee820a991b95 100
Michael_ 2:ee820a991b95 101 /** ROM is a copy of the internal DS1820's ROM
Michael_ 2:ee820a991b95 102 * It is created during the search_ROM() or search_alarm() commands
Michael_ 2:ee820a991b95 103 *
Michael_ 2:ee820a991b95 104 * ROM[0] is the Dallas Family Code
Michael_ 2:ee820a991b95 105 * ROM[1] thru ROM[6] is the 48-bit unique serial number
Michael_ 2:ee820a991b95 106 * ROM[7] is the device CRC
Michael_ 2:ee820a991b95 107 */
Michael_ 2:ee820a991b95 108 char ROM[8];
Michael_ 2:ee820a991b95 109 #define FAMILY_CODE ROM[0]
Michael_ 2:ee820a991b95 110 #define FAMILY_CODE_DS1820 0x10
Michael_ 2:ee820a991b95 111 #define FAMILY_CODE_DS18S20 0x10
Michael_ 2:ee820a991b95 112 #define FAMILY_CODE_DS18B20 0x28
Michael_ 2:ee820a991b95 113
Michael_ 2:ee820a991b95 114 /** RAM is a copy of the internal DS1820's RAM
Michael_ 2:ee820a991b95 115 * It's updated during the read_RAM() command
Michael_ 2:ee820a991b95 116 * which is automaticaly called from any function
pairmand 3:8f2b7f4940b5 117 * using the RAM values except RAM_checksum_error.
Michael_ 2:ee820a991b95 118 */
Michael_ 2:ee820a991b95 119 char RAM[9];
Michael_ 2:ee820a991b95 120
pairmand 3:8f2b7f4940b5 121 /** This function copies the DS1820's RAM into the object's
pairmand 3:8f2b7f4940b5 122 * RAM[]. It is automaticaly called by temperature(),
pairmand 3:8f2b7f4940b5 123 * read_scratchpad(), and recall_scratchpad() of a single probe.
pairmand 3:8f2b7f4940b5 124 */
Michael_ 2:ee820a991b95 125 void read_RAM();
Michael_ 2:ee820a991b95 126
Michael_ 2:ee820a991b95 127 /** This routine initializes the global variables used in
Michael_ 2:ee820a991b95 128 * the search_ROM() and search_alarm() funtions. It should
Michael_ 2:ee820a991b95 129 * be called once before looping to find devices.
Michael_ 2:ee820a991b95 130 */
Michael_ 2:ee820a991b95 131 void search_ROM_setup();
Michael_ 2:ee820a991b95 132
Michael_ 2:ee820a991b95 133 /** This routine will search for an unidentified device
Michael_ 2:ee820a991b95 134 * on the bus. It uses the variables in search_ROM_setup
Michael_ 2:ee820a991b95 135 * to remember the pervious ROM address found.
Michael_ 2:ee820a991b95 136 * It will return FALSE if there were no new devices
Michael_ 2:ee820a991b95 137 * discovered on the bus.
Michael_ 2:ee820a991b95 138 */
Michael_ 2:ee820a991b95 139 bool search_ROM();
Michael_ 2:ee820a991b95 140
Michael_ 2:ee820a991b95 141 /** This routine will search for an unidentified device
Michael_ 2:ee820a991b95 142 * which has the temperature alarm bit set. It uses the
Michael_ 2:ee820a991b95 143 * variables in search_ROM_setup to remember the pervious
Michael_ 2:ee820a991b95 144 * ROM address found. It will return FALSE if there were
Michael_ 2:ee820a991b95 145 * no new devices with alarms discovered on the bus.
Michael_ 2:ee820a991b95 146 */
Michael_ 2:ee820a991b95 147 bool search_alarm();
Michael_ 2:ee820a991b95 148
Michael_ 2:ee820a991b95 149 /** This routine will read the ROM (Family code, serial number
Michael_ 2:ee820a991b95 150 * and Checksum) from a dedicated device on the bus.
Michael_ 2:ee820a991b95 151 *
Michael_ 2:ee820a991b95 152 * NOTE: This command can only be used when there is only one
Michael_ 2:ee820a991b95 153 * DS1820 on the bus. If this command is used when there
Michael_ 2:ee820a991b95 154 * is more than one slave present on the bus, a data
Michael_ 2:ee820a991b95 155 * collision will occur when all the DS1820s attempt to
Michael_ 2:ee820a991b95 156 * respond at the same time.
Michael_ 2:ee820a991b95 157 */
Michael_ 2:ee820a991b95 158 void read_ROM();
Michael_ 2:ee820a991b95 159
Michael_ 2:ee820a991b95 160 /** This routine will initiate the temperature conversion within
pairmand 3:8f2b7f4940b5 161 * one or all DS1820 probes. There is an optional built in delay
pairmand 3:8f2b7f4940b5 162 * (up to 750ms) to allow the conversion to complete.
Michael_ 2:ee820a991b95 163 *
Michael_ 2:ee820a991b95 164 * To update all probes on the bus, use a statement such as this:
pairmand 3:8f2b7f4940b5 165 * probe[0]->convert_temperature(true, DS1820::all_devices);
Michael_ 2:ee820a991b95 166 *
pairmand 3:8f2b7f4940b5 167 * @param wait if true or parisitic power is used, waits up to 750 ms for
pairmand 3:8f2b7f4940b5 168 * conversion otherwise returns immediatly.
pairmand 3:8f2b7f4940b5 169 * @param device allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 170 * to all devices on the 1-Wire bus.
pairmand 3:8f2b7f4940b5 171 * @returns milliseconds untill conversion will complete.
Michael_ 2:ee820a991b95 172 */
pairmand 3:8f2b7f4940b5 173 int convert_temperature(bool wait, devices device=this_device);
Michael_ 2:ee820a991b95 174
pairmand 3:8f2b7f4940b5 175 /** This function will return the probe temperature. Approximately 10ms per
pairmand 3:8f2b7f4940b5 176 * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
pairmand 3:8f2b7f4940b5 177 * This function uses the count remainding values to interpolate the temperature
Michael_ 2:ee820a991b95 178 * to about 1/150th of a degree. Whereas the probe is not spec to
Michael_ 2:ee820a991b95 179 * that precision. It does seem to give a smooth reading to the
Michael_ 2:ee820a991b95 180 * tenth of a degree.
Michael_ 2:ee820a991b95 181 *
Michael_ 2:ee820a991b95 182 * @param scale, may be either 'c' or 'f'
pairmand 3:8f2b7f4940b5 183 * @returns temperature for that scale, or -1000.0 if CRC error detected.
Michael_ 2:ee820a991b95 184 */
Michael_ 2:ee820a991b95 185 float temperature(char scale='c');
Michael_ 2:ee820a991b95 186
Michael_ 2:ee820a991b95 187 /** This function calculates the ROM checksum and compares it to the
Michael_ 2:ee820a991b95 188 * CRC value stored in ROM[7].
Michael_ 2:ee820a991b95 189 *
pairmand 3:8f2b7f4940b5 190 * @returns true if the checksums mis-match, otherwise false.
Michael_ 2:ee820a991b95 191 */
Michael_ 2:ee820a991b95 192 bool ROM_checksum_error();
Michael_ 2:ee820a991b95 193
Michael_ 2:ee820a991b95 194 /** This function calculates the RAM checksum and compares it to the
pairmand 3:8f2b7f4940b5 195 * CRC value stored in RAM[8]. Approx 10 us on LPC1768.
Michael_ 2:ee820a991b95 196 *
pairmand 3:8f2b7f4940b5 197 * @returns true if the checksums mis-matche, otherwise false.
Michael_ 2:ee820a991b95 198 */
Michael_ 2:ee820a991b95 199 bool RAM_checksum_error();
Michael_ 2:ee820a991b95 200
pairmand 3:8f2b7f4940b5 201 /** This function sets the temperature resolution for the DS18B20
pairmand 3:8f2b7f4940b5 202 * in the configuration register.
pairmand 3:8f2b7f4940b5 203 *
pairmand 3:8f2b7f4940b5 204 * @param a number between 9 and 12 to specify resolution
pairmand 3:8f2b7f4940b5 205 * @returns true if successful
pairmand 3:8f2b7f4940b5 206 */
pairmand 3:8f2b7f4940b5 207 bool set_configuration_bits(unsigned int resolution);
pairmand 3:8f2b7f4940b5 208
Michael_ 2:ee820a991b95 209 /** This function returns the values stored in the temperature
Michael_ 2:ee820a991b95 210 * alarm registers.
Michael_ 2:ee820a991b95 211 *
Michael_ 2:ee820a991b95 212 * @returns a 16 bit integer of TH (upper byte) and TL (lower byte).
Michael_ 2:ee820a991b95 213 */
Michael_ 2:ee820a991b95 214 int read_scratchpad();
Michael_ 2:ee820a991b95 215
Michael_ 2:ee820a991b95 216 /** This function will store the passed data into the DS1820's RAM.
Michael_ 2:ee820a991b95 217 * Note: It does NOT save the data to the EEPROM for retention
Michael_ 2:ee820a991b95 218 * during cycling the power off and on.
Michael_ 2:ee820a991b95 219 *
Michael_ 2:ee820a991b95 220 * @param a 16 bit integer of TH (upper byte) and TL (lower byte).
Michael_ 2:ee820a991b95 221 */
Michael_ 2:ee820a991b95 222 void write_scratchpad(int data);
Michael_ 2:ee820a991b95 223
Michael_ 2:ee820a991b95 224 /** This function will transfer the TH and TL registers from the
Michael_ 2:ee820a991b95 225 * DS1820's RAM into the EEPROM.
Michael_ 2:ee820a991b95 226 * Note: There is a built in 10ms delay to allow for the
Michael_ 2:ee820a991b95 227 * completion of the EEPROM write cycle.
Michael_ 2:ee820a991b95 228 *
Michael_ 2:ee820a991b95 229 * @param allows the fnction to apply to a specific device or
Michael_ 2:ee820a991b95 230 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 231 */
Michael_ 2:ee820a991b95 232 void store_scratchpad(devices device=this_device);
Michael_ 2:ee820a991b95 233
Michael_ 2:ee820a991b95 234 /** This function will copy the stored values from the EEPROM
Michael_ 2:ee820a991b95 235 * into the DS1820's RAM locations for TH and TL.
Michael_ 2:ee820a991b95 236 *
Michael_ 2:ee820a991b95 237 * @param allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 238 * to all devices on the 1-Wire bus.
Michael_ 2:ee820a991b95 239 */
Michael_ 2:ee820a991b95 240 int recall_scratchpad(devices device=this_device);
Michael_ 2:ee820a991b95 241
Michael_ 2:ee820a991b95 242 /** This function will return the type of power supply for
Michael_ 2:ee820a991b95 243 * a specific device. It can also be used to query all devices
Michael_ 2:ee820a991b95 244 * looking for any device that is parasite powered.
Michael_ 2:ee820a991b95 245 *
Michael_ 2:ee820a991b95 246 * @returns true if the device (or all devices) are Vcc powered,
Michael_ 2:ee820a991b95 247 * returns false if the device (or ANY device) is parasite powered.
Michael_ 2:ee820a991b95 248 */
Michael_ 2:ee820a991b95 249 bool read_power_supply(devices device=this_device);
Michael_ 2:ee820a991b95 250
Michael_ 2:ee820a991b95 251 private:
Michael_ 2:ee820a991b95 252 bool _parasite_power;
Michael_ 2:ee820a991b95 253 char CRC_byte (char CRC, char byte );
Michael_ 2:ee820a991b95 254 bool onewire_reset();
Michael_ 2:ee820a991b95 255 void match_ROM();
Michael_ 2:ee820a991b95 256 void skip_ROM();
Michael_ 2:ee820a991b95 257 bool search_ROM_routine(char command);
Michael_ 2:ee820a991b95 258 void onewire_bit_out (bool bit_data);
Michael_ 2:ee820a991b95 259 void onewire_byte_out(char data);
Michael_ 2:ee820a991b95 260 bool onewire_bit_in();
Michael_ 2:ee820a991b95 261 char onewire_byte_in();
Michael_ 2:ee820a991b95 262
Michael_ 2:ee820a991b95 263 protected:
Michael_ 2:ee820a991b95 264 DigitalInOut _datapin;
Michael_ 2:ee820a991b95 265 DigitalOut _parasitepin;
Michael_ 2:ee820a991b95 266 };
Michael_ 2:ee820a991b95 267
Michael_ 2:ee820a991b95 268
Michael_ 2:ee820a991b95 269 #endif