TMP102 temperature sensor test using LPC824.

Dependencies:   test_TMP102 mbed

Revision:
1:3241f6a5a51e
Parent:
0:26352a9cd499
Child:
2:40c813b1d6a3
--- a/main.cpp	Sun Nov 19 07:50:57 2017 +0000
+++ b/main.cpp	Sun Nov 19 09:59:21 2017 +0000
@@ -1,11 +1,29 @@
 #include "mbed.h"
-#include "TMP102.h"
+
+// TMP102 I2C slave address(ADD0 connected to GND)
+#define ADDR_TMP102     0x90
+
+// TMP102 registers
+#define TMP102_Temp     0x00
+#define TMP102_Conf     0x01
+#define TMP102_Tlow     0x02
+#define TMP102_Thigh    0x03
 
-TMP102 temp(dp4, dp5, 0x91);
+// Init I2C (SDA: dp4, SCL: dp5)
+I2C i2c(dp4, dp5);
+
+int main() {    
+    while(1) {
+        i2c.write(ADDR_TMP102, 0x00, 1); //Pointer to the temperature register
 
-int main() {
-    while(1) {
-        printf("Temp: %f\n\r", temp.read());
+        char reg[2]={0,0};
+        i2c.read(ADDR_TMP102, reg, 2); // read two bytes data
+
+        // calculate temperature
+        int16_t res = ((int8_t)reg[0] << 4) | ((uint8_t)reg[1] >> 4);
+        float temp = (float) ((float)res * 0.0625);
+
+        printf("Temp: %f\n\r", temp);
         wait(1.0);
     }
 }