ds18b20 for senior design

Dependencies:   mbed

Fork of ds18b20_test by Tuan PM

Committer:
TuanPM
Date:
Thu Mar 27 17:22:57 2014 +0000
Revision:
0:6cb99f312e0e
DS18B20 work for ST Nucleo

Who changed what in which revision?

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