{"id":3019,"date":"2024-11-19T14:09:10","date_gmt":"2024-11-19T14:09:10","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3019"},"modified":"2024-11-19T14:09:12","modified_gmt":"2024-11-19T14:09:12","slug":"how-to-create-a-library-for-sensors-part-3-adding-interrupt-capability","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3019","title":{"rendered":"How to Create a Library for sensors Part 3:\u00a0Adding Interrupt capability"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1.webp\" alt=\"\" class=\"wp-image-3020\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1.webp 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1-768x768.webp 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1-750x750.webp 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1-400x400.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2024-10-26-15.42.39-A-detailed-and-educational-illustration-representing-the-development-of-a-library-for-an-ADXL345-accelerometer-sensor-using-I2C-communication-on-an-ST-1-250x250.webp 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<p>In the third part of creating a library for ADXL345, we shall study the interrupt capabilities of ADXl345 and improve the driver to utilitize the usage of the interrupt of STM32.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Interrupts of ADXL345.<\/li>\n\n\n\n<li>Header file development.<\/li>\n\n\n\n<li>Source file development.<\/li>\n\n\n\n<li>Enabling interrupt in STM32CubeMX.<\/li>\n\n\n\n<li>Connection.<\/li>\n\n\n\n<li>Main code.<\/li>\n\n\n\n<li>Code download.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Interrupts of ADXL345:<\/h2>\n\n\n\n<p>ADXL345 features multiple interrupt source such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data ready.<\/li>\n\n\n\n<li>Single and double tap.<\/li>\n\n\n\n<li>Activity and inactivity.<\/li>\n\n\n\n<li>Freefall.<\/li>\n\n\n\n<li>Watermark.<\/li>\n\n\n\n<li>Overrun.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-860x1024.jpg\" alt=\"\" class=\"wp-image-3021\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-860x1024.jpg 860w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-252x300.jpg 252w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-768x914.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-1290x1536.jpg 1290w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-1720x2048.jpg 1720w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-1150x1369.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-750x893.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-400x476.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58-250x298.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-26-58.jpg 1762w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall focus on using data ready interrupt generation.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The interrupts can be enabled using INT Enable register (0x2E) as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"535\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-1024x535.jpg\" alt=\"\" class=\"wp-image-3022\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-1024x535.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-300x157.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-768x402.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-1536x803.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-1150x601.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-750x392.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-400x209.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20-250x131.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-31-20.jpg 1974w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>When a specific bit is set to, the corespondent interrupt shall be enabled. Since this guide is focused on data ready interrupt, we shall use set D7 (DATA_Ready) to 1.<\/p>\n\n\n\n<p>Also, the ADXL345 features two interrupt pins INT1 and INT2 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"398\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-1024x398.jpg\" alt=\"\" class=\"wp-image-3023\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-1024x398.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-300x117.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-768x299.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-1536x597.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-2048x796.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-1150x447.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-750x292.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-400x156.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-34-10-250x97.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The selection of which pin can be done INT_MAP (0x2F) register as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"423\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-1024x423.jpg\" alt=\"\" class=\"wp-image-3024\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-1024x423.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-300x124.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-768x317.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-1150x475.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-750x310.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-400x165.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12-250x103.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-36-12.jpg 1274w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For the state, either high or low can be used by setting INT_INVERT bit in data format register as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"152\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-1024x152.jpg\" alt=\"\" class=\"wp-image-3025\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-1024x152.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-300x45.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-768x114.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-1150x171.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-750x112.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-400x59.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19-250x37.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-37-19.jpg 1264w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This guide shall use high state which means the bit shall remain zero.<\/p>\n\n\n\n<p>Next, we shall start with developing the library<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. Header File Development:<\/h2>\n\n\n\n<p>In adxl345.h header file, in the ADXL345_InitParam structure, we shall include the following two parameters:<\/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;}\">\tuint8_t InterruptEnable;\n\tuint8_t DataReadyInterrupt;<\/pre><\/div>\n\n\n\n<p>Hence, the updated data structure 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;}\">typedef struct\n{\n\tuint8_t Accel_Range;\n\tuint8_t InterruptEnable;\n\tuint8_t DataReadyInterrupt;\n\t\/*rest of the interrupt shall be implement by the user*\/\n}ADXL345_InitParam;<\/pre><\/div>\n\n\n\n<p>Since the guide focuses on data ready, the rest the can be implemented by the user.<\/p>\n\n\n\n<p>Declare the following enum to handle the interrupt enabling 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;}\">typedef enum\n{\n\tInterrupt_Disabled=0,\n\tInterrupt_Enabled=1\n}adxl345InterruptsEnable;<\/pre><\/div>\n\n\n\n<p>Also, declare the following enumeration to handle the interrupt sources 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;}\">typedef enum\n{\n\tInt_DataReady=(1&lt;&lt;7),\n\tInt_SingleTap=(1&lt;&lt;6),\n\tInt_DoubleTap=(1&lt;&lt;5),\n\tInt_Activity=(1&lt;&lt;4),\n\tInt_Inactivity=(1&lt;&lt;3),\n\tInt_FreeFall=(1&lt;&lt;2),\n\tInt_Watermark=(1&lt;&lt;1),\n\tInt_Overrun=(1&lt;&lt;0)\n}adxl345InterruptsSource;<\/pre><\/div>\n\n\n\n<p>Also, declare 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;}\">adxl345InterruptsSource adxl345_ReadInterrupt(void);<\/pre><\/div>\n\n\n\n<p>This function shall read the interrupt register returns which interrupt is triggered.<\/p>\n\n\n\n<p>Save the header file since thats all for the header file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Source File Development:<\/h2>\n\n\n\n<p>In adxl345.c source file, in the <strong><em><strong>adxl345_Init<\/strong><\/em><\/strong> function, we shall enable the interrupt as following:<\/p>\n\n\n\n<p>After entering the measurement mode, check if the interrupt is enabled or not 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;}\">if(Param-&gt;InterruptEnable==Interrupt_Enabled)<\/pre><\/div>\n\n\n\n<p>If the interrupt is enabled, set the interrupt as the user requirement 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;}\">\t\ttemp[0]=0x00;\n\t\tif(Param-&gt;DataReadyInterrupt==Int_DataReady)\n\t\t{\n\t\t\ttemp[0]|=Int_DataReady;\n\n\t\t}\n\t\t\/*The rest shall be implement by the user\n\t\t * temp[0]|=Int_SingleTap\n\t\t * *\/\n\t\tadxl345_write(ADXL345_INT_ENABLE,temp,1);<\/pre><\/div>\n\n\n\n<p>reset the temp variable which holds the data to be written. Then check which interrupts are needed by oring the temp variable with the interrupt source.<\/p>\n\n\n\n<p>Hence, the update initialization function 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 adxl345_Init(ADXL345_InitParam *Param)\n{\n\t\/*Get the acceleration range *\/\n\tacc_range=Param-&gt;Accel_Range;\n\n\tuint8_t temp[1];\n\n\t\/*Set the acceleration range*\/\n\ttemp[0]=acc_range;\n\tadxl345_write(ADXL345_DATA_FORMAT, temp, 1);\n\n\t\/*Reset Power Control register*\/\n\ttemp[0]=PWR_RST;\n\tadxl345_write(ADXL345_POWER_CTL, temp, 1);\n\n\t\/*Set ADXL345 into measure mode*\/\n\ttemp[0]=MEASUREMODE;\n\tadxl345_write(ADXL345_POWER_CTL, temp, 1);\n\n\tif(Param-&gt;InterruptEnable==Interrupt_Enabled)\n\t{\n\t\ttemp[0]=0x00;\n\t\tif(Param-&gt;DataReadyInterrupt==Int_DataReady)\n\t\t{\n\t\t\ttemp[0]|=Int_DataReady;\n\n\t\t}\n\t\t\/*The rest shall be implement by the user\n\t\t * temp[0]|=Int_SingleTap\n\t\t * *\/\n\t\tadxl345_write(ADXL345_INT_ENABLE,temp,1);\n\n\n\t}\n\n}<\/pre><\/div>\n\n\n\n<p>In order to read the interrupt source, we need to read the INT_Source register and return which interrupt being triggered 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;}\">adxl345InterruptsSource adxl345_ReadInterrupt(void)\n{\n\tuint8_t temp[1];\n\tadxl345_read(ADXL345_INT_SOURCE,temp,1);\n\n\tif((temp[0]&amp;Int_DataReady)==Int_DataReady){return Int_DataReady;}\n\n\t\/*rest shall be implement by the user*\/\n\treturn 0;\n}<\/pre><\/div>\n\n\n\n<p>First, declare a temp variable to store the data, then read the interrupt source register then check the interrupt source and return correct one. Here, we are focusing on the data ready one.<\/p>\n\n\n\n<p>Save the source file since thats all.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11. Enabling External Interrupt in STM32CubeMX:<\/h2>\n\n\n\n<p>Open ADXL345_Library.ioc file and select PA0 as interrupt source as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-1024x584.jpg\" alt=\"\" class=\"wp-image-3026\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-1024x584.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-300x171.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-768x438.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-1536x875.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-2048x1167.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-1150x655.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-750x427.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-400x228.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-54-37-250x142.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note: You can select any other pin as you wish.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">12. Connection:<\/h2>\n\n\n\n<p>The connection as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"614\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-1024x614.jpg\" alt=\"\" class=\"wp-image-3027\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-1024x614.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-300x180.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-768x461.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-1536x921.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-2048x1228.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-1150x690.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-750x450.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-400x240.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-19_16-57-49-250x150.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">12. Main Code:<\/h2>\n\n\n\n<p>In main.c, in user code begin 4, we shall handle the interrupt as following:<\/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;}\">void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)\n{\n  if(GPIO_Pin == GPIO_PIN_0)\n  {\n\t  interrupt=1;\n  }\n\n}<\/pre><\/div>\n\n\n\n<p>Note: in if condition, replace pin number with the pin number set by your, pin 0 in this case.<\/p>\n\n\n\n<p>The interrupt variable is defined as volatile in user code begin PV 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;}\">volatile uint8_t interrupt=0;<\/pre><\/div>\n\n\n\n<p>In the main function, for the initialization of adxl345 structure, 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;}\">  InitParm.InterruptEnable=Interrupt_Enabled;\n  InitParm.DataReadyInterrupt=Int_DataReady;<\/pre><\/div>\n\n\n\n<p>This will enable the interrupt of adxl345.<\/p>\n\n\n\n<p>After the intialization, read the interrupt source and the acceleration data 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;}\">  \/*clear the interrupt by reading the source register and acceleration data *\/\n  adxl345_ReadInterrupt();\n  adxl345_update(&amp;AccData);<\/pre><\/div>\n\n\n\n<p>This will start the interrupt sequence.<\/p>\n\n\n\n<p>In the while loop in user code begin 3:<\/p>\n\n\n\n<p>Check if the interrupt is generated by check if the interrupt variable is set to 1 as following:<\/p>\n\n\n\n<p>If yes, reset the interrupt variable, read the interrupt source, if it is data read, the acceleration data print them.<\/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;}\">if(interrupt==1)\n\t  {\n\t\t  interrupt=0;\n\t\t  if(adxl345_ReadInterrupt()==Int_DataReady)\n\t\t  {\n\t\t  adxl345_update(&amp;AccData);\n\t\t  printf(&quot;Ax =%0.3f \\t Ay =%0.3f \\t Az =%0.3f\\r\\n&quot;,AccData.ax,AccData.ay,AccData.az);\n\t\t  }\n\t  }\n<\/pre><\/div>\n\n\n\n<p>Thats all for the code.<\/p>\n\n\n\n<p>Save the project, build and run it as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"28\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-1024x28.jpg\" alt=\"\" class=\"wp-image-3028\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-1024x28.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-300x8.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-768x21.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-1536x43.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-1150x32.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-750x21.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-400x11.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1-250x7.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-07-12_15-03-3-1.jpg 1734w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">13. Source Code Download:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-58f75201-de46-474a-a955-52eb1e02d966\" href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/ADXL_Library.zip\">ADXL_Library<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/ADXL_Library.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-58f75201-de46-474a-a955-52eb1e02d966\">Download<\/a><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">14. Results:<\/h2>\n\n\n\n<p>Open your favourite serial terminal application, set the baudrate to be 115200 and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"733\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-1024x733.jpg\" alt=\"\" class=\"wp-image-2983\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-1024x733.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-300x215.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-768x550.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-1150x823.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-750x537.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-400x286.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13-250x179.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/11\/2024-11-07_16-23-13.jpg 1168w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Note that we are getting the correct ID and the results are correct.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the third part of creating a library for ADXL345, we shall study the interrupt capabilities of ADXl345 and improve the driver to utilitize the usage of the interrupt of STM32. In this guide, we shall cover the following: 8. Interrupts of ADXL345: ADXL345 features multiple interrupt source such as: In this guide, we shall [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,7,12],"tags":[],"class_list":["post-3019","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-state-machine","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3019"}],"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=3019"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3019\/revisions"}],"predecessor-version":[{"id":3031,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3019\/revisions\/3031"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}