softemp

Dependencies:   mbed DHT22 ATParser SerialGPS

Committer:
svupper
Date:
Wed Nov 21 17:33:07 2018 +0000
Revision:
3:06b0680034a7
Parent:
2:f760e8507750
sofmbedtemp

Who changed what in which revision?

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