This library is designed to work with devices like the LM335 temperature sensor. There are only two requirements for compatibility: 1) The device must be a sensor device that has an analog voltage output. 2) The physical quantity measured must vary linearly with the analog voltage. The LM335 creates an analog voltage proportional to temperature. It can work very well with this library using the nominal multiplier of 0.0050354 and offset of -273.15 or by providing two user defined calibration points to the constructor. It uses a median filter to remove unwanted noise from ADC readings.
Dependents: mbed_measuring_temperature
LinearTemp.cpp@2:989048937c77, 2012-12-23 (annotated)
- Committer:
- PennElectric
- Date:
- Sun Dec 23 07:32:24 2012 +0000
- Revision:
- 2:989048937c77
- Parent:
- 0:d84ef842074b
Fixed operator shorthand
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PennElectric | 0:d84ef842074b | 1 | /* Copyright (c) <2012> <P. Patel>, MIT License |
PennElectric | 0:d84ef842074b | 2 | * |
PennElectric | 0:d84ef842074b | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
PennElectric | 0:d84ef842074b | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
PennElectric | 0:d84ef842074b | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
PennElectric | 0:d84ef842074b | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
PennElectric | 0:d84ef842074b | 7 | * furnished to do so, subject to the following conditions: |
PennElectric | 0:d84ef842074b | 8 | * |
PennElectric | 0:d84ef842074b | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
PennElectric | 0:d84ef842074b | 10 | * substantial portions of the Software. |
PennElectric | 0:d84ef842074b | 11 | * |
PennElectric | 0:d84ef842074b | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
PennElectric | 0:d84ef842074b | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
PennElectric | 0:d84ef842074b | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
PennElectric | 0:d84ef842074b | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
PennElectric | 0:d84ef842074b | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
PennElectric | 0:d84ef842074b | 17 | */ |
PennElectric | 0:d84ef842074b | 18 | |
PennElectric | 0:d84ef842074b | 19 | // --------------------- Median Filtered Linear Temperature Sensor Reader ---------------------------- |
PennElectric | 0:d84ef842074b | 20 | |
PennElectric | 0:d84ef842074b | 21 | #include "LinearTemp.h" |
PennElectric | 0:d84ef842074b | 22 | #include "mbed.h" |
PennElectric | 0:d84ef842074b | 23 | |
PennElectric | 0:d84ef842074b | 24 | // Constructor using two calibration points to define linear multiplier and offset |
PennElectric | 0:d84ef842074b | 25 | LinearTemp::LinearTemp(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2):_pin(pin) { |
PennElectric | 0:d84ef842074b | 26 | _temp = 0.0; // Zero the temperature |
PennElectric | 0:d84ef842074b | 27 | _multiplier = (temp2 - temp1) / (read2 - read1); // Calculate multiplier as slope |
PennElectric | 0:d84ef842074b | 28 | _offset = temp1 - (_multiplier * read1); // Calculate offset |
PennElectric | 0:d84ef842074b | 29 | } |
PennElectric | 0:d84ef842074b | 30 | // Constructor using user defined multiplier and offset |
PennElectric | 0:d84ef842074b | 31 | LinearTemp::LinearTemp(PinName pin, float multiplier, float offset):_pin(pin) { |
PennElectric | 0:d84ef842074b | 32 | _temp = 0.0; // Zero the temperature |
PennElectric | 0:d84ef842074b | 33 | _multiplier = multiplier; // Set multiplier |
PennElectric | 0:d84ef842074b | 34 | _offset = offset; // Set offset |
PennElectric | 0:d84ef842074b | 35 | } |
PennElectric | 0:d84ef842074b | 36 | |
PennElectric | 0:d84ef842074b | 37 | // Populates an array with temperature readings, sorts, and returns median |
PennElectric | 0:d84ef842074b | 38 | float LinearTemp::readTemp() { |
PennElectric | 0:d84ef842074b | 39 | _tempArr[0] = _pin.read_u16(); |
PennElectric | 0:d84ef842074b | 40 | for (int i = 1; i < 9; i++) { |
PennElectric | 0:d84ef842074b | 41 | _tempArr[i] = _pin.read_u16(); |
PennElectric | 0:d84ef842074b | 42 | unsigned short tmp = _tempArr[i]; |
PennElectric | 0:d84ef842074b | 43 | int j; |
PennElectric | 0:d84ef842074b | 44 | for (j = i - 1; j >= 0; j--) { |
PennElectric | 0:d84ef842074b | 45 | if (tmp>=_tempArr[j]) break; |
PennElectric | 0:d84ef842074b | 46 | _tempArr[j + 1] = _tempArr[j]; |
PennElectric | 0:d84ef842074b | 47 | } |
PennElectric | 0:d84ef842074b | 48 | _tempArr[j + 1] = tmp; |
PennElectric | 0:d84ef842074b | 49 | } |
PennElectric | 0:d84ef842074b | 50 | _temp = (_tempArr[4] * _multiplier) + _offset; |
PennElectric | 0:d84ef842074b | 51 | return _temp; |
PennElectric | 0:d84ef842074b | 52 | } |
PennElectric | 0:d84ef842074b | 53 | // Returns last calculated temperature value |
PennElectric | 0:d84ef842074b | 54 | float LinearTemp::getTemp() { |
PennElectric | 0:d84ef842074b | 55 | return _temp; |
PennElectric | 0:d84ef842074b | 56 | } |