Lab 07
Lab 07
Name Reg. No. Lab Tasks Lab Report Viva Marks Total Marks
Marks Marks
20 10 10 30
(Scaled 5) (Scaled 5)
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!!!
Reset Required
(Blinking)
Code:
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
// 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);
while (1) {
// Prompt the user to enter the master password
LCD_clear();
LCD_print("Master PW:");
LCD_setCursor(1, 0);
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
_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);
}
if (row == 1)
address += 0x40; // DDRAM address of the first character in the second row
char keypad_getKey()
{
const unsigned char keypadSymbols[4][4] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
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");
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.