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