Becky Page / Mbed OS Assignment1081
Committer:
asreed
Date:
Fri Nov 20 15:13:10 2020 +0000
Revision:
0:44f87c90421e
Child:
1:aef8ec8d23d8
First commit;

Who changed what in which revision?

UserRevisionLine numberNew 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 0:44f87c90421e 28 int main()
asreed 0:44f87c90421e 29 {
asreed 0:44f87c90421e 30 // Initialise the digital pin LED1 as an output
asreed 0:44f87c90421e 31 DigitalOut led(LED1);
asreed 0:44f87c90421e 32 DigitalIn pushButton(SW2, PullUp);
asreed 0:44f87c90421e 33 AnalogIn vTherm(P10_1);
asreed 0:44f87c90421e 34 DigitalOut thermVcc(P10_3);
asreed 0:44f87c90421e 35 DigitalOut thermGnd(P10_0);
asreed 0:44f87c90421e 36
asreed 0:44f87c90421e 37 thermVcc = 1;
asreed 0:44f87c90421e 38 thermGnd = 0;
asreed 0:44f87c90421e 39 while (true) {
asreed 0:44f87c90421e 40 if (pushButton == 0) {
asreed 0:44f87c90421e 41 led = !led;
asreed 0:44f87c90421e 42 /* read thermistor Voltage */
asreed 0:44f87c90421e 43 float refVoltage = vTherm.read() * 2.4; // Range of ADC 0->2*Vref
asreed 0:44f87c90421e 44 float refCurrent = refVoltage / 10000.0; // 10k Reference Resistor
asreed 0:44f87c90421e 45 float thermVoltage = 3.3 - refVoltage; // Assume supply voltage is 3.3v
asreed 0:44f87c90421e 46 float thermResistance = thermVoltage / refCurrent;
asreed 0:44f87c90421e 47 float logrT = (float32_t)log((float64_t)thermResistance);
asreed 0:44f87c90421e 48
asreed 0:44f87c90421e 49 /* Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation */
asreed 0:44f87c90421e 50 float stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) +
asreed 0:44f87c90421e 51 ((2.041094E-07) * pow((float64)logrT, (float32)3)));
asreed 0:44f87c90421e 52
asreed 0:44f87c90421e 53 float temperatureC = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5);
asreed 0:44f87c90421e 54
asreed 0:44f87c90421e 55 printf("Temperature is %f\r\n", temperatureC);
asreed 0:44f87c90421e 56 }
asreed 0:44f87c90421e 57 thread_sleep_for(BLINKING_RATE_MS);
asreed 0:44f87c90421e 58 }
asreed 0:44f87c90421e 59 }