{"id":1764,"date":"2023-05-17T04:49:05","date_gmt":"2023-05-17T04:49:05","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1764"},"modified":"2023-05-17T04:49:08","modified_gmt":"2023-05-17T04:49:08","slug":"working-with-stm32-and-keypad-part-2-reading-keypad","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1764","title":{"rendered":"Working with STM32 and keypad Part 2: Reading keypad"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-683x1024.jpeg\" alt=\"\" class=\"wp-image-1765\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-683x1024.jpeg 683w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-200x300.jpeg 200w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-768x1151.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-1025x1536.jpeg 1025w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-1367x2048.jpeg 1367w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-1150x1723.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-750x1124.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-400x599.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-250x375.jpeg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-1-scaled.jpeg 1708w\" sizes=\"(max-width: 683px) 100vw, 683px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1758\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1758\" target=\"_blank\">here<\/a>), we took a look at keypad structure and how to initialize the keypad pins. In this guide, we shall develop the algorithm to read from keypad.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Read keypad function.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Read keypad function:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Before developing the keypad_read function, we need first to declare the keypad structure as following:<\/p>\n\n\n\n<p>Since the keypad is 4&#215;4 with same layout in this picture:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"300\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-200x300.jpeg\" alt=\"\" class=\"wp-image-1767\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-200x300.jpeg 200w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-683x1024.jpeg 683w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-768x1151.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-1025x1536.jpeg 1025w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-1367x2048.jpeg 1367w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-1150x1723.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-750x1124.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-400x599.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-250x375.jpeg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/AdobeStock_358888096-2-scaled.jpeg 1708w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/figure><\/div>\n\n\n\n<p>Hence, the keypad layout as following:<\/p>\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;}\">const unsigned char keymap[4][4]=\n\t{\n\t\t{'1', '2', '3', 'A' },\n\t\t{'4', '5', '6', 'B' },\n\t\t{'7', '8', '9', 'C' },\n\t\t{'*', '0', '#', 'D' }\n\n\n\t};\n<\/pre><\/div>\n\n\n\n<p>This will hold the keypad map as two dimensional array.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we need to scan the entire keypad. to do so, we can use simple for loop to do this.<\/p>\n\n\n\n<p>Before dealing with for loop, we shall declare the following array which will hold the state of pins to be write to GPIOC port as following:<\/p>\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;}\">const uint32_t clo_state[4]={\t(GPIO_BSRR_BR4|GPIO_BSRR_BS5|GPIO_BSRR_BS6|GPIO_BSRR_BS7),\n\t\t\t\t\t\t(GPIO_BSRR_BS4|GPIO_BSRR_BR5|GPIO_BSRR_BS6|GPIO_BSRR_BS7),\n\t\t\t\t\t\t(GPIO_BSRR_BS4|GPIO_BSRR_BS5|GPIO_BSRR_BR6|GPIO_BSRR_BS7),\n\t\t\t\t\t\t(GPIO_BSRR_BS4|GPIO_BSRR_BS5|GPIO_BSRR_BS6|GPIO_BSRR_BR7)\n};\n<\/pre><\/div>\n\n\n\n<p>Since the row pins are set to input with pullup resistors, we need to set the desired column to low in order to have the ability to read the pin. Since using pullup requires the push button to be connected to ground as following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Untitled-669x1024.jpg\" alt=\"\" class=\"wp-image-1768\" width=\"502\" height=\"768\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Untitled-669x1024.jpg 669w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Untitled-196x300.jpg 196w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Untitled-400x612.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Untitled-250x383.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Untitled.jpg 731w\" sizes=\"(max-width: 502px) 100vw, 502px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, we need to set the column bit to zero or ground.<\/p>\n\n\n\n<p>Now, we shall develop the keypad_read function:<\/p>\n\n\n\n<p>Within the following function:<\/p>\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;}\">char keypad_read(void)\n{<\/pre><\/div>\n\n\n\n<p>Declare the following two variables:<\/p>\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;}\">unsigned char key=0,data=0;<\/pre><\/div>\n\n\n\n<p>First variable to hold the key value and second one to hold the data read from the row pins.<\/p>\n\n\n\n<p>Now, declare for loop as following:<\/p>\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;}\">for (int i=0;i&lt;4;i++)\n\t{<\/pre><\/div>\n\n\n\n<p>within for loop, set the column state from the array as following:<\/p>\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;}\">GPIOC-&gt;BSRR=clo_state[i];<\/pre><\/div>\n\n\n\n<p>Now read the first for bit of the IDR register as following:<\/p>\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;}\">data=(GPIOC-&gt;IDR)&amp;0xF; \/*Get rid of data from bit 5 to bit31*\/<\/pre><\/div>\n\n\n\n<p>The reason behind anding the read value with 0xF to get rid of values from bit 4 to 31 which they are not needed.<\/p>\n\n\n\n<p>When no key is pressed, the data will be 0xF due to pullup resistors. When a key is pressed, a value other than 0xF will be read, hence, when the value is not 0xF, we shall decode the required value as following:<\/p>\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;}\">if(data != 0xF)\n\t\t{\n\t\t\tkey=decode_keypad(i,data);\n\t\t}<\/pre><\/div>\n\n\n\n<p>Then close the for loop and return the key value as following:<\/p>\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;}\">}\n\n\treturn key;<\/pre><\/div>\n\n\n\n<p>For decoding keypad function:<\/p>\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;}\">static char decode_keypad(uint8_t col, uint8_t row)<\/pre><\/div>\n\n\n\n<p>It takes two arguments as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>col value with is the current column indicated by variable i.<\/li><li>row value which is indicated by the read data.<\/li><\/ul>\n\n\n\n<p>Within the function:<\/p>\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;}\">\tif (row == 0xE) return keymap[0][col];\n\tif (row == 0xD) return keymap[1][col];\n\tif (row == 0xB) return keymap[2][col];\n\tif (row == 0x7) return keymap[3][col];\n\treturn 0;<\/pre><\/div>\n\n\n\n<p>The function will check the row value with 0xE, 0xD, 0xB and 0x7 respectively to determine which key in the column is pressed and return the respective value.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire source code as following:<\/p>\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\n#include &quot;keypad.h&quot;\n\nstatic char decode_keypad(uint8_t col, uint8_t row);\n\n\n\nconst unsigned char keymap[4][4]=\n\t{\n\t\t{'1', '2', '3', 'A' },\n\t\t{'4', '5', '6', 'B' },\n\t\t{'7', '8', '9', 'C' },\n\t\t{'*', '0', '#', 'D' }\n\n\n\t};\n\n\nconst uint32_t clo_state[4]={\t(GPIO_BSRR_BR4|GPIO_BSRR_BS5|GPIO_BSRR_BS6|GPIO_BSRR_BS7),\n\t\t\t\t\t\t(GPIO_BSRR_BS4|GPIO_BSRR_BR5|GPIO_BSRR_BS6|GPIO_BSRR_BS7),\n\t\t\t\t\t\t(GPIO_BSRR_BS4|GPIO_BSRR_BS5|GPIO_BSRR_BR6|GPIO_BSRR_BS7),\n\t\t\t\t\t\t(GPIO_BSRR_BS4|GPIO_BSRR_BS5|GPIO_BSRR_BS6|GPIO_BSRR_BR7)\n};\n\n\n\n\nvoid keypad_init(void)\n{\n\n\t\/*Enable clock access to GPIOC*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOCEN;\n\n\t\/*Set PC0 to PC3 as input*\/\n\tGPIOC-&gt;MODER &amp;= ~(GPIO_MODER_MODE0|GPIO_MODER_MODE1|GPIO_MODER_MODE2|GPIO_MODER_MODE3);\n\n\t\/* Activate internal pullup resistor for PC0 to PC3*\/\n\tGPIOC-&gt;PUPDR|=GPIO_PUPDR_PUPD0_0|GPIO_PUPDR_PUPD1_0|GPIO_PUPDR_PUPD2_0|GPIO_PUPDR_PUPD3_0;\n\n\t\/*Set PC4 to PC7 as output*\/\n\tGPIOC-&gt;MODER |= GPIO_MODER_MODE4_0|GPIO_MODER_MODE5_0|GPIO_MODER_MODE6_0|GPIO_MODER_MODE7_0;\n\n\tGPIOC-&gt;MODER &amp;=~( GPIO_MODER_MODE4_1|GPIO_MODER_MODE5_1|GPIO_MODER_MODE6_1|GPIO_MODER_MODE7_1);\n\n\t\/*Set PC4 to PC7 as high*\/\n\n\tGPIOC-&gt;BSRR = GPIO_BSRR_BS4|GPIO_BSRR_BS5|GPIO_BSRR_BS6|GPIO_BSRR_BS7;\n}\n\n\nchar keypad_read(void)\n{\n\n\tunsigned char key=0,data=0;\n\n\tfor (int i=0;i&lt;4;i++)\n\t{\n\n\t\tGPIOC-&gt;BSRR=clo_state[i];\n\n\t\tdata=(GPIOC-&gt;IDR)&amp;0xF; \/*Get rid of data from bit 5 to bit31*\/\n\n\t\tif(data != 0xF)\n\t\t{\n\t\t\tkey=decode_keypad(i,data);\n\t\t}\n\n\t}\n\n\treturn key;\n}\n\n\nstatic char decode_keypad(uint8_t col, uint8_t row)\n{\n\n\tif (row == 0xE) return keymap[0][col];\n\tif (row == 0xD) return keymap[1][col];\n\tif (row == 0xB) return keymap[2][col];\n\tif (row == 0x7) return keymap[3][col];\n\treturn 0;\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c :<\/p>\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;delay.h&quot;\n#include &quot;stdio.h&quot;\n#include &quot;LiquidCrystal_PCF8574.h&quot;\n#include &quot;keypad.h&quot;\n\n\nchar lcd_data[30];\n\nchar key;\n\n\nint main(void)\n{\n\n\n\tlcd_init();\n\tkeypad_init();\n\n\n\twhile(1)\n\t{\n\t\tkey=keypad_read();\n\t\tif(key)\n\t\t{\n\t\t\tsetCursor(0, 0);\n\t\t\tsprintf(lcd_data,&quot;Key pressed is %c&quot;,key);\n\t\t\tlcd_send_string(lcd_data);\n\t\t}\n\t\tdelay(30);\n\n\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Code:<\/h2>\n\n\n\n<p>You may download from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Keypad.zip\">Keypad<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/05\/Keypad.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Results:<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Keypad interface with STM32F4\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/wDa8iipSq3o?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at keypad structure and how to initialize the keypad pins. In this guide, we shall develop the algorithm to read from keypad. In this guide, we shall cover the following: Read keypad function. Code. Results. 6. Read keypad function: Before developing the keypad_read function, we need [&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,19,11,12],"tags":[],"class_list":["post-1764","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-lcd","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1764"}],"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=1764"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1764\/revisions"}],"predecessor-version":[{"id":1770,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1764\/revisions\/1770"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}