{"id":2323,"date":"2024-02-04T04:20:00","date_gmt":"2024-02-04T04:20:00","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2323"},"modified":"2024-02-04T04:24:18","modified_gmt":"2024-02-04T04:24:18","slug":"working-with-stm32-and-timers-counting-external-clock","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2323","title":{"rendered":"Working with STM32 and Timers: Counting external clock"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"374\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-1024x374.png\" alt=\"\" class=\"wp-image-2324\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-1024x374.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-300x110.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-768x281.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-1150x420.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-750x274.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-400x146.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM-250x91.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.34.56\u202fAM.png 1368w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall use external clock mode of timer in STM32 to count the external events and toggle the LED. (2 presses to turn on and 1 press to turn off).<\/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 ETR mode and it&#8217;s application.<\/li><li>GPIO Configuration.<\/li><li>Timer configuration.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is ETR Mode:<\/h2>\n\n\n\n<p>In STM32 microcontrollers, ETR stands for External Trigger. ETR mode in STM32 timers refers to a mode where the timer\u2019s counting can be triggered or controlled by an external signal applied to a dedicated input pin.<\/p>\n\n\n\n<p>Here\u2019s how ETR mode typically works:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>External Trigger Input (ETR): The ETR mode allows the timer to be triggered by an external signal applied to a dedicated pin, often labeled as ETR or TRGI (Trigger Input). This external signal can be generated by another device or module in the system.<\/li><li>Trigger Source and Edge: You can configure the timer to trigger on a specific edge of the external signal (e.g., rising edge, falling edge, or both). This configuration allows flexibility in synchronizing the timer with external events.<\/li><li>Application: ETR mode is commonly used in applications where precise timing or synchronization is required, such as in motor control, industrial automation, or communication systems. For example, the timer can be triggered by an external sensor or an event from another subsystem.<\/li><\/ol>\n\n\n\n<p>In other word, it allow the timer to use external clock source rather than the internal one.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. GPIO Configuration:<\/h2>\n\n\n\n<p>Before we configure the GPIO, we need to find which pin is the ETR pin of STM32 Timer 2.<\/p>\n\n\n\n<p>From the datasheet of STM32F407:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"440\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-1024x440.png\" alt=\"\" class=\"wp-image-2325\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-1024x440.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-300x129.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-768x330.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-1536x660.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-2048x880.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-1150x494.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-750x322.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-400x172.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.45.40\u202fAM-250x107.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We can find the following informations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PA0 is the ETR for timer 2.<\/li><li>The alternate function number is 0x01<\/li><\/ul>\n\n\n\n<p>We also find that TIM2_CH2 is connected to PA2 which shall be used to toggle the LED.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, we can configure the PA0 and PA1 using the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enable clock access to GPIOA.<\/li><li>Set PA0 and PA1 as alternate function.<\/li><li>Set which alternate function to be used:<\/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\/*GPIO Setup*\/\n\t#define TIM2_AF 0x01\n\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\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\tGPIOA-&gt;AFR[0]|=(TIM2_AF&lt;&lt;GPIO_AFRL_AFSEL0_Pos)|(TIM2_AF&lt;&lt;GPIO_AFRL_AFSEL1_Pos);<\/pre><\/div>\n\n\n\n<p>Thats all for timer configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Timer Configuration:<\/h2>\n\n\n\n<p>First 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;}\">\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;<\/pre><\/div>\n\n\n\n<p>Set the maximum value to be counted is two: (ARR register is the maximum value to be counted)<\/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;}\">\tTIM2-&gt;ARR=0x02;<\/pre><\/div>\n\n\n\n<p>Since STM32F407-Discovery board features push-button connected to PA0 with pull-down resistor, we need to set the detection to be falling edge as following:<\/p>\n\n\n\n<p>From slave mode control register, set External trigger polarity to be inverted:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"347\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-1024x347.png\" alt=\"\" class=\"wp-image-2326\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-1024x347.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-300x102.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-768x260.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-1536x520.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-2048x694.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-1150x390.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-750x254.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-400x135.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-6.58.55\u202fAM-250x85.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\tTIM2-&gt;SMCR|=TIM_SMCR_ETP;<\/pre><\/div>\n\n\n\n<p>Set the external trigger filter to be maximum:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"236\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-1024x236.png\" alt=\"\" class=\"wp-image-2327\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-1024x236.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-300x69.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-768x177.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-1536x353.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-2048x471.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-1150x265.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-750x173.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-400x92.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.28\u202fAM-250x58.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"559\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-1024x559.png\" alt=\"\" class=\"wp-image-2330\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-1024x559.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-300x164.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-768x419.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-1536x839.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-2048x1119.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-1150x628.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-750x410.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-400x218.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.01.54\u202fAM-2-250x137.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\tTIM2-&gt;SMCR|=TIM_SMCR_ETF;<\/pre><\/div>\n\n\n\n<p>Enable External Clock as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"373\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-1024x373.png\" alt=\"\" class=\"wp-image-2331\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-1024x373.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-300x109.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-768x279.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-1536x559.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-2048x745.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-1150x418.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-750x273.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-400x146.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.04.19\u202fAM-250x91.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\tTIM2-&gt;SMCR|=TIM_SMCR_ECE;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, set CH2 to be PWM Mode2 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"457\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-1024x457.png\" alt=\"\" class=\"wp-image-2332\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-1024x457.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-300x134.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-768x342.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-1536x685.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-2048x913.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-1150x513.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-750x334.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-400x178.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.08.31\u202fAM-250x111.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-1024x522.png\" alt=\"\" class=\"wp-image-2333\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-1024x522.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-300x153.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-768x392.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-1536x783.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-1150x586.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-750x382.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-400x204.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM-250x127.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.09.16\u202fAM.png 1906w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC2M;<\/pre><\/div>\n\n\n\n<p>Enable CH2:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"260\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-1024x260.png\" alt=\"\" class=\"wp-image-2334\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-1024x260.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-300x76.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-768x195.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-1536x390.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-1150x292.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-750x190.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-400x101.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM-250x63.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-04-at-7.10.20\u202fAM.png 2026w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">TIM2-&gt;CCER|=TIM_CCER_CC2E;<\/pre><\/div>\n\n\n\n<p>Set the CCR2 value to 2:<\/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;}\">\tTIM2-&gt;CCR2=0x02;<\/pre><\/div>\n\n\n\n<p>Finally, 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;}\">\tTIM2-&gt;CR1|=TIM_CR1_CEN;<\/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<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;\n\n\n\nvoid TIM2_ETR_Init(void);\n\n\nint main(void)\n{\n\tTIM2_ETR_Init();\n\twhile(1)\n\t{\n\n\t}\n}\n\n\nvoid TIM2_ETR_Init(void)\n{\n\n\t\/*GPIO Setup*\/\n\t#define TIM2_AF 0x01\n\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\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\tGPIOA-&gt;AFR[0]|=(TIM2_AF&lt;&lt;GPIO_AFRL_AFSEL0_Pos)|(TIM2_AF&lt;&lt;GPIO_AFRL_AFSEL1_Pos);\n\n\n\t\/*Timer setup*\/\n\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\n\tTIM2-&gt;ARR=0x02;\n\n\tTIM2-&gt;SMCR|=TIM_SMCR_ETP;\n\n\tTIM2-&gt;SMCR|=TIM_SMCR_ETF;\n\n\tTIM2-&gt;SMCR|=TIM_SMCR_ECE;\n\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC2M;\n\n\tTIM2-&gt;CCER|=TIM_CCER_CC2E;\n\n\tTIM2-&gt;CCR2=0x02;\n\n\tTIM2-&gt;CR1|=TIM_CR1_CEN;\n\n\n}\n<\/pre><\/div>\n\n\n\n<p>Notice how the while loop is empty since this is hardware level.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/youtu.be\/6jqdk_hXWuM\n<\/div><\/figure>\n\n\n\n<p>Note: connect a jumper wire from PA1 to PD13 to get the LED working.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall use external clock mode of timer in STM32 to count the external events and toggle the LED. (2 presses to turn on and 1 press to turn off). In this guide, we shall cover the following: What is ETR mode and it&#8217;s application. GPIO Configuration. Timer configuration. Code. Results. 1. [&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-2323","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\/2323"}],"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=2323"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2323\/revisions"}],"predecessor-version":[{"id":2337,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2323\/revisions\/2337"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}