0% found this document useful (0 votes)
4 views

Interfacing with LCD and Keypad

The document provides a guide for writing C code to interface a 4x4 keypad with an LCD screen using an ATmega328P microcontroller. It includes the necessary code for initializing the keypad, reading key presses, and displaying the corresponding character on the LCD. Additionally, it references two instructional videos for further assistance.

Uploaded by

Hiếu Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Interfacing with LCD and Keypad

The document provides a guide for writing C code to interface a 4x4 keypad with an LCD screen using an ATmega328P microcontroller. It includes the necessary code for initializing the keypad, reading key presses, and displaying the corresponding character on the LCD. Additionally, it references two instructional videos for further assistance.

Uploaded by

Hiếu Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

13.

Interfacing with LCD and Keypad

Hướng dẫn

Write C code to display letters on the LCD screen based on which a key of
4x4 keypad has been pressed. The schematic uses an atmega328p, a 4x4
keypad, and an LCD screen.

[See Videos]

1. https://www.youtube.com/watch?v=G3A3Tsr6eQE

2. https://www.youtube.com/watch?v=shQOflhze9Y

KEYS:

#include <avr/io.h>
#include <util/delay.h>

#include <avr/interrupt.h>

#include "lcd.h" // Include LCD library (if you have one, or write your own)

#define F_CPU 16000000UL // Define CPU frequency for delay functions

// Define keypad connections (using PORTC)

#define KEYPAD_PORT PORTC

#define KEYPAD_PIN PINC

#define KEYPAD_DDR DDRC

// Keypad row and column definitions

#define ROWS 4

#define COLS 4

// Keypad layout (assuming the keypad is in the following layout)

char keys[ROWS][COLS] = {

{'1', '2', '3', 'A'},

{'4', '5', '6', 'B'},

{'7', '8', '9', 'C'},

{'*', '0', '#', 'D'}

};

// Function to initialize the keypad

void keypad_init() {

KEYPAD_DDR = 0x0F; // Set the first 4 bits (rows) as outputs, the rest as
inputs (columns)

KEYPAD_PORT = 0xFF; // Set all rows high initially

}
// Function to read the pressed key from the keypad

char keypad_get_key() {

for (uint8_t row = 0; row < ROWS; row++) {

KEYPAD_PORT = ~(1 << row); // Set one row low at a time

_delay_ms(5); // Small delay for stable input

for (uint8_t col = 0; col < COLS; col++) {

if (!(KEYPAD_PIN & (1 << (col + 4)))) { // Check if a key in the


column is pressed

return keys[row][col]; // Return the corresponding key

return '\0'; // Return null character if no key is pressed

int main(void) {

// Initialize the LCD and keypad

lcd_init(LCD_DISP_ON); // Initialize the LCD display

keypad_init(); // Initialize the keypad

// Display initial message on LCD

lcd_clear();

lcd_puts("Press a key:");

char key;

while (1) {

key = keypad_get_key(); // Get the key pressed

if (key != '\0') { // If a key is pressed

lcd_clear();
lcd_puts("Key: ");

lcd_putchar(key); // Display the corresponding key on LCD

_delay_ms(500); // Small delay for debouncing

You might also like