{"id":386,"date":"2021-09-06T05:52:00","date_gmt":"2021-09-06T05:52:00","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=386"},"modified":"2021-09-06T05:52:03","modified_gmt":"2021-09-06T05:52:03","slug":"systick-interrupt-periodic-tasks","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=386","title":{"rendered":"Systick Interrupt: Periodic Tasks"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"613\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-1024x613.png\" alt=\"\" class=\"wp-image-388\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-1024x613.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-768x460.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-1150x688.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-750x449.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-400x239.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM-250x150.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.36.41-AM.png 1310w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>In pervious two guides we took look at how to use systick timer to blink an led without using delay (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=246\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=246\" target=\"_blank\">here<\/a>) and how to use interrupt to blink multiple led (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=310\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=310\" target=\"_blank\">here<\/a>). In this guide, we shall use systick timer to handle multiple periodic tasks. Task 1 will be running each 500 milliseconds. Task 2 will be running each 1000 milliseconds and Task 3 will be running each 1500 milliseconds.<\/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 a periodic task<\/li><li>Required parts<\/li><li>Code<\/li><li>Result<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is periodic tasks  <\/h2>\n\n\n\n<p>A periodic task is the one that repeats after a certain fixed time interval. For example in Automotive, you set the cruise control speed to 100KMPH. The mcu keeps track the speed of the vehicle and desired speed. The rate of obtaining the speed of vehicle called periodic task. The most important feature of periodic tasks is it runs to completion, in another word, it should not include any while (1) loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Required parts<\/h2>\n\n\n\n<p>In this guide, we need only STM32F411RE Nucleo-64 no extra components needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. 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;                  \/\/ Device header\n#include &quot;stdint.h&quot;\n#include &quot;stdio.h&quot;\n#define rate 1000\nuint64_t previous;\nvolatile uint64_t ms,rms, periodic_1,periodic_2,periodic_3;\n\n\nvoid USART2_Init(void);\nvoid UART_Write_String(char *p);\nuint64_t millis(void);\t\nvoid UART_Write_String(char *p);\nint main(void)\n\n{\n\n__disable_irq();\n\t\n\tSysTick-&gt;LOAD=16000-1;\n\tSysTick-&gt;VAL=0;\n\tSysTick-&gt;CTRL=7; \/\/0b00000111;\n\t\n\tUSART2_Init(); \/\/initialize uart2 \n\t\n\tRCC-&gt;AHB1ENR |=RCC_AHB1ENR_GPIOAEN;\n\tGPIOA-&gt;MODER|= GPIO_MODER_MODER5_0; \/\/PA5 as output\n\t__enable_irq();\n\nwhile(1)\n\n{\nif(millis()-previous&gt;rate) \/\/if current time is elapsed, execute the if condition \n\t\t{\n\t\tGPIOA-&gt;ODR^=GPIO_ODR_OD5;\n\t\tprevious=millis();\n\t\t}\n}\n\n}\n\nvoid periodicTask1(void)\n\t{\n\t\t\n\tperiodic_1++;\n\tUART_Write_String(&quot;periodic Task 1 \\r\\n&quot;);\n\t}\n\n\tvoid periodicTask2(void)\n\t{\n\tperiodic_2++;\n\tUART_Write_String(&quot;periodic Task 2 \\r\\n&quot;);\n\t}\n\t\n\tvoid periodicTask3(void)\n\t{\n\tperiodic_3++;\n\tUART_Write_String(&quot;periodic Task 3 \\r\\n&quot;);\n\t}\n\n\t\n\tuint64_t millis(void)\n\t{\n\t__disable_irq();\n\trms=ms; \/\/store current ms in rms\n\t__enable_irq();\n\treturn rms;\n\t}\n\nvoid SysTick_Handler(void){\nms++;\n\t\nif(ms%500==0){periodicTask1();}\t\nif(ms%1000==0){periodicTask2();}\t\nif(ms%1500==0){periodicTask3();}\t\n\t\n}\n\n\nvoid USART2_Init(void){\nRCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\nGPIOA-&gt;AFR[0]=0x0700;\nGPIOA-&gt;MODER|=(1&lt;&lt;5);\/\/set bit5\nGPIOA-&gt;MODER&amp;=~(1&lt;&lt;4);\/\/reset bit4\nUSART2-&gt;BRR  = 0x0681;    \/\/9600 @16MHz\nUSART2-&gt;CR1  |=0x2008; \/\/ enable tx\n\n}\n\nvoid USART_write(int ch){\nwhile(!(USART2-&gt;SR&amp;0x0080)){\n}\nUSART2-&gt;DR=(ch);\n\t\n}\n\nvoid UART_Write_String(char *p)\n\t{\n\twhile(*p!='\\0')\n{\nUSART_write(*p);\np++;\n}\n}\n\n<\/pre><\/div>\n\n\n\n<p>For how initialize systick interrupt, check this guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=310\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=310\" target=\"_blank\">here<\/a>)<\/p>\n\n\n\n<p>For UART initialization, check this guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=329\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=329\" target=\"_blank\">here<\/a>) and how send a string (<a href=\"https:\/\/blog.embeddedexpert.io\/?p=347\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=347\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>)<\/p>\n\n\n\n<p>For periodic tasks, we are check if the ms when divided by 500,1000 and 1500 return only (i.e. 3 not 3.1 ) like this<\/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;}\">\n\t\nif(ms%500==0){periodicTask1();}\t\nif(ms%1000==0){periodicTask2();}\t\nif(ms%1500==0){periodicTask3();}\t\n\n\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Result<\/h2>\n\n\n\n<p>After compiling and upload the code, open teraterm and set the baudrate to 9600 and you should see this in the terminal<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"613\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-1024x613.png\" alt=\"\" class=\"wp-image-389\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-1024x613.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-768x460.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-1150x688.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-750x449.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-400x239.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM-250x150.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-06-at-8.38.58-AM.png 1310w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Notice that each two time task 1 is executed, you will get 1 execution for task 2.<\/p>\n\n\n\n<p>Task 3 is executed after three tasks of task1 and 1 task of task2<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In pervious two guides we took look at how to use systick timer to blink an led without using delay (here) and how to use interrupt to blink multiple led (here). In this guide, we shall use systick timer to handle multiple periodic tasks. Task 1 will be running each 500 milliseconds. Task 2 will [&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-386","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\/386"}],"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=386"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions\/391"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}