{"id":966,"date":"2024-06-23T14:54:59","date_gmt":"2024-06-23T14:54:59","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=966"},"modified":"2024-08-11T14:57:40","modified_gmt":"2024-08-11T14:57:40","slug":"keypad-interfacing","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=966","title":{"rendered":"Keypad Interfacing"},"content":{"rendered":"<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\/keypad1.png\" sizes=\"(max-width: 590px) 100vw, 590px\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/keypad1.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/keypad1-300x157.png 300w\" alt=\"\" width=\"569\" height=\"297\" \/><\/p>\n<p>Here the RAWs R0 to R3 are connected to the input lines of \u00b5C and C0 to C3 are connected to output terminal of the output port. The inputs are active when logic zero that is internal pull up active and the column port are high at that position. The logic is the output port is continuously set and clear and when a switch is the corresponding PIN of output port is low other two pins are high at that time.<\/p>\n<p>Let consider the following picture-<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/33keypad.png\" sizes=\"(max-width: 590px) 100vw, 590px\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/33keypad.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/33keypad-300x123.png 300w\" alt=\"\" width=\"651\" height=\"267\" \/><\/p>\n<p>Suppose that if we connect the pins according to the matrix keypad where PIN[0-2] as output, PIN[3-5] as input and internal pull up active, the generated code will be \u2013<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/3x3-code.jpg\" alt=\"\" width=\"1167\" height=\"52\" \/><\/p>\n<p>Let\u2019s build a simplified model to generate any matrix keypad using one port only is bellow-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/c-structure-1.jpg\" alt=\"\" width=\"200\" height=\"37\" \/><\/p>\n<h6><strong>goto function<\/strong>:-<\/h6>\n<p>goto statement is used for jumping into the C program. It can be forward jump or backward jump.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/goto-logo.jpg\" alt=\"\" width=\"1112\" height=\"263\" \/><\/p>\n<h6><strong>break and continue statements<\/strong>:<\/h6>\n<p>Both break and continue statement is used into a loop. Both has significant impact into the loop. The break statement is used for exit from a nested loop. The continue statement break one iteration (in a loop) if a specified condition occurs and continues with the next iteration in the loop.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/continue-goto.jpg\" alt=\"\" width=\"1172\" height=\"242\" \/><\/p>\n<h6><strong>C:&gt;<\/strong><\/h6>\n<h6><strong>unsigned char press,keycode,keyPress,i;<\/strong><\/h6>\n<h6><strong>unsigned char key_press(unsigned char maximum_value) <\/strong><\/h6>\n<h6><strong>\/*******for 4&#215;4 matrix keypad, the maximum value= 0xff *************\/<\/strong><\/h6>\n<h6><strong>for(i=0;i&lt;column;i++)<\/strong><strong style=\"font-size: 20px; letter-spacing: 0px; text-align: var(--text-align); background-color: transparent;\">{<\/strong><\/h6>\n<h6><strong>_delay_ms(1); \/\/ delay for I\/O port setting<\/strong><\/h6>\n<h6><strong>PORTx=[maximum value] &amp; (~(0x01&lt;&lt;i));<\/strong><\/h6>\n<h6><strong>_delay_ms(1); \/\/ delay for I\/O port setting<\/strong><\/h6>\n<h6><strong>press=PINx|[maximum column value];<\/strong><\/h6>\n<h6><strong>if(press != maximum value)<\/strong><\/h6>\n<h6><strong>_delay_ms(20); \/\/key debouncing\u00a0 delay <\/strong><\/h6>\n<h6><strong>press=PINx|[maximum column value];<\/strong><\/h6>\n<h6><strong>if(press==[maximum value]) goto OUT;<\/strong><\/h6>\n<h6><strong>keycode=(press &amp; [maximum row value])|(maximum column value)&amp;\u00a0<\/strong><strong style=\"font-size: 20px; letter-spacing: 0px; text-align: var(--text-align); background-color: transparent;\">[maximum value] &amp; (~(0x01&lt;&lt;i));<\/strong><\/h6>\n<h6><strong>while(press !=[maximum value])<\/strong><\/h6>\n<h6><strong>press=PINx|[maximum value];<\/strong><\/h6>\n<h6><strong>_delay_ms(20); \/\/key debouncing\u00a0 delay <\/strong><\/h6>\n<h6><strong>switch(keycode)<\/strong><\/h6>\n<h6><strong>case (PORTx value when switch press) : KeyPress=\u2019charecter\u2019\/number;<\/strong><\/h6>\n<h6><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/strong><\/h6>\n<h6><strong>&#8212;&#8212;&#8212;&#8212;&#8212;<\/strong><\/h6>\n<h6><strong>}\u00a0 \/\/end of switch<\/strong><\/h6>\n<h6><strong>OUT:;<\/strong><\/h6>\n<h6><strong>}\/\/end of if<\/strong><\/h6>\n<h6><strong>}\/\/end of for <\/strong><\/h6>\n<h6><strong>return (keyPress);<\/strong><\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/3x4-keypad.jpg\" alt=\"\" width=\"1220\" height=\"55\" \/><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nunsigned char key_press(unsigned char press)\n{\nfor(i=0;i&lt;3;i++)\n\t{\n\t_delay_ms(1);\n\tKB_PORT_OUT=0x7f &amp;(~(0x01&lt;&lt;i));\n\t_delay_ms(1);\n\tpress=KB_PORT_IN | 0x07;\n\tif(press != 0x7f)\n\t{\n\t_delay_ms(20);\n\tpress=KB_PORT_IN | 0x07;\n\tif(press == 0x7f) goto OUT;\n\tkeycode=(press&amp; 0x78) |(0x07 &amp; (0x7f &amp;(~(0x01&lt;&lt;i))));\n\twhile(press != 0x7f)\n\tpress=KB_PORT_IN | 0x07;\n\t_delay_ms(20);\n\tswitch(keycode)\n\t\t{\n\t\t\tcase (0x76): keyPressed = &#039;1&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x6e): keyPressed = &#039;4&#039;;\n\t\t\t\t \t\t break;\n\t\t\tcase (0x5e): keyPressed = &#039;7&#039;; \n\t\t\t   \t\t\t break;\n\t\t\tcase (0x3e): keyPressed = &#039;*&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x75): keyPressed = &#039;2&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x6d): keyPressed = &#039;5&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x5d): keyPressed = &#039;8&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x3d): keyPressed = &#039;0&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x73): keyPressed = &#039;3&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x6b): keyPressed = &#039;6&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x5b): keyPressed = &#039;9&#039;; \n\t\t\t\t \t\t break;\n\t\t\tcase (0x3b): keyPressed = &#039;#&#039;; \n\t\t\t\t \t\t break;\n\t\t}\n\t\tOUT:;\n\t}\n\t}\n\nreturn (keyPressed);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The similar technique can be applied to all keypads. We only tested it in 3&#215;3, 3&#215;4, 4&#215;3, 4&#215;4 matrix keypad and works fine. To use this keypad header files all you need is set Keypad lower order bits as output port and lower higher order bit to input pin. For your simplicity here is a simple program of 4&#215;4 matrix keypad.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"302\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mcu-with-keypad-1024x302.jpg\" alt=\"\" class=\"wp-image-1022\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mcu-with-keypad-1024x302.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mcu-with-keypad-300x89.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mcu-with-keypad-768x227.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mcu-with-keypad.jpg 1344w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If any switch is pressed it will display that character. C main program-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/************************************************************************************************\n*************************************************************************************************\nThis program will display digit of mobile keypad 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;\nchar key&#x5B;3],j;\nint main(void)\n{\nkeypad_init();\nLCD_INIT();\nLCD_Clear();\nLCD_write_string(1,1,&quot;Press any key&quot;);\n_delay_ms(500);\nwhile(1)\n{\nj=key_press(0x7f);\nsprintf(key,&quot;%c&quot;,j);\nLCD_write_string(1,2,key);\n}\nreturn 0;\n}\n<\/pre><\/div>\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\/keypad-Interfacing.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<\/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\/keypad-Interfacing.rar\">Download the full program with proteus simulation<\/a><\/strong><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here the RAWs R0 to R3 are connected to the input lines of \u00b5C and C0 to C3 are connected to output terminal of the output port. The inputs are active when logic zero that is internal pull up active and the column port are high at that position. The logic is the output port [&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-966","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\/966","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=966"}],"version-history":[{"count":52,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/966\/revisions"}],"predecessor-version":[{"id":2549,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/966\/revisions\/2549"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}