{"id":2545,"date":"2024-05-26T04:33:45","date_gmt":"2024-05-26T04:33:45","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2545"},"modified":"2024-05-26T04:33:48","modified_gmt":"2024-05-26T04:33:48","slug":"working-with-stm32-and-sensors-lis3dsh-accelerometer-sensor","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2545","title":{"rendered":"Working with STM32 and Sensors: LIS3DSH Accelerometer Sensor"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-300x300.png\" alt=\"\" class=\"wp-image-2546\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-300x300.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-1024x1024.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-150x150.png 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-768x768.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-1536x1536.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-2048x2048.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-1150x1150.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-750x750.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-400x400.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/AdobeStock_535776199-250x250.png 250w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall take a look at the LIS3DSH Accelerometer that embedded within the STM32F407 discovery and how to read the acceleration data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>LIS3DSH Sensor.<\/li><li>Enable the float point hardware.<\/li><li>Interfacing the sensor.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. LIS3DSH Sensor:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Features<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Wide supply voltage, 1.71 V to 3.6 V<\/p>\n\n\n\n<p>The LIS3DSH has dynamically selectable full scales of \uf0b12g\/\u00b14g\/\uf0b16g\/\uf0b18g\/\uf0b116g and is capable of measuring accelerations with output data rates from 3.125 Hz to 1.6 kHz.<\/p>\n\n\n\n<p>Independent IOs supply (1.8 V) and supply voltage compatible<\/p>\n\n\n\n<p>Ultra-low power consumption<\/p>\n\n\n\n<p>\uf0b12g\/\u00b14g\/\uf0b16g\/\uf0b18g\/\uf0b116g dynamically selectable full scale<\/p>\n\n\n\n<p>I 2 C\/SPI digital output interface<\/p>\n\n\n\n<p>16-bit data output<\/p>\n\n\n\n<p>Programmable embedded state machines<\/p>\n\n\n\n<p>Embedded temperature sensor<\/p>\n\n\n\n<p>Embedded self-test<\/p>\n\n\n\n<p>Embedded FIFO<\/p>\n\n\n\n<p>10000 g high shock survivability<\/p>\n\n\n\n<p>ECOPACK \u00ae , RoHS and \u201cGreen\u201d compliant<\/p>\n\n\n\n<p>The self-test capability allows the user to check the functioning of the sensor in the final application.<\/p>\n\n\n\n<p>The device can be configured to generate interrupt signals activated by user-defined motion patterns.<\/p>\n\n\n\n<p>The LIS3DSH has an integrated first-in, first-out (FIFO) buffer allowing the user to store data in order to limit intervention by the host processor.<\/p>\n\n\n\n<p>The LIS3DSH is available in a small thin plastic land grid array package (LGA) and is guaranteed to operate over an extended temperature range from -40 \u00b0C to +85 \u00b0C.<\/p>\n\n\n\n<p>Applications<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Motion-controlled user interface<\/p>\n\n\n\n<p>Gaming and virtual reality<\/p>\n\n\n\n<p>Pedometers<\/p>\n\n\n\n<p>Intelligent power saving for handheld devices<\/p>\n\n\n\n<p>Display orientation<\/p>\n\n\n\n<p>Click\/double-click recognition<\/p>\n\n\n\n<p>Impact recognition and logging<\/p>\n\n\n\n<p>Vibration monitoring and compensation<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"734\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-6.59.43\u202fAM.png\" alt=\"\" class=\"wp-image-2547\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-6.59.43\u202fAM.png 734w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-6.59.43\u202fAM-300x300.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-6.59.43\u202fAM-150x150.png 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-6.59.43\u202fAM-400x400.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-6.59.43\u202fAM-250x250.png 250w\" sizes=\"(max-width: 734px) 100vw, 734px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Enable Float Point Hardware:<\/h2>\n\n\n\n<p>Before enabling the hardware, please refer to <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1255\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1255\" target=\"_blank\">this guide<\/a> for how to setup STM32CubeIDE.<\/p>\n\n\n\n<p>After that, create new source file with name of <strong><em>sys_init.c<\/em><\/strong><\/p>\n\n\n\n<p>Within the source file, include the following header file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stm32f4xx.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare add the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void SystemInit(void)\n{\n\t\/*Enable FPU*\/\n\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));\n}<\/pre><\/div>\n\n\n\n<p>This function shall be called when the STM32 starts. No need to call it in our application.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Interfacing the sensor:<\/h2>\n\n\n\n<p>Create new source and header file with name of <strong><em>LIS3DSH<\/em><\/strong>.c and <strong><em>LIS3DSH<\/em><\/strong>.h respectively.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Before we start developing the header and source file, we need to find the pins connected to the accelerometer.<\/p>\n\n\n\n<p>From the STM32F407-discovery board schematics:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"636\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-1024x636.png\" alt=\"\" class=\"wp-image-2548\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-1024x636.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-300x186.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-768x477.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-1150x714.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-750x466.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-400x248.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM-250x155.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-26-at-7.07.54\u202fAM.png 1182w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We can find that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PA5, PA6 and PA7 for the SPI pins.<\/li><li>PE3 for chip select (CS)<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>New, within the header file, include the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stdint.h&quot;<\/pre><\/div>\n\n\n\n<p>Add these define statements for the registers of the sensors:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define LIS3DSH_OUT_T_ADDR                   0x0C\n\n\/*******************************************************************************\n*  INFO1 Register: Information Register 1\n*  Read only register\n*  Default value: 0x21\n*******************************************************************************\/\n#define LIS3DSH_INFO1_ADDR                   0x0D\n\n\/*******************************************************************************\n*  INFO2 Register: Information Register 2\n*  Read only register\n*  Default value: 0x00\n*******************************************************************************\/\n#define LIS3DSH_INFO2_ADDR                   0x0E\n\n\/*******************************************************************************\n*  WHO_AM_I Register: Device Identification Register\n*  Read only register\n*  Default value: 0x3F\n*******************************************************************************\/\n#define LIS3DSH_WHO_AM_I_ADDR                0x0F\n\n\/*******************************************************************************\n*  OFF_X Register: X-axis Offset Compensation Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 OFFx_7-OFFx_0: X-axis Offset Compensation Value\n*******************************************************************************\/\n#define LIS3DSH_OFF_X_ADDR                   0x10\n\n\/*******************************************************************************\n*  OFF_Y Register: Y-axis Offset Compensation Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 OFFy_7-OFFy_0: Y-axis Offset Compensation Value\n*******************************************************************************\/\n#define LIS3DSH_OFF_Y_ADDR                   0x11\n\n\/*******************************************************************************\n*  OFF_Z Register: Z-axis Offset Compensation Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 OFFz_7-OFFz_0: Z-axis Offset Compensation Value\n*******************************************************************************\/\n#define LIS3DSH_OFF_Z_ADDR                   0x12\n\n\/*******************************************************************************\n*  CS_X Register: X-axis Constant Shift Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 CS_7-CS_0: X-axis Constant Shift Value\n*******************************************************************************\/\n#define LIS3DSH_CS_X_ADDR                    0x13\n\n\/*******************************************************************************\n*  CS_Y Register: Y-axis Constant Shift Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 CS_7-CS_0: Y-axis Constant Shift Value\n*******************************************************************************\/\n#define LIS3DSH_CS_Y_ADDR                    0x14\n\n\/*******************************************************************************\n*  CS_Z Register: Z-axis Constant Shift Value Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 CS_7-CS_0: Z-axis Constant Shift Value\n*******************************************************************************\/\n#define LIS3DSH_CS_Z_ADDR                    0x15\n\n\/*******************************************************************************\n*  LC_L Register: Long Counter Low Register\n*  Read Write register\n*  Default value: 0x01\n*  7:0 LC_L_7-LC_L_0: Long Counter Low Value\n*******************************************************************************\/\n#define LIS3DSH_LC_L_ADDR                    0x16\n\n\/*******************************************************************************\n*  LC_H Register: Long Counter High Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 LC_H_7-LC_H_0: Long Counter Low Value\n*******************************************************************************\/\n#define LIS3DSH_LC_H_ADDR                    0x17\n\n\/*******************************************************************************\n*  STAT Register: State Machine Register\n*  Read only register\n*  Default value: 0x00\n*  7 LONG: LONG flag common to both State Machines\n*          0 - no interrupt\n*          1 - LongCounter interrupt flag\n*  6 SYNCW: Common information for OUTW host action waiting\n*           0 - no action waiting from Host\n*           1 - Host action is waiting after OUTW command\n*  5 SYNC1:\n*           0 - State Machine 1 running normally\n*           1 - SM1 stopped and waiting for restart request from SM2\n*  4 SYNC2:\n*           0 - State Machine 2 running normally\n*           1 - SM2 stopped and waiting for restart request from SM1\n*  3 INT_SM1: Interrupt signal on SM1 is reset when OUTS1 register is read\n*             0 - no interrupt on State Machine 1\n*             1 - State Machine 1 interrupt happened\n*  2 INT_SM2: Interrupt signal on SM2 is reset when OUTS2 register is read\n*             0 - no interrupt on State Machine 2\n*             1 - State Machine 2 interrupt happened\n*  1 DOR: Data OverRun bit\n*         0 - no overrun\n*         1 - data overrun\n*  0 DRDY: New data are ready in output registers\n*         0 - data not ready\n*         1 - data ready\n*******************************************************************************\/\n#define LIS3DSH_STAT_ADDR                    0x18\n\n\/*******************************************************************************\n*  PEAK1 Register: Peak 1 Register\n*  Read only register\n*  Default value: 0x00\n*  7:0 PKx_7-PKx_0: Peak 1 Value for SM1\n*******************************************************************************\/\n#define LIS3DSH_PEAK1_ADDR                   0x19\n\n\/*******************************************************************************\n*  PEAK2 Register: Peak 2 Register\n*  Read only register\n*  Default value: 0x00\n*  7:0 PKx_7-PKx_0: Peak 2 value for SM2\n*******************************************************************************\/\n#define LIS3DSH_PEAK2_ADDR                   0x1A\n\n\/*******************************************************************************\n*  VFC_1 Register: Vector Filter Coefficient 1 Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 VFC1_7-VFC1_0: Vector Filter Coefficient 1 Value\n*******************************************************************************\/\n#define LIS3DSH_VFC_1_ADDR                   0x1B\n\n\/*******************************************************************************\n*  VFC_2 Register: Vector Filter Coefficient 2 Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 VFC2_7-VFC2_0: Vector Filter Coefficient 2 Value\n*******************************************************************************\/\n#define LIS3DSH_VFC_2_ADDR                   0x1C\n\n\/*******************************************************************************\n*  VFC_3 Register: Vector Filter Coefficient 3 Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 VFC3_7-VFC3_0: Vector Filter Coefficient 3 Value\n*******************************************************************************\/\n#define LIS3DSH_VFC_3_ADDR                   0x1D\n\n\/*******************************************************************************\n*  VFC_4 Register: Vector Filter Coefficient 4 Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 VFC4_7-VFC4_0: Vector Filter Coefficient 4 Value\n*******************************************************************************\/\n#define LIS3DSH_VFC_4_ADDR                   0x1E\n\n\/*******************************************************************************\n*  THRS3 Register: Threshold Value 3 Register\n*  Read Write register\n*  Default value: 0x00\n*  7:0 THRS3_7-THRS3_0: Common Threshold for Overrun Detection Value\n*******************************************************************************\/\n#define LIS3DSH_THRS3_ADDR                   0x1F\n\n\/*******************************************************************************\n*  CTRL_REG4 Register: Control Register 4\n*  Read Write register\n*  Default value: 0x07\n*  7:4 ODR3-ODR0: Data rate selection\n*            ODR3 | ODR2 | ODR1 | ORD0 | ORD Selection\n*            -------------------------------------------\n*              0  |  0   |  0   |  0   | Power Down (Default)\n*              0  |  0   |  0   |  1   | 3.125 Hz\n*              0  |  0   |  1   |  0   | 6.25 Hz\n*              0  |  0   |  1   |  1   | 12.5 Hz\n*              0  |  1   |  0   |  0   | 25 Hz\n*              0  |  1   |  0   |  1   | 50 Hz\n*              0  |  1   |  1   |  0   | 100 Hz\n*              0  |  1   |  1   |  1   | 400 Hz\n*              1  |  0   |  0   |  0   | 800 Hz\n*              1  |  0   |  0   |  1   | 1600 Hz\n*\n*  3 BDU: Block data update\n*         0: Output register not updated until High and Low reading (Default)\n*         1: Continuous update\n*  2 ZEN:\n*         0: Z-axis disable (Default)\n*         1: Z-axis enable\n*  1 YEN:\n*         0: Y-axis disable (Default)\n*         1: Y-axis enable\n*  0 XEN:\n*         0: Y-axis disable (Default)\n*         1: Y-axis enable\n*******************************************************************************\/\n#define LIS3DSH_CTRL_REG4_ADDR               0x20\n\n\/*******************************************************************************\n*  CTRL_REG1 Register: Control Register 1 (SM1 interrupt configuration register)\n*  Read Write register\n*  Default value: 0x00\n*  7:5 HYST1_2-HYST1_0: Hysteresis which is added or subtracted from the\n*                       threshold values (THRS1_1 and THRS2_1) of SM1.\n*                       000 = 0 (Default)\n*                       111 = 7 (maximum Hysteresis)\n*  4 Reserved\n*  3 SM1_INT:\n*            0: State Machine 1 interrupt routed to INT1 (Default)\n*            1: State Machine 1 interrupt routed to INT2\n*  2 Reserved\n*  1 Reserved\n*  0 SM1_EN:\n*           0: State Machine 1 disabled. Temporary memories and registers\n*              related to this State Machine are left intact. (Default)\n*           1: State Machine 1 enabled.\n*******************************************************************************\/\n#define LIS3DSH_CTRL_REG1_ADDR               0x21\n\n\/*******************************************************************************\n*  CTRL_REG2 Register: Control Register 2 (SM2 interrupt configuration register)\n*  Read Write register\n*  Default value: 0x00\n*  7:5 HYST2_2-HYST2_0: Hysteresis which is added or subtracted from the\n*                       threshold values (THRS1_2 and THRS2_2) of SM1.\n*                       000 = 0 (Default)\n*                       111 = 7 (maximum Hysteresis)\n*  4 Reserved\n*  3 SM2_INT:\n*            0: State Machine 2 interrupt routed to INT1 (Default)\n*            1: State Machine 2 interrupt routed to INT2\n*  2 Reserved\n*  1 Reserved\n*  0 SM2_EN:\n*           0: State Machine 2 disabled. Temporary memories and registers\n*              related to this State Machine are left intact. (Default)\n*           1: State Machine 2 enabled.\n*******************************************************************************\/\n#define LIS3DSH_CTRL_REG2_ADDR               0x22\n\n\/*******************************************************************************\n*  CTRL_REG3 Register: Control Register 3\n*  Read Write register\n*  Default value: 0x00\n*  7 DR_EN: Data-ready interrupt\n*          0 - Data-ready interrupt disabled (Default)\n*          1 - Data-ready interrupt enabled and routed to INT1\n*  6 IEA:\n*         0 - Interrupt signal active LOW (Default)\n*         1 - Interrupt signal active HIGH\n*  5 IEL:\n*         0 - Interrupt latched (Default)\n*         1 - Interrupt pulsed\n*  4 INT2_EN:\n*             0 - INT2 signal disabled (High-Z state) (Default)\n*             1 - INT2 signal enabled (signal pin fully functional)\n*  3 INT1_EN:\n*             0 - INT1 (DRDY) signal disabled (High-Z state) (Default)\n*             1 - INT1 (DRDY) signal enabled (signal pin fully functional) DR_EN bit in CTRL_REG3 register should be taken into account too\n*  2 VLIFT:\n*           0 - Vector filter disabled (Default)\n*           1 - Vector filter enabled\n*  1 Reserved\n*  0 STRT: Soft Reset\n*          0 - (Default)\n*          1 - it resets the whole internal logic circuitry. It automatically returns to 0.\n*******************************************************************************\/\n#define LIS3DSH_CTRL_REG3_ADDR               0x23\n\n\/*******************************************************************************\n*  CTRL_REG5 Register: Control Register 5\n*  Read Write register\n*  Default value: 0x00\n*  7:6 BW2-BW1: Anti aliasing filter bandwidth\n*            BW2 | BW1 | BW Selection\n*            -------------------------\n*             0  |  0  | 800 Hz (Default)\n*             0  |  1  | 40 Hz\n*             1  |  0  | 200 Hz\n*             1  |  1  | 50 Hz\n*\n*  5:3 FSCALE2-FSCALE0: Full scale selection\n*            FSCALE2 | FSCALE1 | FSCALE0 | Full scale selection\n*            --------------------------------------------------\n*               0    |    0    |    0    | +\/-2g (Default)\n*               0    |    0    |    1    | +\/-4g\n*               0    |    1    |    0    | +\/-6g\n*               0    |    1    |    1    | +\/-8g\n*               1    |    0    |    0    | +\/-16g\n*\n*  2:1 ST2_ST1: Self-test Enable\n*            ST2 | ST1 | ST Selection\n*            -------------------------\n*             0  |  0  | Normal Mode (Default)\n*             0  |  1  | Positive sign self-test\n*             1  |  0  | Negative sign-test\n*             1  |  1  | Not Allowed\n*\n*  0 SIM: SPI serial internal interface mode selection\n*         0: 4-wire interface (Default)\n*         1: 3-wire interface\n*******************************************************************************\/\n#define LIS3DSH_CTRL_REG5_ADDR               0x24\n\n\/*******************************************************************************\n*  CTRL_REG6 Register: Control Register 6\n*  Read Write register\n*  Default value: 0x00\n*  7 BOOT: Force reboot, cleared as soon as the reboot is finished. Active High.\n*  6 FIFO_EN: FIFO Enable\n*             0: disable (Default)\n*             1: enable\n*  5 STP_WTM: Stop on Watermark - FIFO depth can be limited at the Watermark value, by setting to \ufffd1\ufffd the STP_WTM bit.\n*             0: disable (Default)\n*             1: enable\n*  4 IF_ADD_INC: Register address automatically increased during a multiple byte access with a serial interface (I2C or SPI)\n*                0: disable (Default)\n*                1: enable\n*  3 I1_EMPTY: Enable FIFO Empty indication on INT1 pin.\n*              0: disable (Default)\n*              1: enable\n*  2 I1_WTM: FIFO Watermark interrupt on INT1 pin.\n*            0: disable (Default)\n*            1: enable\n*  1 I1_OVERRUN: FIFO Overrun interrupt on INT1 pin.\n*                0: disable (Default)\n*                1: enable\n*  0 I2_BOOT: BOOT interrupt on INT2 pin.\n*                0: disable (Default)\n*                1: enable\n*******************************************************************************\/\n#define LIS3DSH_CTRL_REG6_ADDR               0x25\n\n\/*******************************************************************************\n*  STATUS Register: Status Data Register\n*  Read only register\n*  Default value: 0x00\n*  7 ZYXOR: X, Y and Z-axis Data Overrun.\n*           0: no Overrun has occurred (Default)\n*           1: a new set of data has overwritten the previous ones\n*  6 ZOR: Z-axis Data Overrun.\n*         0: no Overrun has occurred (Default)\n*         1: a new data for the Z-axis has overwritten the previous ones\n*  5 YOR: Y-axis Data Overrun.\n*         0: no Overrun has occurred (Default)\n*         1: a new data for the Y-axis has overwritten the previous ones\n*  4 XOR: X-axis Data Overrun.\n*         0: no Overrun has occurred (Default)\n*         1: a new data for the X-axis has overwritten the previous ones\n*  3 ZYXDA: X, Y and Z-axis new Data Available.\n*           0: a new set of data is not yet available (Default)\n*           1: a new set of data is available\n*  2 ZDA: Z-axis new data available.\n*         0: a new data for the Z-axis is not yet available (Default)\n*         1: a new data for Z-axis is available\n*  1 YDA: Y-axis new data available.\n*         0: a new data for the Y-axis is not yet available (Default)\n*         1: a new data for Y-axis is available\n*  0 XDA: X-axis new data available.\n*         0: a new data for the X-axis is not yet available (Default)\n*         1: a new data for X-axis is available\n*******************************************************************************\/\n#define LIS3DSH_STATUS_ADDR                  0x27\n\n\/*******************************************************************************\n*  OUT_X_L Register: X-axis Output Acceleration Low Data\n*  Read only register\n*  Default value: output\n*  7:0 XD7-XD0: X-axis output Data\n*******************************************************************************\/\n#define LIS3DSH_OUT_X_L_ADDR                 0x28\n\n\/*******************************************************************************\n*  OUT_X_H Register: X-axis Output Acceleration High Data\n*  Read only register\n*  Default value: output\n*  15:8 XD15-XD8: X-axis output Data\n*******************************************************************************\/\n#define LIS3DSH_OUT_X_H_ADDR                 0x29\n\n\/*******************************************************************************\n*  OUT_Y_L Register: Y-axis Output Acceleration Low Data\n*  Read only register\n*  Default value: output\n*  7:0 YD7-YD0: Y-axis output Data\n*******************************************************************************\/\n#define LIS3DSH_OUT_Y_L_ADDR                 0x2A\n\n\/*******************************************************************************\n*  OUT_Y_H Register: Y-axis Output Acceleration High Data\n*  Read only register\n*  Default value: output\n*  15:8 YD15-YD8: Y-axis output Data\n*******************************************************************************\/\n#define LIS3DSH_OUT_Y_H_ADDR                 0x2B\n\n\/*******************************************************************************\n*  OUT_Z_L Register: Z-axis Output Acceleration Low Data\n*  Read only register\n*  Default value: output\n*  7:0 ZD7-ZD0: Z-axis output Data\n*******************************************************************************\/\n#define LIS3DSH_OUT_Z_L_ADDR                 0x2C\n\n\/*******************************************************************************\n*  OUT_Z_H Register: Z-axis Output Acceleration High Data\n*  Read only register\n*  Default value: output\n*  15:8 ZD15-ZD8: Z-axis output Data\n*******************************************************************************\/\n#define LIS3DSH_OUT_Z_H_ADDR                 0x2D\n\n\/*******************************************************************************\n*  FIFO_CTRL Register: FIFO Control Register\n*  Read\/Write register\n*  Default value: 0x00\n*  7:5 FMODE2-FMODE0: FIFO mode\n*        FMODE2 | FMODE1 | FMODE0 | Mode description\n*        --------------------------------------------------\n*          0    |    0   |    0   | Bypass mode. FIFO turned off. (Default)\n*          0    |    0   |    1   | FIFO mode. Stop collecting data when FIFO is full.\n*          0    |    1   |    0   | Stream mode. If the FIFO is full, the new sample overwrites the older one (circular buffer).\n*          0    |    1   |    1   | Stream mode until trigger is de-asserted, then FIFO mode.\n*          1    |    0   |    0   | Bypass mode until trigger is de-asserted, then Stream mode.\n*          1    |    0   |    1   | Not to use.\n*          1    |    1   |    0   | Not to use.\n*          1    |    1   |    1   | Bypass mode until trigger is de-asserted, then FIFO mode.\n*\n*  4:0 WTMP4-WTMP0: FIFO Watermark pointer. It is the FIFO depth when the Watermark is enabled\n*******************************************************************************\/\n#define LIS3DSH_FIFO_CTRL_ADDR               0x2E\n\n\/*******************************************************************************\n*  FIFO_SRC Register: FIFO Source Register\n*  Read only register\n*  Default value: 0x00\n*  7 WTM: Watermark status.\n*         0: FIFO filling is lower than WTM level (Default)\n*         1: FIFO filling is equal or higher than WTM level\n*  6 OVRN_FIFO: Overrun bit status.\n*               0: FIFO is not completely filled (Default)\n*               1: FIFO is completely filled\n*  5 EMPTY: Overrun bit status.\n*           0: FIFO not empty (Default)\n*           1: FIFO empty\n*  4:0 FSS: Number of samples stored in the FIFO - 1\n*******************************************************************************\/\n#define LIS3DSH_FIFO_SRC_ADDR                0x2F\n\n\/*******************************************************************************\n*  ST1_X Register: State Machine 1 Code Registers\n*  Write only register\n*  Default value: 0x00\n*  7:0 ST1_7-ST1_0: State Machine 1 Code Registers\n*******************************************************************************\/\n#define LIS3DSH_ST1_1_ADDR                   0x40\n#define LIS3DSH_ST1_2_ADDR                   0x41\n#define LIS3DSH_ST1_3_ADDR                   0x42\n#define LIS3DSH_ST1_4_ADDR                   0x43\n#define LIS3DSH_ST1_5_ADDR                   0x44\n#define LIS3DSH_ST1_6_ADDR                   0x45\n#define LIS3DSH_ST1_7_ADDR                   0x46\n#define LIS3DSH_ST1_8_ADDR                   0x47\n#define LIS3DSH_ST1_9_ADDR                   0x48\n#define LIS3DSH_ST1_10_ADDR                  0x49\n#define LIS3DSH_ST1_11_ADDR                  0x4A\n#define LIS3DSH_ST1_12_ADDR                  0x4B\n#define LIS3DSH_ST1_13_ADDR                  0x4C\n#define LIS3DSH_ST1_14_ADDR                  0x4D\n#define LIS3DSH_ST1_15_ADDR                  0x4E\n#define LIS3DSH_ST1_16_ADDR                  0x4F\n\n\/*******************************************************************************\n*  TIM4_1 Register: SM1 General Timer 4 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM1 Timer 4 Counter 1 Value\n*******************************************************************************\/\n#define LIS3DSH_TIM4_1_ADDR                  0x50\n\n\/*******************************************************************************\n*  TIM3_1 Register: SM1 General Timer 3 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM1 Timer 3 Counter 1 Value\n*******************************************************************************\/\n#define LIS3DSH_TIM3_1_ADDR                  0x51\n\n\/*******************************************************************************\n*  TIM2_1_L Register: SM1 General Timer 2 Low Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM1 Timer 2 Counter 1 Low Value\n*******************************************************************************\/\n#define LIS3DSH_TIM2_1_L_ADDR                0x52\n\n\/*******************************************************************************\n*  TIM2_1_H Register: SM1 General Timer 2 High Register\n*  Write only register\n*  Default value: 0x00\n*  15:8 TM_15-TM_8: SM1 Timer 2 Counter 1 High Value\n*******************************************************************************\/\n#define LIS3DSH_TIM2_1_H_ADDR                0x53\n\n\/*******************************************************************************\n*  TIM1_1_L Register: SM1 General Timer 1 Low Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM1 Timer 1 Counter 1 Low Value\n*******************************************************************************\/\n#define LIS3DSH_TIM1_1_L_ADDR                0x54\n\n\/*******************************************************************************\n*  TIM1_1_H Register: SM1 General Timer 1 High Register\n*  Write only register\n*  Default value: 0x00\n*  15:8 TM_15-TM_8: SM1 Timer 1 Counter 1 High Value\n*******************************************************************************\/\n#define LIS3DSH_TIM1_1_H_ADDR                0x55\n\n\/*******************************************************************************\n*  THRS2_1 Register: SM1 Threshold Value 1 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 THS7-THS0: SM1 Threshold Value 1\n*******************************************************************************\/\n#define LIS3DSH_THRS2_1_ADDR                 0x56\n\n\/*******************************************************************************\n*  THRS1_1 Register: SM1 Threshold Value 2 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 THS7-THS0: SM1 Threshold Value 2\n*******************************************************************************\/\n#define LIS3DSH_THRS1_1_ADDR                 0x57\n\n\/*******************************************************************************\n*  MASK1_B Register: SM1 Swap Axis and Sign Mask Register\n*  Write only register\n*  Default value: 0x00\n*  7 P_X: X-Axis Positive Motion Detection\n*         0: X+ disabled\n*         1: X+ enabled\n*  6 N_X: X-Axis Negative Motion Detection\n*         0: X- disabled\n*         1: X- enabled\n*  5 P_Y: Y-Axis Positive Motion Detection\n*         0: Y+ disabled\n*         1: Y+ enabled\n*  4 N_Y: Y-Axis Negative Motion Detection\n*         0: Y- disabled\n*         1: Y- enabled\n*  3 P_Z: X-Axis Positive Motion Detection\n*         0: Z+ disabled\n*         1: Z+ enabled\n*  2 N_Z: X-Axis Negative Motion Detection\n*         0: Z- disabled\n*         1: Z- enabled\n*  1 P_V:\n*         0: V+ disabled\n*         1: V+ enabled\n*  0 N_V:\n*         0: V- disabled\n*         1: V- enabled\n*******************************************************************************\/\n#define LIS3DSH_MASK1_B_ADDR                 0x59\n\n\/*******************************************************************************\n*  MASK1_A Register: SM1 Default Axis and Sign Mask Register\n*  Write only register\n*  Default value: 0x00\n*  7 P_X: X-Axis Positive Motion Detection\n*         0: X+ disabled\n*         1: X+ enabled\n*  6 N_X: X-Axis Negative Motion Detection\n*         0: X- disabled\n*         1: X- enabled\n*  5 P_Y: Y-Axis Positive Motion Detection\n*         0: Y+ disabled\n*         1: Y+ enabled\n*  4 N_Y: Y-Axis Negative Motion Detection\n*         0: Y- disabled\n*         1: Y- enabled\n*  3 P_Z: X-Axis Positive Motion Detection\n*         0: Z+ disabled\n*         1: Z+ enabled\n*  2 N_Z: X-Axis Negative Motion Detection\n*         0: Z- disabled\n*         1: Z- enabled\n*  1 P_V:\n*         0: V+ disabled\n*         1: V+ enabled\n*  0 N_V:\n*         0: V- disabled\n*         1: V- enabled\n*******************************************************************************\/\n#define LIS3DSH_MASK1_A_ADDR                 0x5A\n\n\/*******************************************************************************\n*  SETT1 Register: SM1 Detection Settings Register\n*  Write only register\n*  Default value: 0x00\n*  7 P_DET: SM1 peak detection bit\n*           0: peak detection disabled (Default)\n*           1: peak detection enabled\n*  6 THR3_SA:\n*             0: no action (Default)\n*             1: threshold 3 enabled for axis and sign mask reset (MASK1_B)\n*  5 ABS:\n*         0: unsigned thresholds THRSx (Default)\n*         1: signed thresholds THRSx\n*  4 Reserved\n*  3 Reserved\n*  2 THR3_MA:\n*             0: no action (Default)\n*             1: threshold 3 enabled for axis and sign mask reset (MASK1_A)\n*  1 R_TAM: Next condition validation flag\n*           0: mask frozen on the axis that triggers the condition (Default)\n*           1: standard mask always evaluated\n*  0 SITR:\n*          0: no actions (Default)\n*          1: STOP and CONT commands generate an interrupt and perform output\n*             actions as OUTC command.\n*******************************************************************************\/\n#define LIS3DSH_SETT1_ADDR                   0x5B\n\n\/*******************************************************************************\n*  PR1 Register: SM1 Program and Reset Pointers Register\n*  Read only register\n*  Default value: 0x00\n*  7:4 PP3-PP0: SM1 program pointer address\n*  3:0 RP3-RP0: SM1 reset pointer address\n*******************************************************************************\/\n#define LIS3DSH_PR1_ADDR                     0x5C\n\n\/*******************************************************************************\n*  TC1_L Register: SM1 General Timer Counter Low Register\n*  Read only register\n*  Default value: 0x00\n*  7:0 TC1_7-TC1_0: SM1 General Timer Counter Low Value\n*******************************************************************************\/\n#define LIS3DSH_TC1_L_ADDR                   0x5D\n\n\/*******************************************************************************\n*  TC1_H Register: SM1 General Timer Counter High Register\n*  Read only register\n*  Default value: 0x00\n*  15:8 TC1_15-TC1_8: SM1 General Timer Counter High Value\n*******************************************************************************\/\n#define LIS3DSH_TC1_H_ADDR                   0x5E\n\n\/*******************************************************************************\n*  OUTS1 Register: SM1 Output Set Flag Register\n*  Read only register\n*  Default value: 0x00\n*  7 P_X:\n*         0: X+ noshow\n*         1: X+ show\n*  6 N_X:\n*         0: X- noshow\n*         1: X- show\n*  5 P_Y:\n*         0: Y+ noshow\n*         1: Y+ show\n*  4 N_Y:\n*         0: Y- noshow\n*         1: Y- show\n*  3 P_Z:\n*         0: Z+ noshow\n*         1: Z+ show\n*  2 N_Z:\n*         0: Z- noshow\n*         1: Z- show\n*  1 P_V:\n*         0: V+ noshow\n*         1: V+ show\n*  0 N_V:\n*         0: V- noshow\n*         1: V- show\n*******************************************************************************\/\n#define LIS3DSH_OUTS1_ADDR                   0x5F\n\n\/*******************************************************************************\n*  ST2_X Register: State Machine 2 Code Registers\n*  Write only register\n*  Default value: 0x00\n*  7:0 ST2_7-ST2_0: State Machine 2 Code Registers\n*******************************************************************************\/\n#define LIS3DSH_ST2_1_ADDR                   0x60\n#define LIS3DSH_ST2_2_ADDR                   0x61\n#define LIS3DSH_ST2_3_ADDR                   0x62\n#define LIS3DSH_ST2_4_ADDR                   0x63\n#define LIS3DSH_ST2_5_ADDR                   0x64\n#define LIS3DSH_ST2_6_ADDR                   0x65\n#define LIS3DSH_ST2_7_ADDR                   0x66\n#define LIS3DSH_ST2_8_ADDR                   0x67\n#define LIS3DSH_ST2_9_ADDR                   0x68\n#define LIS3DSH_ST2_10_ADDR                  0x69\n#define LIS3DSH_ST2_11_ADDR                  0x6A\n#define LIS3DSH_ST2_12_ADDR                  0x6B\n#define LIS3DSH_ST2_13_ADDR                  0x6C\n#define LIS3DSH_ST2_14_ADDR                  0x6D\n#define LIS3DSH_ST2_15_ADDR                  0x6E\n#define LIS3DSH_ST2_16_ADDR                  0x6F\n\n\/*******************************************************************************\n*  TIM4_2 Register: SM2 General Timer 4 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM2 Timer 4 Counter 1 Value\n*******************************************************************************\/\n#define LIS3DSH_TIM4_2_ADDR                  0x70\n\n\/*******************************************************************************\n*  TIM3_2 Register: SM2 General Timer 3 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM2 Timer 3 Counter 2 Value\n*******************************************************************************\/\n#define LIS3DSH_TIM3_2_ADDR                  0x71\n\n\/*******************************************************************************\n*  TIM2_2_L Register: SM2 General Timer 2 Low Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM2 Timer 2 Counter 2 Low Value\n*******************************************************************************\/\n#define LIS3DSH_TIM2_2_L_ADDR                0x72\n\n\/*******************************************************************************\n*  TIM2_2_H Register: SM2 General Timer 2 High Register\n*  Write only register\n*  Default value: 0x00\n*  15:8 TM_15-TM_8: SM2 Timer 2 Counter 2 High Value\n*******************************************************************************\/\n#define LIS3DSH_TIM2_2_H_ADDR                0x73\n\n\/*******************************************************************************\n*  TIM1_2_L Register: SM2 General Timer 1 Low Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 TM_7-TM_0: SM2 Timer 1 Counter 2 Low Value\n*******************************************************************************\/\n#define LIS3DSH_TIM1_2_L_ADDR                0x74\n\n\/*******************************************************************************\n*  TIM1_2_H Register: SM2 General Timer 1 High Register\n*  Write only register\n*  Default value: 0x00\n*  15:8 TM_15-TM_8: SM2 Timer 1 Counter 2 High Value\n*******************************************************************************\/\n#define LIS3DSH_TIM1_2_H_ADDR                0x75\n\n\/*******************************************************************************\n*  THRS2_2 Register: SM2 Threshold Value 1 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 THS7-THS0: SM2 Threshold Value\n*******************************************************************************\/\n#define LIS3DSH_THRS2_2_ADDR                 0x76\n\n\/*******************************************************************************\n*  THRS1_2 Register: SM2 Threshold Value 2 Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 THS7-THS0: SM2 Threshold Value\n*******************************************************************************\/\n#define LIS3DSH_THRS1_2_ADDR                 0x77\n\n\/*******************************************************************************\n*  DES2 Register: SM2 Decimation Counter Value Register\n*  Write only register\n*  Default value: 0x00\n*  7:0 D7-D0: SM2 Decimation Counter Value\n*******************************************************************************\/\n#define LIS3DSH_DES2_ADDR                    0x78\n\n\/*******************************************************************************\n*  MASK2_B Register: SM2 Axis and Sign Mask Register\n*  Write only register\n*  Default value: 0x00\n*  7 P_X: X-Axis Positive Motion Detection\n*         0: X+ disabled\n*         1: X+ enabled\n*  6 N_X: X-Axis Negative Motion Detection\n*         0: X- disabled\n*         1: X- enabled\n*  5 P_Y: Y-Axis Positive Motion Detection\n*         0: Y+ disabled\n*         1: Y+ enabled\n*  4 N_Y: Y-Axis Negative Motion Detection\n*         0: Y- disabled\n*         1: Y- enabled\n*  3 P_Z: X-Axis Positive Motion Detection\n*         0: Z+ disabled\n*         1: Z+ enabled\n*  2 N_Z: X-Axis Negative Motion Detection\n*         0: Z- disabled\n*         1: Z- enabled\n*  1 P_V:\n*         0: V+ disabled\n*         1: V+ enabled\n*  0 N_V:\n*         0: V- disabled\n*         1: V- enabled\n*******************************************************************************\/\n#define LIS3DSH_MASK2_B_ADDR                 0x79\n\n\/*******************************************************************************\n*  MASK2_A Register: SM2 Axis and Sign Mask Register\n*  Write only register\n*  Default value: 0x00\n*  7 P_X: X-Axis Positive Motion Detection\n*         0: X+ disabled\n*         1: X+ enabled\n*  6 N_X: X-Axis Negative Motion Detection\n*         0: X- disabled\n*         1: X- enabled\n*  5 P_Y: Y-Axis Positive Motion Detection\n*         0: Y+ disabled\n*         1: Y+ enabled\n*  4 N_Y: Y-Axis Negative Motion Detection\n*         0: Y- disabled\n*         1: Y- enabled\n*  3 P_Z: X-Axis Positive Motion Detection\n*         0: Z+ disabled\n*         1: Z+ enabled\n*  2 N_Z: X-Axis Negative Motion Detection\n*         0: Z- disabled\n*         1: Z- enabled\n*  1 P_V:\n*         0: V+ disabled\n*         1: V+ enabled\n*  0 N_V:\n*         0: V- disabled\n*         1: V- enabled\n*******************************************************************************\/\n#define LIS3DSH_MASK2_A_ADDR                 0x7A\n\n\/*******************************************************************************\n*  SETT2 Register: SM2 Detection Settings Register\n*  Write only register\n*  Default value: 0x00\n*  7 P_DET: SM2 peak detection\n*           0: peak detection disabled (Default)\n*           1: peak detection enabled\n*  6 THR3_SA:\n*             0: no action (Default)\n*             1: threshold 3 limit value for axis and sign mask reset (MASK2_B)\n*  5 ABS:\n*         0: unsigned thresholds (Default)\n*         1: signed thresholds\n*  4 RADI:\n*          0: raw data\n*          1: diff data for State Machine 2\n*  3 D_CS:\n*          0: DIFF2 enabled (difference between current data and previous data)\n*          1: constant shift enabled (difference between current data and constant values)\n*  2 THR3_MA:\n*             0: no action (Default)\n*             1: threshold 3 enabled for axis and sign mask reset (MASK2_A)\n*  1 R_TAM: Next condition validation flag\n*           0: mask frozen on the axis that triggers the condition (Default)\n*           1: standard mask always evaluated\n*  0 SITR:\n*          0: no actions (Default)\n*          1: STOP and CONT commands generate an interrupt and perform output\n*             actions as OUTC command.\n*******************************************************************************\/\n#define LIS3DSH_SETT2_ADDR                   0x7B\n\n\/*******************************************************************************\n*  PR2 Register: SM2 Program and Reset Pointers Register\n*  Read only register\n*  Default value: 0x00\n*  7:4 PP3-PP0: SM1 program pointer address\n*  3:0 RP3-RP0: SM1 reset pointer address\n*******************************************************************************\/\n#define LIS3DSH_PR2_ADDR                     0x7C\n\n\/*******************************************************************************\n*  TC2_L Register: SM2 General Timer Counter Low Register\n*  Read only register\n*  Default value: 0x00\n*  7:0 TC2_7-TC2_0: SM2 General Timer Counter Low Value\n*******************************************************************************\/\n#define LIS3DSH_TC2_L_ADDR                   0x7D\n\n\/*******************************************************************************\n*  TC2_H Register: SM2 General Timer Counter High Register\n*  Read only register\n*  Default value: 0x00\n*  15:8 TC2_15-TC2_8: SM2 General Timer Counter High Value\n*******************************************************************************\/\n#define LIS3DSH_TC2_H_ADDR                   0x7E\n\n\/*******************************************************************************\n*  OUTS2 Register: SM2 Output Set Flag Register\n*  Read only register\n*  Default value: 0x00\n*  7 P_X:\n*         0: X+ noshow\n*         1: X+ show\n*  6 N_X:\n*         0: X- noshow\n*         1: X- show\n*  5 P_Y:\n*         0: Y+ noshow\n*         1: Y+ show\n*  4 N_Y:\n*         0: Y- noshow\n*         1: Y- show\n*  3 P_Z:\n*         0: Z+ noshow\n*         1: Z+ show\n*  2 N_Z:\n*         0: Z- noshow\n*         1: Z- show\n*  1 P_V:\n*         0: V+ noshow\n*         1: V+ show\n*  0 N_V:\n*         0: V- noshow\n*         1: V- show\n*******************************************************************************\/\n#define LIS3DSH_OUTS2_ADDR                   0x7F\n\n\/******************************************************************************\/\n\/**************************** END REGISTER MAPPING  ***************************\/\n\/******************************************************************************\/\n#define I_AM_LIS3DSH                         0x3F\n\n\/** @defgroup Sensitivity_selection\n  * @{\n  *\/\n#define LIS3DSH_SENSITIVITY_0_06G            0.06  \/* 0.06 mg\/digit*\/\n#define LIS3DSH_SENSITIVITY_0_12G            0.12  \/* 0.12 mg\/digit*\/\n#define LIS3DSH_SENSITIVITY_0_18G            0.18  \/* 0.18 mg\/digit*\/\n#define LIS3DSH_SENSITIVITY_0_24G            0.24  \/* 0.24 mg\/digit*\/\n#define LIS3DSH_SENSITIVITY_0_73G            0.73  \/* 0.73 mg\/digit*\/\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Data_Rate_Power_Mode_selection\n  * @{\n  *\/\n#define LIS3DSH_DATARATE_POWERDOWN           ((uint8_t)0x00)  \/* Power Down Mode*\/\n#define LIS3DSH_DATARATE_3_125               ((uint8_t)0x10)  \/* 3.125 Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_6_25                ((uint8_t)0x20)  \/* 6.25  Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_12_5                ((uint8_t)0x30)  \/* 12.5  Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_25                  ((uint8_t)0x40)  \/* 25    Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_50                  ((uint8_t)0x50)  \/* 50    Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_100                 ((uint8_t)0x60)  \/* 100   Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_400                 ((uint8_t)0x70)  \/* 400   Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_800                 ((uint8_t)0x80)  \/* 800   Hz Normal Mode *\/\n#define LIS3DSH_DATARATE_1600                ((uint8_t)0x90)  \/* 1600  Hz Normal Mode *\/\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Full_Scale_selection\n  * @{\n  *\/\n#define LIS3DSH_FULLSCALE_2                  ((uint8_t)0x00)  \/* 2 g  *\/\n#define LIS3DSH_FULLSCALE_4                  ((uint8_t)0x08)  \/* 4 g  *\/\n#define LIS3DSH_FULLSCALE_6                  ((uint8_t)0x10)  \/* 6 g  *\/\n#define LIS3DSH_FULLSCALE_8                  ((uint8_t)0x18)  \/* 8 g  *\/\n#define LIS3DSH_FULLSCALE_16                 ((uint8_t)0x20)  \/* 16 g *\/\n#define LIS3DSH__FULLSCALE_SELECTION         ((uint8_t)0x38)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Anti-Aliasing_Filter_Bandwidth\n  * @{\n  *\/\n#define LIS3DSH_FILTER_BW_800                ((uint8_t)0x00)  \/* 800 Hz *\/\n#define LIS3DSH_FILTER_BW_40                 ((uint8_t)0x08)  \/* 40 Hz  *\/\n#define LIS3DSH_FILTER_BW_200                ((uint8_t)0x10)  \/* 200 Hz *\/\n#define LIS3DSH_FILTER_BW_50                 ((uint8_t)0x18)  \/* 50 Hz  *\/\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Self_Test_selection\n  * @{\n  *\/\n#define LIS3DSH_SELFTEST_NORMAL              ((uint8_t)0x00)\n#define LIS3DSH_SELFTEST_P                   ((uint8_t)0x02)\n#define LIS3DSH_SELFTEST_M                   ((uint8_t)0x04)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Direction_XYZ_selection\n  * @{\n  *\/\n#define LIS3DSH_X_ENABLE                     ((uint8_t)0x01)\n#define LIS3DSH_Y_ENABLE                     ((uint8_t)0x02)\n#define LIS3DSH_Z_ENABLE                     ((uint8_t)0x04)\n#define LIS3DSH_XYZ_ENABLE                   ((uint8_t)0x07)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup SPI_Serial_Interface_Mode_selection\n  * @{\n  *\/\n#define LIS3DSH_SERIALINTERFACE_4WIRE        ((uint8_t)0x00)\n#define LIS3DSH_SERIALINTERFACE_3WIRE        ((uint8_t)0x01)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Interrupt_Request_selection    Interrupt_Selection_Enable\n  * @{\n  *\/\n#define LIS3DSH_INTERRUPT_REQUEST_LATCHED    ((uint8_t)0x00)\n#define LIS3DSH_INTERRUPT_REQUEST_PULSED     ((uint8_t)0x20)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Interrupt_Selection_Enable\n  * @{\n  *\/\n#define LIS3DSH_INTERRUPT_1_ENABLE           ((uint8_t)0x88)\n#define LIS3DSH_INTERRUPT_2_ENABLE           ((uint8_t)0x00)\n#define LIS3DSH_INTERRUPT_1_2_ENABLE         ((uint8_t)0x88)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Interrupt_Signal_Active_Selection\n  * @{\n  *\/\n#define LIS3DSH_INTERRUPT_SIGNAL_LOW         ((uint8_t)0x00)\n#define LIS3DSH_INTERRUPT_SIGNAL_HIGH        ((uint8_t)0x40)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup State_Machine_Enable\n  * @{\n  *\/\n#define LIS3DSH_SM_ENABLE                   ((uint8_t)0x01)\n#define LIS3DSH_SM_DISABLE                  ((uint8_t)0x00)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup State_Machine_Routed_Interrupt\n  * @{\n  *\/\n#define LIS3DSH_SM_INT1                     ((uint8_t)0x00)\n#define LIS3DSH_SM_INT2                     ((uint8_t)0x08)\n\/**\n  * @}\n  *\/\n\n\/** @defgroup Boot_Mode_selection\n  * @{\n  *\/\n#define LIS3DSH_BOOT_NORMALMODE              ((uint8_t)0x00)\n#define LIS3DSH_BOOT_FORCED                  ((uint8_t)0x80)\n\/**\n  * @}\n  *\/\n\n\n\/** @defgroup FIFO_Mode_selection\n  * @{\n  *\/\n#define LIS3DSH_FIFO_BYPASS_MODE             ((uint8_t)0x00)\n#define LIS3DSH_FIFO_MODE                    ((uint8_t)0x20)\n#define LIS3DSH_FIFO_STREAM_MODE             ((uint8_t)0x40)\n#define LIS3DSH_FIFO_SF_TRIGGER_MODE         ((uint8_t)0x60)\n#define LIS3DSH_FIFO_BS_TRIGGER_MODE         ((uint8_t)0x80)\n#define LIS3DSH_FIFO_BF_TRIGGER_MODE         ((uint8_t)0xE0)\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Declare the following structure for the initialization sequence:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef struct\n{\n  uint8_t Power_Mode;                         \/* Power-down\/Active Mode *\/\n  uint8_t Output_DataRate;                    \/* OUT data rate 100 Hz \/ 400 Hz *\/\n  uint8_t Axes_Enable;                        \/* Axes enable *\/\n  uint8_t Full_Scale;                         \/* Full scale *\/\n  uint8_t Self_Test;                          \/* Self test *\/\n}LIS302DL_InitTypeDef;\n\n\/* Interrupt struct *\/\ntypedef struct\n{\n  uint8_t Latch_Request;                      \/* Latch interrupt request into CLICK_SRC register*\/\n  uint8_t SingleClick_Axes;                   \/* Single Click Axes Interrupts *\/\n  uint8_t DoubleClick_Axes;                   \/* Double Click Axes Interrupts *\/\n}LIS302DL_InterruptConfigTypeDef;\n\n\/* High Pass Filter struct *\/\ntypedef struct\n{\n  uint8_t HighPassFilter_Data_Selection;      \/* Internal filter bypassed or data from internal filter send to output register*\/\n  uint8_t HighPassFilter_CutOff_Frequency;    \/* High pass filter cut-off frequency *\/\n  uint8_t HighPassFilter_Interrupt;           \/* High pass filter enabled for Freefall\/WakeUp #1 or #2 *\/\n}LIS302DL_FilterConfigTypeDef;\n\ntypedef struct\n{\n  uint8_t Output_DataRate;              \/* Output Data Rate into CTRL_REG4 register *\/\n  uint8_t Axes_Enable;                  \/* Axes enable into CTRL_REG4 register *\/\n  uint8_t SPI_Wire;                     \/* SPI Wire into CTRL_REG5 register *\/\n  uint8_t Self_Test;                    \/* Self test into CTRL_REG5 register *\/\n  uint8_t Full_Scale;                   \/* Full scale into CTRL_REG5 register *\/\n  uint8_t Filter_BW;                    \/* Filter BW into CTRL_REG5 register *\/\n}LIS3DSH_InitTypeDef;\n<\/pre><\/div>\n\n\n\n<p>Declare the following functions:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void LIS3DSH_IO_Init(void);\nvoid LIS3DSH_Init(void);\nvoid LIS3DSH_GetXYZ(float *x_value,float *y_value,float *z_value);\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Don&#8217;t forget to add the header guard as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef LIS3DSH_H_\n#define LIS3DSH_H_\n\n\n#endif \/* LIS3DSH_H_ *\/<\/pre><\/div>\n\n\n\n<p>And add all the above within the header guard.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the header file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In the source file, include the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;LIS3DSH.h&quot;\n#include &quot;stm32f4xx.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare the following variable:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t scale;<\/pre><\/div>\n\n\n\n<p>This variable shall store the scale needed to calculate the acceleration in each direction.<\/p>\n\n\n\n<p>Next, we shall initialize the sensor IO as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void LIS3DSH_IO_Init(void)\n{\n\n\t\/*SPI Pins*\/\n\n\t#define AF05 0x05\n\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN; \/\/enable clock for GPIOA\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE5_1|GPIO_MODER_MODE6_1|GPIO_MODER_MODE7_1; \/\/set PA5, PA6 and PA7 to alternate function mode\n\tGPIOA-&gt;MODER &amp;=~(GPIO_MODER_MODE5_0|GPIO_MODER_MODE6_0|GPIO_MODER_MODE7_0);\n\n\tGPIOA-&gt;AFR[0]|=(AF05&lt;&lt;GPIO_AFRL_AFSEL5_Pos)|(AF05&lt;&lt;GPIO_AFRL_AFSEL6_Pos)|(AF05&lt;&lt;GPIO_AFRL_AFSEL7_Pos);\n\n\t\/*CS Pin*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOEEN;\n\n\tGPIOE-&gt;MODER|=GPIO_MODER_MODE3_0;\n\tGPIOE-&gt;MODER&amp;=~GPIO_MODER_MODE3_1;\n\n\t\/*Set PE3 to high*\/\n\tGPIOE-&gt;BSRR|=GPIO_BSRR_BS3;\n\n\t\/*SPI Configuration*\/\n\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_SPI1EN;\n\n\tSPI1-&gt;CR1|=SPI_CR1_SSM|SPI_CR1_BR_0|SPI_CR1_SSI|SPI_CR1_MSTR|SPI_CR1_SPE;\n\n}\n<\/pre><\/div>\n\n\n\n<p>For more information how to configure the SPI, please refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=466\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=466\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a>.<\/p>\n\n\n\n<p>Note: Select the baudrate that shall not exceed the 10MHz since this is maximum can be handled by the sensor.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"532\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-1024x532.jpg\" alt=\"\" class=\"wp-image-2549\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-1024x532.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-300x156.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-768x399.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-1536x797.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-1150x597.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-750x389.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-400x208.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03-250x130.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-22-03.jpg 1930w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, we need the following functions to read\/write from\/to SPI bus:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void SPI1_Write(uint8_t *data, uint32_t size)\n{\n\tuint32_t i=0;\n\n\twhile(i&lt;size)\n\t{\n\t\t\/*Wait until TXE is set*\/\n\t\twhile(!(SPI1-&gt;SR &amp; (SPI_SR_TXE))){}\n\n\t\t\/*Write the data to the data register*\/\n\t\tSPI1-&gt;DR = data[i];\n\t\ti++;\n\t}\n\t\/*Wait until TXE is set*\/\n\twhile(!(SPI1-&gt;SR &amp; (SPI_SR_TXE))){}\n\n\t\/*Wait for BUSY flag to reset*\/\n\twhile((SPI1-&gt;SR &amp; (SPI_SR_BSY))){}\n\n\t\/*Clear OVR flag*\/\n\t(void)SPI1-&gt;DR;\n\t(void)SPI1-&gt;SR;\n}\n\nstatic void SPI1_Read(uint8_t *data,uint32_t size)\n{\n\twhile(size)\n\t{\n\t\t\/*Send dummy data*\/\n\t\tSPI1-&gt;DR =0;\n\n\t\t\/*Wait for RXNE flag to be set*\/\n\t\twhile(!(SPI1-&gt;SR &amp; (SPI_SR_RXNE))){}\n\n\t\t\/*Read data from data register*\/\n\t\t*data++ = (SPI1-&gt;DR);\n\t\tsize--;\n\t}\n}\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, we need the following functions that are specific for the sensor:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void LIS3DSH_IO_Write(uint8_t reg,uint8_t data)\n{\n\t\/*set both the reg and the data as single array*\/\n\tuint8_t temp[2]={reg,data};\n\t\/*Set CS Line to low*\/\n\tGPIOE-&gt;BSRR|=GPIO_BSRR_BR3;\n\t\/*Write the data*\/\n\tSPI1_Write(temp,2);\n\t\/*Set PE3 to high*\/\n\tGPIOE-&gt;BSRR|=GPIO_BSRR_BS3;\n}\n\nstatic void LIS3DSH_IO_Read(uint8_t MemoryAddress, uint8_t *data, uint32_t size)\n{\n\t\/*Set CS Line to low*\/\n\tGPIOE-&gt;BSRR|=GPIO_BSRR_BR3;\n\n\tuint8_t address=MemoryAddress|0x80;\n\n\t\/*Write the register*\/\n\tSPI1_Write(&amp;address,1);\n\n\t\/*Write the data*\/\n\tSPI1_Read(data,size);\n\t\/*Set PE3 to high*\/\n\tGPIOE-&gt;BSRR|=GPIO_BSRR_BS3;\n}\n<\/pre><\/div>\n\n\n\n<p>If you noticed, when the address being OR with 0x80. Since this sensor can work with two different buses (i2c and SPI).<\/p>\n\n\n\n<p>In order for the sensor to distinguish between read and write operation, the MSB shall be set to 0 when writing and 1 when reading as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"560\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-1024x560.jpg\" alt=\"\" class=\"wp-image-2550\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-1024x560.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-300x164.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-768x420.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-1536x840.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-2048x1121.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-1150x629.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-750x410.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-400x219.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-25_20-15-21-250x137.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For the sensor initialization:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void LIS3DSH_Init(void)\n{\n\tuint16_t ctrl = 0x0000;\n\tLIS302DL_InitTypeDef         lis302dl_initstruct;\n\tLIS302DL_FilterConfigTypeDef lis302dl_filter = {0,0,0};\n\tLIS3DSH_InitTypeDef          l1s3dsh_InitStruct;\n\n    l1s3dsh_InitStruct.Output_DataRate = LIS3DSH_DATARATE_100;\n    l1s3dsh_InitStruct.Axes_Enable = LIS3DSH_XYZ_ENABLE;\n    l1s3dsh_InitStruct.SPI_Wire = LIS3DSH_SERIALINTERFACE_4WIRE;\n    l1s3dsh_InitStruct.Self_Test = LIS3DSH_SELFTEST_NORMAL;\n    l1s3dsh_InitStruct.Full_Scale = LIS3DSH_FULLSCALE_2;\n    l1s3dsh_InitStruct.Filter_BW = LIS3DSH_FILTER_BW_800;\n\n    scale=l1s3dsh_InitStruct.Full_Scale;\n\n    \/* Configure MEMS: power mode(ODR) and axes enable *\/\n    ctrl = (uint16_t) (l1s3dsh_InitStruct.Output_DataRate | \\\n                       l1s3dsh_InitStruct.Axes_Enable);\n\n    LIS3DSH_IO_Write(LIS3DSH_CTRL_REG4_ADDR,ctrl);\n\n    \/* Configure MEMS: full scale and self test *\/\n    ctrl = (uint16_t) ((l1s3dsh_InitStruct.SPI_Wire    | \\\n                         l1s3dsh_InitStruct.Self_Test   | \\\n                         l1s3dsh_InitStruct.Full_Scale  | \\\n                         l1s3dsh_InitStruct.Filter_BW) &lt;&lt; 8);\n\n    LIS3DSH_IO_Write(LIS3DSH_CTRL_REG5_ADDR,ctrl);\n}<\/pre><\/div>\n\n\n\n<p>Feel free to modify according to your needs and add interrupt if you want.<\/p>\n\n\n\n<p>Note: Refer to the datasheet for information about each register.<\/p>\n\n\n\n<p>For getting the sensor values:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void LIS3DSH_GetXYZ(float *x_value,float *y_value,float *z_value)\n{\n\n\tint8_t xyz_data[6];\n\n\tfloat sensitivity;\n\n\tLIS3DSH_IO_Read(LIS3DSH_OUT_X_L_ADDR,xyz_data,6);\n\n\tswitch (scale)\n\t{\n\t\tcase LIS3DSH_FULLSCALE_2:  sensitivity=LIS3DSH_SENSITIVITY_0_06G; break;\n\t\tcase LIS3DSH_FULLSCALE_4:  sensitivity=LIS3DSH_SENSITIVITY_0_12G; break;\n\t\tcase LIS3DSH_FULLSCALE_6:  sensitivity=LIS3DSH_SENSITIVITY_0_18G; break;\n\t\tcase LIS3DSH_FULLSCALE_8:  sensitivity=LIS3DSH_SENSITIVITY_0_24G; break;\n\t\tcase LIS3DSH_FULLSCALE_16: sensitivity=LIS3DSH_SENSITIVITY_0_73G; break;\n\n\t}\n\n\n\tint16_t temp;\n\ttemp= ((xyz_data[1] &lt;&lt; 8) + xyz_data[0]);\n\t*x_value=  ((temp*sensitivity));\n\n\ttemp= ((xyz_data[3] &lt;&lt; 8) + xyz_data[2]);\n\t*y_value=  ((temp*sensitivity*-1));\n\n\ttemp= ((xyz_data[5] &lt;&lt; 8) + xyz_data[4]);\n\t*z_value =  ((temp*sensitivity));\n\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;delay.h&quot;\n#include &quot;stdlib.h&quot;\n#include &quot;LIS3DSH.h&quot;\n\nfloat x,y,z;\n\n\nint main(void)\n{\n\n\tdelay_init(16000000);\n\tLIS3DSH_IO_Init();\n\tLIS3DSH_Init();\n\twhile(1)\n\t{\n\t\tLIS3DSH_GetXYZ(&amp;x,&amp;y,&amp;z);\n\t\tdelay(10);\n\t}\n\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Build the project and start a debug session.<\/p>\n\n\n\n<p>Within the debug session, live expression, add the x, y and z variable as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"890\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-890x1024.jpg\" alt=\"\" class=\"wp-image-2551\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-890x1024.jpg 890w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-261x300.jpg 261w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-768x883.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-1335x1536.jpg 1335w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-1150x1323.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-750x863.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-400x460.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27-250x288.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/05\/2024-05-26_07-31-27.jpg 1344w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/figure>\n\n\n\n<p>You will get the results.<\/p>\n\n\n\n<p>Note: the values are in mg (milli g) where 1000 represents 1G (9.81 m\/s^2).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall take a look at the LIS3DSH Accelerometer that embedded within the STM32F407 discovery and how to read the acceleration data. In this guide, we shall cover the following: LIS3DSH Sensor. Enable the float point hardware. Interfacing the sensor. Results. 1. LIS3DSH Sensor: Features Wide supply voltage, 1.71 V to 3.6 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-2545","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2545"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2545"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2545\/revisions"}],"predecessor-version":[{"id":2552,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2545\/revisions\/2552"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}