Skip to content

Commit 75adcc5

Browse files
committed
Adds interrupt example, replaces the runCommandTest function
1 parent b1c9ba5 commit 75adcc5

File tree

3 files changed

+101
-13
lines changed

3 files changed

+101
-13
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
int dataReadyPin = D1;
7+
int timeThen;
8+
int timeNow;
9+
10+
void setup() {
11+
12+
pinMode(dataReadyPin, OUTPUT);
13+
digitalWrite(dataReadyPin, HIGH);
14+
15+
while(!Serial){
16+
delay(50);
17+
}
18+
19+
Serial.begin(115200);
20+
Serial.println("Welcome.");
21+
22+
Wire.begin();
23+
if( !kxAccel.begin() ){
24+
Serial.println("Could not communicate with the the KX13X. Freezing.");
25+
while(1);
26+
}
27+
else
28+
Serial.println("Ready.");
29+
30+
31+
32+
if( !kxAccel.initialize(INT_SETTINGS)){
33+
Serial.println("Could not initialize the chip.");
34+
while(1);
35+
}
36+
else
37+
Serial.println("Initialized...");
38+
39+
// kxAccel.setRange(KX132_RANGE16G);
40+
timeThen = millis();
41+
42+
}
43+
44+
void loop() {
45+
46+
timeNow = millis();
47+
48+
if(timeNow - timeThen > 1000){
49+
timeThen = timeNow;
50+
digitalWrite(dataReadyPin, LOW);
51+
Serial.println("click.");
52+
}
53+
54+
myData = kxAccel.getAccelData();
55+
//digitalWrite(dataReadyPin, HIGH);
56+
//Serial.print("X: ");
57+
//Serial.print(myData.xData, 4);
58+
//Serial.print("g ");
59+
//Serial.print(" Y: ");
60+
//Serial.print(myData.zData, 4);
61+
//Serial.print("g ");
62+
//Serial.print(" Z: ");
63+
//Serial.print(myData.zData, 4);
64+
//Serial.println("g ");
65+
66+
// kxAccel.clearInterrupt();
67+
68+
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
69+
}

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ bool QwiicKX13xCore::routeHardwareInterrupt(uint8_t rdr){
216216

217217
}
218218

219+
220+
221+
bool QwiicKX13xCore::clearInterrupt(){
222+
223+
uint8_t tempRegVal;
224+
KX13X_STATUS_t returnError;
225+
returnError = readRegister(&tempRegVal, KX13X_INT_REL);
226+
if( returnError == KX13X_SUCCESS )
227+
return true;
228+
else
229+
return false;
230+
}
231+
219232
// Address: 0x17 , bit[4]: default value is: 0
220233
// This function triggers collection of data by the KX13X.
221234
bool QwiicKX13xCore::dataTrigger(){
@@ -302,7 +315,19 @@ bool QwiicKX13xCore::enableBuffer(bool enable, bool enableInterrupt){
302315
//Tests functionality of the integrated circuit
303316
bool QwiicKX13xCore::runCommandTest()
304317
{
305-
return true;
318+
319+
uint8_t tempRegVal;
320+
KX13X_STATUS_t returnError;
321+
322+
returnError = writeRegister(KX13X_CNTL2, 0xBF, 1, 6);
323+
if( returnError != KX13X_SUCCESS )
324+
return false;
325+
326+
returnError = readRegister(&tempRegVal, KX13X_COTR);
327+
if( returnError == KX13X_SUCCESS && tempRegVal == COTR_POS_STATE)
328+
return true;
329+
else
330+
return false;
306331
}
307332

308333

src/SparkFun_Qwiic_KX13X.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,8 @@ Distributed as-is; no warranty is given.
2727

2828
#define KX132_WHO_AM_I 0x3D
2929
#define KX134_WHO_AM_I 0x46
30-
#define TOTAL_ACCEL_DATA 6 //bytes
31-
#define MAX_BUFFER_LENGTH 32 //bytes
32-
33-
#define COTR_SUCCESS 0xAA //Succesfull COTR Register setting value
34-
#define PC1 7 //1:high performance/low power mode 0: stand by
35-
#define COTC 6 //1: Sets COTR to 0xAA to verify proper IC functionality 0: no action
36-
#define RES 6 //1:high performance mode 0: Low Power mode
37-
#define IEN1 5 //1:physical interrupt pin is enabled 0: disabled
38-
#define IEA1 4 //1:active HIGH 0: active LOW
39-
#define IEL1 3 //1:pulsed interrupt 0:latched interrupt until cleared by reading INT_REL
40-
#define STPOL 1 //1: positive self test polarity 0: negative
41-
#define SPI3E 0 //1: SPI Enabled 0: SPI Disabled
30+
#define TOTAL_ACCEL_DATA 6
31+
#define MAX_BUFFER_LENGTH 32
4232

4333
#define XLSB 0
4434
#define XMSB 1
@@ -56,6 +46,9 @@ Distributed as-is; no warranty is given.
5646
#define BUFFER_SETTINGS 0xE2
5747
#define TILT_SETTINGS 0xE3
5848

49+
#define COTR_DEF_STATE 0x55
50+
#define COTR_POS_STATE 0xAA
51+
5952
#define BUFFER_16BIT_SAMPLES 0x01
6053
#define BUFFER_8BIT_SAMPLES 0x00
6154
#define BUFFER_MODE_FIFO 0x00
@@ -205,6 +198,7 @@ class QwiicKX13xCore
205198
float readOutputDataRate();
206199
bool setInterruptPin(bool enable, uint8_t polarity = 0, uint8_t pulseWidth = 0, bool latchControl = false);
207200
bool routeHardwareInterrupt(uint8_t);
201+
bool clearInterrupt();
208202
bool dataTrigger();
209203
bool setBufferThreshold(uint8_t);
210204
bool setBufferOperation(uint8_t, uint8_t);

0 commit comments

Comments
 (0)