yasuyuki onodera / ADT7410

Dependents:   mbed_DEMO mbed_BLE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADT7410.cpp Source File

ADT7410.cpp

00001 //**********************
00002 // ADT7410.cpp for mbed
00003 //
00004 // ADT7410 temperature(P0_5,P0_4);
00005 // or
00006 // I2C i2c(P0_5,P0_4);
00007 // ADT7410 temperature(i2c);
00008 //
00009 // (C)Copyright 2014 All rights reserved by Y.Onodera
00010 // http://einstlab.web.fc2.com
00011 //**********************
00012 
00013 #include "mbed.h"
00014 #include "ADT7410.h"
00015 
00016 //#define SPS
00017 #define ONESHOT
00018 
00019 ADT7410::ADT7410 (PinName sda, PinName scl) : _i2c(sda, scl) {
00020     init();
00021 }
00022 ADT7410::ADT7410 (I2C& p_i2c) : _i2c(p_i2c) {
00023     init();
00024 }
00025 
00026 void ADT7410::put(unsigned char a, unsigned char b)
00027 {
00028     buf[0]=a;
00029     buf[1]=b;
00030     _i2c.write(ADT7410_ADDR, buf, 2);
00031 }
00032 
00033 
00034 void ADT7410::get(unsigned char a)
00035 {
00036     buf[0] = a;
00037     _i2c.write(ADT7410_ADDR, buf, 1, false); // no stop, repeated
00038     _i2c.read( ADT7410_ADDR, buf, 2);
00039 
00040 }
00041 
00042 short ADT7410::value()
00043 {
00044 
00045 #ifdef ONESHOT
00046     // wakeup
00047     // set 16bit resolution with one shot mode
00048     put(ADT7410_CONFIG, 0xA0);
00049     wait_ms(240);
00050 #endif
00051 
00052     // RDY?
00053 //    do{
00054 //        get(ADT7410_STATUS);
00055 //    }while(buf[0] & 0x80);
00056 
00057     // get temp_high and low
00058     get(ADT7410_TEMP_H);
00059     temp.byte.HB=buf[0];
00060     temp.byte.LB=buf[1];
00061     return temp.S;
00062     
00063     // C = temp.S / 128
00064     
00065 }
00066 
00067 void ADT7410::init()
00068 {
00069 #ifdef SPS
00070     // set 16bit resolution with 1 sps mode
00071     put(ADT7410_CONFIG, 0xC0);
00072 #endif
00073 #ifdef ONSHOT
00074     // set 16bit resolution with one shot mode
00075     put(ADT7410_CONFIG, 0xA0);
00076 #endif
00077 
00078 }
00079 
00080