{"id":3572,"date":"2025-06-21T14:18:08","date_gmt":"2025-06-21T14:18:08","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3572"},"modified":"2025-06-21T14:18:11","modified_gmt":"2025-06-21T14:18:11","slug":"stm32-timers-applications-external-trigger-as-totalizer","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3572","title":{"rendered":"STM32 Timers Applications: External Trigger as Totalizer"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM.png\" alt=\"\" class=\"wp-image-3573\" style=\"width:840px;height:auto\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM-300x300.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM-150x150.png 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM-768x768.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM-750x750.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM-400x400.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/ChatGPT-Image-Jun-21-2025-at-04_48_31-PM-250x250.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>This application demonstrates how to configure an STM32 timer to use its External Trigger (ETR) input as the clock source, effectively turning the timer into a high-speed hardware totalizer. By feeding external pulses into the ETR pin, the timer counts each pulse directly in hardware, enabling accurate and efficient measurement of pulse-based quantities such as fluid flow, shaft rotations, or production events without burdening the CPU.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Introduction.<\/li>\n\n\n\n<li>STM32CubeIDE setup.<\/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. Introduction:<\/h2>\n\n\n\n<p>This guide presents a robust method for implementing a high-speed totalizer using the External Trigger (ETR) input of an STM32 timer. A totalizer is a fundamental component in many embedded and industrial systems, primarily used to count discrete events such as pulses generated by flow meters, rotary encoders, digital switches, or any other transducer that emits electrical pulses in response to physical actions. These counts can be used to calculate quantities such as fluid volume, distance traveled, production output, or mechanical revolutions, depending on the nature of the input signal and the sensor generating it.<\/p>\n\n\n\n<p>Traditionally, pulse counting in embedded systems could be handled in software using GPIO interrupts or polling methods. However, as pulse frequency increases, this software approach becomes inefficient and unreliable due to limited processing speed, interrupt latency, or missed events during other critical tasks. The use of a hardware timer configured to count incoming pulses provides a highly efficient and accurate alternative, especially when coupled with the ETR (External Trigger) feature of STM32 timers.<\/p>\n\n\n\n<p>In STM32 microcontrollers, many general-purpose and advanced-control timers include an ETR input pin that can be used as a dedicated external clock source. When the timer is configured in External Clock Mode 1, the ETR input effectively becomes the source of clock pulses for the timer\u2019s counter. Each pulse on the ETR pin increments the timer&#8217;s internal counter automatically, completely offloading the CPU from having to process each individual event. This hardware-driven counting mechanism allows for extremely high-speed and reliable totalization of pulses, even under noisy or high-load conditions.<\/p>\n\n\n\n<p>Another key advantage of using the ETR input for totalizing is the built-in flexibility and robustness it offers. STM32 timers allow configuration of the ETR input with digital filtering to suppress noise and debounce erratic signals. Additionally, the ETR path includes an optional prescaler that can divide down very high-frequency inputs to more manageable rates, which is useful when counting very fast pulses or when trying to extend the life of the counter before overflow. These features make the ETR-based totalizer highly suitable for harsh industrial environments, such as those found in manufacturing, energy systems, or process control.<\/p>\n\n\n\n<p>To ensure long-term accuracy, especially in applications where large numbers of pulses are expected, the totalizer can be extended in software to 64 bits by tracking timer overflows through update interrupts. This approach allows developers to maintain an effectively unlimited count value, while still relying on the underlying hardware for fast and precise pulse detection.<\/p>\n\n\n\n<p>In summary, configuring an STM32 timer in ETR mode provides a clean, reliable, and CPU-efficient solution for totalizing external pulses in real time. Whether you&#8217;re measuring fluid throughput, machine revolutions, or discrete process events, this method enables your embedded application to scale gracefully from simple counting tasks to complex, high-speed industrial metering systems. The remainder of this guide focuses on the hardware configuration and timer settings necessary to implement this solution. The software implementation, including overflow handling and data retrieval, is covered in the next sections.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. STM32CubeIDE Setup:<\/h2>\n\n\n\n<p>Open STM32CubeIDE after selecting the workspace and create new project as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"355\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1024x355.jpg\" alt=\"\" class=\"wp-image-2940\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1024x355.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-300x104.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-768x266.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1536x532.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-2048x709.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1150x398.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-750x260.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-400x139.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-250x87.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Select the MCU:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"832\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1024x832.jpg\" alt=\"\" class=\"wp-image-2941\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1024x832.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-300x244.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-768x624.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1536x1248.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-2048x1664.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1150x934.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-750x609.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-400x325.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-250x203.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Give the project a name:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37-813x1024.jpg\" alt=\"\" class=\"wp-image-3574\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37-813x1024.jpg 813w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37-238x300.jpg 238w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37-768x968.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37-750x945.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37-400x504.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37-250x315.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_15-47-37.jpg 984w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/figure>\n\n\n\n<p>Click on Finish.<\/p>\n\n\n\n<p>Next, STM32CubeMX window will appear, from timer configure TIM2 as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clock Source to ETR2, this will set PA0 as ETR source for TIM2.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"565\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-1024x565.jpg\" alt=\"\" class=\"wp-image-3575\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-1024x565.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-300x165.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-768x424.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-1536x847.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-1150x634.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-750x414.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-400x221.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40-250x138.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-55-40.jpg 2002w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Leave the parameters settings as is:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"767\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file-1024x767.jpg\" alt=\"\" class=\"wp-image-3576\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file-1024x767.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file-300x225.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file-768x575.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file-750x561.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file-400x299.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file-250x187.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/unnamed-file.jpg 1106w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, from RCC, enable external oscillator as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"442\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-1024x442.jpg\" alt=\"\" class=\"wp-image-3577\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-1024x442.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-300x130.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-768x332.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-1536x663.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-1150x497.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-750x324.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-400x173.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52-250x108.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-57-52.jpg 1908w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>From clock configuration, configure the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set Input frequency according to your oscillator (8MHz in my case).<\/li>\n\n\n\n<li>Set PLL source to HSE.<\/li>\n\n\n\n<li>Set HCLK to maximum (180MHz in this case).<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"429\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-1024x429.jpg\" alt=\"\" class=\"wp-image-3578\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-1024x429.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-300x126.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-768x322.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-1536x644.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-2048x859.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-1150x482.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-750x315.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-400x168.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-59-01-250x105.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, set TIM5 Channel 2 as PWM as follows:<\/p>\n\n\n\n<p>For more details how to configure timer in PWM mode, refer to this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=3363\" data-type=\"link\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=3363\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"842\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-1024x842.jpg\" alt=\"\" class=\"wp-image-3579\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-1024x842.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-300x247.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-768x632.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-1536x1263.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-1150x946.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-750x617.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-400x329.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26-250x206.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-02-26.jpg 1916w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, enable UART to print the results as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1014\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-1014x1024.jpg\" alt=\"\" class=\"wp-image-3580\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-1014x1024.jpg 1014w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-297x300.jpg 297w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-150x150.jpg 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-768x775.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-1521x1536.jpg 1521w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-1150x1161.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-750x757.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-400x404.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54-250x252.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_17-06-54.jpg 1646w\" sizes=\"(max-width: 1014px) 100vw, 1014px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the configuration. Save the project this will generate the project.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Firmware Development:<\/h2>\n\n\n\n<p>In user code begin includes, include stdio.h for printf.<\/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;}\">#include &quot;stdio.h&quot;<\/pre><\/div>\n\n\n\n<p>In user code begin PV, declare the following variables:<\/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;}\">volatile uint64_t totalizer;\nvolatile uint32_t last_cnt;\nuint8_t uartBuffer[100]={0};<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 0, 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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint64_t GetCounts(void)\n{\n\tuint32_t current_cnt = __HAL_TIM_GET_COUNTER(&amp;htim2);\n\tuint32_t delta;\n\n\t\/\/ Handle overflow (32-bit timer)\n\tif (current_cnt &gt;= last_cnt)\n\t\tdelta = current_cnt - last_cnt;\n\telse\n\t\tdelta = (0xFFFFFFFF - last_cnt) + current_cnt + 1;\n\n\ttotalizer += delta;\n\tlast_cnt = current_cnt;\n\n\treturn totalizer;\n}<\/pre><\/div>\n\n\n\n<p>The function first reads the current timer counter and then compare it with the last value to handle the overflow situation of the timer.<\/p>\n\n\n\n<p>Finally, return the totalized value.<\/p>\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_Base_Start(&amp;htim2);\n  HAL_TIM_PWM_Start(&amp;htim5, TIM_CHANNEL_2);<\/pre><\/div>\n\n\n\n<p>Start timer2 in base mode.<\/p>\n\n\n\n<p>Start timer5 in PWM mode for channel 2.<\/p>\n\n\n\n<p>In user code code begin 3 in while loop:<\/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;}\">\t  uint16_t len=sprintf(uartBuffer,&quot;Totalizer Value = %u\\r\\n&quot;,GetCounts());\n\t  HAL_UART_Transmit(&amp;huart2,uartBuffer,len,1000);\n\t  HAL_Delay(100);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the firmware. <\/p>\n\n\n\n<p>Build the project and run it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><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<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. 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\">5. Results:<\/h2>\n\n\n\n<p>Open your serial monitor application and set the baudrate to 115200 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=\"733\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-1024x733.jpg\" alt=\"\" class=\"wp-image-3581\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-1024x733.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-300x215.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-768x550.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-1150x823.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-750x537.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-400x286.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57-250x179.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-21_16-43-57.jpg 1168w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This application demonstrates how to configure an STM32 timer to use its External Trigger (ETR) input as the clock source, effectively turning the timer into a high-speed hardware totalizer. By feeding external pulses into the ETR pin, the timer counts each pulse directly in hardware, enabling accurate and efficient measurement of pulse-based quantities such as [&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-3572","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\/3572"}],"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=3572"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3572\/revisions"}],"predecessor-version":[{"id":3582,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3572\/revisions\/3582"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}