{"id":3504,"date":"2025-06-06T14:14:38","date_gmt":"2025-06-06T14:14:38","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3504"},"modified":"2025-06-06T14:14:40","modified_gmt":"2025-06-06T14:14:40","slug":"stm32-timers-applications-input-capture-using-dma-measure-frequency-and-duty-cycle","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3504","title":{"rendered":"STM32 Timers Applications: Input capture Using DMA Measure Frequency and Duty Cycle"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-1024x683.png\" alt=\"\" class=\"wp-image-3505\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-1024x683.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-300x200.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-768x512.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-1150x767.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-750x500.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-400x267.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM-250x167.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-May-31-2025-at-03_06_13-PM.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this guide, we shall use DMA to capture the both, rising and falling edge of a signal. Once they have been captured, we shall use these to measure the frequency and duty cycle of the signal.<\/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>STM32CubeMX configuration.<\/li>\n\n\n\n<li>Firmware development.<\/li>\n\n\n\n<li>Connection.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. STM32CubeMX Configuration:<\/h2>\n\n\n\n<p>We shall continue from the previous guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=3492\" data-type=\"link\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=3492\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>Open the .ioc file and STM32CubeMX window shall appear.<\/p>\n\n\n\n<p>Once STM32CubeMX appears, we shall configure the clock to maximum as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"530\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-1024x530.jpg\" alt=\"\" class=\"wp-image-3507\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-1024x530.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-300x155.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-768x397.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-1536x795.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-2048x1059.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-1150x595.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-750x388.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-400x207.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-29-08-250x129.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Set the frequency to maximum allowed by your MCU. In this case, 100MHz.<\/p>\n\n\n\n<p>Next, from Pinout and Configuration, configure TIM2 as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clock Source to internal Clock.<\/li>\n\n\n\n<li>Channel1 input capture direct mode.<\/li>\n\n\n\n<li>Channel2 input capture indirect mode.<\/li>\n<\/ul>\n\n\n\n<p>Configure Channels as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Channel1 Polarity to Rising Edge.<\/li>\n\n\n\n<li>Channel2 Polarity to Falling Edge.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"878\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-878x1024.jpg\" alt=\"\" class=\"wp-image-3508\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-878x1024.jpg 878w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-257x300.jpg 257w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-768x896.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-1317x1536.jpg 1317w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-1756x2048.jpg 1756w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-1150x1341.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-750x875.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-400x467.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29-250x292.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-31-29.jpg 1826w\" sizes=\"(max-width: 878px) 100vw, 878px\" \/><\/figure>\n\n\n\n<p>Next, we shall configure the DMA as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add DMA handler for TIM2_CH1 and TIM_CH2\/CH4 (they are sharing same DMA).<\/li>\n\n\n\n<li>Set mode to Circular for both.<\/li>\n\n\n\n<li>Data width for Word.<\/li>\n\n\n\n<li>Make sure the direction is Peripheral to Memory.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"872\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-1024x872.jpg\" alt=\"\" class=\"wp-image-3509\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-1024x872.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-300x256.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-768x654.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-1536x1308.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-1150x980.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-750x639.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-400x341.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10-250x213.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-39-10.jpg 1552w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Next, enable TIM2 interrupt as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"605\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-1024x605.jpg\" alt=\"\" class=\"wp-image-3510\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-1024x605.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-300x177.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-768x454.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-1150x679.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-750x443.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-400x236.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06-250x148.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-44-06.jpg 1524w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we shall configure TIM5_CH2 as PWM output as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"830\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-1024x830.jpg\" alt=\"\" class=\"wp-image-3511\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-1024x830.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-300x243.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-768x622.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-1536x1245.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-1150x932.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-750x608.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-400x324.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15-250x203.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_16-46-15.jpg 2024w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>For more details about PWM in timers, please refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=3363\" data-type=\"link\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=3363\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a> guide.<\/p>\n\n\n\n<p>Thats all for the guide.<\/p>\n\n\n\n<p>Save the project this will generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Firmware Development:<\/h2>\n\n\n\n<p>Once the project has been generated, main.c shall be opened.<\/p>\n\n\n\n<p>In user code begin PV of main.c, declare 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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">long unsigned int IC_Value1 = 0;\nlong unsigned int IC_Value2 = 0;\n\nlong unsigned int period1=0;\nlong unsigned int period2=0;\nfloat frequency=0;\nfloat duty=0;\n\nvolatile long unsigned int previous_IC_Value1=0;\nvolatile long unsigned int previous_IC_Value2=0;<\/pre><\/div>\n\n\n\n<p>The IC_Values are for Rising and falling edges respectively.<\/p>\n\n\n\n<p>periods are for rising and falling edges respectively.<\/p>\n\n\n\n<p>Previous_IC_value for Rising and falling edges respectively.<\/p>\n\n\n\n<p>Next, once the timer has captured the edge and DMA moved the data from timer to memory location, the  <strong>AL_TIM_IC_CaptureCallback<\/strong> shall be called.<\/p>\n\n\n\n<p>The function as follows:<\/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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)\n{\n    if (htim-&gt;Channel == HAL_TIM_ACTIVE_CHANNEL_1) \/\/ Rising edge\n    {\n        IC_Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);\n\n        if (IC_Value1 &gt;= previous_IC_Value1)\n            period1 = IC_Value1 - previous_IC_Value1;\n        else\n            period1 = (0xFFFFFFFF - previous_IC_Value1 + 1) + IC_Value1;\n\n        previous_IC_Value1 = IC_Value1;\n\n        \/\/ Calculate frequency in Hz (timer running at 100 MHz \u2192 period in \u00b5s)\n        frequency = 100000000.0f \/ period1;\n    }\n\n    if (htim-&gt;Channel == HAL_TIM_ACTIVE_CHANNEL_2) \/\/ Falling edge\n    {\n        IC_Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);\n\n        if (IC_Value2 &gt;= previous_IC_Value1)\n            period2 = IC_Value2 - previous_IC_Value1;  \/\/ high time = fall - last rise\n        else\n            period2 = (0xFFFFFFFF - previous_IC_Value1 + 1) + IC_Value2;\n\n        previous_IC_Value2 = IC_Value2;\n\n        \/\/ Optionally, calculate duty cycle\n        duty = (float)period2 \/ period1 * 100.0f;\n    }\n}<\/pre><\/div>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<p>Check the interrupt channel, if it is channel1:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Get the current captured value.<\/li>\n\n\n\n<li>Compare the stored previously value with the current and calculate the period and overflow management and calculate the frequency.<\/li>\n<\/ul>\n\n\n\n<p>If it is channel2:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Calculate the period in order to calculate the duty cycle as follows:<\/li>\n<\/ul>\n\n\n\n<p>Duty cycle is measured by capturing the\u00a0<strong>time the signal stays high<\/strong>\u00a0during one full period of the PWM cycle:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Rising edge (CH1)<\/strong>: Marks the start of the high pulse.<\/li>\n\n\n\n<li><strong>Falling edge (CH2)<\/strong>: Marks the end of the high pulse.<\/li>\n\n\n\n<li><strong>Next rising edge<\/strong>: Marks the start of the next cycle.<\/li>\n\n\n\n<li>Formula:<\/li>\n\n\n\n<li>Duty\u00a0Cycle=(High\u00a0TimePeriod)\u00d7100%Duty\u00a0Cycle=(PeriodHigh\u00a0Time\u200b)\u00d7100%<\/li>\n\n\n\n<li>In code:<\/li>\n\n\n\n<li><code>period1 = time_between_rising_edges<\/code><\/li>\n\n\n\n<li><code>period2 = time_from_rising_to_falling_edge (high time)<\/code><\/li>\n<\/ol>\n\n\n\n<p>In user code begin 2 in main 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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">  HAL_TIM_IC_Start_DMA(&amp;htim2, TIM_CHANNEL_1,(uint32_t)&amp;IC_Value1,1);\n  HAL_TIM_IC_Start_DMA(&amp;htim2, TIM_CHANNEL_2,(uint32_t)&amp;IC_Value2,1);\n  HAL_TIM_PWM_Start(&amp;htim5, TIM_CHANNEL_2);<\/pre><\/div>\n\n\n\n<p>Start both channel in DMA mode.<\/p>\n\n\n\n<p>The function shall take the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Instant to the timer which htim2 in this case.<\/li>\n\n\n\n<li>Timer channel (TIM_CHANNEL_1 and CHANNEL_2).<\/li>\n\n\n\n<li>Variable to store the captured value.<\/li>\n\n\n\n<li>Size of the array which is 1 in this case.<\/li>\n<\/ul>\n\n\n\n<p>Last start the PWM signal.<\/p>\n\n\n\n<p>Thats all for the guide.<\/p>\n\n\n\n<p>Build the project and start a debugging session as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"43\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1024x43.png\" alt=\"\" class=\"wp-image-3512\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1024x43.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-300x13.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-768x32.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1536x65.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1150x48.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-750x31.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-400x17.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-250x10.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3. Connection:<\/h2>\n\n\n\n<p>The connection as simple as connecting jumper wires between PA0 and PA1. In case of Nucleo-64 board, connect A0 arduino pin to A1 Arduino pin as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"636\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-1024x636.jpg\" alt=\"\" class=\"wp-image-3500\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-1024x636.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-300x186.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-768x477.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-1536x954.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-2048x1272.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-1150x714.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-750x466.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-400x249.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/05\/2025-05-31_15-41-31-250x155.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>Add IC_Value1, IC_Value2, period1, period3, duty and frequency to Live Expression.<\/p>\n\n\n\n<p>Run the program and 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=\"399\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-1024x399.jpg\" alt=\"\" class=\"wp-image-3513\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-1024x399.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-300x117.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-768x300.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-1150x449.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-750x293.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-400x156.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18-250x98.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-06_17-11-18.jpg 1400w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The duty which is 49 as set in the code.<\/p>\n\n\n\n<p>Frequency of 10KHz which is the frequency of the PWM.<\/p>\n\n\n\n<p>In next part, we shall use more efficient method of PWM input to measure the frequency and duty cycle.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Staty tuned.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall use DMA to capture the both, rising and falling edge of a signal. Once they have been captured, we shall use these to measure the frequency and duty cycle of the signal. In this guide, we shall cover the following: 1. STM32CubeMX Configuration: We shall continue from the previous guide [&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-3504","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\/3504"}],"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=3504"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3504\/revisions"}],"predecessor-version":[{"id":3514,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3504\/revisions\/3514"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}