{"id":1031,"date":"2024-06-23T15:41:47","date_gmt":"2024-06-23T15:41:47","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1031"},"modified":"2024-08-11T15:00:05","modified_gmt":"2024-08-11T15:00:05","slug":"advanced-c-topic-digital-calculator","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1031","title":{"rendered":"Advanced C topic Digital Calculator"},"content":{"rendered":"<p>We already learn how to interface LCD and keypad to microcontroller. In this topic we cover some math calculation in C and build a function for calculating the basic parameter. This a simple calculator to display value.<\/p>\n<p><style>\/*! elementor - v3.22.0 - 17-06-2024 *\/<br \/>\n.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=\".svg\"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}<\/style>\n<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/calculator-connection-1024x495.jpg\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/calculator-connection-1024x495.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/calculator-connection-300x145.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/calculator-connection-768x371.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/calculator-connection.jpg 1383w\" alt=\"\" width=\"640\" height=\"309\" \/><\/p>\n<p>Here 4&#215;4 matrix keypad is used. Our first goal is to display our fist digit and store in a variable. Since we can input our fist digit until any function key is pressed, so our looping will terminate when &#8216;+&#8217; or &#8216;-&#8216; or &#8216;\/&#8217; or \u00a0&#8216;*&#8217; is pressed.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-1.jpg\" alt=\"\" width=\"1179\" height=\"379\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2.jpg\" sizes=\"(max-width: 1181px) 100vw, 1181px\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2.jpg 1181w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2-300x171.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2-1024x583.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2-768x437.jpg 768w\" alt=\"\" width=\"1181\" height=\"672\" \/><\/p>\n<p>Similar process we can find our second digit. If we pressed &#8216;=&#8217; \u00a0signed than the final result will display. So for display and calculation we need simple steps of else-if statement in C. Suppose we pressed &#8216;+&#8217; function and summation will be display as follow-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-3.jpg\" alt=\"\" width=\"977\" height=\"197\" \/><\/p>\n<p>Suppose after find the second digit again &#8216;+&#8217; is pressed in spate of\u00a0 &#8216;=&#8217;\u00a0 sign. In that case our result value became again the first value and again we need to enter the second digit. Let\u2019s look at the code-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4.jpg\" alt=\"\" width=\"1176\" height=\"336\" \/><\/p>\n<h6><strong>(data type)expression;<\/strong><\/h6>\n<p>Example:-<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 float a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a=(int)(3\/2);<\/p>\n<p>The value of a will be &#8212;&#8211;&gt; a=1.000000<\/p>\n<p>Now our basic structure is ready and we need our clear and ON\/OFF function. Note that every time we get our final result we have to clear our important variables. Our main function will be the integration of all of our functions.\u00a0<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/************************************************************************************************\n*************************************************************************************************\nThis program will display digit of calculator in LCD display\n*************************************************************************************************\n\t\tSubeer Kumar Sarkar\n\t\tElectrical &amp; Electronic Engineer\n************************************************************************************************\n************************************************************************************************\n***********************************************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;stdio.h&gt;\n#include&quot;keypad.h&quot;\n#include&quot;lcd.h&quot;\n#include&quot;calculation.h&quot;\nuint32_t key&#x5B;2];\nunsigned char enter;\nint main(void)\n{\nkeypad_init();\nLCD_INIT();\nLCD_Clear();\n_delay_ms(500);\nSTART:\n   LCD_write_string(1,1,&quot;Normal Calculator&quot;);\n   key&#x5B;0]=digit_1();\n   key&#x5B;1]=digit_2();\n   _delay_ms(1000);\n   enter=key_press(0xFF);\n\twhile(1)\n\t{\n\tWAIT:\n\t   if(enter==0x18)\n\t   {\n\t\t  cash_clear();  \/\/clear the cash\n\t\t  goto START;\n\t   }else\n\t   {\n\t\t _delay_ms(50);\n\t\t enter=key_press(0xFF);\n\t\t goto WAIT;\n\t   }\n   }\n   \nreturn 0;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here we use cancel CAN(cancel) character 0x18 for clear function. Here 2 goto function used. One for calculation and another for reset the calculator to start from the beginning.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Calculator-Atmega8.rar\" style=\"border-radius:10px;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download Calculator<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-upper-heading-font-size wp-block-paragraph\"><strong><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Calculator-Atmega8.rar\">Download the full program with proteus simulation<\/a><\/strong><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We already learn how to interface LCD and keypad to microcontroller. In this topic we cover some math calculation in C and build a function for calculating the basic parameter. This a simple calculator to display value. Here 4&#215;4 matrix keypad is used. Our first goal is to display our fist digit and store in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1031","post","type-post","status-publish","format-standard","hentry","category-i-o-port-operations"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1031"}],"version-history":[{"count":39,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":2551,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions\/2551"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}