{"id":2013,"date":"2023-09-30T15:18:09","date_gmt":"2023-09-30T15:18:09","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2013"},"modified":"2023-09-30T15:18:12","modified_gmt":"2023-09-30T15:18:12","slug":"input-capture-application-part-1-measure-the-distance-from-ultrasonic","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2013","title":{"rendered":"Input Capture Application Part 1: Measure the distance from ultrasonic"},"content":{"rendered":"\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=\"541\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-1024x541.jpeg\" alt=\"\" class=\"wp-image-2014\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-1024x541.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-300x158.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-768x405.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-1536x811.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-2048x1081.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-1150x607.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-750x396.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-400x211.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_472888653-250x132.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>In part 1 of input capture guides, we shall see how to use input capture to measure the distance from an ultrasonic (HC-SR04) and display the measured distance on the serial terminal.<\/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>What is input capture.<\/li><li>HC-SR04 connection with STM32.<\/li><li>Developing the driver.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is input capture:<\/h2>\n\n\n\n<p>Each Capture\/Compare channel&nbsp;is built around a capture\/compare register (including a shadow register), an input stage for capture (with a digital filter, multiplexing, and Prescaler) and an output stage (with comparator and output control). The input stage samples the corresponding TIx input to generate a filtered signal TIxF. Then, an edge detector with polarity selection generates a signal (TIxFPx) which can be used as trigger input by the slave mode controller or as the capture command. It is prescaled before the capture register (ICxPS).<\/p>\n\n\n\n<p><strong>Here is a diagram for a capture\/compare channel\u2019s input stage.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-0.deepbluembedded.com\/wp-content\/uploads\/2020\/06\/STM32-Input-Capture-Example-HAL-Frequency-Counter-Measurement.png?ezimgfmt=rs:1128x429\/rscb6\/ng:webp\/ngcb6\" alt=\"STM32 Input Capture Example HAL - Frequency Counter Measurement\" class=\"wp-image-4634\" \/><\/figure>\n\n\n\n<p><strong>And here is a diagram for the capture\/compare channel 1 Full Circuitry<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-0.deepbluembedded.com\/wp-content\/uploads\/2020\/06\/STM32-Input-Capture-Compare-Channel-Tutorial-Frequency-Counter-Measurement.png?ezimgfmt=rs:1128x689\/rscb6\/ng:webp\/ngcb6\" alt=\"STM32 Input Capture Compare Channel Tutorial - Frequency Counter Measurement\" class=\"wp-image-4635\" \/><\/figure>\n\n\n\n<p><strong>STM32 Timers In Input Capture Mode<\/strong><\/p>\n\n\n\n<p>In Input capture mode, the Capture\/Compare Registers (TIMx_CCRx) are used to latch the value of the counter after a transition detected by the corresponding ICx signal. When a capture occurs, the corresponding CCXIF flag (TIMx_SR register) is set and an interrupt or a DMA request can be sent if they are enabled. If a capture occurs while the CCxIF flag was already high, then the over-capture flag CCxOF (TIMx_SR register) is set. CCxIF can be cleared by software by writing it to 0 or by reading the captured data stored in the TIMx_CCRx register. CCxOF is cleared when written to 0. (<a href=\"https:\/\/deepbluembedded.com\/stm32-input-capture-frequency-measurement-example-timer-input-capture-mode\/#:~:text=STM32%20Timers%20In%20Input%20Capture,by%20the%20corresponding%20ICx%20signal.\" data-type=\"URL\" data-id=\"https:\/\/deepbluembedded.com\/stm32-input-capture-frequency-measurement-example-timer-input-capture-mode\/#:~:text=STM32%20Timers%20In%20Input%20Capture,by%20the%20corresponding%20ICx%20signal.\" target=\"_blank\" rel=\"noreferrer noopener\">source<\/a>)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. HC-SR04 Connection with STM32:<\/h2>\n\n\n\n<p>The connection 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=\"861\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-1024x861.png\" alt=\"\" class=\"wp-image-2015\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-1024x861.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-300x252.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-768x646.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-1536x1291.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-1150x967.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-750x631.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-400x336.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM-250x210.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screen-Shot-2021-07-23-at-10.01.54-AM.png 2034w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes\"><table><tbody><tr><td>HC-SR04<\/td><td>STM32F411 Nucle0<\/td><\/tr><tr><td>Vcc<\/td><td> 5V<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>Trig<\/td><td>PA0<\/td><\/tr><tr><td>Echo<\/td><td>PA1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Developing the driver:<\/h2>\n\n\n\n<p>Before we develop the driver, take a look how the ultrasonic sensor works from <a href=\"https:\/\/blog.embeddedexpert.io\/?p=172\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=172\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>Also, take a look how to configure the pin as PWM from <a href=\"https:\/\/blog.embeddedexpert.io\/?p=363\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=363\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>How to configure a timer in input capture from <a href=\"https:\/\/blog.embeddedexpert.io\/?p=816\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=816\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>From the theory of how ultrasonic works, we need a pulse to trigger the sensor to start measure the distance. We can achieve this using PWM signal generated on PA0 and input capture mode on PA1.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by declaring two variables:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>One to hold the measured pulse width.<\/li><li>One to hold the measured distance.<\/li><\/ul>\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;}\">\/*Variable to hold the measured pulse length*\/\nuint32_t period;\n\n\/*variable to hold the measured distance*\/\nfloat distance;<\/pre><\/div>\n\n\n\n<p>Within the main function:<\/p>\n\n\n\n<p>Enable floating point for float operation:<\/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\/*Enable float point hardware*\/\n\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Set the core speed to be 100MHz: (check <a href=\"https:\/\/blog.embeddedexpert.io\/?p=454\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=454\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a> for how to set it to 100MHz)<\/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;}\">\t\/*Set the clock to 100MHz*\/\n\tSysClockConfig();<\/pre><\/div>\n\n\n\n<p>Initialize the UART:<\/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\/*Initialize UART2*\/\n\tuart2_init();<\/pre><\/div>\n\n\n\n<p>Since we need two channels from timer2, we shall use PA0 and PA1 to generate the trigger signal and and measure the echo signal.<\/p>\n\n\n\n<p>Start by enabling clock access to GPIOA 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\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Set PA0 and PA1 as alternate 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;}\">\t\/*Set PA0 and PA1 to alternate function*\/\n\tGPIOA-&gt;MODER|=(GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1);\n\tGPIOA-&gt;MODER&amp;=~(GPIO_MODER_MODE0_0|GPIO_MODER_MODE1_0);<\/pre><\/div>\n\n\n\n<p>Set the type of alternate 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;}\">\t\/*Select AF01 for TIM2*\/\n\t#define AF01 0x01\n\tGPIOA-&gt;AFR[0]|=(AF01&lt;&lt;GPIO_AFRL_AFSEL0_Pos)|(AF01&lt;&lt;GPIO_AFRL_AFSEL1_Pos);<\/pre><\/div>\n\n\n\n<p>Now, we shall configure timer2.<\/p>\n\n\n\n<p>Start with enable clock access to TIM2 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\/*enable clock access to tim2*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;<\/pre><\/div>\n\n\n\n<p>Set the perscaler to 99: Since the timer clock running at 100MHz, with 99, we shall get 1MHz frequency<\/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 the prescaler to 99 to get 1MHZ*\/\nTIM2-&gt;PSC=99;<\/pre><\/div>\n\n\n\n<p>Set the ARR to 999999 to get 1 second total period:<\/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 the maximum to 199999 to get 5 Hz update rate*\/\n\tTIM2-&gt;ARR=1000000-1;<\/pre><\/div>\n\n\n\n<p>Reset the counter to ensure we start from 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;}\">\t\/*Reset current counter*\/\n\tTIM2-&gt;CNT=0;<\/pre><\/div>\n\n\n\n<p>Set PA0 as PWM: <\/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 PA0 as PWM*\/\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_2| TIM_CCMR1_OC1M_1;<\/pre><\/div>\n\n\n\n<p>Set PA1 as input capture:<\/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 PA1 as Input capture*\/\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_CC2S_0;<\/pre><\/div>\n\n\n\n<p>Enable both channel of the timer:<\/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\/*Enable CH1 and CH2*\/\n\tTIM2-&gt;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E;<\/pre><\/div>\n\n\n\n<p>Enable capture\/compare for channel2 DMA request:<\/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\/*Enable capture\/compare 2 DMA*\/\n\tTIM2-&gt;DIER|=TIM_DIER_CC2DE;<\/pre><\/div>\n\n\n\n<p>Set the trigger of the input to be both edge rising and falling:<\/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 CH2 to be on both edge (falling\/rising edge)*\/\n\tTIM2-&gt;CCER|=TIM_CCER_CC2P|TIM_CCER_CC2NP;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Before enable the timer, we shall configure the DMA:<\/p>\n\n\n\n<p>We start by enabling clock access to DMA1 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\/*Enable clock access to DMA1*\/\n\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA1EN;<\/pre><\/div>\n\n\n\n<p>Disable the DMA stream and wait until it is disabled:<\/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\/*Disable the stream and wait until it is disabled*\/\n\tDMA1_Stream6-&gt;CR &amp;=~DMA_SxCR_EN;\n\n\twhile((DMA1_Stream6-&gt;CR &amp; DMA_SxCR_EN) == DMA_SxCR_EN );<\/pre><\/div>\n\n\n\n<p>Configure the DMA1_Stream6 as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Channel to be channel 3.<\/li><li>Memory and peripheral size to be word size (32-bit).<\/li><li>Circular mode.<\/li><\/ul>\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\/*Configure the DMA with the following parameters\n\t *\n\t * Channel is channel 3\n\t * Memory and peripheral size is word (32-bit)\n\t * Circular mode\n\t *\n\t * *\/\n\n\n\t#define CH3 0x03\n\n\tDMA1_Stream6-&gt;CR |= (CH3&lt;&lt;DMA_SxCR_CHSEL_Pos)|DMA_SxCR_PSIZE_1|DMA_SxCR_MSIZE_1|DMA_SxCR_CIRC;\n<\/pre><\/div>\n\n\n\n<p>Set the DMA1 Stream6 to have those parameters also:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Number of transfer to be 1.<\/li><li>Peripheral address to be TIM2-&gt;CCR2.<\/li><li>Memory address to be period.<\/li><\/ul>\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;}\">\t\/*Set number of transfers to be 1\n\t * Peripheral address to TIM2-&gt;CCR2\n\t * Memory address to be the period variable\n\t * *\/\n\tDMA1_Stream6-&gt;NDTR=1;\n\tDMA1_Stream6-&gt;PAR= (uint32_t)&amp;TIM2-&gt;CCR2;\n\tDMA1_Stream6-&gt;M0AR=(uint32_t)&amp;period;<\/pre><\/div>\n\n\n\n<p>Give a delay of 30ms to ensure that ultrasonic is up and running:<\/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 to ensure the ultrasonic sensor is up*\/\n\tdelay(30);<\/pre><\/div>\n\n\n\n<p>Enable the DMA:<\/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\/*Enable DMA*\/\n\tDMA1_Stream6-&gt;CR |=DMA_SxCR_EN;<\/pre><\/div>\n\n\n\n<p>Enable the timer:<\/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\/*Enable timer*\/\n\tTIM2-&gt;CR1=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p>Generate 12 microsecond pulse:<\/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\/*Give 12 microsecond pules to trigger pin*\/\n\tTIM2-&gt;CCR1=12;<\/pre><\/div>\n\n\n\n<p>In while 1 loop:<\/p>\n\n\n\n<p>Convert the period to distance 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;}\">\/*calculate the distance*\/\n\n\t\tdistance=(float)((float)period-(float)538.0)*((float)0.034\/(float)2);<\/pre><\/div>\n\n\n\n<p>Print 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;}\">\t\t\/*Print the distance*\/\n\t\tprintf(&quot;Distance=%0.2f cm\\r\\n&quot;,distance);<\/pre><\/div>\n\n\n\n<p>Delay by half of second for uart stability:<\/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\/*Delay between each print*\/\n\t\tdelay(500);<\/pre><\/div>\n\n\n\n<p>Hence, the entire code 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;}\">#include &quot;delay.h&quot;\n#include &quot;uart.h&quot;\n#include &quot;stdio.h&quot;\nextern void SysClockConfig(void);\n\n\/*Variable to hold the measured pulse length*\/\nuint32_t period;\n\n\/*variable to hold the measured distance*\/\nfloat distance;\n\nint main(void)\n{\n\n\n\n\t\/*Enable float point hardware*\/\n\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));\n\t\/*Set the clock to 100MHz*\/\n\tSysClockConfig();\n\n\t\/*Initialize UART2*\/\n\tuart2_init();\n\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\n\t\/*Set PA0 and PA1 to alternate function*\/\n\tGPIOA-&gt;MODER|=(GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1);\n\tGPIOA-&gt;MODER&amp;=~(GPIO_MODER_MODE0_0|GPIO_MODER_MODE1_0);\n\n\t\/*Select AF01 for TIM2*\/\n\t#define AF01 0x01\n\tGPIOA-&gt;AFR[0]|=(AF01&lt;&lt;GPIO_AFRL_AFSEL0_Pos)|(AF01&lt;&lt;GPIO_AFRL_AFSEL1_Pos);\n\n\t\/*enable clock access to tim2*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\n\t\/*set the prescaler to 99 to get 1MHZ*\/\n\tTIM2-&gt;PSC=99;\n\n\t\/*set the maximum to 199999 to get 5 Hz update rate*\/\n\tTIM2-&gt;ARR=1000000-1;\n\n\t\/*Reset current counter*\/\n\tTIM2-&gt;CNT=0;\n\n\t\/*Set PA0 as PWM*\/\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_2| TIM_CCMR1_OC1M_1;\n\n\t\/*Set PA1 as Input capture*\/\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_CC2S_0;\n\n\t\/*Enable CH1 and CH2*\/\n\tTIM2-&gt;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E;\n\n\n\t\/*Enable capture\/compare 2 DMA*\/\n\tTIM2-&gt;DIER|=TIM_DIER_CC2DE;\n\n\n\t\/*Set CH2 to be on both edge (falling\/rising edge)*\/\n\tTIM2-&gt;CCER|=TIM_CCER_CC2P|TIM_CCER_CC2NP;\n\n\t\/*DMA configuration*\/\n\n\t\/*Enable clock access to DMA1*\/\n\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA1EN;\n\n\t\/*Disable the stream and wait until it is disabled*\/\n\tDMA1_Stream6-&gt;CR &amp;=~DMA_SxCR_EN;\n\n\twhile((DMA1_Stream6-&gt;CR &amp; DMA_SxCR_EN) == DMA_SxCR_EN );\n\n\t\/*Configure the DMA with the following parameters\n\t *\n\t * Channel is channel 3\n\t * Memory and peripheral size is word (32-bit)\n\t * Circular mode\n\t *\n\t * *\/\n\n\n\t#define CH3 0x03\n\n\tDMA1_Stream6-&gt;CR |= (CH3&lt;&lt;DMA_SxCR_CHSEL_Pos)|DMA_SxCR_PSIZE_1|DMA_SxCR_MSIZE_1|DMA_SxCR_CIRC;\n\n\t\/*Set number of transfers to be 1\n\t * Peripheral address to TIM2-&gt;CCR2\n\t * Memory address to be the period variable\n\t * *\/\n\tDMA1_Stream6-&gt;NDTR=1;\n\tDMA1_Stream6-&gt;PAR= (uint32_t)&amp;TIM2-&gt;CCR2;\n\tDMA1_Stream6-&gt;M0AR=(uint32_t)&amp;period;\n\n\t\/*Delay to ensure the ultrasonic sensor is up*\/\n\tdelay(30);\n\n\t\/*Enable DMA*\/\n\tDMA1_Stream6-&gt;CR |=DMA_SxCR_EN;\n\n\t\/*Enable timer*\/\n\tTIM2-&gt;CR1=TIM_CR1_CEN;\n\n\t\/*Give 12 microsecond pules to trigger pin*\/\n\tTIM2-&gt;CCR1=12;\n\n\twhile(1)\n\t{\n\n\t\t\/*calculate the distance*\/\n\n\t\tdistance=(float)((float)period-(float)538.0)*((float)0.034\/(float)2);\n\n\t\t\/*Print the distance*\/\n\t\tprintf(&quot;Distance=%0.2f cm\\r\\n&quot;,distance);\n\n\t\t\/*Delay between each print*\/\n\t\tdelay(500);\n\n\n\t}\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. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Ultrasonic_dma_input_capture.zip\">Ultrasonic_dma_input_capture<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Ultrasonic_dma_input_capture.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>After uploading the code to your Nucleo-64 F411RE, open the serial terminal and set the baudrate to be 115200, you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"870\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-1024x870.png\" alt=\"\" class=\"wp-image-2017\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-1024x870.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-300x255.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-768x652.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-1150x977.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-750x637.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-400x340.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM-250x212.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-30-at-5.19.09-PM.png 1286w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In part 1 of input capture guides, we shall see how to use input capture to measure the distance from an ultrasonic (HC-SR04) and display the measured distance on the serial terminal. In this guide, we shall cover the following: What is input capture. HC-SR04 connection with STM32. Developing the driver. Code. Results. 1. What [&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-2013","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\/2013"}],"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=2013"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2013\/revisions"}],"predecessor-version":[{"id":2018,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2013\/revisions\/2018"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}