Sample program to get ambient temperature from MCP9700 sensor

Dependencies:   mbed

Committer:
todotani
Date:
Tue Nov 23 00:46:29 2010 +0000
Revision:
0:6dde232b3b3b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
todotani 0:6dde232b3b3b 1 /* mbed Linear Temperature Sensor library
todotani 0:6dde232b3b3b 2 * Supports Microchip MCP9700/9701, National Semiconductor LM35
todotani 0:6dde232b3b3b 3 *
todotani 0:6dde232b3b3b 4 * Written by Todotani, Nov 22 2010
todotani 0:6dde232b3b3b 5 */
todotani 0:6dde232b3b3b 6
todotani 0:6dde232b3b3b 7 #ifndef MBED_LINEARTEMPSENSOR_H
todotani 0:6dde232b3b3b 8 #define MBED_LINEARTEMPSENSOR_H
todotani 0:6dde232b3b3b 9
todotani 0:6dde232b3b3b 10 #include "mbed.h"
todotani 0:6dde232b3b3b 11
todotani 0:6dde232b3b3b 12 /** Linear Temperature Sensor class.
todotani 0:6dde232b3b3b 13 * Sample and store sensor acuired value in N (default=10) times and
todotani 0:6dde232b3b3b 14 * calculate avarage temperature from sampled data
todotani 0:6dde232b3b3b 15 * Supports Microchip MCP9700/9701, National Semiconductor LM35
todotani 0:6dde232b3b3b 16 * @author Todotani
todotani 0:6dde232b3b3b 17 */
todotani 0:6dde232b3b3b 18 class LinearTempSensor {
todotani 0:6dde232b3b3b 19 public:
todotani 0:6dde232b3b3b 20 /** Sensor Type Definitions */
todotani 0:6dde232b3b3b 21 enum SensorType {
todotani 0:6dde232b3b3b 22 MCP9700, /**< Microchip MCP9700 (Default) */
todotani 0:6dde232b3b3b 23 MCP9701, /**< Microchip MCP9701 */
todotani 0:6dde232b3b3b 24 LM35 /**< National Semiconductor LM35 */
todotani 0:6dde232b3b3b 25 };
todotani 0:6dde232b3b3b 26
todotani 0:6dde232b3b3b 27 /** Create a Temperature Sensor instanse
todotani 0:6dde232b3b3b 28 *
todotani 0:6dde232b3b3b 29 * @param ain PinName of analog input
todotani 0:6dde232b3b3b 30 * @param N Number of samples to calculate average temperature (default = 10)
todotani 0:6dde232b3b3b 31 * @param type Sensor type (default = MCP9700)
todotani 0:6dde232b3b3b 32 */
todotani 0:6dde232b3b3b 33 LinearTempSensor(PinName ain, int N = 10, SensorType type = MCP9700);
todotani 0:6dde232b3b3b 34
todotani 0:6dde232b3b3b 35 /** Sample (read) sensor data and store to buffer
todotani 0:6dde232b3b3b 36 *
todotani 0:6dde232b3b3b 37 * @param None
todotani 0:6dde232b3b3b 38 * @return Sensor-acuired value (mV)
todotani 0:6dde232b3b3b 39 */
todotani 0:6dde232b3b3b 40 float Sense();
todotani 0:6dde232b3b3b 41
todotani 0:6dde232b3b3b 42 /** Calculate average temperature from sample buffer
todotani 0:6dde232b3b3b 43 *
todotani 0:6dde232b3b3b 44 * @param None
todotani 0:6dde232b3b3b 45 * @return Average temperature from N times of sumple (Centigrade)
todotani 0:6dde232b3b3b 46 */
todotani 0:6dde232b3b3b 47 float GetAverageTemp();
todotani 0:6dde232b3b3b 48
todotani 0:6dde232b3b3b 49 /** Calculate temperature from the latest sample
todotani 0:6dde232b3b3b 50 *
todotani 0:6dde232b3b3b 51 * @param None
todotani 0:6dde232b3b3b 52 * @return Temperature from the latest sampled data (Centigrade)
todotani 0:6dde232b3b3b 53 */
todotani 0:6dde232b3b3b 54 float GetLatestTemp();
todotani 0:6dde232b3b3b 55
todotani 0:6dde232b3b3b 56 ~LinearTempSensor();
todotani 0:6dde232b3b3b 57
todotani 0:6dde232b3b3b 58 private:
todotani 0:6dde232b3b3b 59 AnalogIn _ain;
todotani 0:6dde232b3b3b 60 int _samples;
todotani 0:6dde232b3b3b 61 SensorType _type;
todotani 0:6dde232b3b3b 62
todotani 0:6dde232b3b3b 63 float *sampleBuffer; // Buffer to store sensor acuired data
todotani 0:6dde232b3b3b 64 bool bufferNotFilled; // Flag shows that buffer have not filled
todotani 0:6dde232b3b3b 65 uint32_t sampleCount;
todotani 0:6dde232b3b3b 66 uint32_t index;
todotani 0:6dde232b3b3b 67
todotani 0:6dde232b3b3b 68 float V0; // Sensor read value in case 0 degree
todotani 0:6dde232b3b3b 69 float Tc; // Tmperature coefficient (temprature inclease) in each degree
todotani 0:6dde232b3b3b 70 float Vref; // Reference volgate for ADC
todotani 0:6dde232b3b3b 71 };
todotani 0:6dde232b3b3b 72
todotani 0:6dde232b3b3b 73 #endif