Library for MAX31850 thermocouple temperature sensor
MAX31850.h@0:5530bf2d0e94, 2019-01-23 (annotated)
- Committer:
- croberts21
- Date:
- Wed Jan 23 23:44:57 2019 +0000
- Revision:
- 0:5530bf2d0e94
All files added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
croberts21 | 0:5530bf2d0e94 | 1 | /* mbed MAX31850 |
croberts21 | 0:5530bf2d0e94 | 2 | Library, for the Dallas (Maxim) 1-Wire Digital Thermometer |
croberts21 | 0:5530bf2d0e94 | 3 | * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com |
croberts21 | 0:5530bf2d0e94 | 4 | * |
croberts21 | 0:5530bf2d0e94 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
croberts21 | 0:5530bf2d0e94 | 6 | * of this software and associated documentation files (the "Software"), to deal |
croberts21 | 0:5530bf2d0e94 | 7 | * in the Software without restriction, including without limitation the rights |
croberts21 | 0:5530bf2d0e94 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
croberts21 | 0:5530bf2d0e94 | 9 | * copies of the Software, and to permit persons to whom the Software is |
croberts21 | 0:5530bf2d0e94 | 10 | * furnished to do so, subject to the following conditions: |
croberts21 | 0:5530bf2d0e94 | 11 | * |
croberts21 | 0:5530bf2d0e94 | 12 | * The above copyright notice and this permission notice shall be included in |
croberts21 | 0:5530bf2d0e94 | 13 | * all copies or substantial portions of the Software. |
croberts21 | 0:5530bf2d0e94 | 14 | * |
croberts21 | 0:5530bf2d0e94 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
croberts21 | 0:5530bf2d0e94 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
croberts21 | 0:5530bf2d0e94 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
croberts21 | 0:5530bf2d0e94 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
croberts21 | 0:5530bf2d0e94 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
croberts21 | 0:5530bf2d0e94 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
croberts21 | 0:5530bf2d0e94 | 21 | * THE SOFTWARE. |
croberts21 | 0:5530bf2d0e94 | 22 | * |
croberts21 | 0:5530bf2d0e94 | 23 | * |
croberts21 | 0:5530bf2d0e94 | 24 | * This library has been adapted from the DS1820 library to work with MAX31850 thermocouple sensor |
croberts21 | 0:5530bf2d0e94 | 25 | * Chris Roberts (croberts21@my.whitworth.edu) |
croberts21 | 0:5530bf2d0e94 | 26 | * Craig Russell (crussell21@my.whitworth.edu) |
croberts21 | 0:5530bf2d0e94 | 27 | * |
croberts21 | 0:5530bf2d0e94 | 28 | * For our application, we modified readAll() (previously convertTemperature) to force a small |
croberts21 | 0:5530bf2d0e94 | 29 | * wait rather than it being optional, and removed the ability to select a single sensor. |
croberts21 | 0:5530bf2d0e94 | 30 | * |
croberts21 | 0:5530bf2d0e94 | 31 | * getTemp() (previously temperature) has been updated to deal with the correct number of bits that |
croberts21 | 0:5530bf2d0e94 | 32 | * the temperature is stored in as well as updated convertions to all four units |
croberts21 | 0:5530bf2d0e94 | 33 | * |
croberts21 | 0:5530bf2d0e94 | 34 | * as well as adding a setUnits() function to set the |
croberts21 | 0:5530bf2d0e94 | 35 | * units that the sensor will return. |
croberts21 | 0:5530bf2d0e94 | 36 | * |
croberts21 | 0:5530bf2d0e94 | 37 | * We removed some functionality that dealt with parasite power. |
croberts21 | 0:5530bf2d0e94 | 38 | * |
croberts21 | 0:5530bf2d0e94 | 39 | * |
croberts21 | 0:5530bf2d0e94 | 40 | */ |
croberts21 | 0:5530bf2d0e94 | 41 | |
croberts21 | 0:5530bf2d0e94 | 42 | #ifndef MBED_MAX31850_H |
croberts21 | 0:5530bf2d0e94 | 43 | #define MBED_MAX31850_H |
croberts21 | 0:5530bf2d0e94 | 44 | |
croberts21 | 0:5530bf2d0e94 | 45 | #include "mbed.h" |
croberts21 | 0:5530bf2d0e94 | 46 | #include "LinkedList.h" |
croberts21 | 0:5530bf2d0e94 | 47 | |
croberts21 | 0:5530bf2d0e94 | 48 | #define C 0 //celsius |
croberts21 | 0:5530bf2d0e94 | 49 | #define K 1 //Kelvin |
croberts21 | 0:5530bf2d0e94 | 50 | #define F 2 //fahrenheit |
croberts21 | 0:5530bf2d0e94 | 51 | #define R 3 //Rankin |
croberts21 | 0:5530bf2d0e94 | 52 | |
croberts21 | 0:5530bf2d0e94 | 53 | extern Serial pc; |
croberts21 | 0:5530bf2d0e94 | 54 | |
croberts21 | 0:5530bf2d0e94 | 55 | /** MAX31850 |
croberts21 | 0:5530bf2d0e94 | 56 | * |
croberts21 | 0:5530bf2d0e94 | 57 | * Example: |
croberts21 | 0:5530bf2d0e94 | 58 | * @code |
croberts21 | 0:5530bf2d0e94 | 59 | * #include "mbed.h" |
croberts21 | 0:5530bf2d0e94 | 60 | * #include "MAX31850.h" |
croberts21 | 0:5530bf2d0e94 | 61 | * |
croberts21 | 0:5530bf2d0e94 | 62 | * MAX31850 probe(DATA_PIN); |
croberts21 | 0:5530bf2d0e94 | 63 | * |
croberts21 | 0:5530bf2d0e94 | 64 | * int main() { |
croberts21 | 0:5530bf2d0e94 | 65 | * while(true) { |
croberts21 | 0:5530bf2d0e94 | 66 | * probe.convertTemperature(true, MAX31850::all_devices); //Start temperature conversion, wait until ready |
croberts21 | 0:5530bf2d0e94 | 67 | * printf("It is %3.1foC\r\n", probe.temperature()); |
croberts21 | 0:5530bf2d0e94 | 68 | * wait(1); |
croberts21 | 0:5530bf2d0e94 | 69 | * } |
croberts21 | 0:5530bf2d0e94 | 70 | * } |
croberts21 | 0:5530bf2d0e94 | 71 | * @endcode |
croberts21 | 0:5530bf2d0e94 | 72 | */ |
croberts21 | 0:5530bf2d0e94 | 73 | class MAX31850 |
croberts21 | 0:5530bf2d0e94 | 74 | { |
croberts21 | 0:5530bf2d0e94 | 75 | public: |
croberts21 | 0:5530bf2d0e94 | 76 | enum devices{ |
croberts21 | 0:5530bf2d0e94 | 77 | this_device, // command applies to only this device |
croberts21 | 0:5530bf2d0e94 | 78 | all_devices }; // command applies to all devices |
croberts21 | 0:5530bf2d0e94 | 79 | |
croberts21 | 0:5530bf2d0e94 | 80 | enum { |
croberts21 | 0:5530bf2d0e94 | 81 | invalid_conversion = -1000 |
croberts21 | 0:5530bf2d0e94 | 82 | }; |
croberts21 | 0:5530bf2d0e94 | 83 | |
croberts21 | 0:5530bf2d0e94 | 84 | /** Create a probe object connected to the specified pins |
croberts21 | 0:5530bf2d0e94 | 85 | * |
croberts21 | 0:5530bf2d0e94 | 86 | * The probe might either by regular powered or parasite powered. If it is parasite |
croberts21 | 0:5530bf2d0e94 | 87 | * powered and power_pin is set, that pin will be used to switch an external mosfet connecting |
croberts21 | 0:5530bf2d0e94 | 88 | * data to Vdd. If it is parasite powered and the pin is not set, the regular data pin |
croberts21 | 0:5530bf2d0e94 | 89 | * is used to supply extra power when required. This will be sufficient as long as the |
croberts21 | 0:5530bf2d0e94 | 90 | * number of probes is limitted. |
croberts21 | 0:5530bf2d0e94 | 91 | * |
croberts21 | 0:5530bf2d0e94 | 92 | * For our application, we have the functions set up to only use external power. The constructor still supports |
croberts21 | 0:5530bf2d0e94 | 93 | * parasite power but the functions to not support it. |
croberts21 | 0:5530bf2d0e94 | 94 | */ |
croberts21 | 0:5530bf2d0e94 | 95 | MAX31850 (PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin |
croberts21 | 0:5530bf2d0e94 | 96 | |
croberts21 | 0:5530bf2d0e94 | 97 | ~MAX31850(); |
croberts21 | 0:5530bf2d0e94 | 98 | |
croberts21 | 0:5530bf2d0e94 | 99 | /** Function to see if there are MAX31850 |
croberts21 | 0:5530bf2d0e94 | 100 | * devices left on a pin which do not have a corresponding MAX31850 |
croberts21 | 0:5530bf2d0e94 | 101 | * object |
croberts21 | 0:5530bf2d0e94 | 102 | * |
croberts21 | 0:5530bf2d0e94 | 103 | * @return - true if there are one or more unassigned devices, otherwise false |
croberts21 | 0:5530bf2d0e94 | 104 | */ |
croberts21 | 0:5530bf2d0e94 | 105 | static bool unassignedProbe(PinName pin); |
croberts21 | 0:5530bf2d0e94 | 106 | |
croberts21 | 0:5530bf2d0e94 | 107 | /** This routine will initiate the temperature conversion within |
croberts21 | 0:5530bf2d0e94 | 108 | * all MAX31850 |
croberts21 | 0:5530bf2d0e94 | 109 | * probes. |
croberts21 | 0:5530bf2d0e94 | 110 | */ |
croberts21 | 0:5530bf2d0e94 | 111 | void readAll(); |
croberts21 | 0:5530bf2d0e94 | 112 | |
croberts21 | 0:5530bf2d0e94 | 113 | /** This function will return the probe temperature. Approximately 10ms per |
croberts21 | 0:5530bf2d0e94 | 114 | * probe to read its scratchpad, do CRC check and convert temperature on the LPC1768. |
croberts21 | 0:5530bf2d0e94 | 115 | * |
croberts21 | 0:5530bf2d0e94 | 116 | * @scratchpad scale, may be either 'c', 'f', 'k', and 'r' |
croberts21 | 0:5530bf2d0e94 | 117 | * @returns temperature for that scale, or MAX31850::invalid_conversion (-1000) if CRC error detected. |
croberts21 | 0:5530bf2d0e94 | 118 | */ |
croberts21 | 0:5530bf2d0e94 | 119 | float getTemp(int scale = 4); |
croberts21 | 0:5530bf2d0e94 | 120 | |
croberts21 | 0:5530bf2d0e94 | 121 | /** This function returns the 64 bit address associated with a |
croberts21 | 0:5530bf2d0e94 | 122 | * particular DS18B20. |
croberts21 | 0:5530bf2d0e94 | 123 | * |
croberts21 | 0:5530bf2d0e94 | 124 | * Contributed by John M. Larkin (jlarkin@whitworth.edu) |
croberts21 | 0:5530bf2d0e94 | 125 | * |
croberts21 | 0:5530bf2d0e94 | 126 | * @returns unsigned long long |
croberts21 | 0:5530bf2d0e94 | 127 | */ |
croberts21 | 0:5530bf2d0e94 | 128 | unsigned long long getId(); |
croberts21 | 0:5530bf2d0e94 | 129 | |
croberts21 | 0:5530bf2d0e94 | 130 | |
croberts21 | 0:5530bf2d0e94 | 131 | /** This function sets the _units variable to the |
croberts21 | 0:5530bf2d0e94 | 132 | * users desired temperature units |
croberts21 | 0:5530bf2d0e94 | 133 | */ |
croberts21 | 0:5530bf2d0e94 | 134 | void setUnits(int); |
croberts21 | 0:5530bf2d0e94 | 135 | |
croberts21 | 0:5530bf2d0e94 | 136 | |
croberts21 | 0:5530bf2d0e94 | 137 | private: |
croberts21 | 0:5530bf2d0e94 | 138 | bool _parasite_power; |
croberts21 | 0:5530bf2d0e94 | 139 | bool _power_mosfet; |
croberts21 | 0:5530bf2d0e94 | 140 | bool _power_polarity; |
croberts21 | 0:5530bf2d0e94 | 141 | |
croberts21 | 0:5530bf2d0e94 | 142 | static char CRC_byte(char _CRC, char byte ); |
croberts21 | 0:5530bf2d0e94 | 143 | static bool onewire_reset(DigitalInOut *pin); |
croberts21 | 0:5530bf2d0e94 | 144 | void match_ROM(); |
croberts21 | 0:5530bf2d0e94 | 145 | void skip_ROM(); |
croberts21 | 0:5530bf2d0e94 | 146 | static bool search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address); |
croberts21 | 0:5530bf2d0e94 | 147 | static void onewire_bit_out (DigitalInOut *pin, bool bit_data); |
croberts21 | 0:5530bf2d0e94 | 148 | void onewire_byte_out(char data); |
croberts21 | 0:5530bf2d0e94 | 149 | static bool onewire_bit_in(DigitalInOut *pin); |
croberts21 | 0:5530bf2d0e94 | 150 | char onewire_byte_in(); |
croberts21 | 0:5530bf2d0e94 | 151 | static bool ROM_checksum_error(char *_ROM_address); |
croberts21 | 0:5530bf2d0e94 | 152 | bool scratchpad_checksum_error(); |
croberts21 | 0:5530bf2d0e94 | 153 | void read_scratchpad(); |
croberts21 | 0:5530bf2d0e94 | 154 | static bool unassignedProbe(DigitalInOut *pin, char *ROM_address); |
croberts21 | 0:5530bf2d0e94 | 155 | |
croberts21 | 0:5530bf2d0e94 | 156 | DigitalInOut _datapin; |
croberts21 | 0:5530bf2d0e94 | 157 | DigitalOut _parasitepin; |
croberts21 | 0:5530bf2d0e94 | 158 | |
croberts21 | 0:5530bf2d0e94 | 159 | char _ROM[8]; |
croberts21 | 0:5530bf2d0e94 | 160 | char scratchpad[9]; |
croberts21 | 0:5530bf2d0e94 | 161 | int _units; |
croberts21 | 0:5530bf2d0e94 | 162 | |
croberts21 | 0:5530bf2d0e94 | 163 | static LinkedList<node> probes; |
croberts21 | 0:5530bf2d0e94 | 164 | }; |
croberts21 | 0:5530bf2d0e94 | 165 | #endif |