Skip to content

Commit d03837b

Browse files
committed
Adds software interrupt example
1 parent 419f953 commit d03837b

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <Wire.h>
2+
#include "SparkFun_Qwiic_KX13X.h"
3+
4+
QwiicKX132 kxAccel;
5+
outputData myData; // This will hold the accelerometer's output.
6+
7+
void setup() {
8+
9+
while(!Serial){
10+
delay(50);
11+
}
12+
13+
Serial.begin(115200);
14+
Serial.println("Welcome.");
15+
16+
Wire.begin();
17+
if( !kxAccel.begin() ){
18+
Serial.println("Could not communicate with the the KX13X. Freezing.");
19+
while(1);
20+
}
21+
else
22+
Serial.println("Ready.");
23+
24+
25+
26+
if( !kxAccel.initialize(SOFT_INT_SETTINGS)){
27+
Serial.println("Could not initialize the chip. Freezing.");
28+
while(1);
29+
}
30+
else
31+
Serial.println("Initialized...");
32+
33+
// kxAccel.setRange(KX132_RANGE16G);
34+
35+
}
36+
37+
void loop() {
38+
39+
40+
if( kxAccel.dataTrigger() ) {
41+
Serial.println("Data Ready.");
42+
// Interrupt is cleared on reading registers.
43+
myData = kxAccel.getAccelData();
44+
Serial.print("X: ");
45+
Serial.print(myData.xData, 4);
46+
Serial.print("g ");
47+
Serial.print(" Y: ");
48+
Serial.print(myData.zData, 4);
49+
Serial.print("g ");
50+
Serial.print(" Z: ");
51+
Serial.print(myData.zData, 4);
52+
Serial.println("g ");
53+
}
54+
55+
56+
57+
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
58+
}

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,16 @@ bool QwiicKX13xCore::clearInterrupt(){
255255
// This function triggers collection of data by the KX13X.
256256
bool QwiicKX13xCore::dataTrigger(){
257257

258+
uint8_t tempRegVal;
258259
KX13X_STATUS_t returnError;
259-
returnError = writeRegister(KX13X_INS2, 0xEF, true, 0);
260-
if( returnError == KX13X_SUCCESS )
261-
return true;
262-
else
260+
returnError = readRegister(&tempRegVal, KX13X_INS2);
261+
if( returnError == KX13X_SUCCESS ){
262+
if( tempRegVal & 0x10 )
263+
return true;
264+
}
265+
else
263266
return false;
267+
264268
}
265269

266270
// Address: 0x5E , bit[7:0]: default value is: unknown

0 commit comments

Comments
 (0)