{"id":482,"date":"2021-10-07T05:25:26","date_gmt":"2021-10-07T05:25:26","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=482"},"modified":"2021-10-07T05:25:30","modified_gmt":"2021-10-07T05:25:30","slug":"working-with-stm32-and-ultrasonic-using-timer-and-interrupt-to-measure-the-distance","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=482","title":{"rendered":"Working with STM32 and ultrasonic: using timer and interrupt to measure the distance"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front.jpg\" alt=\"\" class=\"wp-image-173\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front-300x169.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front-400x225.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/HC-SR04-Ultrasonic-Sensor-Module-Distance-Measurement-Component-Part-Front-250x141.jpg 250w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide(<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=172\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=172\" target=\"_blank\">here<\/a>), we looked at the operation principle  of how ultrasonic works and we used very crude code to measure the distance. In this guide, we shall use external GPIO interrupt and timer 2 of the STM32F4 to measure the distance.<\/p>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>External interrupt programming<\/li><li>Timer configuration<\/li><li>Measuring the distance<\/li><li>Schematic<\/li><li>Code<\/li><li>Results<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. External interrupt programming <\/h2>\n\n\n\n<p>Before we head into the interrupt programming, for detailed interrupt guide, please read the following article from here (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=72\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=72\" target=\"_blank\">Link<\/a>).<\/p>\n\n\n\n<p>From the schematic, the trigger pin is connected to PA0 and echo pin to PA1. Hence we need to enable clock access to GPIOA (for details use the following article <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=246\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=246\" target=\"_blank\">link<\/a>).<\/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;}\">\/\/Enable clock access to GPIO A\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Then we need to enable clock access to SYSCFG.<\/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;}\">\/\/Enable clock access to System Configuration Control\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_SYSCFGEN;<\/pre><\/div>\n\n\n\n<p>Configure the PA0 as output and PA1 as input<\/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;}\">\/\/set PA0 to output\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0_0;\n\t\/\/Make sure that PA0 is low\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BR_0;\n\t\/\/Set PA1 to input\n\tGPIOA-&gt;MODER&amp;=~(GPIO_MODER_MODE1_0|GPIO_MODER_MODE1_1);<\/pre><\/div>\n\n\n\n<p>Now, we enable interrupt for port A from SYSCFG external interrupt configuration register 1 as following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"711\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-1024x711.png\" alt=\"\" class=\"wp-image-483\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-1024x711.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-300x208.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-768x533.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-1536x1067.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-1150x799.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-750x521.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-400x278.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM-250x174.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-7.58.15-AM.png 1768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\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;}\">\/\/Enable PA1 interrupt\n\tSYSCFG-&gt;EXTICR[0]|=SYSCFG_EXTICR1_EXTI1_PA;<\/pre><\/div>\n\n\n\n<p>Now, we need to unmask the interrupt for line1<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"489\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-1024x489.png\" alt=\"\" class=\"wp-image-484\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-1024x489.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-300x143.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-768x367.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-1536x734.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-2048x978.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-1150x549.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-750x358.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-400x191.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.00.32-AM-250x119.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\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;}\">\/\/unmask line 1\n\tEXTI-&gt;IMR|=EXTI_IMR_MR1;<\/pre><\/div>\n\n\n\n<p>Finally we enable External interrupt line 1 in NVIC 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;}\">NVIC_EnableIRQ(EXTI1_IRQn);<\/pre><\/div>\n\n\n\n<p>We have not yet select on which edge to trigger since we will change this during code exectution<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Timer configuration:<\/h2>\n\n\n\n<p>In this guide, we shall use SysTick  to generate delay in microseconds and TIM2 to measure the pulse length.<\/p>\n\n\n\n<p>For more information about SysTick timer, you can read the following guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=79\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=79\" target=\"_blank\">here<\/a>).<\/p>\n\n\n\n<p>How to enable TIM2 you can use this guide (from <a href=\"https:\/\/blog.embeddedexpert.io\/?p=356\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=356\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>)<\/p>\n\n\n\n<p>SysTick delay for microseconds delay<\/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 delay_uS(int uS)\n\t{\n\tSysTick-&gt;LOAD=16-1;\n\tSysTick-&gt;VAL=0;\n\tSysTick-&gt;CTRL=0x5;\n\t\tfor (int i=0;i&lt;uS;i++)\n\t\t{\n\t\t\twhile(!(SysTick-&gt;CTRL &amp;0x10000)){}\n\t\t}\n\tSysTick-&gt;CTRL=0;\n\t}<\/pre><\/div>\n\n\n\n<p>Enable TIM2, set prescaler to 16-1 and ARR to 250000 and start the timer 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;}\">\/\/Enable clock access to TIM2\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\t\/\/set prescaler to 16-1 for 1MHz\n\tTIM2-&gt;PSC=16-1;\n\t\/\/Set the ARR to 250000 (250 millis second max)\n\tTIM2-&gt;ARR=250000;\n\t\/\/Enable Timer\n\tTIM2-&gt;CR1=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3.1 Measuring the distance:<\/h2>\n\n\n\n<p>We start of by setting PA0 to high for 10 microseconds, configure the interrupt to rising edge then setting the PA0 to low<\/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\/\/set trigger pin to high\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BS_0;\n\t\t\t\/\/Set PA1 interrupt to Rising Edge\n\t\t\tEXTI-&gt;RTSR|=EXTI_RTSR_TR1;\t\t\t\t\n\t\t\t\/\/delay for 10 microseconds\n\t\t\tdelay_uS(10);\n\t\t\t\/\/ set trigger pin to low\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BR_0;<\/pre><\/div>\n\n\n\n<p>we will wait until the sensor is finished<\/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\t\t\/\/wait until finish is set \n\t\t\twhile(finished==0){;}<\/pre><\/div>\n\n\n\n<p>Then we reset the finished back to 0 and start calculation the distance<\/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;}\">\/\/reset the finished value\n\t\t\tfinished=0;\n\t\t\t\/\/calculate the disatnce\n\t\t\tdisatnce=((duration\/2)\/29.1);<\/pre><\/div>\n\n\n\n<p>Now we delay by half second 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\/\/delay by half second\n\t\t\tdelay_uS(500000);<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3.2 Interrupt handling<\/h2>\n\n\n\n<p>first we check the direction if it is rising or falling edge as following<\/p>\n\n\n\n<p>If yes, change the trigger to falling edge, reset timer2 counter and set direction to 1<\/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(dir==0)\n\t\t\t\t{\n\t\t\t\tEXTI-&gt;FTSR|=EXTI_FTSR_TR1;\n\t\t\t\tTIM2-&gt;CNT=0;\n\t\t\t\tdir=1;\n\t\t\t\t}<\/pre><\/div>\n\n\n\n<p>if not <\/p>\n\n\n\n<p>This means the we are at the falling edge and we need to store the current timer value, set finished to 1 and dir back to zero<\/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;}\">else\n\t\t\t\t{\n\t\t\t\tfinished=1;\n\t\t\t\tduration=TIM2-&gt;CNT;\n\t\t\t\tdir=0;\n\t\t\t\t}<\/pre><\/div>\n\n\n\n<p>After that, we clear the interrupt pending flag 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;}\">\tEXTI-&gt;PR=EXTI_PR_PR1;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Schematics<\/h2>\n\n\n\n<p>he wiring of HC-SR04 with STM32 is shown as below. The Vcc of the HC-SR04 is connected to the 5V of the Nucleo board, GND to GND of the Nucleo board, Trig to A0 (PA0) and echo to A1 (PA1).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"861\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-1024x861.png\" alt=\"\" class=\"wp-image-185\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-1024x861.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-300x252.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-768x646.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-1536x1291.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-1150x967.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-750x631.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-400x336.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM-250x210.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-23-at-10.01.54-AM.png 2034w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>HC-SR04 Connection with STM32<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-768x1024.jpg\" alt=\"\" class=\"wp-image-187\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-768x1024.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-225x300.jpg 225w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-1152x1536.jpg 1152w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-1536x2048.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-1150x1533.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-750x1000.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-400x533.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-250x333.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/IMG_5916-scaled.jpg 1920w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. Code:<\/h2>\n\n\n\n<p>Here is the entire code:<\/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;                  \/\/ Device header\n\n\/*\npinout\nTRIG of Ultrasonic to PA0\nECHO of Ultrasonic to PA1\n*\/\n\nvolatile int finished,duration,dir;\nfloat disatnce;\n\nvoid delay_uS(int uS)\n\t{\n\tSysTick-&gt;LOAD=16-1;\n\tSysTick-&gt;VAL=0;\n\tSysTick-&gt;CTRL=0x5;\n\t\tfor (int i=0;i&lt;uS;i++)\n\t\t{\n\t\t\twhile(!(SysTick-&gt;CTRL &amp;0x10000)){}\n\t\t}\n\tSysTick-&gt;CTRL=0;\n\t}\n\n\t\n\t\n\t\nint main(void)\n\t{\n\t\/\/Enable clock access to GPIO A\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\t\/\/Enable clock access to System Configuration Control\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_SYSCFGEN;\n\t\/\/set PA0 to output\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0_0;\n\t\/\/Make sure that PA0 is low\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BR_0;\n\t\/\/Set PA1 to input\n\tGPIOA-&gt;MODER&amp;=~(GPIO_MODER_MODE1_0|GPIO_MODER_MODE1_1);\n\t\/\/Enable PA1 interrupt\n\tSYSCFG-&gt;EXTICR[0]|=SYSCFG_EXTICR1_EXTI1_PA;\n\t\/\/unmask line 1\n\tEXTI-&gt;IMR|=EXTI_IMR_MR1;\n\t\n\t\/\/Enable clock access to TIM2\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\t\/\/set prescaler to 16-1 for 1MHz\n\tTIM2-&gt;PSC=16-1;\n\t\/\/Set the ARR to 250000 (250 millis second max)\n\tTIM2-&gt;ARR=250000;\n\t\/\/Enable Timer\n\tTIM2-&gt;CR1=TIM_CR1_CEN;\n\t\n\t\n\tNVIC_EnableIRQ(EXTI1_IRQn);\n\t\t\n\t\t\n\twhile(1)\n\t\t\t{\n\t\t\t\/\/set trigger pin to high\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BS_0;\n\t\t\t\/\/Set PA1 interrupt to Rising Edge\n\t\t\tEXTI-&gt;RTSR|=EXTI_RTSR_TR1;\t\t\t\t\n\t\t\t\/\/delay for 10 microseconds\n\t\t\tdelay_uS(10);\n\t\t\t\/\/ set trigger pin to low\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BR_0;\n\t\t\t\/\/wait until finish is set \n\t\t\twhile(finished==0){;}\n\t\t\t\/\/reset the finished value\n\t\t\tfinished=0;\n\t\t\t\/\/calculate the disatnce\n\t\t\tdisatnce=((duration\/2)\/29.1);\n\t\t\t\/\/delay by half second\n\t\t\tdelay_uS(500000);\n\t\t\t}\n\t}\n\n\t\n\tvoid EXTI1_IRQHandler(void)\n\t\t\t{\n\t\t\tif(dir==0)\n\t\t\t\t{\n\t\t\t\tEXTI-&gt;FTSR|=EXTI_FTSR_TR1;\n\t\t\t\tTIM2-&gt;CNT=0;\n\t\t\t\tdir=1;\n\t\t\t\t}\n\t\t\t\t\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tfinished=1;\n\t\t\t\tduration=TIM2-&gt;CNT;\n\t\t\t\tdir=0;\n\t\t\t\t}\n\t\t\t\t\n\t\t\tEXTI-&gt;PR=EXTI_PR_PR1;\n\t\t\t}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">6. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"416\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-1024x416.png\" alt=\"\" class=\"wp-image-485\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-1024x416.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-300x122.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-768x312.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-1150x467.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-750x305.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-400x162.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM-250x102.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-07-at-8.24.28-AM.png 1512w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide(here), we looked at the operation principle of how ultrasonic works and we used very crude code to measure the distance. In this guide, we shall use external GPIO interrupt and timer 2 of the STM32F4 to measure the distance. In this guide, we will cover the following: External interrupt programming Timer [&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-482","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\/482"}],"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=482"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":486,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions\/486"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}