{"id":15072,"date":"2016-07-21T10:00:54","date_gmt":"2016-07-21T17:00:54","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=15072"},"modified":"2021-06-16T14:01:39","modified_gmt":"2021-06-16T21:01:39","slug":"timers-huh-what-are-they-good-for","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/timers-huh-what-are-they-good-for\/","title":{"rendered":"Timers Huh? What are they good for?"},"content":{"rendered":"<p>It won&#8217;t surprise you to find out that timers are really useful for stuff involving\u00a0<em>time<\/em>. They can generate a regular, consistent signal that&#8217;s less like a timer and more like a metronome, only much faster. Instead of beats per minute, their signal is measured in Hertz, and usually in mega-Hertz at that.<\/p>\n<p>So what exactly does this mean, and what applications are timers\u00a0good for?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-15087\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/IMG_7432-600x450.jpg\" alt=\"IMG_7432\" width=\"600\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/IMG_7432-600x450.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/IMG_7432-768x576.jpg 768w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/IMG_7432-1024x768.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/IMG_7432-800x600.jpg 800w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/IMG_7432-1200x900-cropped.jpg 1200w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Personally, I got interested in timer interrupts after playing around with stepper motors. In my <a href=\"http:\/\/www.instructables.com\/id\/Identifying-Stepper-Wiring-With-the-Pmod-STEP\/\">Pmod STEP tutorial<\/a>, I use a technique known as &#8220;bit-banging&#8221; to control the time between each step. With bit-banging, the output signal is handled inside the main loop of the microcontroller code. In particularly lazy versions (like mine), a delay function is used to pause the code until a certain amount of timer has passed.<\/p>\n<p>This isn&#8217;t the best technique for two reasons. If you&#8217;re not careful when you mix signal generation with your other code, you can accidentally write code that takes too long to execute, causing your signal to hang. To demonstrate this, I&#8217;ve written the following code. This is possibly the worst example of bit-banging I could come up with!<\/p>\n<pre>void loop()\r\n{\r\n  digitalWrite(PIN_LED1, HIGH);\r\n  delay(1000);\r\n  digitalWrite(PIN_LED1, LOW);\r\n  delay(1000);\r\n  \r\n  \/\/ Go through one full rotation\r\n  \/\/ Step 1\r\n  digitalWrite(coil4, LOW);\r\n  digitalWrite(coil1, HIGH);\r\n  delay(10); \/\/ Give the motor time to move\r\n\r\n  \/\/ Step 2\r\n  digitalWrite(coil1, LOW);\r\n  digitalWrite(coil2, HIGH);\r\n  delay(10); \/\/ Give the motor time to move\r\n\r\n  \/\/ Step 3\r\n  digitalWrite(coil2, LOW);\r\n  digitalWrite(coil3, HIGH);\r\n  delay(10); \/\/ Give the motor time to move\r\n\r\n  \/\/ Step 4\r\n  digitalWrite(coil3, LOW);\r\n  digitalWrite(coil4, HIGH);\r\n  delay(10); \/\/ Give the motor time to move\r\n}<\/pre>\n<p>(This is just the loop() segment)<\/p>\n<p>If that code made you cringe, that&#8217;s just a sign that you are a sensible person. Here&#8217;s a GIF\u00a0of it in action:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15091\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/giphy1.gif\" alt=\"Bit Banging\" width=\"480\" height=\"360\" \/><\/p>\n<p>Notice how the motor can only move when the LED isn&#8217;t blinking? That&#8217;s because bit-banging can only do one thing at a time. For microcontrollers, that&#8217;s not good. This is where timers come in!<\/p>\n<p>At the bottom, I&#8217;ve included the text for an example that uses timer interrupts to control the stepper, instead of bit-banging.\u00a0Here&#8217;s a GIF\u00a0of it in action:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15092\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/06\/giphy2.gif\" alt=\"Timer Interrupts\" width=\"480\" height=\"360\" \/><\/p>\n<p>As you can see, now the motor can spin freely, regardless of what the loop() segment is doing!\u00a0The timer interrupt, just like other interrupts, allows us to break out of our normal code and take care of important side functions, but timers allow us to be very precise as well.<\/p>\n<p>Check out <a href=\"http:\/\/chipkit.net\/interrupts-made-easy-with-chipkit\/\">this tutorial<\/a>\u00a0by Shaz on the ChipKIT site. She will\u00a0show you just how inaccurate the timing of the delay() function really is! It works by\u00a0using the timer to measure how long the delay function really lasts, and\u00a0these counts can vary wildly!<\/p>\n<p>I used this code as the basis for my <a href=\"http:\/\/www.instructables.com\/id\/Timer-Interrupts-on-the-DP32\/\">Timer Interrupts tutorial on Instructables<\/a>. In it, I dive deeper into what timers are and how they work, and I explain each step of the code used to control those timers. If you&#8217;re interested in adding timers to your next problem, you should check it out!<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/cdn.instructables.com\/F55\/KUST\/IPYC6CP4\/F55KUSTIPYC6CP4.MEDIUM.jpg\" alt=\"Picture of Timer Interrupts on the DP32\" \/><\/p>\n<p>What will you use timers for?<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-15072 jlk' data-task='like' data-post_id='15072' data-nonce='aa0ba4060c' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-15072 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-15072 jlk' data-task='unlike' data-post_id='15072' data-nonce='aa0ba4060c' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-15072 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-15072 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Jay takes us through the basics of managing timers on a microcontroller!<\/p>\n","protected":false},"author":21,"featured_media":15626,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4312,1563],"tags":[],"ppma_author":[4472],"class_list":["post-15072","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-usb-scopes-analyzers-generators","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2016\/07\/F55KUSTIPYC6CP4.MEDIUM.jpg","authors":[{"term_id":4472,"user_id":21,"is_guest":0,"slug":"jayweeks","display_name":"Jay Weeks","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/c9032c373b20f91ecd2c2ebd3b378c3da3e5f6e50cf0d516941ba1dafaf29b73?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/15072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=15072"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/15072\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/15626"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=15072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=15072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=15072"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=15072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}