AVR Interfacing:Hex Kepad
Let's look at interfacing a Hex keypad, the one with 16 switches; arranged in 4 columns and 4 rows. The interesting challenge is to interface 16 switches on 8 pins. So how does this work? The simple answer is the MCU is faster than the your fingers. The code has been tested and demonstrated on Ultra AVR Dev Kit with Atmega32, however it should work on any AVR MCU with appropriate modifications.
Contents
[hide]Basics
Figure below shows the way in which the keys of a hex hey pad are arranged.
Hook Up
Code
- #include "keypad.h"
- #include "lcd.h"
- int main()
- {
- uint8_t key;
- /*Connect RS->PB0, RW->PB1, EN->PB2 and data bus PORTB.4 to PORTB.7*/
- LCD_SetUp(PB_0,PB_1,PB_2,P_NC,P_NC,P_NC,P_NC,PB_4,PB_5,PB_6,PB_7);
- LCD_Init(2,16);
- /*Connect R1->PD0, R2->PD1, R3->PD2 R4->PD3, C1->PD4, C2->PD5 C3->PD6, C4->PD7 */
- KEYPAD_Init(PD_0,PD_1,PD_2,PD_3,PD_4,PD_5,PD_6,PD_7);
- LCD_Printf("Key Pressed:");
- while (1)
- {
- key = KEYPAD_GetKey();
- LCD_GoToLine(1);
- LCD_DisplayChar(key);
- }
- return (0);
- }
Let's look at the most important function in the code in a little more details
- key = KEYPAD_GetKey();
As you can guess, the function return the ASCII value of the key being pressed. It follows the following sequences to decode the key pressed:
- Wait till the previous key is released.
- Wait for the new key press.
- Scan all the rows one at a time for the pressed key.
- Decodes the key pressed depending on ROW-COL combination and returns its ASCII value.
- It is defined in keypad.c as below:
- uint8_t KEYPAD_GetKey(void)
- {
- uint8_t i,j,v_KeyPressed_u8 = 0;
- keypad_WaitForKeyRelease();
- keypad_WaitForKeyPress();
- for (i=0;i<C_MaxRows_U8;i++)
- {
- GPIO_PinWrite(A_RowsPins_U8[i],HIGH);
- }
- for (i=0;(i<C_MaxRows_U8);i++)
- {
- GPIO_PinWrite(A_RowsPins_U8[i],LOW);
- for(j=0; (j<C_MaxCols_U8); j++)
- {
- if(GPIO_PinRead(A_ColsPins_U8[j]) == 0)
- {
- v_KeyPressed_u8 = 1;
- break;
- }
- }
- if(v_KeyPressed_u8 ==1)
- {
- break;
- }
- GPIO_PinWrite(A_RowsPins_U8[i],HIGH);
- }
- if(i<C_MaxRows_U8)
- v_KeyPressed_u8 = A_KeyLookUptable_U8[i][j];
- else
- v_KeyPressed_u8 = C_DefaultKey_U8;
- return v_KeyPressed_u8;
- }
Video Tutorial
For those of you, who would like to watch instead of read we have made a video with all the gyan.
Downloads
Download the complete project folder from the below link:
https://github.com/ExploreEmbedded/ATmega32_ExploreUltraAvrDevKit/archive/master.zip
Have a opinion, suggestion , question or feedback about the article let it out here!

AVR I/O Register Configuration
In this tutorial we are going to discuss the port configuration of AVR/Atmel controllers or in general Atmega family. In this tutorial we will be using Atmega32 as reference, same will be applicable...

Basics of AVR 'C'
Let us look at the basics of 'C' for programming AVR Micrcontrollers in this tutorial. Simple stuff like setting and clearing bits is important to any project you do. It is often required to set...
AVR Hardware and Software Setup
In this tutorial we will look at the basic setup required to get started with AVR series of microcontrollers. There are two aspects to it, the software and the hardware. Fortunately, we for AVR...

AVR Timer programming
Basics Timers come in handy when you want to set some time interval like your alarm. This can be very precise to a few microseconds. Timers/Counters are essential part of any modern...