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);
    }
}

Files at this revision

API Documentation at this revision

Comitter:
Pawel Zarembski
Date:
Tue Mar 03 14:28:12 2020 +0100
Parent:
1:82fcfd05add8
Commit message:
blocker for rise and fall functions

Changed in this revision

TMP06.cpp Show annotated file Show diff for this revision Revisions of this file
TMP06.h Show annotated file Show diff for this revision Revisions of this file
diff -r 82fcfd05add8 -r 1ff2f041925a TMP06.cpp
--- a/TMP06.cpp	Tue Feb 25 11:36:34 2020 +0100
+++ b/TMP06.cpp	Tue Mar 03 14:28:12 2020 +0100
@@ -15,14 +15,15 @@
 int TMP06::read(float *temperature)
 {
     _pwm_pin.disable_irq();
-    _first_run = true;
+    _first_run = true; // blocker for "rise"
+    _started = false;  // blocker for "fall"
     _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, 
+    // 250 - 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
@@ -39,6 +40,7 @@
     if (_first_run) {
         _timer1.start();
         _first_run = false;
+        _started = true;
     }
     else {
         _timer2.stop();
@@ -49,6 +51,9 @@
 
 void TMP06::on_fall()
 {
-    _timer1.stop();
-    _timer2.start();
+    if (_started) {
+        _timer1.stop();
+        _timer2.start();
+        _started = false;
+    }
 }
diff -r 82fcfd05add8 -r 1ff2f041925a TMP06.h
--- a/TMP06.h	Tue Feb 25 11:36:34 2020 +0100
+++ b/TMP06.h	Tue Mar 03 14:28:12 2020 +0100
@@ -7,7 +7,7 @@
 
 #define SUCCESS 0
 #define FAILURE 1
-#define SEM_TIMEOUT_MS 200 
+#define SEM_TIMEOUT_MS 250 
 
 class TMP06 {
     public:
@@ -24,6 +24,7 @@
         Timer _timer2;
         Semaphore _semaphore;
         bool _first_run;
+        bool _started;
 };
 
 #endif /* _TMP06_H_ */
\ No newline at end of file