Skip to content

Commit af57786

Browse files
committed
Fixes various minor logical errors
1 parent 6cde8c5 commit af57786

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

examples/Ex1_BasicReadings/Ex1_BasicReadings.ino

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,57 @@ Distributed as-is; no warranty is given.
1717
#include <Wire.h>
1818
#include "SparkFun_KX13X.h"
1919

20-
SparkFun_KX132 kxAccel;
20+
SparkFun_KX132_SPI kxAccel;
2121

2222
outputData myData; // This will hold the accelerometer's output.
23+
byte chipSelect = 1;
2324

2425
void setup() {
26+
27+
pinMode(chipSelect, OUTPUT);
28+
digitalWrite(chipSelect, HIGH);
2529

26-
Wire.begin();
30+
SPI.begin();
2731
Serial.begin(115200);
2832
Serial.println("Welcome.");
2933

30-
while(!Serial)
31-
delay(50);
34+
// while(!Serial)
35+
// delay(50);
36+
//
3237

33-
34-
if( !kxAccel.begin() )
38+
if( !kxAccel.begin(chipSelect) )
3539
{
3640
Serial.println("Could not communicate with the the KX13X. Freezing.");
3741
while(1);
3842
}
3943

44+
kxAccel.enableAccel(false); // The following settings will only be applied when the
45+
// KX13X is off.
46+
4047
Serial.println("Ready.");
4148

42-
kxAccel.setRange(KX134_RANGE32G); // For a larger range uncomment
49+
kxAccel.setRange(0x18); // 16g Range
50+
kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
51+
// kxAccel.setOutputDataRate(); //Keeping default of 400Hz
52+
kxAccel.enableAccel();
53+
4354

4455
}
4556

4657
void loop() {
4758

48-
kxAccel.getAccelData(&myData);
49-
Serial.print("X: ");
50-
Serial.print(myData.xData, 4);
51-
Serial.print("g ");
52-
Serial.print(" Y: ");
53-
Serial.print(myData.yData, 4);
54-
Serial.print("g ");
55-
Serial.print(" Z: ");
56-
Serial.print(myData.zData, 4);
57-
Serial.println("g ");
58-
59-
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
59+
60+
if( kxAccel.dataReady() )
61+
{
62+
kxAccel.getAccelData(&myData);
63+
Serial.print("X: ");
64+
Serial.print(myData.xData, 4);
65+
Serial.print(" Y: ");
66+
Serial.print(myData.yData, 4);
67+
Serial.print(" Z: ");
68+
Serial.print(myData.zData, 4);
69+
Serial.println();
70+
}
71+
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 1/50ODR
6072

6173
}

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ bool QwDevKX13X::enableAccel(bool enable)
113113
if( retVal != 0 )
114114
return false;
115115

116-
tempVal = (tempVal | enable);
116+
tempVal = (tempVal | (enable << 7));
117117

118118
retVal = writeRegisterByte(SFE_KX13X_CNTL1, tempVal);
119119

@@ -418,21 +418,19 @@ bool QwDevKX13X::clearInterrupt()
418418
return true;
419419
}
420420

421-
// Address: 0x17 , bit[4]: default value is: 0
422-
// This function triggers collection of data by the KX13X.
423-
bool QwDevKX13X::dataTrigger()
421+
bool QwDevKX13X::dataReady()
424422
{
425423

426424
int retVal;
427425
uint8_t tempVal;
428426

429427
retVal = readRegisterRegion(SFE_KX13X_INS2, &tempVal, 1);
430428

431-
if( retVal != 0 )
432-
return false;
429+
if( retVal != 0 )
430+
return false;
433431

434432
if( tempVal & 0x10 )
435-
return true;
433+
return true;
436434

437435
return false;
438436
}

src/SparkFun_Qwiic_KX13X.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class QwDevKX13X
140140
bool setOutputDataRate(uint8_t);
141141
float getOutputDataRate();
142142
bool routeHardwareInterrupt(uint8_t, uint8_t pin = 1);
143-
bool dataTrigger();
143+
bool dataReady();
144144
bool runCommandTest();
145145
uint8_t readAccelState();
146146
bool getRawAccelData(rawOutputData*);

0 commit comments

Comments
 (0)