6°A PIO IX TP1. Ejercicio 1. ALMADA, Santiago MAVER, Francisco

Dependencies:   LinkedList

Dependents:   temp_prueba

Committer:
Michael_
Date:
Wed Dec 14 20:22:16 2011 +0000
Revision:
1:6a427f54e82c
Parent:
0:61d83318f2d6
Child:
2:ee820a991b95
Added code to support the DS18B20

Who changed what in which revision?

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