{"id":996,"date":"2022-06-05T04:39:25","date_gmt":"2022-06-05T04:39:25","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=996"},"modified":"2022-06-05T04:46:02","modified_gmt":"2022-06-05T04:46:02","slug":"getting-started-with-stm32l053-delay-using-timers","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=996","title":{"rendered":"Getting started with STM32L053: Delay using timers"},"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 500 millisecond) 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<p><\/p>\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<h2 class=\"wp-block-heading\">2. Configure the timer to generate delay<\/h2>\n\n\n\n<p><\/p>\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<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-759x1024.png\" alt=\"\" class=\"wp-image-997\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-759x1024.png 759w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-222x300.png 222w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-768x1037.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-750x1012.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-400x540.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-250x337.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM.png 858w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, we can enable clock access to timer2 as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enabled clock access to TIM2<\/li><li>Set prescaller to 2097 since STM32l053 is running at 2.09MHz.<\/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;}\">void tim2_1ms_init()\n\t{\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\tTIM2-&gt;PSC=2097-1;\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Since timer2 is 16-bit, that will be able to generate delay up to 65535 milliseconds which is about 65.5 seconds:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set the delay amount in ARR register.<\/li><li>Reset the counter.<\/li><li>Start the timer.<\/li><li>Wait until the timer overflow.<\/li><li>Disable the timer.<\/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;}\">void delay(int ms)\n\t{\n\tif(ms&gt;65535)return;\n\tTIM2-&gt;CNT=0;\n\tTIM2-&gt;ARR=ms;\n\tTIM2-&gt;CR1|=TIM_CR1_CEN;\n\twhile(!(TIM2-&gt;SR &amp; TIM_SR_UIF)){}\n\tTIM2-&gt;SR &amp;=~(TIM_SR_UIF);\n\tTIM2-&gt;CR1&amp;=~TIM_CR1_CEN;\n\t}\n<\/pre><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"239\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1024x239.png\" alt=\"\" class=\"wp-image-998\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1024x239.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-300x70.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-768x179.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1536x358.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-2048x478.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1150x268.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-750x175.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-400x93.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-250x58.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Enable timer <\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"239\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-1024x239.png\" alt=\"\" class=\"wp-image-999\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-1024x239.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-300x70.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-768x179.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-1536x358.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-2048x478.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-1150x268.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-750x175.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-400x93.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.33.06-AM-250x58.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Wait to overflow<\/figcaption><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In the main function:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set PA5 as output (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=837\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=837\" target=\"_blank\">here<\/a>).<\/li><li>Toggle the LED.<\/li><li>Delay by the amount you need (500ms in this guide).<\/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;}\">#include &quot;tim.h&quot;\n#include &quot;stm32l0xx.h&quot;\nuint32_t adc_data;\nint main(void)\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;\n\n\n\t\/*Set PA5 as output pin*\/\n\tGPIOA-&gt;MODER |= GPIO_MODER_MODE5_0;\n\tGPIOA-&gt;MODER  &amp;=~GPIO_MODER_MODE5_1;\n\ttim2_1ms_init();\n\twhile(1)\n\t{\n\t\tGPIOA-&gt;ODR^=GPIO_ODR_OD5;\n\t\tdelay(500);\n\n\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Delay_timer.zip\">Delay_timer<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Delay_timer.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo:<\/h2>\n\n\n\n<p><\/p>\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\">\n<iframe loading=\"lazy\" title=\"Delay using timer\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/GZk1lxmCk9k?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\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 500 millisecond) 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-996","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\/996"}],"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=996"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/996\/revisions"}],"predecessor-version":[{"id":1003,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/996\/revisions\/1003"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}