A digital thermometer
Dependencies: DigitDisplay mbed
main.cpp@0:9fd28909b48d, 2014-04-28 (annotated)
- Committer:
- yihui
- Date:
- Mon Apr 28 01:11:23 2014 +0000
- Revision:
- 0:9fd28909b48d
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:9fd28909b48d | 1 | #include "mbed.h" |
yihui | 0:9fd28909b48d | 2 | #include "DigitDisplay.h" |
yihui | 0:9fd28909b48d | 3 | |
yihui | 0:9fd28909b48d | 4 | DigitDisplay display(P1_14, P1_13); // 4-Digit Display connected to UART Grove connector |
yihui | 0:9fd28909b48d | 5 | AnalogIn thermistor(P0_12); // Thermistor output connected to P0_12 |
yihui | 0:9fd28909b48d | 6 | |
yihui | 0:9fd28909b48d | 7 | int main() { |
yihui | 0:9fd28909b48d | 8 | unsigned int a; |
yihui | 0:9fd28909b48d | 9 | unsigned int beta = 3975; |
yihui | 0:9fd28909b48d | 10 | float temperature, resistance; |
yihui | 0:9fd28909b48d | 11 | |
yihui | 0:9fd28909b48d | 12 | while(1) { |
yihui | 0:9fd28909b48d | 13 | a = thermistor.read_u16(); /* Read analog value */ |
yihui | 0:9fd28909b48d | 14 | |
yihui | 0:9fd28909b48d | 15 | /* Calculate the resistance of the thermistor from analog votage read. */ |
yihui | 0:9fd28909b48d | 16 | resistance= (float) 10000.0 * ((65536.0 / a) - 1.0); |
yihui | 0:9fd28909b48d | 17 | |
yihui | 0:9fd28909b48d | 18 | /* Convert the resistance to temperature using Steinhart's Hart equation */ |
yihui | 0:9fd28909b48d | 19 | temperature = (1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15; |
yihui | 0:9fd28909b48d | 20 | |
yihui | 0:9fd28909b48d | 21 | display = (int)temperature; |
yihui | 0:9fd28909b48d | 22 | |
yihui | 0:9fd28909b48d | 23 | wait(0.5); |
yihui | 0:9fd28909b48d | 24 | } |
yihui | 0:9fd28909b48d | 25 | } |