Driver for TMP06 temperature sensor. Data is sent by PWM. Tested on Hani-IoT board with TMP06BRTZ sensor.
Example usage
main.cpp
#include "mbed.h" #include "TMP06.h" int main() { TMP06 temp_sensor(P0_1); float temperature; while (true) { if(temp_sensor.read(&temperature) == SUCCESS) { printf("Temperature: %f\n", temperature); } else { printf("Error!\n"); } ThisThread::sleep_for(2000); } }
TMP06.cpp
- Committer:
- Pawel Zarembski
- Date:
- 2020-02-25
- Revision:
- 0:cee46eec60a8
- Child:
- 1:82fcfd05add8
File content as of revision 0:cee46eec60a8:
#include "TMP06.h" TMP06::TMP06(PinName pin) : _pwm_pin(pin), _semaphore(0, 1) { } TMP06::~TMP06() { } int TMP06::read(float *temperature) { _pwm_pin.disable_irq(); _first_run = true; _timer1.reset(); _timer2.reset(); _pwm_pin.rise(callback(this, &TMP06::on_rise)); _pwm_pin.fall(callback(this, &TMP06::on_fall)); _pwm_pin.enable_irq(); // 200 - period of sensor should be around 100, // more would means there is some kind of issue if (_semaphore.try_acquire_for(SEM_TIMEOUT_MS)) { // based on TMP05/TMP06 datasheet rev. C *temperature = (421 - 751 * ((float)_timer1.read_us() / (float)_timer2.read_us())); return SUCCESS; } else { return FAILURE; } } void TMP06::on_rise() { if (_first_run) { _timer1.start(); _first_run = false; } else { _timer2.stop(); _semaphore.release(); _pwm_pin.disable_irq(); } } void TMP06::on_fall() { _timer1.stop(); _timer2.start(); }