4×4 keypad example using AVR-GCC C language

Summary of 4×4 keypad example using AVR-GCC C language


This article explains a simple method to read a 4x4 keypad using an AVR microcontroller and AVR-GCC. It configures PORTB, splitting it into input rows (pins 0-3) and output columns (pins 4-7). The code sets the high nibble as outputs and enables internal pull-ups for the low nibble. A loop checks for pressed keys by setting column bits to zero and reading row states. 10k resistors are mentioned for protection against shorts.

Parts used in Keypad Reading Project:

  • AVR Microcontroller
  • 4x4 Keypad
  • 10k Resistors
  • Port B (PINB0-PINB7)

This is as simple routine how to read 4×4 keypad keys using AVR-GCC language. The keypad is connected to AVR microcontroller 8 bit port. In this example it is B port. You can change ports depending on your needs – this is only an example ant it is not the only way to this.

keypad

Howt it works. Well very simply. PORTB is divided into two nibbles PINB0 – PINB3 as inputs (as rows) and PINB4-PINB7 as outputs (columns). The keys are checked in a loop in series. Lets say if we set first row output (PORTB bit 7) to 0 then when checking rows we are looking which bit is set to 0, because of key pressed with function bit_is_set(PINB, bitNo). This function gives non-zero if bit is clear.
10k resistors protect AVR from shortcuts.
4x4 keypad

#include <avr/io.h>
int main()
{
//high nibble for output(columns) low for input(rows);
DDRB=0xF0;
//enable internal pullups for PB0-PB3
PORTB=0x0F;
//Port D for indication only
DDRD=0xFF;
while (1) //loop key check forever
For more detail: 4x4 keypad example using AVR-GCC C language

 

Quick Solutions to Questions related to Keypad Reading Project:

  • Which port is used for the keypad connection?
    The example uses Port B, though other ports can be changed based on needs.
  • How is Port B divided for this project?
    Pins 0 through 3 are set as inputs for rows, while pins 4 through 7 are set as outputs for columns.
  • What function checks if a key is pressed?
    The function bit_is_set(PINB, bitNo) is used to check if a bit is clear when a key is pressed.
  • Why are 10k resistors included in the circuit?
    They protect the AVR microcontroller from shortcuts.
  • What value is assigned to DDRB to configure the port?
    DDRB is set to 0xF0 to make the high nibble outputs and low nibble inputs.
  • How are internal pull-ups enabled in the code?
    PORTB is set to 0x0F to enable internal pull-ups for PB0 through PB3.
  • What is the purpose of Port D in this example?
    Port D is configured only for indication purposes.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top