Dependents:   BMS_BMUCore_Max_DummyData

Committer:
yoonghm
Date:
Tue Feb 28 07:42:07 2012 +0000
Revision:
0:245af9360f0d

        

Who changed what in which revision?

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