Sample program to get ambient temperature from MCP9700 sensor

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
todotani
Date:
Tue Nov 23 00:46:29 2010 +0000
Commit message:

Changed in this revision

LinearTempSensor/LinearTempSensor.cpp Show annotated file Show diff for this revision Revisions of this file
LinearTempSensor/LinearTempSensor.h Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LinearTempSensor/LinearTempSensor.cpp	Tue Nov 23 00:46:29 2010 +0000
@@ -0,0 +1,92 @@
+/* mbed Linear Temperature Sensor library
+ * Supports Microchip MCP9700/9701, National Semiconductor LM35
+ *
+ * Written by Todotani, Nov 22 2010
+ */
+
+#include "LinearTempSensor.h"
+
+LinearTempSensor::LinearTempSensor(PinName ain, int N, SensorType type):
+        // Initialize private variables using Initialization Lists
+        // _ain(ain) is equivalent to _ain = new AnalogIn(ain)
+        // _sample(N) is equivalent to _sample = N
+        _ain(ain), _samples(N), _type(type) {
+    sampleBuffer = (float *)malloc(sizeof(float) * _samples);
+    sampleCount = 0;
+    index       = 0;
+    Vref        = 3300;
+    bufferNotFilled = true;
+
+    // Set constants to calculate temperature from sensor value
+    switch (_type) {
+        case LM35:
+            V0 = 0.0f;
+            Tc = 10.0f;
+            break;
+      case MCP9701:
+            V0 = 400.0f;
+            Tc = 19.5f;
+            break;
+      case MCP9700:
+       default:
+            V0 = 500.0f;
+            Tc = 10.0f;
+    }
+    
+    for (int i = 0; i < _samples; i++)
+        sampleBuffer[i] = 0.0f;
+}
+
+
+LinearTempSensor::~LinearTempSensor() {
+    free(sampleBuffer);
+}
+
+
+float LinearTempSensor::Sense() {
+    float val;
+
+    val = _ain * Vref;
+    sampleBuffer[index] = val;
+    //printf("Index:%d  ", index);    // Debug print
+    if ( ++index >= _samples )
+        index = 0;
+
+    if ( bufferNotFilled && (sampleCount == _samples) ) {
+        bufferNotFilled = false;
+    }
+    //printf("flag:%d  ", bufferNotFilled);   // Debug print
+    sampleCount++;
+
+    return val;
+}
+
+
+float LinearTempSensor::GetAverageTemp() {
+    float sum = 0;
+    int i, numberOfsumples;
+
+    if (sampleCount == 0)
+        return 0;
+
+    if (bufferNotFilled) {
+        // In case number of samples less than buffer lenght
+        for (i = 0; i < sampleCount; i++) {
+            sum += sampleBuffer[i];
+        }
+        numberOfsumples = sampleCount;
+    } else {
+        // In case buffer is filled
+        for (i = 0; i < _samples; i++) {
+            sum += sampleBuffer[i];
+        }
+        numberOfsumples = _samples;
+    }
+
+    return ((sum / numberOfsumples) - V0) / Tc;    //  Temp = (Vout - V0) / Tc
+}
+
+
+float LinearTempSensor::GetLatestTemp() {
+    return (sampleBuffer[(sampleCount-1)%_samples] - V0) / Tc;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LinearTempSensor/LinearTempSensor.h	Tue Nov 23 00:46:29 2010 +0000
@@ -0,0 +1,73 @@
+/* mbed Linear Temperature Sensor library
+ * Supports Microchip MCP9700/9701, National Semiconductor LM35
+ *
+ * Written by Todotani, Nov 22 2010
+ */
+ 
+#ifndef MBED_LINEARTEMPSENSOR_H
+#define MBED_LINEARTEMPSENSOR_H
+
+#include "mbed.h"
+
+/** Linear Temperature Sensor class.
+ * Sample and store sensor acuired value in N (default=10) times and
+ * calculate avarage temperature from sampled data
+ * Supports Microchip MCP9700/9701, National Semiconductor LM35
+ * @author Todotani
+ */
+class LinearTempSensor {
+public:
+    /** Sensor Type Definitions  */
+    enum SensorType {
+        MCP9700,    /**< Microchip MCP9700 (Default)  */
+        MCP9701,    /**< Microchip MCP9701 */
+        LM35        /**< National Semiconductor LM35 */
+    };
+    
+    /** Create a Temperature Sensor instanse
+     *
+     * @param ain       PinName of analog input
+     * @param N         Number of samples to calculate average temperature (default = 10)
+     * @param type      Sensor type (default = MCP9700)
+     */
+    LinearTempSensor(PinName ain, int N = 10, SensorType type = MCP9700);
+    
+    /** Sample (read) sensor data and store to buffer
+     *
+     * @param None
+     * @return Sensor-acuired value (mV)
+     */
+     float Sense();
+     
+    /** Calculate average temperature from sample buffer
+     *
+     * @param None
+     * @return Average temperature from N times of sumple (Centigrade)
+     */
+     float GetAverageTemp();
+     
+    /** Calculate temperature from the latest sample
+     *
+     * @param None
+     * @return Temperature from the latest sampled data (Centigrade)
+     */
+     float GetLatestTemp();
+
+     ~LinearTempSensor();
+
+private:
+    AnalogIn    _ain;
+    int         _samples;
+    SensorType  _type;
+    
+    float       *sampleBuffer;      // Buffer to store sensor acuired data
+    bool        bufferNotFilled;    // Flag shows that buffer have not filled
+    uint32_t    sampleCount;
+    uint32_t    index;
+
+    float   V0;                      // Sensor read value in case 0 degree
+    float   Tc;                      // Tmperature coefficient (temprature inclease) in each degree
+    float   Vref;                    // Reference volgate for ADC
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Tue Nov 23 00:46:29 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#a53b3e2d6f1e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Nov 23 00:46:29 2010 +0000
@@ -0,0 +1,28 @@
+#include "mbed.h"
+#include "TextLCD.h"
+#include "LinearTempSensor.h"
+
+TextLCD lcd(p24, p26, p27, p28, p29, p30, TextLCD::LCD16x2);  // RS, E, DB4, DB5, DB6, DB7
+LinearTempSensor sensor(p20);                                 // With default parameters
+//LinearTempSensor sensor(p20, 5, LinearTempSensor::MCP9700); // With option parameters
+
+int main() 
+{
+    float Vout, Tav, To;
+
+    lcd.cls();
+    lcd.printf("TEMP:");
+    
+    while(true)
+    {
+        Vout = sensor.Sense();          // Sample data (read sensor)
+        Tav  = sensor.GetAverageTemp(); // Calculate average temperature from N samples
+        To   = sensor.GetLatestTemp();  // Calculate temperature from the latest sample
+
+        lcd.locate(5, 0);
+        lcd.printf("%4.1f", Tav);
+        printf("Vout:%f  Tav:%f  To:%f\n\r", Vout, Tav, To);    // Debug print
+
+        wait(2.0);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Nov 23 00:46:29 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e