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