Tomo Yamanaka / Mbed 2 deprecated Temperature_Sensor_Sample

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADT7410.cpp Source File

ADT7410.cpp

00001 #include "mbed.h"
00002 #include "ADT7410.h"
00003 
00004 ADT7410::ADT7410(PinName sda, PinName scl) : i2c(sda, scl) {
00005     // set config (16bit resolution)
00006     send(ADT7410_CFG_ADDR, 0x80);
00007 }
00008 
00009 void ADT7410::send(char data_0, char data_1) {
00010     buf[0] = data_0;
00011     buf[1] = data_1;
00012 
00013     i2c.write(ADT7410_I2C_ADDR, buf, 2);
00014 }
00015 
00016 void ADT7410::recv(char data) {
00017     buf[0] = data;
00018 
00019     // no stop condition (restared)
00020     i2c.write(ADT7410_I2C_ADDR, buf, 1, true);
00021     i2c.read(ADT7410_I2C_ADDR, buf, 2);
00022 }
00023 
00024 float ADT7410::temp_value() {
00025     int temp;
00026     float temp_fix;
00027 
00028     // set config (16bit resolution with one shot mode)
00029     send(ADT7410_CFG_ADDR, 0xA0);
00030     wait_ms(250);
00031     // recieve temperatue value(high and low)
00032     recv(ADT7410_TMPH_ADDR);
00033     temp = buf[0] << 0x08;
00034     temp += buf[1];
00035 
00036     if (temp & 0x8000) {
00037         temp_fix = (float)(temp - 65536) / 128;
00038     } else {
00039         temp_fix = (float)temp / 128;
00040     }
00041 
00042     return temp_fix;
00043 }