{"id":629,"date":"2021-12-13T04:50:19","date_gmt":"2021-12-13T04:50:19","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=629"},"modified":"2021-12-13T04:50:21","modified_gmt":"2021-12-13T04:50:21","slug":"working-with-stm32f7-and-timers-delay-using-timer","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=629","title":{"rendered":"Working with STM32F7 and Timers: Delay using timer"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"411\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-1-400x411.png\" alt=\"\" class=\"wp-image-357\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-1-400x411.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-1-292x300.png 292w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-1-768x789.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-1-750x771.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-1-250x257.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-1.png 938w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>In this guide, we shall investigate how to use timer to generate precise delay in down to microseconds ( will use 1 second delay here for demonstrations purposes only).<\/p>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is the timer and how it works<\/li><li>Configure the timer to generate delay<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1 What is the timer and how it works<\/h2>\n\n\n\n<p>A Timer Module in its most basic form is a digital logic circuit that counts up every clock cycle. More functionalities are implemented in hardware to support the timer module so it can count up or down. It can have a Prescaler to divide the input clock frequency by a selectable value. It can also have circuitry for input capture, PWM signal generation, and much more as we\u2019ll see in this tutorial.<\/p>\n\n\n\n<p>Let\u2019s consider a basic 16-Bit timer like the one shown below. As a 16-Bit time, it can count from 0 up to 65535. Every clock cycle, the value of the timer is incremented by 1. And as you can see, the Fsys is not the frequency that is incrementing the timer module. But it gets divided by the Prescaler, then it gets fed to the timer.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2020\/06\/STM32-Timers-Explained-Tutorial-With-Modes-PWM-Encoder.png?ezimgfmt=rs:625x113\/rscb6\/ngcb6\/notWebP\" alt=\"STM32 Timers Explained Tutorial With Modes PWM Encoder\" class=\"wp-image-4531\" \/><\/figure>\n\n\n\n<p>Basically, in timer mode, the TCNT register is incremented by 1 each clock cycle @ the following frequency (Fsys\/PSC). This means if the Fsys is 80MHz &amp; PSC is 1:1024, the TCNT gets incremented by 1 every 12.8\u03bcSec. Therefore, if you start this timer to count from 0 until it reaches overflow (at 65535), a flag will be set and starts over from zero<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure the timer to generate delay<br><\/h2>\n\n\n\n<p>We starting by getting to know which timer to use, in this case , we will use TIMER2.&nbsp;<\/p>\n\n\n\n<p>Hence, we need to know which bus is connected to. From the diagram, TIMER2 is connected to APB1 bus<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"830\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-830x1024.png\" alt=\"\" class=\"wp-image-630\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-830x1024.png 830w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-243x300.png 243w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-768x948.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-1245x1536.png 1245w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-1150x1419.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-750x925.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-400x493.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM-250x308.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.38.48-AM.png 1506w\" sizes=\"(max-width: 830px) 100vw, 830px\" \/><\/figure><\/div>\n\n\n\n<p>Then we enable clock access to the timer as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">RCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;<\/pre><\/div>\n\n\n\n<p>After we enabled clock access to TIM2 we shall set the prescaller and auto-reload value 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;}\">TIM2-&gt;PSC=16000-1; \/\/16 000 000 Hz \/ 16 000 = 1 000 Hz\nTIM2-&gt;ARR=1000-1;   \/\/ 1000 Hz\/ 1000 = 1 Hz<\/pre><\/div>\n\n\n\n<p>The ARR value is the maximum value can the counter counts. Since TIM2 is 32-bit timer, it can counts up to (2^32 -1 = 4294967295).<\/p>\n\n\n\n<p>now we enable timer as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"228\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-1024x228.png\" alt=\"\" class=\"wp-image-631\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-1024x228.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-300x67.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-768x171.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-1536x341.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-2048x455.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-1150x256.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-750x167.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-400x89.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-13-at-7.43.36-AM-250x56.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;}\">TIM2-&gt;CR1=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p>For delay purposes, we shall wait to the UIF to be set in SR register then reset it as following<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"227\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-1024x227.png\" alt=\"\" class=\"wp-image-360\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-1024x227.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-300x66.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-768x170.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-1536x340.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-2048x453.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-1150x254.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-750x166.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-400x89.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-26-at-8.49.30-AM-250x55.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;}\">while(!(TIM2-&gt;SR&amp;TIM_SR_UIF)){} \/\/wait UIF to be set\n\t\tTIM2-&gt;SR&amp;=~TIM_SR_UIF; \/\/reset UIF<\/pre><\/div>\n\n\n\n<p>Then we can toggle the LED<\/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;}\"> GPIOB-&gt;ODR^=GPIO_ODR_OD7;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>The code shall be this for timer delay to blink an LED each second<\/p>\n\n\n\n<p>For how to enable the build-in LED, you can check this guide (<a href=\"https:\/\/blog.embeddedexpert.io\/?p=514\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=514\" target=\"_blank\" rel=\"noreferrer noopener\">Getting started with STM32F7: Blinking LED<\/a>)<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stm32f7xx.h&quot;                  \/\/ Device header\n\nint main(void)\n\t{\n\t\/\/enable clock access to GPIOB\n\t\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOBEN;\n\t\/\/set PB7 as output\n\t\tGPIOB-&gt;MODER|=GPIO_MODER_MODER7_0;\n\t\tGPIOB-&gt;MODER&amp;=~GPIO_MODER_MODER7_1;\n\t\/\/timer2 configure\n\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\t\tTIM2-&gt;PSC=16000-1; \/\/16 000 000 Hz \/ 16 000 = 1 000 Hz\n\t\tTIM2-&gt;ARR=1000-1;   \/\/ 1000 Hz\/ 1000 = 1 Hz\n\t\tTIM2-&gt;CNT=0;\n\t\tTIM2-&gt;CR1=TIM_CR1_CEN;\n\t\t\n\t\t\n\t\t\n\twhile(1)\n\t\t{\n\t\t\/\/set PB7 to high\n\t\tGPIOB-&gt;ODR^=GPIO_ODR_OD7;\n\t\twhile(!(TIM2-&gt;SR&amp;TIM_SR_UIF)){} \/\/wait UIF to be set\n\t\tTIM2-&gt;SR&amp;=~TIM_SR_UIF; \/\/reset UIF\n\t\t}\n\t}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6834.mp4\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall investigate how to use timer to generate precise delay in down to microseconds ( will use 1 second delay here for demonstrations purposes only). In this guide, we will cover the following: What is the timer and how it works Configure the timer to generate delay Code Demo 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-629","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\/629"}],"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=629"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}