{"id":4249,"date":"2015-01-02T10:59:14","date_gmt":"2015-01-02T18:59:14","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=4249"},"modified":"2021-06-17T08:26:18","modified_gmt":"2021-06-17T15:26:18","slug":"a-listening-calculator","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/a-listening-calculator\/","title":{"rendered":"A Listening Calculator"},"content":{"rendered":"<p>Welcome back to the Digilent Blog!<\/p>\n<p>&nbsp;<\/p>\n<p>When working with microcontrollers, it&#8217;s pretty straightforward to have your system board &#8220;listen&#8221; for an input that you would give it and have it do some sort of action to show that it noticed your input, such as pressing a button to light up an LED. Listening to a set of inputs and then comparing them to a predetermined set, like in the <a title=\"Simon Says\u2026 with LEDs!\" href=\"https:\/\/digilent.com\/blog\/index.php\/simon-says-with-leds\/\">Simon Says game<\/a>, is a little more involved but definitely doable. But what if we did not compare to any internal values and the system board has no idea how many inputs we might provide? This is a common situation in things like four function calculators; it has no idea how big the numbers will be, nor when we might press the = sign or + sign. The microcontroller can compensate for this, at least in my design, by performing what is known as active listening.<\/p>\n<p>&nbsp;<\/p>\n<p>So what is active listening (in terms of an electronic system)? This is where the system board continually checks if a certain condition has been met, or in the case of a four function calculator, continually checks to see if a button is being pressed. This continual checking is known as polling and is a pretty efficient listening method if you are not concerned with potentially not receiving the input for a few microseconds (over a 1000 times faster than a chameleons tongue catching a bug) or the larger power draw as your system board is continually working to check the status of the inputs. In terms of a calculator, I&#8217;m not concerned about noticing a button being pressed immediately or about the power draw since I have my chipKIT&#x2122; uC32 powered from\u00a0my computer&#8217;s USB port.<\/p>\n<p>&nbsp;<\/p>\n<p>Essentially, this boils down to that it&#8217;s pretty straightforward to receive a &#8220;random&#8221; input by polling (however, please leave a comment if you have any questions). But after we get this input, which will translate to a &#8216;logic high&#8217; voltage signal, how do we then merge this signal into both a number that we are typing and a single character to display?<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_4264\" aria-describedby=\"caption-attachment-4264\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/logic-high-to-characters-and-integers.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4264\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/logic-high-to-characters-and-integers.jpg\" alt=\"How do we take a logic '1' and get both a char and an int?\" width=\"600\" height=\"448\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/logic-high-to-characters-and-integers.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/logic-high-to-characters-and-integers-225x168.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4264\" class=\"wp-caption-text\">How do we take a logic &#8216;1&#8217; and get both a char and an int?<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>What I ended up doing for my calculator was creating a switch-case statement that looks at which button on the PmodKYPD was pressed and gives another function the character that represents that particular button. The received character can then be immediately printed to the PmodOLED screen in the appropriate location depending on if it is one of the &#8216;operator&#8217; characters such as the &#8216;+&#8217; or &#8216;=&#8217; signs or if it is a number character. When the pressed button is a number character, its decimal value is added to the number we were originally typing out. However, this raises an important question. How would the microcontroller &#8216;know&#8217; what the decimal value of a character is? One way to do this is through typecasting. This is a technique where you can designate how you want the system board to interpret a particular <a title=\"True or false: Boolean is a Data Type?\" href=\"https:\/\/digilent.com\/blog\/index.php\/true-or-false-boolean-is-a-data-type\/\" target=\"_blank\" rel=\"noopener\">data type<\/a>, such as interpreting a character as an integer.<\/p>\n<p>&nbsp;<\/p>\n<p>For example, if we wanted to type the number &#8217;37&#8217; we would first press the &#8216;3&#8217; button on the keypad to get our &#8216;3&#8217; character. According to the ASCII Table, the character &#8216;3&#8217; corresponds to the decimal number 51, meaning if we subtracted a decimal value of 48 from our &#8216;3&#8217; character, we would get the character corresponding to the decimal value of 3 (&#8216;ETX&#8217;). We can then typecast this character as an integer (to get our decimal value of 3) and add it to ten times our pre-existing number value, which in this case is a zero, giving us a value of 3.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_4260\" aria-describedby=\"caption-attachment-4260\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4260\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-3.jpg\" alt=\"Typecasting to turn a character 3 into a number 3\" width=\"600\" height=\"237\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-3.jpg 845w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-3-600x237.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-3-225x89.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4260\" class=\"wp-caption-text\">Typecasting to turn a character 3 into a number 3 (click on the picture\u00a0to enlarge)<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Then we would press the &#8216;7&#8217; button to get the &#8216;7&#8217; character and subtract a decimal value of 48 to get the character with the decimal value of 7. We then typecast this new character as an integer and add it to ten times our pre-existing number value, a 3 turned into a 30, to give us a new value of 37.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_4261\" aria-describedby=\"caption-attachment-4261\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4261\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37.jpg\" alt=\"Typecasting to turn a character 7 into a number 7 and add it to an existing number\" width=\"600\" height=\"406\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37.jpg 853w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37-600x406.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37-225x152.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4261\" class=\"wp-caption-text\">Typecasting to turn a character 7 into a number 7 and add it to an existing number (click on the picture\u00a0to enlarge)<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Naturally, calculators (even four-function calculators) do more than just allow you to type numbers onto a screen; they also allow you to do some basic math without forcing you to remember how to do long division. In terms of our calculator, the system board just needs to keep track of the first value that we put in and the second value that we entered and perform the appropriate operation, like adding or dividing, based on another switch-case statement. The tricky bit comes when writing a function to print out our answer on the screen since we have to individually convert every number (and the decimal point if there is one) into the correct character. Otherwise the number 78 would print as the character &#8216;N&#8217;, which isn&#8217;t terribly helpful in terms of math.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_4259\" aria-describedby=\"caption-attachment-4259\" style=\"width: 601px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/N-50um.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4259\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/N-50um.jpg\" alt=\"N - 50 is rather hard to compute when you don't know what N is\" width=\"601\" height=\"387\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/N-50um.jpg 3657w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/N-50um-600x387.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/N-50um-1024x660.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/N-50um-225x145.jpg 225w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><figcaption id=\"caption-attachment-4259\" class=\"wp-caption-text\">N &#8211; 50 is rather hard to compute when you don&#8217;t know what N is<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>One way to accomplish this is to slowly divide your number by larger and larger increments of 10 (starting from 1) and wait to see how many of these increments it takes to get your calculated number to a single digit. This lets you know how many digits long your number is. You can then start at that largest division value to get your first (and largest) digit and turn it into the appropriate ASCII char value by adding 0x30 (decimal value of 48 in hexadecimal) and then displaying it on the screen. You can then find your second largest digit by dividing by the next smallest factor of 10 and then subtracting anything &#8220;tens place&#8221; and larger from it to be left with just the single digit number.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_4255\" aria-describedby=\"caption-attachment-4255\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-number-places.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4255\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-number-places.jpg\" alt=\"Calculating and placing main number places\" width=\"600\" height=\"285\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-number-places.jpg 1055w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-number-places-600x285.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-number-places-1024x486.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-number-places-225x107.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4255\" class=\"wp-caption-text\">Calculating and placing main number places (click on the picture to enlarge)<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Once you have gone through and displayed your digits you could also, if you so desired, check for any decimal points. Here, we would need our initially calculated value to be a <a title=\"True or false: Boolean is a Data Type?\" href=\"https:\/\/digilent.com\/blog\/index.php\/true-or-false-boolean-is-a-data-type\/\" target=\"_blank\" rel=\"noopener\">double<\/a>\u00a0so that any decimal points would exist in the first place. We can then subtract from our calculated value its &#8220;typecasted&#8221; integer version to leave us with just the decimal points. With just the decimal points, we can then multiply them by factors of 10 to get our single digit values, remembering to subtract off the higher numbers after we start multiplying by 100 or greater.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_4256\" aria-describedby=\"caption-attachment-4256\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-decimal-places.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4256\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-decimal-places.jpg\" alt=\"Calculating and placing decimal places\" width=\"600\" height=\"184\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-decimal-places.jpg 1245w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-decimal-places-600x184.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-decimal-places-1024x314.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/calculating-and-placing-decimal-places-225x69.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4256\" class=\"wp-caption-text\">Calculating and placing decimal places (click on the picture to enlarge)<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>In terms of practicality, you might be wondering how you could make sure that you keep everything looking neat and pretty on the display since there is a lot of background manipulation involved. The answer to this is to place limits on how big the numbers can be and how much of them will be shown on the display. A signed integer (can be positive or negative) on a chipKIT processor is only able to be as large as +- 2,147,483,647. With this in mind, it would be a good idea to keep the input numbers within the range of +- 1,000,000,000 where anything larger produces (as per your screen output) an overflow error forcing the user to work with smaller numbers if they want the calculator to keep working correctly.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_4266\" aria-describedby=\"caption-attachment-4266\" style=\"width: 599px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4266\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error.jpg\" alt=\"An example of an overflow error\" width=\"599\" height=\"354\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error.jpg 3893w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error-600x354.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error-1024x605.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error-225x133.jpg 225w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error-800x473.jpg 800w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/a><figcaption id=\"caption-attachment-4266\" class=\"wp-caption-text\">An example of an overflow error<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>The code that I used to create this setup can be found <a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/Four_Function_Calculator.txt\">here<\/a>.<\/p>\n<p>What cool projects would you want to try out with a combination of Pmods?<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-4249 jlk' data-task='like' data-post_id='4249' data-nonce='d8c4d58d14' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-4249 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-4249 jlk' data-task='unlike' data-post_id='4249' data-nonce='d8c4d58d14' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-4249 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-4249 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>When working with microcontrollers, it&#8217;s pretty straightforward to have your system board &#8220;listen&#8221; for an input that you would give it and have it do some sort of action to show that it noticed your input, such as pressing a button to light up an LED. Listening to a set of inputs and then comparing them to a predetermined set, like in the Simon Says game, is a little more involved but definitely doable. But what if we did not compare to any internal values and the system board has no idea how many inputs we might provide? <\/p>\n","protected":false},"author":17,"featured_media":4266,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38,4327],"tags":[],"ppma_author":[4469],"class_list":["post-4249","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-expansion-modules","category-projects"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/overfow-error.jpg","jetpack_sharing_enabled":true,"authors":[{"term_id":4469,"user_id":17,"is_guest":0,"slug":"jamescolvin","display_name":"James Colvin","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/8aa85f7d11711acc7e571e1ed26c901b614a7064a2e15e522f54d9f26792ea9e?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\/4249","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\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=4249"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/4249\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/4266"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=4249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=4249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=4249"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=4249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}