2021-03-17 04:37 AM
Single-tap example
Hereafter an example code which implements the SW routine for single-tap detection.
1. Write 60h in CTRL1
// Turn on the accelerometer
// ODR = 400 Hz, FS = ±2 g
2. Write 38h in CTRL3 // Enable tap detection on X, Y, Z-axis
3. Write 09h in TAP_6D_THS // Set tap threshold
4. Write 06h in INT_DUR // Set quiet and shock time windows
5. Write 00h in WAKE_UP_THS // Only single tap enabled (SINGLE_DOUBLE_TAP = 0)
6. Write 40h in CTRL4 // Single-tap interrupt driven to INT1 pin
2021-03-17 08:15 AM
Hi @FBald.1 ,
I suggest you to consider the C implementation of the "Single Tap" feature on Github --> lis2ds12_tap_single.c
This code implements not only the configuration, but also the main loop for the interrupt management and clearing.
Here below the detailed script:
/* Main Example --------------------------------------------------------------*/
void lis2ds12_single_tap_(void)
{
/* Initialize mems driver interface */
stmdev_ctx_t dev_ctx;
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &SENSOR_BUS;
/* Check device ID */
lis2ds12_device_id_get(&dev_ctx, &whoamI);
if (whoamI != LIS2DS12_ID)
while (1) {
/* manage here device not found */
}
/* Restore default configuration */
lis2ds12_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
lis2ds12_reset_get(&dev_ctx, &rst);
} while (rst);
/* Set XL Output Data Rate */
lis2ds12_xl_data_rate_set(&dev_ctx, LIS2DS12_XL_ODR_400Hz_HR);
/* Set 2g full XL scale */
lis2ds12_xl_full_scale_set(&dev_ctx, LIS2DS12_2g);
/* Enable Tap detection on X, Y, Z */
lis2ds12_tap_detection_on_z_set(&dev_ctx, PROPERTY_ENABLE);
lis2ds12_tap_detection_on_y_set(&dev_ctx, PROPERTY_ENABLE);
lis2ds12_tap_detection_on_x_set(&dev_ctx, PROPERTY_ENABLE);
lis2ds12_4d_mode_set(&dev_ctx, PROPERTY_ENABLE);
/* Set Tap threshold to 01001b, therefore the tap threshold is
* 562.5 mg (= 9 * FS_XL / 2 5 )
*/
lis2ds12_tap_threshold_set(&dev_ctx, 0x09);
/* Configure Single Tap parameter */
//lis2ds12_tap_dur_set(&dev_ctx, 0x0);
lis2ds12_tap_quiet_set(&dev_ctx, 0x01);
lis2ds12_tap_shock_set(&dev_ctx, 0x02);
/* Enable Single Tap detection only */
lis2ds12_tap_mode_set(&dev_ctx, LIS2DS12_ONLY_SINGLE);
/* Wait Events */
while (1) {
lis2ds12_all_sources_t all_source;
lis2ds12_all_sources_get(&dev_ctx, &all_source);
/* Check if Single Tap events */
if (all_source.tap_src.single_tap) {
sprintf((char *)tx_buffer, "Tap Detected\r\n");
tx_com(tx_buffer, strlen((char const *)tx_buffer));
}
}
}
Let me please know if you can find this code useful.
-Eleon