11/* ***************************************************************
22 * Example3_Interrupts.ino
3- * ICM 20948 Arduino Library Demo
3+ * ICM 20948 Arduino Library Demo
44 * Builds on Example2_Advanced.ino to set up interrupts when data is ready
55 * Owen Lyke @ SparkFun Electronics
66 * Original Creation Date: June 5 2019
7- *
8- * For this example you must connect the interrupt pin "INT" on the breakout
7+ *
8+ * For this example you must connect the interrupt pin "INT" on the breakout
99 * board to the pin specified by "INT_PIN" on your microcontroller.
10- *
11- * This code is beerware; if you see me (or any other SparkFun employee) at the
12- * local, and you've found our code helpful, please buy us a round!
13- *
10+ *
11+ * Please see License.md for the license information.
12+ *
1413 * Distributed as-is; no warranty is given.
1514 ***************************************************************/
1615#include " ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU
2928#define CS_PIN 2 // Which pin you connect CS to. Used only when "USE_SPI" is defined
3029
3130#define WIRE_PORT Wire // Your desired Wire port. Used when "USE_SPI" is not defined
32- #define AD0_VAL 1 // The value of the last bit of the I2C address.
33- // On the SparkFun 9DoF IMU breakout the default is 1, and when
31+ #define AD0_VAL 1 // The value of the last bit of the I2C address.
32+ // On the SparkFun 9DoF IMU breakout the default is 1, and when
3433 // the ADR jumper is closed the value becomes 0
3534
3635#ifdef USE_SPI
@@ -61,12 +60,12 @@ void setup() {
6160 WIRE_PORT.begin ();
6261 WIRE_PORT.setClock (400000 );
6362#endif
64-
63+
6564 bool initialized = false ;
6665 while ( !initialized ){
6766
6867#ifdef USE_SPI
69- myICM.begin ( CS_PIN, SPI_PORT, SPI_FREQ ); // Here we are using the user-defined SPI_FREQ as the clock speed of the SPI bus
68+ myICM.begin ( CS_PIN, SPI_PORT, SPI_FREQ ); // Here we are using the user-defined SPI_FREQ as the clock speed of the SPI bus
7069#else
7170 myICM.begin ( WIRE_PORT, AD0_VAL );
7271#endif
@@ -91,7 +90,7 @@ void setup() {
9190 SERIAL_PORT.println (myICM.statusString ());
9291 }
9392 delay (250 );
94-
93+
9594 // Now wake the sensor up
9695 myICM.sleep ( sensorSleep );
9796 myICM.lowPower ( false );
@@ -101,7 +100,7 @@ void setup() {
101100 // Set Gyro and Accelerometer to a particular sample mode
102101 // options: ICM_20948_Sample_Mode_Continuous
103102 // ICM_20948_Sample_Mode_Cycled
104- myICM.setSampleMode ( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), ICM_20948_Sample_Mode_Cycled );
103+ myICM.setSampleMode ( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), ICM_20948_Sample_Mode_Cycled );
105104 SERIAL_PORT.print (F (" setSampleMode returned: " ));
106105 SERIAL_PORT.println (myICM.statusString ());
107106
@@ -111,23 +110,23 @@ void setup() {
111110 myICM.setSampleRate ( ICM_20948_Internal_Gyr, mySmplrt );
112111 SERIAL_PORT.print (F (" setSampleRate returned: " ));
113112 SERIAL_PORT.println (myICM.statusString ());
114-
113+
115114 // Set full scale ranges for both acc and gyr
116115 ICM_20948_fss_t myFSS; // This uses a "Full Scale Settings" structure that can contain values for all configurable sensors
117-
116+
118117 myFSS.a = gpm2; // (ICM_20948_ACCEL_CONFIG_FS_SEL_e)
119118 // gpm2
120119 // gpm4
121120 // gpm8
122121 // gpm16
123-
122+
124123 myFSS.g = dps250; // (ICM_20948_GYRO_CONFIG_1_FS_SEL_e)
125124 // dps250
126125 // dps500
127126 // dps1000
128127 // dps2000
129-
130- myICM.setFullScale ( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myFSS );
128+
129+ myICM.setFullScale ( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myFSS );
131130 if ( myICM.status != ICM_20948_Stat_Ok){
132131 SERIAL_PORT.print (F (" setFullScale returned: " ));
133132 SERIAL_PORT.println (myICM.statusString ());
@@ -154,7 +153,7 @@ void setup() {
154153 // gyr_d11bw6_n17bw8
155154 // gyr_d5bw7_n8bw9
156155 // gyr_d361bw4_n376bw5
157-
156+
158157 myICM.setDLPFcfg ( (ICM_20948_Internal_Acc | ICM_20948_Internal_Gyr), myDLPcfg );
159158 if ( myICM.status != ICM_20948_Stat_Ok){
160159 SERIAL_PORT.print (F (" setDLPcfg returned: " ));
@@ -193,7 +192,7 @@ void setup() {
193192 myICM.cfgIntLatch (true ); // Latch the interrupt until cleared
194193 SERIAL_PORT.print (F (" cfgIntLatch returned: " ));
195194 SERIAL_PORT.println (myICM.statusString ());
196-
195+
197196 myICM.intEnableRawDataReady (true ); // enable interrupts on raw data ready
198197 SERIAL_PORT.print (F (" intEnableRawDataReady returned: " ));
199198 SERIAL_PORT.println (myICM.statusString ());
@@ -207,7 +206,7 @@ void setup() {
207206// ICM_20948_execute_w( &myICM._device, AGB0_REG_INT_ENABLE, (uint8_t*)&zero_0, sizeof(uint8_t) );
208207
209208 SERIAL_PORT.println ();
210- SERIAL_PORT.println (F (" Configuration complete!" ));
209+ SERIAL_PORT.println (F (" Configuration complete!" ));
211210}
212211
213212void loop () {
@@ -217,19 +216,19 @@ void loop() {
217216// myICM.clearInterrupts(); // This would be efficient... but not compatible with Uno
218217 }
219218
220- myICM.clearInterrupts (); // clear interrupts for next time -
221- // usually you'd do this only if an interrupt has occurred, however
219+ myICM.clearInterrupts (); // clear interrupts for next time -
220+ // usually you'd do this only if an interrupt has occurred, however
222221 // on the 328p I2C usage can block interrupts. This means that sometimes
223222 // an interrupt is missed. When missed, if using an edge-based interrupt
224- // and only clearing interrupts when one was detected there will be no more
225- // edges to respond to, so no more interrupts will be detected. Here are
223+ // and only clearing interrupts when one was detected there will be no more
224+ // edges to respond to, so no more interrupts will be detected. Here are
226225 // some possible solutions:
227226 // 1. use a level based interrupt
228227 // 2. use the pulse-based interrupt in ICM settings (set cfgIntLatch to false)
229228 // 3. use a microcontroller with nestable interrupts
230229 // 4. clear the interrupts often
231-
232-
230+
231+
233232 if ( (millis ()%1000 ) < 5 ){ // This is a method to turn the sensor on and off once per second without using delays
234233 if ( canToggle ){
235234 sensorSleep = !sensorSleep;
0 commit comments