Skip to content

Commit 2a07ec1

Browse files
committed
Update getRawAccelData and Example3
1 parent 8fbe78f commit 2a07ec1

File tree

2 files changed

+61
-67
lines changed

2 files changed

+61
-67
lines changed
Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,112 @@
11
/*
2-
example3-Buffer
2+
example3-Buffer
33
4-
This example shows both how to setup the buffer but also how to route the buffer's
5-
interrupt to a physical interrupt pin.
4+
This example shows both how to setup the buffer but also how to route the buffer's
5+
interrupt to a physical interrupt pin.
66
77
Written by Elias Santistevan @ SparkFun Electronics, October 2022
88
9-
Products:
9+
Products:
1010
11-
SparkFun Triple Axis Accelerometer Breakout - KX132:
12-
https://www.sparkfun.com/products/17871
11+
SparkFun Triple Axis Accelerometer Breakout - KX132:
12+
https://www.sparkfun.com/products/17871
1313
14-
SparkFun Triple Axis Accelerometer Breakout - KX134:
15-
https://www.sparkfun.com/products/17589
14+
SparkFun Triple Axis Accelerometer Breakout - KX134:
15+
https://www.sparkfun.com/products/17589
1616
1717
1818
Repository:
1919
20-
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
20+
https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
2121
22-
SparkFun code, firmware, and software is released under the MIT
23-
License (http://opensource.org/licenses/MIT).
22+
SparkFun code, firmware, and software is released under the MIT
23+
License (http://opensource.org/licenses/MIT).
2424
*/
2525

2626
#include <Wire.h>
2727
#include "SparkFun_KX13X.h"
2828

29-
SparkFun_KX132 kxAccel;
30-
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
29+
SparkFun_KX132 kxAccel;
30+
//SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
3131

3232
outputData myData; // Struct for the accelerometer's data
3333
byte dataReadyPin = 2; // Change to fit your project.
3434

35-
void setup()
35+
void setup()
3636
{
37-
38-
Wire.begin();
3937

40-
Serial.begin(115200);
38+
Wire.begin();
39+
40+
Serial.begin(115200);
4141
Serial.println("Welcome.");
4242

43-
// Wait for the Serial monitor to be opened.
44-
while(!Serial)
43+
// Wait for the Serial monitor to be opened.
44+
while (!Serial)
4545
delay(50);
4646

47-
48-
if( !kxAccel.begin() )
49-
{
47+
if (!kxAccel.begin())
48+
{
5049
Serial.println("Could not communicate with the the KX13X. Freezing.");
51-
while(1);
52-
}
50+
while (1)
51+
;
52+
}
5353

54-
Serial.println("Ready.");
54+
Serial.println("Ready.");
5555

56-
// Reset the chip so that old settings don't apply to new setups.
57-
if( kxAccel.softwareReset() )
58-
Serial.println("Reset.");
56+
// Reset the chip so that old settings don't apply to new setups.
57+
if (kxAccel.softwareReset())
58+
Serial.println("Reset.");
5959

60-
//Give some time for the accelerometer to reset.
61-
//It needs two, but give it five for good measure.
62-
delay(5);
60+
// Give some time for the accelerometer to reset.
61+
// It needs two, but give it five for good measure.
62+
delay(5);
6363

64-
// Many settings for KX13X can only be
65-
// applied when the accelerometer is powered down.
66-
// However there are many that can be changed "on-the-fly"
67-
// check datasheet for more info, or the comments in the
68-
// "...regs.h" file which specify which can be changed when.
69-
kxAccel.enableAccel(false);
64+
// Many settings for KX13X can only be
65+
// applied when the accelerometer is powered down.
66+
// However there are many that can be changed "on-the-fly"
67+
// check datasheet for more info, or the comments in the
68+
// "...regs.h" file which specify which can be changed when.
69+
kxAccel.enableAccel(false);
7070

71-
kxAccel.enableBufferInt(); // Enables the Buffer interrupt
72-
kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1
73-
kxAccel.routeHardwareInterrupt(0x40); // Routes the data ready bit to pin 1
71+
kxAccel.enableBufferInt(); // Enables the Buffer interrupt
72+
kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1
73+
kxAccel.routeHardwareInterrupt(0x40); // Routes the data ready bit to pin 1
7474

75-
kxAccel.enableSampleBuffer(); // Enable buffer.
75+
kxAccel.enableSampleBuffer(); // Enable buffer.
7676
kxAccel.setBufferOperationMode(0x00); // Enable the buffer to be FIFO.
7777

78-
// Additional Buffer Settings
79-
80-
//uint8_t numSamples = 140;
81-
//kxAccel.setBufferThreshold(numSamples); // Set the number of sample that can be stored in the buffer
78+
// Additional Buffer Settings
8279

83-
//kxAccel.setBufferResolution(); // Change sample resolution to 16 bit, 8 bit by default.
84-
// This will change how many samples can be held in buffer.
80+
// uint8_t numSamples = 140;
81+
// kxAccel.setBufferThreshold(numSamples); // Set the number of sample that can be stored in the buffer
8582

86-
//kxAccel.clearBuffer(); // Clear the buffer
87-
//kxAccel.getSampleLevel(); // Get the number of samples in the buffer. This number
88-
// Changes depending on the resolution, see datasheet for more info.
83+
kxAccel.setBufferResolution(); // Change sample resolution to 16 bit, 8 bit by default.
84+
// This will change how many samples can be held in buffer.
8985

90-
kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
91-
//kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134
92-
93-
//kxAccel.setOutputDataRate(); // Default is 50Hz
94-
kxAccel.enableAccel();
86+
// kxAccel.clearBuffer(); // Clear the buffer
87+
// kxAccel.getSampleLevel(); // Get the number of samples in the buffer. This number
88+
// Changes depending on the resolution, see datasheet for more info.
9589

90+
kxAccel.setRange(SFE_KX132_RANGE2G); // 2g Range
91+
// kxAccel.setRange(SFE_KX134_RANGE8G); // 8g for the KX134
9692

93+
// kxAccel.setOutputDataRate(); // Default is 50Hz
94+
kxAccel.enableAccel();
9795
}
9896

