root / quad1 / AeroQuad / I2C.h @ 9240aaa3
History | View | Annotate | Download (2.1 KB)
1 | 9240aaa3 | Alex | /*
|
---|---|---|---|
2 | AeroQuad v2.1 - October 2010
|
||
3 | www.AeroQuad.com
|
||
4 | Copyright (c) 2010 Ted Carancho. All rights reserved.
|
||
5 | An Open Source Arduino based multicopter.
|
||
6 |
|
||
7 | This program is free software: you can redistribute it and/or modify
|
||
8 | it under the terms of the GNU General Public License as published by
|
||
9 | the Free Software Foundation, either version 3 of the License, or
|
||
10 | (at your option) any later version.
|
||
11 | |||
12 | This program is distributed in the hope that it will be useful,
|
||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
15 | GNU General Public License for more details.
|
||
16 | |||
17 | You should have received a copy of the GNU General Public License
|
||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
19 | */
|
||
20 | |||
21 | // I2C functions
|
||
22 | |||
23 | void sendByteI2C(int deviceAddress, byte dataValue) { |
||
24 | Wire.beginTransmission(deviceAddress); |
||
25 | Wire.send(dataValue); |
||
26 | Wire.endTransmission(); |
||
27 | } |
||
28 | |||
29 | byte readByteI2C(int deviceAddress) {
|
||
30 | Wire.requestFrom(deviceAddress, 1);
|
||
31 | return Wire.receive();
|
||
32 | } |
||
33 | |||
34 | int readWordI2C(int deviceAddress) { |
||
35 | Wire.requestFrom(deviceAddress, 2);
|
||
36 | return (Wire.receive() << 8) | Wire.receive(); |
||
37 | } |
||
38 | |||
39 | int readWordWaitI2C(int deviceAddress) { |
||
40 | unsigned char msb, lsb; |
||
41 | Wire.requestFrom(deviceAddress, 2); // request two bytes |
||
42 | while(!Wire.available()); // wait until data available |
||
43 | msb = Wire.receive(); |
||
44 | while(!Wire.available()); // wait until data available |
||
45 | lsb = Wire.receive(); |
||
46 | return (((int)msb<<8) | ((int)lsb)); |
||
47 | } |
||
48 | |||
49 | int readReverseWordI2C(int deviceAddress) { |
||
50 | byte lowerByte; |
||
51 | Wire.requestFrom(deviceAddress, 2);
|
||
52 | lowerByte = Wire.receive(); |
||
53 | return (Wire.receive() << 8) | lowerByte; |
||
54 | } |
||
55 | |||
56 | byte readWhoI2C(int deviceAddress) {
|
||
57 | // read the ID of the I2C device
|
||
58 | Wire.beginTransmission(deviceAddress); |
||
59 | Wire.send(0x00);
|
||
60 | Wire.endTransmission(); |
||
61 | delay(100);
|
||
62 | Wire.requestFrom(deviceAddress, 1);
|
||
63 | return Wire.receive();
|
||
64 | } |
||
65 | |||
66 | void updateRegisterI2C(int deviceAddress, byte dataAddress, byte dataValue) { |
||
67 | Wire.beginTransmission(deviceAddress); |
||
68 | Wire.send(dataAddress); |
||
69 | Wire.send(dataValue); |
||
70 | Wire.endTransmission(); |
||
71 | } |