DevLab_ICM20948 1.0.0
ICM-20948 9-Axis Motion Sensor Driver
Loading...
Searching...
No Matches
interrupt.ino
Go to the documentation of this file.
1/**
2 * @file interrupt.ino
3 * @brief I2C interrupt example for the 7Semi ICM-20948.
4 * @details Configures the IMU raw-data-ready interrupt pin and counts ISR
5 * events on an Arduino-compatible board.
6 */
7
8#include <Wire.h>
9#include <DevLab_ICM20948.h>
10
11/** @brief I2C SDA pin used by the example board. */
12#define SDA_PIN 6
13/** @brief I2C SCL pin used by the example board. */
14#define SCL_PIN 7
15/** @brief I2C bus speed in hertz. */
16#define I2C_FREQ 400000UL
17/** @brief ICM-20948 I2C address selected by the AD0 pin. */
18#define ICM_ADDR 0x69
19/** @brief MCU pin connected to the IMU interrupt output. */
20#define PIN_INT D7 // ajusta a tu pin real
21
22/** @brief Global ICM-20948 driver instance. */
23DevLab_ICM20948 imu;
24
25/** @brief Print a separator line to Serial. */
26void printLinea() { Serial.println(F("------------------------------------------------------------")); }
27/** @brief Print a double separator line to Serial. */
28void printDobleLinea() { Serial.println(F("============================================================")); }
29
30/** @brief Interrupt pin electrical and latch configuration. */
31ICM20948_IntPinConfig pinCfg = {
32 .activeLevel = 1,
33 .driveMode = 0,
34 .latchMode = 0,
35 .clearMode = 1,
36 .fsyncActLevel = 0,
37 .fsyncIntEn = 0,
38 .bypassEn = 0
39};
40
41/** @brief Interrupt enable flags used by this example. */
42ICM20948_IntEnableConfig intEn = {
43 .rawDataRdyEn = 1 // inicializacion directa
44};
45
46/** @brief Number of interrupt events observed by the ISR. */
47volatile uint32_t isrCount = 0;
48
49/**
50 * @brief Interrupt service routine for the IMU data-ready signal.
51 */
52void IRAM_ATTR isrHandler() {
53 isrCount++;
54}
55
56/**
57 * @brief Initialize I2C, verify the IMU, configure interrupts, and attach ISR.
58 */
59void setup() {
60 Serial.begin(115200);
61 delay(200);
62
63 Wire.begin(SDA_PIN, SCL_PIN);
64
65 if (!imu.beginI2C(ICM_ADDR, Wire, I2C_FREQ)) {
66 Serial.println(F("ERROR: beginI2C() failed."));
67 while (1) delay(200);
68 }
69 Serial.println(F("ICM-20948 ready."));
70
71 uint8_t who;
72 if (!imu.readWhoAmI(who) || who != 0xEA) {
73 Serial.println(F("ERROR: WHO_AM_I mismatch"));
74 while (1) delay(200);
75 }
76 Serial.printf("WHO_AM_I = 0x%02X\n", who);
77
78 if(!imu.intInit(pinCfg)){
79 Serial.println(F("ERROR: Initialization interruption failed"));
80 while(1) delay(200);
81 }; // nombre correcto
82 if(!imu.intEnableConfig(intEn)){
83 Serial.println(F("ERROR: Initialization interruption failed"));
84 while(1) delay(200);
85 }
86
87 attachInterrupt(digitalPinToInterrupt(PIN_INT), isrHandler, FALLING);
88 Serial.println(F("Interrupt configured. Waiting..."));
89}
90
91/**
92 * @brief Report interrupt events when the ISR count changes.
93 */
94void loop() {
95 static uint32_t lastCount = 0;
96
97 if (isrCount != lastCount) {
98 lastCount = isrCount;
99 Serial.printf(">>> ISR disparada! Total: %lu\n", isrCount);
100 }
101}