{"id":310,"date":"2021-08-09T06:00:43","date_gmt":"2021-08-09T06:00:43","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=310"},"modified":"2021-08-09T06:00:45","modified_gmt":"2021-08-09T06:00:45","slug":"systick-timer-taking-it-further-with-multiple-blinking-leds","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=310","title":{"rendered":"Systick Timer, taking it further with multiple blinking LEDs"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium_image\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"411\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-400x411.png\" alt=\"\" class=\"wp-image-311\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-400x411.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-292x300.png 292w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-768x789.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-750x771.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2-250x257.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Untitled-2.png 938w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>In previous guide (<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>) we discussed how to handle Systick timer interrupt and blink a single led using interrupt generated by systick and millis function. In this guid, we will take it even further by blinking multiple leds (3 leds) in interrupt itself rather externally and one using crude inefficient delay to demonstrate the capability of interrupt.<\/p>\n\n\n\n<p>In this guide will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Locating LEDs in STM32F407 Discovery board<\/li><li>Configuring GPIO and Systick timer<\/li><li>Code<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Locating LEDs in STM32F407 Discovery Board<\/h2>\n\n\n\n<p>In this guide, we shall use STM32F407Discovery (<a rel=\"noreferrer noopener\" href=\"https:\/\/www.st.com\/en\/evaluation-tools\/stm32f4discovery.html\" data-type=\"URL\" data-id=\"https:\/\/www.st.com\/en\/evaluation-tools\/stm32f4discovery.html\" target=\"_blank\">Link<\/a>), since it has 4 built-in leds which fit perfectly for this experiment.<\/p>\n\n\n\n<p>According to this evaluation board user manual (<a rel=\"noreferrer noopener\" href=\"https:\/\/www.st.com\/resource\/en\/user_manual\/um1472-discovery-kit-with-stm32f407vg-mcu-stmicroelectronics.pdf\" data-type=\"URL\" data-id=\"https:\/\/www.st.com\/resource\/en\/user_manual\/um1472-discovery-kit-with-stm32f407vg-mcu-stmicroelectronics.pdf\" target=\"_blank\">link<\/a>), the leds are connected to the following GPIO-pins:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes\"><table><tbody><tr><td>LED color<\/td><td class=\"has-text-align-center\" data-align=\"center\">            Port connected to <\/td><\/tr><tr><td>Orange<\/td><td class=\"has-text-align-center\" data-align=\"center\">PD13<\/td><\/tr><tr><td>Green<\/td><td class=\"has-text-align-center\" data-align=\"center\">PD12<\/td><\/tr><tr><td>Red<\/td><td class=\"has-text-align-center\" data-align=\"center\">PD14<\/td><\/tr><tr><td>Blue<\/td><td class=\"has-text-align-center\" data-align=\"center\">PD15<\/td><\/tr><\/tbody><\/table><figcaption>LED connection<\/figcaption><\/figure>\n\n\n\n<p>From the table we concluded that the LEDs are connected to PORTD.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configuring GPIO and SysTick<\/h2>\n\n\n\n<p>To enable clock access to GPIOD we need to set bit 3 from AHB1ENR register show here.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"431\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-1024x431.png\" alt=\"\" class=\"wp-image-313\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-1024x431.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-300x126.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-768x323.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-1536x646.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-2048x862.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-1150x484.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-750x316.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-400x168.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-09-at-8.41.35-AM-250x105.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Enable Clock to GPIOD <\/figcaption><\/figure>\n\n\n\n<p>After enabling the clock, we need to set PD12, 13, 14 and 15 as output.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"455\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-1024x455.png\" alt=\"\" class=\"wp-image-314\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-1024x455.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-300x133.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-768x341.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-1536x683.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-1150x511.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-750x333.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-400x178.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1-250x111.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-05-at-10.14.00-AM-2048x910-1.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>MODER register for GPIO<\/figcaption><\/figure>\n\n\n\n<p>In order to configure the pins as output, we need set bits 24, 26, 28 and 30.<\/p>\n\n\n\n<p>For configuring Systick timer, it will be as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Disable Systick Timer (SysTick-&gt;CTRL=0;).<\/li><li>load value 16000-1 for 1mS operation (SysTick-&gt;LOAD=16000-1;).<\/li><li>Select clock source to processor clock, enable interrupt and start counter (SysTick-&gt;CTRL=7;)<\/li><\/ul>\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;                \n\/\/ clock access to GPIOD\n#define GPIOD_CLOCK    (1&lt;&lt;3)        \t\t\n\/\/bits of the pins \n#define LED_red_BIT    (1U&lt;&lt;28)\t\t\t\t\t\n#define LED_green_BIT  (1U&lt;&lt;24)\n#define LED_orange_BIT (1U&lt;&lt;26)\n#define LED_blue_BIT   (1U&lt;&lt;30)\n\/\/LED port \n#define\tLED_PORT\t\t    GPIOD\t\t\t\t\t\t\n\/\/bit possition for color\n#define\tLED_red\t\t \t\t (1U&lt;&lt;14)         \n#define LED_green  \t\t (1U&lt;&lt;12)\n#define LED_blue       (1U&lt;&lt;15)\n#define LED_orange     (1U&lt;&lt;13)\n\/\/blinking rate\n#define Rate_green      200\n#define Rate_blue       500\n#define Rate_orange     1000\n\/\/variable to store time in milliseconds\nvolatile uint64_t ms;\n\n\/\/ineffecient delay prototype\nvoid delay(int delayms);\n\nint main(void)\n{\n\/\/enable GPIO clock, configure pins as output and systick\n__disable_irq();\t\nRCC-&gt;AHB1ENR |=GPIOD_CLOCK;\nGPIOD-&gt;MODER |= LED_red_BIT|LED_green_BIT|LED_orange_BIT|LED_blue_BIT;\nSysTick-&gt;LOAD=16000-1;\nSysTick-&gt;VAL=0;\nSysTick-&gt;CTRL=7; \/\/0b00000111;\n__enable_irq();\nwhile(1)\n{\n\/\/toggle red led each 2 seconds\nLED_PORT-&gt;ODR^=LED_red;\ndelay(2000);\n}\n\n}\n\/\/ineffecient delay function\n\/\/spin lock the cpu for doing nothing\nvoid delay(int delayms){\nint i;\n\tfor(; delayms&gt;0;delayms--){\n\tfor(i=0;i&lt;3192;i++);\n\t}\n}\n\/\/systick handler\nvoid SysTick_Handler(void){\nms++;\/\/increment the variable\n\/\/check if time to toggle \nif(ms%Rate_blue==0){LED_PORT-&gt;ODR^=LED_blue;}\t\nif(ms%Rate_green==0){LED_PORT-&gt;ODR^=LED_green;}\t\nif(ms%Rate_orange==0){LED_PORT-&gt;ODR^=LED_orange;}\t\n}\n<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6074.mp4\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In previous guide (here) we discussed how to handle Systick timer interrupt and blink a single led using interrupt generated by systick and millis function. In this guid, we will take it even further by blinking multiple leds (3 leds) in interrupt itself rather externally and one using crude inefficient delay to demonstrate the capability [&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-310","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\/310"}],"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=310"}],"version-history":[{"count":4,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions\/318"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}