source: rtos_arduino/trunk/examples/CompositeExample/Adafruit_TMP007_Library/Adafruit_TMP007.cpp@ 137

Last change on this file since 137 was 137, checked in by ertl-honda, 8 years ago

サンプルの追加.

File size: 3.8 KB
Line 
1/***************************************************
2 This is a library for the TMP007 Temp Sensor
3
4 Designed specifically to work with the Adafruit TMP007 Breakout
5 ----> https://www.adafruit.com/products/2023
6
7 These displays use I2C to communicate, 2 pins are required to
8 interface
9 Adafruit invests time and resources providing this open source code,
10 please support Adafruit and open-source hardware by purchasing
11 products from Adafruit!
12
13 Written by Limor Fried/Ladyada for Adafruit Industries.
14 BSD license, all text above must be included in any redistribution
15 ****************************************************/
16
17#include "Adafruit_TMP007.h"
18//#include <util/delay.h>
19
20//#define TESTDIE 0x0C78
21//#define TESTVOLT 0xFEED
22
23Adafruit_TMP007::Adafruit_TMP007(uint8_t i2caddr) {
24 _addr = i2caddr;
25}
26
27
28boolean Adafruit_TMP007::begin(uint16_t samplerate) {
29 Wire.begin();
30 write16(TMP007_CONFIG, TMP007_CFG_MODEON | TMP007_CFG_ALERTEN |
31 TMP007_CFG_TRANSC | samplerate);
32 write16(TMP007_STATMASK, TMP007_STAT_ALERTEN |TMP007_STAT_CRTEN);
33 // enable conversion ready alert
34 uint16_t did;
35 did = read16(TMP007_DEVID);
36#ifdef TMP007_DEBUG
37 Serial.print("did = 0x"); Serial.println(did, HEX);
38#endif
39 if (did != 0x78) return false;
40 return true;
41}
42
43//////////////////////////////////////////////////////
44
45double Adafruit_TMP007::readDieTempC(void) {
46 double Tdie = readRawDieTemperature();
47 Tdie *= 0.03125; // convert to celsius
48#ifdef TMP007_DEBUG
49 Serial.print("Tdie = "); Serial.print(Tdie); Serial.println(" C");
50#endif
51 return Tdie;
52}
53
54double Adafruit_TMP007::readObjTempC(void) {
55 int16_t raw = read16(TMP007_TOBJ);
56 // invalid
57 if (raw & 0x1) return NAN;
58 raw >>=2;
59
60 double Tobj = raw;
61 Tobj *= 0.03125; // convert to celsius
62#ifdef TMP007_DEBUG
63 Serial.print("Tobj = "); Serial.print(Tobj); Serial.println(" C");
64#endif
65 return Tobj;
66}
67
68
69
70int16_t Adafruit_TMP007::readRawDieTemperature(void) {
71 int16_t raw = read16(TMP007_TDIE);
72
73#if TMP007_DEBUG == 1
74
75#ifdef TESTDIE
76 raw = TESTDIE;
77#endif
78
79 Serial.print("Raw Tambient: 0x"); Serial.print (raw, HEX);
80
81
82 float v = raw/4;
83 v *= 0.03125;
84 Serial.print(" ("); Serial.print(v); Serial.println(" *C)");
85#endif
86 raw >>= 2;
87 return raw;
88}
89
90int16_t Adafruit_TMP007::readRawVoltage(void) {
91 int16_t raw;
92
93 raw = read16(TMP007_VOBJ);
94
95#if TMP007_DEBUG == 1
96
97#ifdef TESTVOLT
98 raw = TESTVOLT;
99#endif
100
101 Serial.print("Raw voltage: 0x"); Serial.print (raw, HEX);
102 float v = raw;
103 v *= 156.25;
104 v /= 1000;
105 Serial.print(" ("); Serial.print(v); Serial.println(" uV)");
106#endif
107 return raw;
108}
109
110
111/*********************************************************************/
112
113uint16_t Adafruit_TMP007::read16(uint8_t a) {
114 uint16_t ret;
115
116 Wire.beginTransmission(_addr); // start transmission to device
117#if (ARDUINO >= 100)
118 Wire.write(a); // sends register address to read from
119#else
120 Wire.send(a); // sends register address to read from
121#endif
122 Wire.endTransmission(); // end transmission
123
124 Wire.beginTransmission(_addr); // start transmission to device
125 Wire.requestFrom(_addr, (uint8_t)2);// send data n-bytes read
126#if (ARDUINO >= 100)
127 ret = Wire.read(); // receive DATA
128 ret <<= 8;
129 ret |= Wire.read(); // receive DATA
130#else
131 ret = Wire.receive(); // receive DATA
132 ret <<= 8;
133 ret |= Wire.receive(); // receive DATA
134#endif
135 Wire.endTransmission(); // end transmission
136
137 return ret;
138}
139
140void Adafruit_TMP007::write16(uint8_t a, uint16_t d) {
141 Wire.beginTransmission(_addr); // start transmission to device
142#if (ARDUINO >= 100)
143 Wire.write(a); // sends register address to read from
144 Wire.write(d>>8); // write data
145 Wire.write(d); // write data
146#else
147 Wire.send(a); // sends register address to read from
148 Wire.send(d>>8); // write data
149 Wire.send(d); // write data
150#endif
151 Wire.endTransmission(); // end transmission
152}
153
Note: See TracBrowser for help on using the repository browser.