forked from lincomatic/open_evse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_MCP9808.cpp
117 lines (95 loc) · 2.88 KB
/
Adafruit_MCP9808.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**************************************************************************/
/*!
@file Adafruit_MCP9808.cpp
@author K.Townsend (Adafruit Industries)
@license BSD (see license.txt)
I2C Driver for Microchip's MCP9808 I2C Temp sensor
This is a library for the Adafruit MCP9808 breakout
----> http://www.adafruit.com/products/1782
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#ifdef __AVR_ATtiny85__
#include "TinyWireM.h"
#define Wire TinyWireM
#else
#include "./Wire.h"
#endif
static inline void wiresend(uint8_t x) {
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
static inline uint8_t wirerecv(void) {
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
#include "Adafruit_MCP9808.h"
/**************************************************************************/
/*!
@brief Instantiates a new MCP9808 class
*/
/**************************************************************************/
Adafruit_MCP9808::Adafruit_MCP9808() {
}
/**************************************************************************/
/*!
@brief Setups the HW
*/
/**************************************************************************/
boolean Adafruit_MCP9808::begin(uint8_t addr) {
_i2caddr = addr;
//don't need - open_evse.ino does it Wire.begin();
if (read16(MCP9808_REG_MANUF_ID) != 0x0054) return false;
if (read16(MCP9808_REG_DEVICE_ID) != 0x0400) return false;
return true;
}
/**************************************************************************/
/*!
@brief Reads the 16-bit temperature register and returns Centigrade*10
*/
/**************************************************************************/
int16_t Adafruit_MCP9808::readTempC10( void )
{
uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);
uint32_t temp = ((t & 0x0FFF)*10)/16;
if (t & 0x1000) temp -= 256;
return (int16_t) temp;
}
/**************************************************************************/
/*!
@brief Low level 16 bit read and write procedures!
*/
/**************************************************************************/
void Adafruit_MCP9808::write16(uint8_t reg, uint16_t value) {
Wire.beginTransmission(_i2caddr);
wiresend((uint8_t)reg);
wiresend(value >> 8);
wiresend(value & 0xFF);
Wire.endTransmission();
}
uint16_t Adafruit_MCP9808::read16(uint8_t reg) {
uint16_t val;
Wire.beginTransmission(_i2caddr);
wiresend((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)2);
val = wirerecv();
val <<= 8;
val |= wirerecv();
return val;
}