-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathI2C.pde
More file actions
107 lines (86 loc) · 2.31 KB
/
I2C.pde
File metadata and controls
107 lines (86 loc) · 2.31 KB
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
void writeRegister(unsigned char r, unsigned char v)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r);
Wire.send(v);
Wire.endTransmission();
}
void writeInteger(unsigned char r, unsigned int v) {
writeRegister(r,(unsigned byte)v);
writeRegister(r+1,(unsigned byte)(v>>8));
}
unsigned char readRegister(unsigned char r)
{
unsigned char v;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 1); // read a byte
while(Wire.available()==0) {
// waiting
}
v = Wire.receive();
return v;
}
void readRegisters(unsigned char r, unsigned int numberOfBytes, unsigned char buffer[])
{
unsigned char v;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, numberOfBytes); // read a byte
char i = 0;
while (i<numberOfBytes) {
while(!Wire.available()) {
// waiting
}
buffer[i] = Wire.receive();
i++;
}
}
unsigned int readInteger(unsigned char r) {
union {
char data[2];
unsigned int value;
}
byteMappedInt;
byteMappedInt.value = 0;
Wire.beginTransmission(I2C_ADDRESS); // begin read cycle
Wire.send(0); //pointer to first cap data register
Wire.endTransmission(); // end cycle
//after this, the address pointer is set to 0 - since a stop condition has been sent
Wire.requestFrom(I2C_ADDRESS,r+2); // reads 2 bytes plus all bytes before the register
while (!Wire.available()==r+2) {
; //wait
}
for (int i=r+1; i>=0; i--) {
uint8_t c = Wire.receive();
if (i<2) {
byteMappedInt.data[i]= c;
}
}
return byteMappedInt.value;
}
unsigned long readLong(unsigned char r) {
union {
char data[4];
unsigned long value;
}
byteMappedLong;
byteMappedLong.value = 0L;
Wire.beginTransmission(I2C_ADDRESS); // begin read cycle
Wire.send(0); //pointer to first data register
Wire.endTransmission(); // end cycle
//the data pointer is reset anyway - so read from 0 on
Wire.requestFrom(I2C_ADDRESS,r+4); // reads 2 bytes plus all bytes before the register
while (!Wire.available()==r+4) {
; //wait
}
for (int i=r+3; i>=0; i--) {
uint8_t c = Wire.receive();
if (i<4) {
byteMappedLong.data[i]= c;
}
}
return byteMappedLong.value;
}