Skip to content

Commit 419f953

Browse files
committed
Updates example that uses interrupt
* Adds additional parameter to routeHardwareIntterupt so that the hardware pin can be selected, defaults to pin one * Fixes the hardware interrupt selection values * Adds power control to the various functions that can't be changed without the chip sleeping.
1 parent 75adcc5 commit 419f953

File tree

3 files changed

+64
-46
lines changed

3 files changed

+64
-46
lines changed

examples/Ex2_Interrupts/Ex2_Interrupts.ino

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
QwiicKX132 kxAccel;
55
outputData myData; // This will hold the accelerometer's output.
66
int dataReadyPin = D1;
7-
int timeThen;
8-
int timeNow;
97

108
void setup() {
119

12-
pinMode(dataReadyPin, OUTPUT);
13-
digitalWrite(dataReadyPin, HIGH);
10+
pinMode(dataReadyPin, INPUT);
1411

1512
while(!Serial){
1613
delay(50);
@@ -30,40 +27,35 @@ void setup() {
3027

3128

3229
if( !kxAccel.initialize(INT_SETTINGS)){
33-
Serial.println("Could not initialize the chip.");
30+
Serial.println("Could not initialize the chip. Freezing.");
3431
while(1);
3532
}
3633
else
3734
Serial.println("Initialized...");
3835

3936
// kxAccel.setRange(KX132_RANGE16G);
40-
timeThen = millis();
4137

4238
}
4339

4440
void loop() {
4541

46-
timeNow = millis();
47-
48-
if(timeNow - timeThen > 1000){
49-
timeThen = timeNow;
50-
digitalWrite(dataReadyPin, LOW);
51-
Serial.println("click.");
42+
if( digitalRead(dataReadyPin) == HIGH ){ // Wait for new data to be ready.
43+
44+
myData = kxAccel.getAccelData();
45+
digitalWrite(dataReadyPin, HIGH);
46+
Serial.print("X: ");
47+
Serial.print(myData.xData, 4);
48+
Serial.print("g ");
49+
Serial.print(" Y: ");
50+
Serial.print(myData.zData, 4);
51+
Serial.print("g ");
52+
Serial.print(" Z: ");
53+
Serial.print(myData.zData, 4);
54+
Serial.println("g ");
55+
56+
//kxAccel.clearInterrupt();// Because the data is being read in "burst"
57+
//mode, meaning that all the acceleration data is being read at once, we don't
58+
//need to clear the interrupt.
5259
}
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-
6860
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
6961
}

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bool QwiicKX13xCore::initialize(uint8_t settings)
7575
if( settings == DEFAULT_SETTINGS )
7676
returnError = writeRegister(KX13X_CNTL1, 0x00, DEFAULT_SETTINGS, 0);
7777
if( settings == INT_SETTINGS ){
78-
setInterruptPin(true);
78+
setInterruptPin(true, 1);
7979
routeHardwareInterrupt(HI_DATA_READY);
8080
returnError = writeRegister(KX13X_CNTL1, 0x00, INT_SETTINGS, 0);
8181
}
@@ -190,29 +190,51 @@ bool QwiicKX13xCore::setInterruptPin(bool enable, uint8_t polarity, uint8_t puls
190190
else if( latchControl < 0 | latchControl > 4 )
191191
return false;
192192

193+
uint8_t accelState = readAccelState(); // Put it back where we found it.
194+
accelControl(false); // Can't adjust without putting to sleep
195+
193196
uint8_t combinedArguments = ((pulseWidth << 6) | (enable << 5) | (polarity << 4) | (latchControl << 3));
194197
KX13X_STATUS_t returnError;
195-
returnError = writeRegister(KX13X_INC1, 0x07, combinedArguments, 3);
196-
if( returnError == KX13X_SUCCESS )
198+
returnError = writeRegister(KX13X_INC1, 0x07, combinedArguments, 0);
199+
if( returnError == KX13X_SUCCESS ){
200+
accelControl(accelState);
197201
return true;
202+
}
198203
else
199204
return false;
200205
}
201206

202207

203-
// Address: 0x25, bit[4]: default value is 0: disabled
204-
// Enables the data ready bit to be reported on the hardware interrupt.
205-
bool QwiicKX13xCore::routeHardwareInterrupt(uint8_t rdr){
208+
// Address: 0x25, bits[7:0]: default value is 0: disabled
209+
// Enables anyone of the various interrupt settings to be routed to hardware
210+
// interrupt pin one or pin two.
211+
bool QwiicKX13xCore::routeHardwareInterrupt(uint8_t rdr, uint8_t pin){
206212

207-
if( rdr < 0 | rdr > 6 )
213+
if( rdr < 0 | rdr > 128 )
214+
return false;
215+
if( pin != 1 && pin != 2)
208216
return false;
209217

218+
uint8_t accelState = readAccelState(); // Put it back where we found it.
219+
accelControl(false); // Can't adjust without putting to sleep
220+
210221
KX13X_STATUS_t returnError;
211-
returnError = writeRegister(KX13X_INC4, 0xEF, rdr, 4);
212-
if( returnError == KX13X_SUCCESS )
213-
return true;
222+
223+
if( pin == 1 ){
224+
returnError = writeRegister(KX13X_INC4, 0x00, rdr, 0);
225+
if( returnError == KX13X_SUCCESS ){
226+
accelControl(accelState);
227+
return true;
228+
}
214229
else
215-
return false;
230+
returnError = writeRegister(KX13X_INC6, 0x00, rdr, 0);
231+
if( returnError == KX13X_SUCCESS ){
232+
accelControl(accelState);
233+
return true;
234+
}
235+
}
236+
237+
return false;
216238

217239
}
218240

@@ -513,6 +535,7 @@ KX13X_STATUS_t QwiicKX13xCore::writeRegister(uint8_t reg, uint8_t mask, uint8_t
513535
return KX13X_I2C_ERROR;
514536
tempRegVal &= mask;
515537
tempRegVal |= (data << bitPos);
538+
Serial.println(tempRegVal, HEX);
516539

517540
if( _i2cPort == NULL ) {
518541

src/SparkFun_Qwiic_KX13X.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,16 @@ typedef enum {
171171
} KX13X_STATUS_t;
172172

173173
enum HARDWARE_INTERRUPT {
174-
HI_FREEFALL = 0x00,
175-
HI_WATERMARK,
176-
HI_DATA_READY,
177-
HI_BACK_TO_SLEEP,
178-
HI_TAP_DOUBLE_TAP,
179-
HI_WAKE_UP,
180-
HI_TILT_POSITION
174+
175+
HI_TILT_POSITION = 0x01,
176+
HI_WAKE_UP = 0x02,
177+
HI_TAP_DOUBLE_TAP = 0x04,
178+
HI_BACK_TO_SLEEP = 0x08,
179+
HI_DATA_READY = 0x10,
180+
HI_WATERMARK = 0x20,
181+
HI_BUFFER_FULL = 0x40,
182+
HI_FREEFALL = 0x80
183+
181184
};
182185

183186

@@ -197,7 +200,7 @@ class QwiicKX13xCore
197200
bool setOutputDataRate(uint8_t);
198201
float readOutputDataRate();
199202
bool setInterruptPin(bool enable, uint8_t polarity = 0, uint8_t pulseWidth = 0, bool latchControl = false);
200-
bool routeHardwareInterrupt(uint8_t);
203+
bool routeHardwareInterrupt(uint8_t, uint8_t pin = 1);
201204
bool clearInterrupt();
202205
bool dataTrigger();
203206
bool setBufferThreshold(uint8_t);

0 commit comments

Comments
 (0)