{"id":1367,"date":"2022-11-30T05:11:34","date_gmt":"2022-11-30T05:11:34","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1367"},"modified":"2022-11-30T05:11:37","modified_gmt":"2022-11-30T05:11:37","slug":"getting-started-with-stm32f103-timer-interrupt","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1367","title":{"rendered":"Getting Started with STM32F103: Timer Interrupt"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-1024x1024.jpeg\" alt=\"\" class=\"wp-image-1368\" width=\"512\" height=\"512\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-1024x1024.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-300x300.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-150x150.jpeg 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-768x768.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-1536x1536.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-2048x2048.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-1150x1150.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-750x750.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-400x400.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/AdobeStock_139809605-250x250.jpeg 250w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1361\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1361\" target=\"_blank\">here<\/a>), we took a look how to configure TIM2 of STM32F103 to generate precise delay from 1 milliseconds up to 65 seconds.<\/p>\n\n\n\n<p>In this guide, we shall use timer interrupt to generate interrupt each second and toggle the LED within the interrupt.<\/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 an interrupt.<\/li><li>Connection.<\/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. Connection:<\/h2>\n\n\n\n<p>The connection of LED to PA5 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1005\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-1005x1024.png\" alt=\"\" class=\"wp-image-1369\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-1005x1024.png 1005w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-295x300.png 295w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-768x782.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-1508x1536.png 1508w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-1150x1171.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-750x764.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-400x407.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM-250x255.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-7.48.34-AM.png 1836w\" sizes=\"(max-width: 1005px) 100vw, 1005px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You will need the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>STM32F103C8 aka BluePil<\/li><li>220Ohm Resistor.<\/li><li>Any color LED.<\/li><li>Hookup wires.<\/li><li>Breadboard.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Configure TIM2 to generate Interrupt:<\/h2>\n\n\n\n<p>First thing is to set PA5 as output:<\/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;APB2ENR|=RCC_APB2ENR_IOPAEN;\n\n\t\/*Configure PA5 as output*\/\n\tGPIOA-&gt;CRL|=GPIO_CRL_MODE5;\n\tGPIOA-&gt;CRL&amp;=~(GPIO_CRL_CNF5);<\/pre><\/div>\n\n\n\n<p>Then configure the timer.<\/p>\n\n\n\n<p>First enable clock access to TIM2 (For details, check the previous guide from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1361\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1361\" target=\"_blank\">here<\/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;}\">\t\/*Enable clock access to timer2*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;<\/pre><\/div>\n\n\n\n<p>Since the MCU is running at 8MHz and we need 1 second interrupt, we shall set the prescaler to 8000-1 to get 1KHz rate which is 1 millisecond:<\/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=8000-1; \/*8000000\/8000 =1000 i.e. 1ms*\/<\/pre><\/div>\n\n\n\n<p>Then set the maximum value to reach using ARR register. Since we need 1 second, we need to ARR to 1000-1 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;ARR=1000-1; \/*count up to 1000 which is 1 second*\/<\/pre><\/div>\n\n\n\n<p>To enable timer interrupt, we need to set UIE(Update interrupt enable) bit in TIMx DMA\/Interrupt enable register (TIMx_DIER):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"209\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-1024x209.png\" alt=\"\" class=\"wp-image-1370\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-1024x209.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-300x61.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-768x157.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-1536x314.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-2048x419.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-1150x235.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-750x153.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-400x82.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-30-at-8.03.04-AM-250x51.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;}\">TIM2-&gt;DIER|=TIM_DIER_UIE;<\/pre><\/div>\n\n\n\n<p>Then enable timer2 interrupt in NVIC:<\/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;}\">NVIC_EnableIRQ(TIM2_IRQn);<\/pre><\/div>\n\n\n\n<p>Finally enable the timer:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"220\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-1024x220.png\" alt=\"\" class=\"wp-image-1364\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-1024x220.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-300x65.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-768x165.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-1536x330.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-2048x440.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-1150x247.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-750x161.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-400x86.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.14.45-AM-250x54.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><\/p>\n\n\n\n<p>Within 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;}\">\nvoid TIM2_IRQHandler(void)\n{\n\tif(TIM2-&gt;SR &amp; TIM_SR_UIF)\n\t{\n\t\tGPIOA-&gt;ODR ^=GPIO_ODR_ODR5;\n\n\t\tTIM2-&gt;SR &amp;=~TIM_SR_UIF;\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Check the interrupt source if it is from update:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"220\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-1024x220.png\" alt=\"\" class=\"wp-image-1365\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-1024x220.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-300x65.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-768x165.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-1536x330.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-2048x440.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-1150x247.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-750x161.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-400x86.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/11\/Screenshot-2022-11-27-at-7.16.43-AM-250x54.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;}\">if(TIM2-&gt;SR &amp; TIM_SR_UIF)<\/pre><\/div>\n\n\n\n<p>IF it is, toggle PA5 and clear the flag:<\/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;}\">\t\tGPIOA-&gt;ODR ^=GPIO_ODR_ODR5;\n\n\t\tTIM2-&gt;SR &amp;=~TIM_SR_UIF;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire code 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;}\">#include &quot;stm32f1xx.h&quot;\n\nint main(void)\n{\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_IOPAEN;\n\n\t\/*Configure PA5 as output*\/\n\tGPIOA-&gt;CRL|=GPIO_CRL_MODE5;\n\tGPIOA-&gt;CRL&amp;=~(GPIO_CRL_CNF5);\n\n\t\/*Enable clock access to timer2*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\n\t\/*Configure timer2*\/\n\tTIM2-&gt;PSC=8000-1; \/*8000000\/8000 =1000 i.e. 1ms*\/\n\tTIM2-&gt;ARR=1000-1; \/*count up to 1000 which is 1 second*\/\n\tTIM2-&gt;DIER|=TIM_DIER_UIE;\n\tNVIC_EnableIRQ(TIM2_IRQn);\n\tTIM2-&gt;CR1|=TIM_CR1_CEN;\n\n\twhile(1)\n\t{\n\n\n\t}\n}\n\nvoid TIM2_IRQHandler(void)\n{\n\tif(TIM2-&gt;SR &amp; TIM_SR_UIF)\n\t{\n\t\tGPIOA-&gt;ODR ^=GPIO_ODR_ODR5;\n\n\t\tTIM2-&gt;SR &amp;=~TIM_SR_UIF;\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Demo:<\/h2>\n\n\n\n<p>When you compile the code and connect the LED as shown in the connection you should get 1 second toggle rate.<\/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 on STM32F1\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/0EAgGxb0KVQ?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 the previous guide (here), we took a look how to configure TIM2 of STM32F103 to generate precise delay from 1 milliseconds up to 65 seconds. In this guide, we shall use timer interrupt to generate interrupt each second and toggle the LED within the interrupt. In this guide, we shall cover the following: 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-1367","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\/1367"}],"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=1367"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1367\/revisions"}],"predecessor-version":[{"id":1371,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1367\/revisions\/1371"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}