TMP102

Files at this revision

API Documentation at this revision

Comitter:
Nikollao
Date:
Wed Feb 10 16:10:42 2016 +0000
Commit message:
now the library is ready to use;

Changed in this revision

TMP102.cpp Show annotated file Show diff for this revision Revisions of this file
TMP102.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fea9b4d9a373 TMP102.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102.cpp	Wed Feb 10 16:10:42 2016 +0000
@@ -0,0 +1,82 @@
+#include "TMP102"
+
+TMP102::TMP102(PinName sda, PinName scl)
+{
+    is2_ = new I2C(sda,scl);
+    led_ = new DigitalOut(LED_RED);
+}
+
+ void TMP102::init() {
+    
+    i2c_->frequency(400000);
+    int ack;
+    char config_data[2];
+    char reg CONFIG_REG;
+    
+    ack = i2c_->write(TMP102_W_ADD,&reg,1);
+    
+    if (ack)
+        
+        error();
+        
+        i2c_->read(TMP102_R_ADD,&config_data,1);
+    
+    if (ack)
+        
+        error();
+      
+    //set conversion rate to 1 Hz  
+    config_data[1] |= (1<<6); //set bit 6
+    config_data[1] &= ~(1<<7); //clear bit 7
+    
+    char data_packet[3] = {reg, config_data[0], config_data[1]};
+    
+    ack = i2c_->write(TMP102_W_ADD,data_packet,3);
+    
+    if (ack)
+        error();
+        
+}
+
+void TMP102::read_temperature() {
+    
+    int ack;
+    char data[2];
+    char reg TEMP_REG;
+    
+    ack = i2c_->write (TMP102_W_ADD,&reg,1);
+    
+    if (ack) 
+        error();
+    
+    ack = i2c_->read(TMP102_R_ADD,data,2);
+    
+    if (ack)
+        error(); //if we don't receive acknowledgment, flash error message
+        
+    int temperature = (data[0] << 4) | (data[1] >> 4);
+    
+    temperature_ = temperature*0.0625F;
+    
+}
+
+float TMP102::get_temperature() {
+        
+        read_temperature();
+        return temperature_;
+}
+
+void TMP102::error() {
+        
+        while (1) {
+            
+            led_-> write(0);
+            wait(0.2);
+            led_> write(1);
+            wait(0.2);
+        }
+}
+        
+    
+    
+    
\ No newline at end of file
diff -r 000000000000 -r fea9b4d9a373 TMP102.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102.h	Wed Feb 10 16:10:42 2016 +0000
@@ -0,0 +1,46 @@
+#ifndef TMP102_H
+#define TMP102_H
+
+//address for ADD0 connected to GND
+#define TMP102_ADD 0x48
+#define TMP102_R_ADD 0x91
+#define TMP102_W_ADD 0x90
+
+//register addresses
+
+#define TEMP_REG 0x00
+#define CONF_REG 0x01
+#define THIGH_REG 0x02
+#define TLOW_REG 0x03
+
+#include "mbed.h"
+
+class TMP102
+
+//define the methods that are public, which could be used by the user
+
+ public:
+ 
+TMP102(PinName sda, PinName scl);
+
+void init();
+
+float get_temperature;
+
+//define private methods, could be used in the library itself, but could not called by the user
+
+void error();
+
+void read_temperature();
+
+public:
+
+private:
+
+I2C* i2c_;
+DigitalOut* led;
+float temperature_;
+
+#endif
+
+