ruches 2

Dependencies:   mbed Ruche-2 Temps HX711 DHT22 Sigfox GPS

Committer:
mohayonor
Date:
Sun Feb 03 20:27:56 2019 +0000
Revision:
3:fe4b3bfd2237
Parent:
0:126a8e290bd5
all;

Who changed what in which revision?

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