Single Click and Double Click Detection Using LIS3DH Single Click Interrupt
This example shows how to sense a free-fall condition on a sensor by using the Single Click interrupt of a LIS3DH linear accelerometer that is connected to a STMicroelectronics STM32 processor-based board.
Prerequisites
Before working with this example, it is helpful to get familiar with the information in Getting Started with STMicroelectronics STM32 Processor Based Boards. This topic provides information about working with the STMicroelectronics CubeMX software for board I/O configuration.
Required Hardware
To run this example, you need these hardware:
Supported STMicroelectronics STM32F767ZI processor board
USB cable
Breadboard wires
Hardware Connection
Connect the INT1 pin of the LIS3DH sensor to pin PE11 on the STMicroelectronics STM32 processor-based board.
Connect the SDA, SCL, 3.3V, and GND pin of the STMicroelectronics STM32 processor-based board to the respective pins on the LIS3DH sensor.
Hardware Configuration in the Model
Open the model stnucleo_i2c_lis3dh_clickdetection_embeddedCoder
. In the Command Window, type:
open_system('stnucleo_i2c_lis3dh_clickdetection_embeddedCoder.slx');
The model is preconfigured to work with STM32F767Zi based board. If you are using a different STMicroelectronics STM32 processor-based board, change the hardware board by performing the following steps:
1. Navigate to Modeling > Model Settings to open the Configuration Parameters dialog box.
2. Open the Hardware Implementation pane, and from the Hardware board list, select the STMicroelectronics STM32 processor-based board that you are using.
3. Click Apply. Click OK to close the dialog box.
Configure the Click Detection on LIS3DH for Interrupt Service Routine (ISR)
1. In the model, open the External Interrupt block. Make sure that the parameters are set as shown. The LIS3DH sensor block is placed in the Initialize Function subsystem.
2. This example includes an STM32 CubeMX project file i2c_lis3dh_clickdetection.ioc
that configures I/O connections for code generation. As shown in Getting Started with STMicroelectronics STM32 Processor Based Boards, you can browse to the project file from the Configuration Parameters dialog box. Or, you could use the STM32 CubeMX software to create a project file. Launch the STM32CubeMX project in the STM32CubeMX tool and apply configurations to enable to GPIO as shown in STM32 CubeMX.
3. In STM32 processor-based boards, for the I2C calls to work from within ISR, the interrupt priority of the external interrupt must be modified. The interrupt priority is modified using the System Outputs block. In this example, since PE11 is configured as the external interrupt, NVIC_SetPriority(EXTI4_IRQn,0xFF);
is added to the System Outputs block. The IRQn can vary with respect to targets and pins. You can modify the IRQn based on the pin and target selection.
Ensure that the Enable click detection parameter is selected, and also ensure that Single click
is selected for Type parameter.
4. Perform the following configurations to enable and configure I2C1 to enable the communication between host and target as shown in STM32 CubeMX.
Initiate Monitor and Tune Action for the Model
During Monitor and Tune action, the model is deployed as a C code on the hardware. The code obtains real-time data from the hardware. The data acquisition and parameter tuning are done while the application is running on the hardware.
1. On the Hardware tab, in the Mode section, select Run on board (External Mode) and then click Monitor & Tune.
The lower-left corner of the model window displays status while Simulink prepares, downloads, and runs the Simulink model on the hardware. During simulation, the pin PE11 on STMicroelectronics STM32 processor-based board generates an interrupt on every rising edge of the signal, which triggers the function-call subsystem.
2. Tap the sensor to verify the output in the display data area of the model. Single click will be displayed as 1
and double click will be displayed as 2
.
Close Model
bdclose('stnucleo_i2c_lis3dh_clickdetection_embeddedCoder');