-
Notifications
You must be signed in to change notification settings - Fork 1
/
I2C.ino
91 lines (73 loc) · 2.74 KB
/
I2C.ino
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
/*
AeroQuad v2.5 Beta 1 - July 2011
www.AeroQuad.com
Copyright (c) 2011 Ted Carancho. All rights reserved.
An Open Source Arduino based multicopter.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Modified by Denis Shulyaka */
// I2C functions
void sendByteI2C(const int deviceAddress, const byte dataValue) {
Wire.beginTransmission(deviceAddress);
Wire.write(dataValue);
Wire.endTransmission();
}
byte readByteI2C(const int deviceAddress) {
Wire.requestFrom(deviceAddress, 1);
return Wire.read();
}
int readWordI2C(const int deviceAddress) {
Wire.requestFrom(deviceAddress, 2);
return (Wire.read() << 8) | Wire.read();
}
int readWordI2C() {
return (Wire.read() << 8) | Wire.read();
}
int readWordWaitI2C(int deviceAddress, unsigned char retryCount=0) {
Wire.requestFrom(deviceAddress, 2); // request two bytes
while(Wire.available()<2) // wait until data available
{
if(!retryCount--) // or retryCount is zero
return -1;
delay(10);
}
return (Wire.read()<<8) | Wire.read();
}
int readReverseWordI2C(const int deviceAddress) {
Wire.requestFrom(deviceAddress, 2);
return Wire.read()|(Wire.read() << 8);
}
int readReverseWordI2C() {
return Wire.read()|(Wire.read() << 8);
}
byte readWhoI2C(const int deviceAddress) {
// read the ID of the I2C device
sendByteI2C(deviceAddress, 0x00);
delay(100);
return readByteI2C(deviceAddress);
}
byte readRegisterI2C(const int deviceAddress, const byte dataAddress) {
sendByteI2C(deviceAddress, dataAddress);
return readByteI2C(deviceAddress);
}
byte readRegisterI2C(const int deviceAddress, const byte dataAddress, const byte dataMask) {
return readRegisterI2C(deviceAddress, dataAddress) & dataMask;
}
void updateRegisterI2C(const int deviceAddress, const byte dataAddress, const byte dataValue) {
Wire.beginTransmission(deviceAddress);
Wire.write(dataAddress);
Wire.write(dataValue);
Wire.endTransmission();
}
void updateRegisterI2C(const int deviceAddress, const byte dataAddress, const byte dataValue, const byte dataMask) {
updateRegisterI2C(deviceAddress, dataAddress, readRegisterI2C(deviceAddress, dataAddress, ~dataMask) | (dataValue & dataMask) );
}