99-
void loop()
97+
void loop()
10098
{
10199

102-
if( digitalRead(dataReadyPin) == HIGH ) // Check for data ready pin
103-
{
104-
kxAccel.getAccelData(&myData);
100+
// getAccelData will return false if there is no data in the buffer
101+
if (kxAccel.getAccelData(&myData) == true)
102+
{
103+
105104
Serial.print("X: ");
106105
Serial.print(myData.xData, 4);
107106
Serial.print(" Y: ");
108107
Serial.print(myData.yData, 4);
109108
Serial.print(" Z: ");
110109
Serial.print(myData.zData, 4);
111-
Serial.println();
112-
110+
Serial.println();
113111
}
114-
115-
delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
116112
}

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,6 @@ bool QwDevKX13X::getRawAccelData(rawOutputData *rawAccelData)
13271327
uint8_t tempVal;
13281328
uint8_t tempRegData[6] = {0};
13291329

1330-
// Check if the buffer full interrupt is activated
1331-
//retVal = readRegisterRegion(SFE_KX13X_INC4, &tempVal, 1); // inc4.bfi1 indicates if the buffer full interrupt is reported on INT1
13321330
retVal = readRegisterRegion(SFE_KX13X_BUF_CNTL2, &tempVal, 1); // bufCntl2.bits.bufe indicates if the buffer is enabled
13331331

13341332
if (retVal != 0)
@@ -1354,9 +1352,9 @@ bool QwDevKX13X::getRawAccelData(rawOutputData *rawAccelData)
13541352
else
13551353
// No buffer data to read!
13561354
// We can either:
1357-
//return false;
1355+
return false;
13581356
// Or, be kind and read the normal registers
1359-
retVal = readRegisterRegion(SFE_KX13X_XOUT_L, tempRegData, 6); // Read 3 * 16-bit
1357+
//retVal = readRegisterRegion(SFE_KX13X_XOUT_L, tempRegData, 6); // Read 3 * 16-bit
13601358
}
13611359
else
13621360
retVal = readRegisterRegion(SFE_KX13X_XOUT_L, tempRegData, 6); // Read 3 * 16-bit

0 commit comments

Comments
 (0)