Sample program to get ambient temperature from MCP9700 sensor
Fork of TempSensor by
LinearTempSensor/LinearTempSensor.cpp@1:6a589f1dd263, 2013-06-17 (annotated)
- Committer:
- d0773d
- Date:
- Mon Jun 17 06:26:09 2013 +0000
- Revision:
- 1:6a589f1dd263
- Parent:
- 0:6dde232b3b3b
Who changed what in which revision?
User | Revision | Line number | New 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 | #include "LinearTempSensor.h" |
todotani | 0:6dde232b3b3b | 8 | |
todotani | 0:6dde232b3b3b | 9 | LinearTempSensor::LinearTempSensor(PinName ain, int N, SensorType type): |
todotani | 0:6dde232b3b3b | 10 | // Initialize private variables using Initialization Lists |
todotani | 0:6dde232b3b3b | 11 | // _ain(ain) is equivalent to _ain = new AnalogIn(ain) |
todotani | 0:6dde232b3b3b | 12 | // _sample(N) is equivalent to _sample = N |
todotani | 0:6dde232b3b3b | 13 | _ain(ain), _samples(N), _type(type) { |
todotani | 0:6dde232b3b3b | 14 | sampleBuffer = (float *)malloc(sizeof(float) * _samples); |
todotani | 0:6dde232b3b3b | 15 | sampleCount = 0; |
todotani | 0:6dde232b3b3b | 16 | index = 0; |
todotani | 0:6dde232b3b3b | 17 | Vref = 3300; |
todotani | 0:6dde232b3b3b | 18 | bufferNotFilled = true; |
todotani | 0:6dde232b3b3b | 19 | |
todotani | 0:6dde232b3b3b | 20 | // Set constants to calculate temperature from sensor value |
todotani | 0:6dde232b3b3b | 21 | switch (_type) { |
todotani | 0:6dde232b3b3b | 22 | case LM35: |
todotani | 0:6dde232b3b3b | 23 | V0 = 0.0f; |
todotani | 0:6dde232b3b3b | 24 | Tc = 10.0f; |
todotani | 0:6dde232b3b3b | 25 | break; |
todotani | 0:6dde232b3b3b | 26 | case MCP9701: |
todotani | 0:6dde232b3b3b | 27 | V0 = 400.0f; |
todotani | 0:6dde232b3b3b | 28 | Tc = 19.5f; |
todotani | 0:6dde232b3b3b | 29 | break; |
todotani | 0:6dde232b3b3b | 30 | case MCP9700: |
todotani | 0:6dde232b3b3b | 31 | default: |
todotani | 0:6dde232b3b3b | 32 | V0 = 500.0f; |
todotani | 0:6dde232b3b3b | 33 | Tc = 10.0f; |
todotani | 0:6dde232b3b3b | 34 | } |
todotani | 0:6dde232b3b3b | 35 | |
todotani | 0:6dde232b3b3b | 36 | for (int i = 0; i < _samples; i++) |
todotani | 0:6dde232b3b3b | 37 | sampleBuffer[i] = 0.0f; |
todotani | 0:6dde232b3b3b | 38 | } |
todotani | 0:6dde232b3b3b | 39 | |
todotani | 0:6dde232b3b3b | 40 | |
todotani | 0:6dde232b3b3b | 41 | LinearTempSensor::~LinearTempSensor() { |
todotani | 0:6dde232b3b3b | 42 | free(sampleBuffer); |
todotani | 0:6dde232b3b3b | 43 | } |
todotani | 0:6dde232b3b3b | 44 | |
todotani | 0:6dde232b3b3b | 45 | |
todotani | 0:6dde232b3b3b | 46 | float LinearTempSensor::Sense() { |
todotani | 0:6dde232b3b3b | 47 | float val; |
todotani | 0:6dde232b3b3b | 48 | |
todotani | 0:6dde232b3b3b | 49 | val = _ain * Vref; |
todotani | 0:6dde232b3b3b | 50 | sampleBuffer[index] = val; |
todotani | 0:6dde232b3b3b | 51 | //printf("Index:%d ", index); // Debug print |
todotani | 0:6dde232b3b3b | 52 | if ( ++index >= _samples ) |
todotani | 0:6dde232b3b3b | 53 | index = 0; |
todotani | 0:6dde232b3b3b | 54 | |
todotani | 0:6dde232b3b3b | 55 | if ( bufferNotFilled && (sampleCount == _samples) ) { |
todotani | 0:6dde232b3b3b | 56 | bufferNotFilled = false; |
todotani | 0:6dde232b3b3b | 57 | } |
todotani | 0:6dde232b3b3b | 58 | //printf("flag:%d ", bufferNotFilled); // Debug print |
todotani | 0:6dde232b3b3b | 59 | sampleCount++; |
todotani | 0:6dde232b3b3b | 60 | |
todotani | 0:6dde232b3b3b | 61 | return val; |
todotani | 0:6dde232b3b3b | 62 | } |
todotani | 0:6dde232b3b3b | 63 | |
todotani | 0:6dde232b3b3b | 64 | |
todotani | 0:6dde232b3b3b | 65 | float LinearTempSensor::GetAverageTemp() { |
todotani | 0:6dde232b3b3b | 66 | float sum = 0; |
todotani | 0:6dde232b3b3b | 67 | int i, numberOfsumples; |
todotani | 0:6dde232b3b3b | 68 | |
todotani | 0:6dde232b3b3b | 69 | if (sampleCount == 0) |
todotani | 0:6dde232b3b3b | 70 | return 0; |
todotani | 0:6dde232b3b3b | 71 | |
todotani | 0:6dde232b3b3b | 72 | if (bufferNotFilled) { |
todotani | 0:6dde232b3b3b | 73 | // In case number of samples less than buffer lenght |
todotani | 0:6dde232b3b3b | 74 | for (i = 0; i < sampleCount; i++) { |
todotani | 0:6dde232b3b3b | 75 | sum += sampleBuffer[i]; |
todotani | 0:6dde232b3b3b | 76 | } |
todotani | 0:6dde232b3b3b | 77 | numberOfsumples = sampleCount; |
todotani | 0:6dde232b3b3b | 78 | } else { |
todotani | 0:6dde232b3b3b | 79 | // In case buffer is filled |
todotani | 0:6dde232b3b3b | 80 | for (i = 0; i < _samples; i++) { |
todotani | 0:6dde232b3b3b | 81 | sum += sampleBuffer[i]; |
todotani | 0:6dde232b3b3b | 82 | } |
todotani | 0:6dde232b3b3b | 83 | numberOfsumples = _samples; |
todotani | 0:6dde232b3b3b | 84 | } |
todotani | 0:6dde232b3b3b | 85 | |
todotani | 0:6dde232b3b3b | 86 | return ((sum / numberOfsumples) - V0) / Tc; // Temp = (Vout - V0) / Tc |
todotani | 0:6dde232b3b3b | 87 | } |
todotani | 0:6dde232b3b3b | 88 | |
todotani | 0:6dde232b3b3b | 89 | |
todotani | 0:6dde232b3b3b | 90 | float LinearTempSensor::GetLatestTemp() { |
todotani | 0:6dde232b3b3b | 91 | return (sampleBuffer[(sampleCount-1)%_samples] - V0) / Tc; |
todotani | 0:6dde232b3b3b | 92 | } |