teste de publish
Fork of DS1820 by
DS1820.h@7:58b61681818f, 2014-04-08 (annotated)
- Committer:
- Sissors
- Date:
- Tue Apr 08 09:59:18 2014 +0000
- Revision:
- 7:58b61681818f
- Parent:
- 6:abfdd851218a
- Child:
- 11:1a3c3002b50c
Added define for invalid conversion
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Michael_ | 2:ee820a991b95 | 1 | /* mbed DS1820 Library, for the Dallas (Maxim) 1-Wire Digital Thermometer |
Michael_ | 2:ee820a991b95 | 2 | * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com |
Michael_ | 2:ee820a991b95 | 3 | * |
Michael_ | 2:ee820a991b95 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Michael_ | 2:ee820a991b95 | 5 | * of this software and associated documentation files (the "Software"), to deal |
Michael_ | 2:ee820a991b95 | 6 | * in the Software without restriction, including without limitation the rights |
Michael_ | 2:ee820a991b95 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Michael_ | 2:ee820a991b95 | 8 | * copies of the Software, and to permit persons to whom the Software is |
Michael_ | 2:ee820a991b95 | 9 | * furnished to do so, subject to the following conditions: |
Michael_ | 2:ee820a991b95 | 10 | * |
Michael_ | 2:ee820a991b95 | 11 | * The above copyright notice and this permission notice shall be included in |
Michael_ | 2:ee820a991b95 | 12 | * all copies or substantial portions of the Software. |
Michael_ | 2:ee820a991b95 | 13 | * |
Michael_ | 2:ee820a991b95 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Michael_ | 2:ee820a991b95 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Michael_ | 2:ee820a991b95 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Michael_ | 2:ee820a991b95 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Michael_ | 2:ee820a991b95 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Michael_ | 2:ee820a991b95 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Michael_ | 2:ee820a991b95 | 20 | * THE SOFTWARE. |
Michael_ | 2:ee820a991b95 | 21 | */ |
Michael_ | 2:ee820a991b95 | 22 | |
Michael_ | 2:ee820a991b95 | 23 | #ifndef MBED_DS1820_H |
Michael_ | 2:ee820a991b95 | 24 | #define MBED_DS1820_H |
Michael_ | 2:ee820a991b95 | 25 | |
Michael_ | 2:ee820a991b95 | 26 | #include "mbed.h" |
Sissors | 5:2cd4928e8147 | 27 | #include "LinkedList.h" |
Michael_ | 2:ee820a991b95 | 28 | |
Sissors | 5:2cd4928e8147 | 29 | #define FAMILY_CODE _ROM[0] |
Sissors | 5:2cd4928e8147 | 30 | #define FAMILY_CODE_DS1820 0x10 |
Sissors | 5:2cd4928e8147 | 31 | #define FAMILY_CODE_DS18B20 0x28 |
Sissors | 5:2cd4928e8147 | 32 | #define FAMILY_CODE_DS1822 0x22 |
Michael_ | 2:ee820a991b95 | 33 | |
Michael_ | 2:ee820a991b95 | 34 | /** DS1820 Dallas 1-Wire Temperature Probe |
Michael_ | 2:ee820a991b95 | 35 | * |
Michael_ | 2:ee820a991b95 | 36 | * Example: |
Michael_ | 2:ee820a991b95 | 37 | * @code |
Michael_ | 2:ee820a991b95 | 38 | * #include "mbed.h" |
Michael_ | 2:ee820a991b95 | 39 | * #include "DS1820.h" |
Michael_ | 2:ee820a991b95 | 40 | * |
Sissors | 5:2cd4928e8147 | 41 | * DS1820 probe(DATA_PIN); |
Sissors | 5:2cd4928e8147 | 42 | * |
Michael_ | 2:ee820a991b95 | 43 | * int main() { |
Sissors | 5:2cd4928e8147 | 44 | * while(1) { |
Sissors | 5:2cd4928e8147 | 45 | * probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
Sissors | 5:2cd4928e8147 | 46 | * printf("It is %3.1foC\r\n", probe.temperature()); |
Sissors | 5:2cd4928e8147 | 47 | * wait(1); |
Michael_ | 2:ee820a991b95 | 48 | * } |
Michael_ | 2:ee820a991b95 | 49 | * } |
Michael_ | 2:ee820a991b95 | 50 | * @endcode |
Michael_ | 2:ee820a991b95 | 51 | */ |
Michael_ | 2:ee820a991b95 | 52 | class DS1820 { |
Michael_ | 2:ee820a991b95 | 53 | public: |
Michael_ | 2:ee820a991b95 | 54 | enum devices{ |
Michael_ | 2:ee820a991b95 | 55 | this_device, // command applies to only this device |
Michael_ | 2:ee820a991b95 | 56 | all_devices }; // command applies to all devices |
Sissors | 7:58b61681818f | 57 | |
Sissors | 7:58b61681818f | 58 | enum { |
Sissors | 7:58b61681818f | 59 | invalid_conversion = -1000 |
Sissors | 7:58b61681818f | 60 | }; |
Michael_ | 2:ee820a991b95 | 61 | |
Michael_ | 2:ee820a991b95 | 62 | /** Create a probe object connected to the specified pins |
Sissors | 5:2cd4928e8147 | 63 | * |
Sissors | 5:2cd4928e8147 | 64 | * The probe might either by regular powered or parasite powered. If it is parasite |
Sissors | 5:2cd4928e8147 | 65 | * powered and power_pin is set, that pin will be used to switch an external mosfet connecting |
Sissors | 5:2cd4928e8147 | 66 | * data to Vdd. If it is parasite powered and the pin is not set, the regular data pin |
Sissors | 5:2cd4928e8147 | 67 | * is used to supply extra power when required. This will be sufficient as long as the |
Sissors | 5:2cd4928e8147 | 68 | * number of probes is limitted. |
Michael_ | 2:ee820a991b95 | 69 | * |
Michael_ | 2:ee820a991b95 | 70 | * @param data_pin DigitalInOut pin for the data bus |
Sissors | 5:2cd4928e8147 | 71 | * @param power_pin DigitalOut (optional) pin to control the power MOSFET |
Sissors | 5:2cd4928e8147 | 72 | * @param power_polarity bool (optional) which sets active state (0 for active low (default), 1 for active high) |
Michael_ | 2:ee820a991b95 | 73 | */ |
Sissors | 5:2cd4928e8147 | 74 | DS1820(PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin |
Sissors | 5:2cd4928e8147 | 75 | ~DS1820(); |
Michael_ | 2:ee820a991b95 | 76 | |
Sissors | 5:2cd4928e8147 | 77 | /** Function to see if there are DS1820 devices left on a pin which do not have a corresponding DS1820 object |
Sissors | 5:2cd4928e8147 | 78 | * |
Sissors | 5:2cd4928e8147 | 79 | * @return - true if there are one or more unassigned devices, otherwise false |
Michael_ | 2:ee820a991b95 | 80 | */ |
Sissors | 5:2cd4928e8147 | 81 | static bool unassignedProbe(PinName pin); |
Michael_ | 2:ee820a991b95 | 82 | |
Michael_ | 2:ee820a991b95 | 83 | /** This routine will initiate the temperature conversion within |
Sissors | 5:2cd4928e8147 | 84 | * one or all DS1820 probes. |
Michael_ | 2:ee820a991b95 | 85 | * |
pairmand | 3:8f2b7f4940b5 | 86 | * @param wait if true or parisitic power is used, waits up to 750 ms for |
pairmand | 3:8f2b7f4940b5 | 87 | * conversion otherwise returns immediatly. |
pairmand | 3:8f2b7f4940b5 | 88 | * @param device allows the function to apply to a specific device or |
Michael_ | 2:ee820a991b95 | 89 | * to all devices on the 1-Wire bus. |
pairmand | 3:8f2b7f4940b5 | 90 | * @returns milliseconds untill conversion will complete. |
Michael_ | 2:ee820a991b95 | 91 | */ |
Sissors | 6:abfdd851218a | 92 | int convertTemperature(bool wait, devices device=all_devices); |
Michael_ | 2:ee820a991b95 | 93 | |
pairmand | 3:8f2b7f4940b5 | 94 | /** This function will return the probe temperature. Approximately 10ms per |
pairmand | 3:8f2b7f4940b5 | 95 | * probe to read its RAM, do CRC check and convert temperature on the LPC1768. |
Michael_ | 2:ee820a991b95 | 96 | * |
Michael_ | 2:ee820a991b95 | 97 | * @param scale, may be either 'c' or 'f' |
Sissors | 7:58b61681818f | 98 | * @returns temperature for that scale, or DS1820::invalid_conversion (-1000) if CRC error detected. |
Michael_ | 2:ee820a991b95 | 99 | */ |
Michael_ | 2:ee820a991b95 | 100 | float temperature(char scale='c'); |
Michael_ | 2:ee820a991b95 | 101 | |
pairmand | 3:8f2b7f4940b5 | 102 | /** This function sets the temperature resolution for the DS18B20 |
pairmand | 3:8f2b7f4940b5 | 103 | * in the configuration register. |
pairmand | 3:8f2b7f4940b5 | 104 | * |
pairmand | 3:8f2b7f4940b5 | 105 | * @param a number between 9 and 12 to specify resolution |
pairmand | 3:8f2b7f4940b5 | 106 | * @returns true if successful |
pairmand | 3:8f2b7f4940b5 | 107 | */ |
Sissors | 5:2cd4928e8147 | 108 | bool setResolution(unsigned int resolution); |
Michael_ | 2:ee820a991b95 | 109 | |
Michael_ | 2:ee820a991b95 | 110 | private: |
Michael_ | 2:ee820a991b95 | 111 | bool _parasite_power; |
Sissors | 5:2cd4928e8147 | 112 | bool _power_mosfet; |
Sissors | 5:2cd4928e8147 | 113 | bool _power_polarity; |
Sissors | 5:2cd4928e8147 | 114 | |
Sissors | 5:2cd4928e8147 | 115 | static char CRC_byte (char CRC, char byte ); |
Sissors | 5:2cd4928e8147 | 116 | static bool onewire_reset(DigitalInOut *pin); |
Michael_ | 2:ee820a991b95 | 117 | void match_ROM(); |
Michael_ | 2:ee820a991b95 | 118 | void skip_ROM(); |
Sissors | 5:2cd4928e8147 | 119 | static bool search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address); |
Sissors | 5:2cd4928e8147 | 120 | static void onewire_bit_out (DigitalInOut *pin, bool bit_data); |
Michael_ | 2:ee820a991b95 | 121 | void onewire_byte_out(char data); |
Sissors | 5:2cd4928e8147 | 122 | static bool onewire_bit_in(DigitalInOut *pin); |
Michael_ | 2:ee820a991b95 | 123 | char onewire_byte_in(); |
Sissors | 5:2cd4928e8147 | 124 | static bool ROM_checksum_error(char *_ROM_address); |
Sissors | 5:2cd4928e8147 | 125 | bool RAM_checksum_error(); |
Sissors | 5:2cd4928e8147 | 126 | void read_RAM(); |
Sissors | 5:2cd4928e8147 | 127 | static bool unassignedProbe(DigitalInOut *pin, char *ROM_address); |
Sissors | 5:2cd4928e8147 | 128 | void write_scratchpad(int data); |
Sissors | 5:2cd4928e8147 | 129 | bool read_power_supply(devices device=this_device); |
Michael_ | 2:ee820a991b95 | 130 | |
Michael_ | 2:ee820a991b95 | 131 | DigitalInOut _datapin; |
Michael_ | 2:ee820a991b95 | 132 | DigitalOut _parasitepin; |
Sissors | 5:2cd4928e8147 | 133 | |
Sissors | 5:2cd4928e8147 | 134 | char _ROM[8]; |
Sissors | 5:2cd4928e8147 | 135 | char RAM[9]; |
Sissors | 5:2cd4928e8147 | 136 | |
Sissors | 5:2cd4928e8147 | 137 | static LinkedList<node> probes; |
Michael_ | 2:ee820a991b95 | 138 | }; |
Michael_ | 2:ee820a991b95 | 139 | |
Michael_ | 2:ee820a991b95 | 140 | |
Michael_ | 2:ee820a991b95 | 141 | #endif |