Tomo Yamanaka / Mbed 2 deprecated Temperature_Sensor_Sample

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
1050186
Date:
Thu May 12 10:16:22 2016 +0000
Commit message:
first commit

Changed in this revision

ADT7410.cpp Show annotated file Show diff for this revision Revisions of this file
ADT7410.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6e109b79e249 ADT7410.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADT7410.cpp	Thu May 12 10:16:22 2016 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "ADT7410.h"
+
+ADT7410::ADT7410(PinName sda, PinName scl) : i2c(sda, scl) {
+    // set config (16bit resolution)
+    send(ADT7410_CFG_ADDR, 0x80);
+}
+
+void ADT7410::send(char data_0, char data_1) {
+    buf[0] = data_0;
+    buf[1] = data_1;
+
+    i2c.write(ADT7410_I2C_ADDR, buf, 2);
+}
+
+void ADT7410::recv(char data) {
+    buf[0] = data;
+
+    // no stop condition (restared)
+    i2c.write(ADT7410_I2C_ADDR, buf, 1, true);
+    i2c.read(ADT7410_I2C_ADDR, buf, 2);
+}
+
+float ADT7410::temp_value() {
+    int temp;
+    float temp_fix;
+
+    // set config (16bit resolution with one shot mode)
+    send(ADT7410_CFG_ADDR, 0xA0);
+    wait_ms(250);
+    // recieve temperatue value(high and low)
+    recv(ADT7410_TMPH_ADDR);
+    temp = buf[0] << 0x08;
+    temp += buf[1];
+
+    if (temp & 0x8000) {
+        temp_fix = (float)(temp - 65536) / 128;
+    } else {
+        temp_fix = (float)temp / 128;
+    }
+
+    return temp_fix;
+}
diff -r 000000000000 -r 6e109b79e249 ADT7410.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADT7410.h	Thu May 12 10:16:22 2016 +0000
@@ -0,0 +1,25 @@
+#ifndef ADT7410_H_
+#define ADT7410_H_
+
+#include "mbed.h"
+
+#define ADT7410_I2C_ADDR    0x90
+#define ADT7410_TMPH_ADDR   0x00
+#define ADT7410_CFG_ADDR    0x03
+
+class ADT7410 {
+public:
+    ADT7410(PinName sda, PinName scl);
+
+    void send(char data_0, char data_1);
+    void recv(char data);
+    float temp_value();
+
+protected:
+    I2C i2c;
+
+    char buf[2];
+
+};
+
+#endif /* ADT7410_H_ */
diff -r 000000000000 -r 6e109b79e249 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 12 10:16:22 2016 +0000
@@ -0,0 +1,18 @@
+#include "mbed.h"
+#include "ADT7410.h"
+
+ADT7410 sensor(I2C_SDA, I2C_SCL);
+DigitalOut led1(LED1);
+
+int main() {
+    float temp;
+
+    printf("Sensor start!\n");
+    led1 = 1;
+    while(1) {
+        temp = sensor.temp_value();
+        printf("Temperature = %2.2f\n",temp);  
+        led1 = !led1;
+        wait_ms(250);
+    }
+}
diff -r 000000000000 -r 6e109b79e249 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu May 12 10:16:22 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7c328cabac7e
\ No newline at end of file