Grove temperature sensor library

Dependents:   Grove_Temp_Hello_world

Committer:
peipei123
Date:
Thu Mar 10 20:16:10 2016 +0000
Revision:
6:2d65df11758a
Parent:
4:7e6f5d859469
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
peipei123 1:813f0d53a75c 1 #include "mbed.h"
peipei123 1:813f0d53a75c 2 #include "Grove_temp_sensor.h"
peipei123 1:813f0d53a75c 3 //Setup a new class for Grove_temp_sensor sensor
peipei123 1:813f0d53a75c 4
peipei123 1:813f0d53a75c 5 Grove_temp_sensor::Grove_temp_sensor(PinName pin) : _pin(pin)
peipei123 1:813f0d53a75c 6 {
peipei123 1:813f0d53a75c 7 wait(0.5);
peipei123 1:813f0d53a75c 8 }
peipei123 1:813f0d53a75c 9
peipei123 3:2b40039c6e54 10 float Grove_temp_sensor::read_F()
peipei123 1:813f0d53a75c 11 {
peipei123 1:813f0d53a75c 12
peipei123 1:813f0d53a75c 13 double a;
peipei123 4:7e6f5d859469 14 a=_pin.read()*1023;
peipei123 4:7e6f5d859469 15 double resistance=(float)(1023-a)*10000/a;
peipei123 4:7e6f5d859469 16 double temperature=1/(log(resistance/10000)/3975+1/298.15)-276.05;
peipei123 1:813f0d53a75c 17 //conver C to F;
peipei123 1:813f0d53a75c 18 temperature = (9.0*temperature)/5.0 + 32.0;
peipei123 1:813f0d53a75c 19 return temperature;
peipei123 1:813f0d53a75c 20 }
peipei123 3:2b40039c6e54 21
peipei123 3:2b40039c6e54 22 float Grove_temp_sensor::read_C()
peipei123 3:2b40039c6e54 23 {
peipei123 3:2b40039c6e54 24
peipei123 3:2b40039c6e54 25 double a;
peipei123 4:7e6f5d859469 26 a=_pin.read()*1023;
peipei123 4:7e6f5d859469 27 double resistance=(float)(1023-a)*10000/a;
peipei123 4:7e6f5d859469 28 double temperature=1/(log(resistance/10000)/3975+1/298.15)-276.05;
peipei123 3:2b40039c6e54 29 return temperature;
peipei123 3:2b40039c6e54 30 }
peipei123 6:2d65df11758a 31
peipei123 6:2d65df11758a 32 float Grove_temp_sensor::operator= (Grove_temp_sensor& rhs) {
peipei123 6:2d65df11758a 33
peipei123 6:2d65df11758a 34 return rhs.read_C();
peipei123 6:2d65df11758a 35 }