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.
Dependencies: BLE_API color_pixels mbed-src-nrf51822 nRF51822
Fork of BLE_LoopbackUART by
unified_driver/analog_thermometer.cpp@9:84cb66d0375d, 2014-11-06 (annotated)
- Committer:
- yihui
- Date:
- Thu Nov 06 02:22:01 2014 +0000
- Revision:
- 9:84cb66d0375d
Grove Node initial
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yihui | 9:84cb66d0375d | 1 | |
| yihui | 9:84cb66d0375d | 2 | #include "unified_driver.h" |
| yihui | 9:84cb66d0375d | 3 | #include "mbed.h" |
| yihui | 9:84cb66d0375d | 4 | |
| yihui | 9:84cb66d0375d | 5 | int analog_thermometer_init(void *obj, void *params) |
| yihui | 9:84cb66d0375d | 6 | { |
| yihui | 9:84cb66d0375d | 7 | int pin = *(int *)params; |
| yihui | 9:84cb66d0375d | 8 | *(AnalogIn **)obj = new AnalogIn((PinName)pin); |
| yihui | 9:84cb66d0375d | 9 | |
| yihui | 9:84cb66d0375d | 10 | return 0; |
| yihui | 9:84cb66d0375d | 11 | } |
| yihui | 9:84cb66d0375d | 12 | |
| yihui | 9:84cb66d0375d | 13 | |
| yihui | 9:84cb66d0375d | 14 | int analog_thermometer_read(void *obj, void *data) |
| yihui | 9:84cb66d0375d | 15 | { |
| yihui | 9:84cb66d0375d | 16 | const uint32_t beta = 3975; |
| yihui | 9:84cb66d0375d | 17 | float a; |
| yihui | 9:84cb66d0375d | 18 | float temperature; |
| yihui | 9:84cb66d0375d | 19 | float resistance; |
| yihui | 9:84cb66d0375d | 20 | AnalogIn *probe = *(AnalogIn **)obj; |
| yihui | 9:84cb66d0375d | 21 | |
| yihui | 9:84cb66d0375d | 22 | a = probe->read(); |
| yihui | 9:84cb66d0375d | 23 | |
| yihui | 9:84cb66d0375d | 24 | /* Calculate the resistance of the thermistor from analog votage read. */ |
| yihui | 9:84cb66d0375d | 25 | resistance = (float) 10000.0 * ((1 / a) - 1); |
| yihui | 9:84cb66d0375d | 26 | |
| yihui | 9:84cb66d0375d | 27 | /* Convert the resistance to temperature using Steinhart's Hart equation */ |
| yihui | 9:84cb66d0375d | 28 | temperature = (1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15; |
| yihui | 9:84cb66d0375d | 29 | |
| yihui | 9:84cb66d0375d | 30 | *(float *)data = temperature; |
| yihui | 9:84cb66d0375d | 31 | |
| yihui | 9:84cb66d0375d | 32 | return 0; |
| yihui | 9:84cb66d0375d | 33 | } |
| yihui | 9:84cb66d0375d | 34 | |
| yihui | 9:84cb66d0375d | 35 | int analog_thermometer_write(void *obj, void *data) |
| yihui | 9:84cb66d0375d | 36 | { |
| yihui | 9:84cb66d0375d | 37 | return 0; |
| yihui | 9:84cb66d0375d | 38 | } |
| yihui | 9:84cb66d0375d | 39 | |
| yihui | 9:84cb66d0375d | 40 | int analog_thermometer_fini(void *obj) |
| yihui | 9:84cb66d0375d | 41 | { |
| yihui | 9:84cb66d0375d | 42 | AnalogIn *ptr = *(AnalogIn **)obj; |
| yihui | 9:84cb66d0375d | 43 | delete ptr; |
| yihui | 9:84cb66d0375d | 44 | |
| yihui | 9:84cb66d0375d | 45 | return 0; |
| yihui | 9:84cb66d0375d | 46 | } |
| yihui | 9:84cb66d0375d | 47 | |
| yihui | 9:84cb66d0375d | 48 | driver_t analog_thermometer_driver = |
| yihui | 9:84cb66d0375d | 49 | { |
| yihui | 9:84cb66d0375d | 50 | "analog thermometer", |
| yihui | 9:84cb66d0375d | 51 | 1, |
| yihui | 9:84cb66d0375d | 52 | analog_thermometer_init, |
| yihui | 9:84cb66d0375d | 53 | analog_thermometer_read, |
| yihui | 9:84cb66d0375d | 54 | analog_thermometer_write, |
| yihui | 9:84cb66d0375d | 55 | analog_thermometer_fini, |
| yihui | 9:84cb66d0375d | 56 | }; |
