teste de publish

Dependencies:   LinkedList

Fork of DS1820 by Erik -

Committer:
Michael_
Date:
Thu Dec 15 19:07:36 2011 +0000
Revision:
2:ee820a991b95
Parent:
1:6a427f54e82c
Child:
3:8f2b7f4940b5
Updated documentation. (maybe)

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