Skip to content

Commit d6fc04b

Browse files
committed
Added SPI example
1 parent 343fd00 commit d6fc04b

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "SparkFun_LIS331.h"
2+
#include <SPI.h>
3+
4+
LIS331 xl;
5+
6+
void setup()
7+
{
8+
// put your setup code here, to run once:
9+
pinMode(9,INPUT); // Interrupt pin input
10+
pinMode(10, OUTPUT); // CS for SPI
11+
digitalWrite(10, HIGH); // Make CS high
12+
pinMode(11, OUTPUT); // MOSI for SPI
13+
pinMode(12, INPUT); // MISO for SPI
14+
pinMode(13, OUTPUT); // SCK for SPI
15+
SPI.begin();
16+
xl.setSPICSPin(10); // This MUST be called BEFORE .begin() so
17+
// .begin() can communicate with the chip
18+
xl.begin(LIS331::USE_SPI); // Selects the bus to be used and sets
19+
// the power up bit on the accelerometer.
20+
// Also zeroes out all accelerometer
21+
// registers that are user writable.
22+
// This next section configures an interrupt. It will cause pin
23+
// INT1 on the accelerometer to go high when the absolute value
24+
// of the reading on the Z-axis exceeds a certain level for a
25+
// certain number of samples.
26+
xl.intSrcConfig(LIS331::INT_SRC, 1); // Select the source of the
27+
// signal which appears on pin INT1. In
28+
// this case, we want the corresponding
29+
// interrupt's status to appear.
30+
xl.setIntDuration(50, 1); // Number of samples a value must meet
31+
// the interrupt condition before an
32+
// interrupt signal is issued. At the
33+
// default rate of 50Hz, this is one sec.
34+
xl.setIntThreshold(2, 1); // Threshold for an interrupt. This is
35+
// not actual counts, but rather, actual
36+
// counts divided by 16.
37+
xl.enableInterrupt(LIS331::Z_AXIS, LIS331::TRIG_ON_HIGH, 1, true);
38+
// Enable the interrupt. Parameters indicate
39+
// which axis to sample, when to trigger
40+
// (in this case, when the absolute mag
41+
// of the signal exceeds the threshold),
42+
// which interrupt source we're configuring,
43+
// and whether to enable (true) or disable
44+
// (false) the interrupt.
45+
SerialUSB.begin(115200);
46+
}
47+
48+
void loop()
49+
{
50+
static long loopTimer = 0;
51+
int16_t x, y, z;
52+
if (millis() - loopTimer > 1000)
53+
{
54+
loopTimer = millis();
55+
xl.readAxes(x, y, z); // The readAxes() function transfers the
56+
// current axis readings into the three
57+
// parameter variables passed to it.
58+
SerialUSB.println(x);
59+
SerialUSB.println(y);
60+
SerialUSB.println(z);
61+
SerialUSB.println(xl.convertToG(6,x)); // The convertToG() function
62+
SerialUSB.println(xl.convertToG(6,y)); // accepts as parameters the
63+
SerialUSB.println(xl.convertToG(6,z)); // raw value and the current
64+
SerialUSB.println(" "); // maximum g-rating.
65+
}
66+
if (digitalRead(9) == HIGH)
67+
{
68+
SerialUSB.println("Interrupt");
69+
}
70+
}

src/SparkFun_LIS331.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void LIS331::axesEnable(bool enable)
4141
}
4242
LIS331_write(CTRL_REG1, &data, 1);
4343
}
44+
4445
void LIS331::setPowerMode(power_mode pmode)
4546
{
4647
uint8_t data;
@@ -295,26 +296,6 @@ bool LIS331::newZData()
295296
}
296297
}
297298

298-
void LIS331::intSrcAndEnable(bool enable, uint8_t interrupt)
299-
{
300-
uint8_t data, reg;
301-
if (interrupt == 1)
302-
{
303-
reg = INT1_CFG;
304-
}
305-
else
306-
{
307-
reg = INT2_CFG;
308-
}
309-
LIS331_read(reg, &data, 1);
310-
data &= ~0x80;
311-
if (enable)
312-
{
313-
data |= 1<<7;
314-
}
315-
LIS331_write(reg, &data, 1);
316-
}
317-
318299
void LIS331::enableInterrupt(int_axis axis, trig_on_level trigLevel,
319300
uint8_t interrupt, bool enable)
320301
{

src/SparkFun_LIS331.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class LIS331
6363
bool newXData();
6464
bool newYData();
6565
bool newZData();
66-
void intSrcAndEnable(bool enable, uint8_t interrupt);
6766
void enableInterrupt(int_axis axis, trig_on_level trigLevel,
6867
uint8_t interrupt, bool enable);
6968
void setIntDuration(uint8_t duration, uint8_t intSource);

0 commit comments

Comments
 (0)