-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexample2_interrupts.ino
113 lines (82 loc) · 3.33 KB
/
example2_interrupts.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
example2-Interrupts
This example shows how to route the data ready bit to either physical interrupt pin one and pin two.
Written by Elias Santistevan @ SparkFun Electronics, October 2022
Products:
SparkFun Triple Axis Accelerometer Breakout - KX132:
https://www.sparkfun.com/products/17871
SparkFun Triple Axis Accelerometer Breakout - KX134:
https://www.sparkfun.com/products/17589
Repository:
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
SparkFun code, firmware, and software is released under the MIT
License (http://opensource.org/licenses/MIT).
*/
#include <Wire.h>
#include <SPI.h>
#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X
SparkFun_KX132 kxAccel;
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
outputData myData; // Struct for the accelerometer's data
byte dataReadyPin = 2; // Change to fit your project.
void setup()
{
Wire.begin();
Serial.begin(115200);
Serial.println("Welcome.");
// Wait for the Serial monitor to be opened.
while (!Serial)
delay(50);
if (!kxAccel.begin())
{
Serial.println("Could not communicate with the the KX13X. Freezing.");
while (1)
;
}
Serial.println("Ready.");
// Reset the chip so that old settings don't apply to new setups.
if (kxAccel.softwareReset())
Serial.println("Reset.");
// Give some time for the accelerometer to reset.
// It needs two, but give it five for good measure.
delay(5);
// Many settings for KX13X can only be
// applied when the accelerometer is powered down.
// However there are many that can be changed "on-the-fly"
// check datasheet for more info, or the comments in the
// "...regs.h" file which specify which can be changed when.
kxAccel.enableAccel(false);
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1
kxAccel.routeHardwareInterrupt(0x10); // Routes the data ready bit to pin 1
// Routing Data Ready pin to interrupt pin 2.
// kxAccel.enablePhysInterrupt(true, 2); // Enables interrupt pin 2
// kxAccel.routeHardwareInterrupt(0x10, 2); // Routes the data ready bit to pin 2
// This will change the interrupt behavior to latch instead of pulse
// In this case you'll need to release directly with clearInterrupt();.
// kxAccel.setLatchControl();
// kxAccel.setPinMode(); // Change interrupt to active HIGH
// kxAccel.setPulseWidth(); // Change the length of a pulsed (non latched) interrupt
kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
// kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134
// kxAccel.setOutputDataRate(); // Default is 400Hz
kxAccel.enableAccel();
}
void loop()
{
if (digitalRead(dataReadyPin) == HIGH) // Check for data ready pin
{
kxAccel.getAccelData(&myData);
Serial.print("X: ");
Serial.print(myData.xData, 4);
Serial.print(" Y: ");
Serial.print(myData.yData, 4);
Serial.print(" Z: ");
Serial.print(myData.zData, 4);
Serial.println();
// If interrupt behavior has been changed to latch, use the
// following function to clear it after reading data.
// kxAccel.clearInterrupt();
}
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
}