{"id":1005,"date":"2022-06-08T07:00:41","date_gmt":"2022-06-08T07:00:41","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1005"},"modified":"2022-06-08T07:03:56","modified_gmt":"2022-06-08T07:03:56","slug":"getting-started-with-stm32l053-timer-interrupt","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1005","title":{"rendered":"Getting started with STM32L053: Timer interrupt"},"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 previous two guides, we discussed how to use timer to generate delay (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=356\" target=\"_blank\">here<\/a>). <\/p>\n\n\n\n<p>In this guide, we shall look how to configure TIM2 to generate an interrupt every second and toggle an led every second.<\/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 an interrupt<\/li><li>Configure TIM2 to generate Interrupt<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is an interrupt:<\/h2>\n\n\n\n<p>Interrupt is a process to notify the cpu that there is a request and it should be handled as soon as possible See figure below.<\/p>\n\n\n\n<p>The CPU will find the current instruction in the main loop (which is empty in our case) and jump to ISR function to toggle PA5 and jump back to main loop.<\/p>\n\n\n\n<p>For more information, please refer to this guide (<a href=\"https:\/\/blog.embeddedexpert.io\/UnderstandingInterrupts.pdf?utm_content=12797937&amp;utm_medium=Email&amp;utm_name=Id&amp;utm_source=Actionetics&amp;utm_term=Email\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>)<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"551\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM-400x551.png\" alt=\"\" class=\"wp-image-380\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM-400x551.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM-218x300.png 218w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM-743x1024.png 743w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM-768x1059.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM-750x1034.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM-250x345.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-02-at-7.38.00-AM.png 914w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure TIM2 to generate interrupt:<\/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\"><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><\/div>\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><li>Set ARR to 999.<\/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;}\">\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\tTIM2-&gt;PSC=2097-1;\n\tTIM2-&gt;ARR=1000-1;<\/pre><\/div>\n\n\n\n<p>Then enable Update interrupt in DIER register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"247\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-1024x247.png\" alt=\"\" class=\"wp-image-1006\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-1024x247.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-300x72.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-768x185.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-1536x371.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-2048x494.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-1150x277.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-750x181.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-400x96.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-08-at-9.54.43-AM-250x60.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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">TIM2-&gt;DIER|=TIM_DIER_UIE;<\/pre><\/div>\n\n\n\n<p>Enable the interrupt in the NVIC 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;}\">\tNVIC_EnableIRQ(TIM2_IRQn);<\/pre><\/div>\n\n\n\n<p>Finally, start 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>For the interrupt handler:<\/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;}\">void TIM2_IRQHandler(void)\n\t{\n\tif(1==(TIM_SR_UIF&amp;TIM2-&gt;SR)) \/*Check interrupt update flag *\/\n\t\t{\n\t\tGPIOA-&gt;ODR^=GPIO_ODR_OD5; \/*Toggle the LED*\/\n\t\tTIM2-&gt;SR&amp;=~TIM_SR_UIF; \/*Clear update interrupt flag*\/\n\t\t}\n\n\t}\n<\/pre><\/div>\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 code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/timer_interrupt_1hz.zip\">timer_interrupt_1hz<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/timer_interrupt_1hz.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo:<\/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\">\n<iframe loading=\"lazy\" title=\"Blinking LED On STM32L053 using Timer interrupt\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/Ytq3c8hBM9Y?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 previous two guides, we discussed how to use timer to generate delay (here). In this guide, we shall look how to configure TIM2 to generate an interrupt every second and toggle an led every second. In this guide, we will cover the following What is an interrupt Configure TIM2 to generate Interrupt Code Demo [&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-1005","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\/1005"}],"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=1005"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1005\/revisions"}],"predecessor-version":[{"id":1010,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1005\/revisions\/1010"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}