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

Lab 07

Uploaded by

Rasham Saqib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 07

Uploaded by

Rasham Saqib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Electrical and Computer Engineering

Course Instructor: Dr. Asadullah Bukhari Dated: 24th May 2023

Semester: 4th Section: EE2

ECE-261L: Introduction to Embedded Systems Lab

Lab 07: Design & Implementation of a Password Protected System using


Keypad and LCD with AVR

Name Reg. No. Lab Tasks Lab Report Viva Marks Total Marks
Marks Marks

20 10 10 30
(Scaled 5) (Scaled 5)

Rasham Saqib B21F0476EE019


Lab 07: Design & Implementation of a Password Protected System using Keypad
and LCD with AVR

Lab Task: Implement a password protected system for a single user by interfacing a Keypad and
16x2 LCD with ATMEGA328P device. The system should ask the user for an 8-digit predefined
password and only allow 3 attempts. The typed password should be shown as ‘*’ on the system.
After the third consecutive unsuccessful attempt an 8-digit master password starting with the
character ‘*’ should be entered to the reset the number of attempts to 0. There would be only 2
consecutive unsuccessful attempts for the master password. After that the system should not
respond until it is hard reset. A few runs of the system are given below:

Password Password
********

Wrong Password
Try Again (1)
Password
******** 1 secs delay

Password
Welcome
********
Username
Wrong Password
10 secs delay Try Again (2)

1 secs delay
Password
Password
********

System Locked!!!

System Locked!!! System Locked!!!


******** ********

Password System Locked!!!


********

Reset Required
(Blinking)
Code:
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>

// Define the LCD connections


#define LCD_DATA_PORT PORTC
#define LCD_DATA_DDR DDRC
#define LCD_EN_PIN PC2

// Define the keypad connections


#define KEYPAD_PORT PORTD
#define KEYPAD_PIN PIND
#define KEYPAD_DDR DDRD

// Define the password and master password


char password[] = "12345678";
char masterPassword[] = "*masterpw";

// Function prototypes
void LCD_init();
void LCD_command(unsigned char cmd);
void LCD_data(unsigned char data);
void LCD_setCursor(uint8_t row, uint8_t col);
char keypad_getKey();
void keypad_waitForKeyRelease();
void keypad_resetAttempts();

int main()
{
// Initialize LCD and keypad
LCD_init();
keypad_resetAttempts();

while (1) {

LCD_clear();
LCD_print("Password:");
LCD_setCursor(1, 0);

// Read the user's input


char enteredPassword[9] = { 0 };
for (uint8_t i = 0; i < 8; i++) {
char key = keypad_getKey();
enteredPassword[i] = key;
LCD_data('*');
_delay_ms(200);
}

// Check if the entered password is correct


if (strcmp(enteredPassword, password) == 0) {
// Password is correct, grant access
LCD_clear();
LCD_print("Welcome!");
_delay_ms(2000);
break;
} else {
// Password is incorrect, increment attempt count
keypad_resetAttempts();
}
}

while (1) {
// Prompt the user to enter the master password
LCD_clear();
LCD_print("Master PW:");
LCD_setCursor(1, 0);

// Read the user's input


char enteredMasterPassword[9] = { 0 };
for (uint8_t i = 0; i < 8; i++) {
char key = keypad_getKey();
enteredMasterPassword[i] = key;
LCD_data('*');
_delay_ms(200);
}

while (1) {
// System locked, no response until hard reset
LCD_clear();
LCD_print("System Locked!!!");
_delay_ms(200);
}

return 0;
}

void LCD_init()
{
LCD_DATA_DDR = 0xFF; // Set the LCD data port as output
DDRB |= (1 << LCD_RS_PIN) | (1 << LCD_RW_PIN) | (1 << LCD_EN_PIN); // Set the control
pins as output

// Initialize the LCD in 4-bit mode


_delay_ms(20); // Wait for the LCD power-up time
LCD_command(0x02); // 4-bit mode, 2-line, 5x7 font
LCD_command(0x28); // Display on, cursor off, blink off
LCD_command(0x06); // Entry mode: increment, no shift
LCD_command(0x01); // Clear the display
_delay_ms(2); // Wait for the LCD to clear
LCD_command(0x80); // Set the cursor to the beginning of the first line
}

void LCD_command(unsigned char cmd)


{
LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | (cmd & 0xF0); // Send the higher nibble
PORTB &= ~(1 << LCD_RS_PIN); // RS = 0 for command
PORTB &= ~(1 << LCD_RW_PIN); // RW = 0 for write
PORTB |= (1 << LCD_EN_PIN); // EN = 1 to enable
_delay_us(1); // Wait for the enable pulse width
PORTB &= ~(1 << LCD_EN_PIN); // EN = 0 to disable

_delay_us(100);

LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | (cmd << 4); // Send the lower nibble
PORTB |= (1 << LCD_EN_PIN); // EN = 1 to enable
_delay_us(1); // Wait for the enable pulse width
PORTB &= ~(1 << LCD_EN_PIN); // EN = 0 to disable

_delay_us(100);
}

LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | (data << 4); // Send the lower nibble
PORTB |= (1 << LCD_EN_PIN); // EN = 1 to enable
_delay_us(1); // Wait for the enable pulse width
PORTB &= ~(1 << LCD_EN_PIN); // EN = 0 to disable

_delay_us(100);
}

void LCD_setCursor(uint8_t row, uint8_t col)


{
uint8_t address = 0x80; // DDRAM address of the first character in the first row

if (row == 1)
address += 0x40; // DDRAM address of the first character in the second row

address += col; // DDRAM address of the desired character position

LCD_command(address | 0x80); // Set the cursor to the desired position


}

char keypad_getKey()
{
const unsigned char keypadSymbols[4][4] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};

uint8_t row, col;

for (col = 0; col < 4; col++) {


KEYPAD_DDR = 0x00; // Set the keypad column pins as inputs
KEYPAD_PORT = 0xFF; // Enable pull-up resistors for the column pins
KEYPAD_PORT &= ~(1 << col); // Ground the current column

for (row = 0; row < 4; row++) {


_delay_us(20); // Wait for the signal to settle

if (!(KEYPAD_PIN & (1 << row))) {


// Key is pressed, return the corresponding symbol
return keypadSymbols[row][col];
}
}
}

return '\0'; // No key pressed


}

void keypad_waitForKeyRelease()
{
while (keypad_getKey() != '\0') {
// Wait for all keys to be released
}

if (attempts >= 3) {
keypad_waitForKeyRelease();
// Prompt the user to enter the master password to reset attempts
LCD_clear();
LCD_print("Reset Required");

// Blink the message


for (uint8_t i = 0; i < 10; i++) {
_delay_ms(500);
LCD_clear();
_delay_ms(500);
LCD_print("Reset Required");
}

keypad_waitForKeyRelease();
attempts = 0;
return;
}
else
{
attempts++;
}
}

Conclusion:
In conclusion, the design and implementation of a password-protected system using a keypad
and LCD with AVR microcontroller provide a secure and user-friendly solution for access
control.

You might also like