-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADXL345.c
91 lines (80 loc) · 1.96 KB
/
ADXL345.c
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
#include <Wire.h>
// ADXL345 I2C address is 0x53(83)
#define Addr 0x53
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select bandwidth rate register
Wire.write(0x2C);
// Normal mode, Output data rate = 100 Hz
Wire.write(0x0A);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select power control register
Wire.write(0x2D);
// Auto-sleep disable
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data format register
Wire.write(0x31);
// Self test disabled, 4-wire interface, Full resolution, Range = +/-2g
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
delay(300);
}
void loop()
{
unsigned int data[6];
for(int i = 0; i < 6; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((50 + i));
// Stop I2C transmission
Wire.endTransmission();
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
// Read 6 bytes of data
// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
if(Wire.available() == 1)
{
data[i] = Wire.read();
}
}
// Convert the data to 10-bits
int xAccl = (((data[1] & 0x03) * 256) + data[0]);
if(xAccl > 511)
{
xAccl -= 1024;
}
int yAccl = (((data[3] & 0x03) * 256) + data[2]);
if(yAccl > 511)
{
yAccl -= 1024;
}
int zAccl = (((data[5] & 0x03) * 256) + data[4]);
if(zAccl > 511)
{
zAccl -= 1024;
}
// Output data to serial monitor
printf("Acceleration in X-Axis is : ");
printf(xAccl);
printf("Acceleration in Y-Axis is : ");
printf(yAccl);
printf("Acceleration in Z-Axis is : ");
printf(zAccl);
delay(300);
}