Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@1:aef8ec8d23d8, 2020-11-27 (annotated)
- Committer:
- asreed
- Date:
- Fri Nov 27 09:25:06 2020 +0000
- Revision:
- 1:aef8ec8d23d8
- Parent:
- 0:44f87c90421e
- Child:
- 2:daceae26f5df
27th Nov start
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| asreed | 0:44f87c90421e | 1 | /* mbed Microcontroller Library |
| asreed | 0:44f87c90421e | 2 | * Copyright (c) 2019 ARM Limited |
| asreed | 0:44f87c90421e | 3 | * SPDX-License-Identifier: Apache-2.0 |
| asreed | 0:44f87c90421e | 4 | */ |
| asreed | 0:44f87c90421e | 5 | |
| asreed | 0:44f87c90421e | 6 | #include "mbed.h" |
| asreed | 0:44f87c90421e | 7 | #include "platform/mbed_thread.h" |
| asreed | 0:44f87c90421e | 8 | #include "stdio.h" |
| asreed | 0:44f87c90421e | 9 | /* Reference resistor in series with the thermistor is of 10 KOhm */ |
| asreed | 0:44f87c90421e | 10 | #define R_REFERENCE (float)(10000) |
| asreed | 0:44f87c90421e | 11 | /* Beta constant of this thermistor is 3380 Kelvin. See the thermistor |
| asreed | 0:44f87c90421e | 12 | (NCP18XH103F03RB) data sheet for more details. */ |
| asreed | 0:44f87c90421e | 13 | #define B_CONSTANT (float)(3380) |
| asreed | 0:44f87c90421e | 14 | /* Resistance of the thermistor is 10K at 25 degrees C (from data sheet) |
| asreed | 0:44f87c90421e | 15 | Therefore R0 = 10000 Ohm, and T0 = 298.15 Kelvin, which gives |
| asreed | 0:44f87c90421e | 16 | R_INFINITY = R0 e^(-B_CONSTANT / T0) = 0.1192855 */ |
| asreed | 0:44f87c90421e | 17 | #define R_INFINITY (float)(0.1192855) |
| asreed | 0:44f87c90421e | 18 | /* Zero Kelvin in degree C */ |
| asreed | 0:44f87c90421e | 19 | #define ABSOLUTE_ZERO (float)(-273.15) |
| asreed | 0:44f87c90421e | 20 | //static DigitalIn thermVDD(P10_0); // if wing is detached and powered from 3.3v |
| asreed | 0:44f87c90421e | 21 | //static DigitalIn thermGND(P10_3); // don't need to control power to thermistor |
| asreed | 0:44f87c90421e | 22 | |
| asreed | 0:44f87c90421e | 23 | |
| asreed | 0:44f87c90421e | 24 | // Blinking rate in milliseconds |
| asreed | 0:44f87c90421e | 25 | #define BLINKING_RATE_MS 500 |
| asreed | 0:44f87c90421e | 26 | #define SW2 P0_4 |
| asreed | 0:44f87c90421e | 27 | |
| asreed | 1:aef8ec8d23d8 | 28 | char buffer[80]; |
| asreed | 1:aef8ec8d23d8 | 29 | /* prototype of function */ |
| asreed | 1:aef8ec8d23d8 | 30 | void displayAt( int x, int y, char *buffer ); |
| asreed | 1:aef8ec8d23d8 | 31 | |
| asreed | 0:44f87c90421e | 32 | int main() |
| asreed | 0:44f87c90421e | 33 | { |
| asreed | 0:44f87c90421e | 34 | // Initialise the digital pin LED1 as an output |
| asreed | 0:44f87c90421e | 35 | DigitalOut led(LED1); |
| asreed | 0:44f87c90421e | 36 | DigitalIn pushButton(SW2, PullUp); |
| asreed | 0:44f87c90421e | 37 | AnalogIn vTherm(P10_1); |
| asreed | 0:44f87c90421e | 38 | DigitalOut thermVcc(P10_3); |
| asreed | 0:44f87c90421e | 39 | DigitalOut thermGnd(P10_0); |
| asreed | 1:aef8ec8d23d8 | 40 | printf("\033[2J\033[H"); // clear screen and move the cursor to 0, 0 |
| asreed | 1:aef8ec8d23d8 | 41 | printf("\033[?25l"); // Turn off visible cursor |
| asreed | 1:aef8ec8d23d8 | 42 | fflush(stdout); // send the codes to the terminal |
| asreed | 0:44f87c90421e | 43 | thermVcc = 1; |
| asreed | 0:44f87c90421e | 44 | thermGnd = 0; |
| asreed | 0:44f87c90421e | 45 | while (true) { |
| asreed | 0:44f87c90421e | 46 | if (pushButton == 0) { |
| asreed | 0:44f87c90421e | 47 | led = !led; |
| asreed | 0:44f87c90421e | 48 | /* read thermistor Voltage */ |
| asreed | 0:44f87c90421e | 49 | float refVoltage = vTherm.read() * 2.4; // Range of ADC 0->2*Vref |
| asreed | 0:44f87c90421e | 50 | float refCurrent = refVoltage / 10000.0; // 10k Reference Resistor |
| asreed | 0:44f87c90421e | 51 | float thermVoltage = 3.3 - refVoltage; // Assume supply voltage is 3.3v |
| asreed | 0:44f87c90421e | 52 | float thermResistance = thermVoltage / refCurrent; |
| asreed | 0:44f87c90421e | 53 | float logrT = (float32_t)log((float64_t)thermResistance); |
| asreed | 0:44f87c90421e | 54 | |
| asreed | 0:44f87c90421e | 55 | /* Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation */ |
| asreed | 0:44f87c90421e | 56 | float stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) + |
| asreed | 0:44f87c90421e | 57 | ((2.041094E-07) * pow((float64)logrT, (float32)3))); |
| asreed | 0:44f87c90421e | 58 | |
| asreed | 0:44f87c90421e | 59 | float temperatureC = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5); |
| asreed | 0:44f87c90421e | 60 | |
| asreed | 1:aef8ec8d23d8 | 61 | sprintf(buffer, "Temperature is %f\r\n", temperatureC); |
| asreed | 1:aef8ec8d23d8 | 62 | displayAt(0, 3, buffer); |
| asreed | 0:44f87c90421e | 63 | } |
| asreed | 0:44f87c90421e | 64 | thread_sleep_for(BLINKING_RATE_MS); |
| asreed | 0:44f87c90421e | 65 | } |
| asreed | 0:44f87c90421e | 66 | } |
| asreed | 1:aef8ec8d23d8 | 67 | |
| asreed | 1:aef8ec8d23d8 | 68 | void displayAt( int x, int y, char *buffer ) { |
| asreed | 1:aef8ec8d23d8 | 69 | printf( "\033[%d;%dH%s", y, x, buffer); |
| asreed | 1:aef8ec8d23d8 | 70 | fflush(stdout); |
| asreed | 1:aef8ec8d23d8 | 71 | } |