Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Thu Mar 03 08:45:49 2011 +0000
Revision:
0:8d62137f7ff4
For FRIENDs.

Who changed what in which revision?

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