Yet another DS1820 lib, but this one is make in sweat of struggling with other ones. It's a fork really of <insert original as mbed doesn't track idk why> with *added capability* of not blocking.

Dependents:   Nucleo_praktyki

Committer:
amateusz
Date:
Sat Sep 02 20:13:38 2017 +0000
Revision:
1:1020ba9ab5d2
Parent:
0:3968e3a2e047
Non blocking convert. Updates internal variable, which can be read by temperature() method.

Who changed what in which revision?

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