#include <P18F452.h>
#include <string.h> // Include for strcmp function
#define RS PORTCbits.RC2
#define RW PORTCbits.RC1
#define EN PORTCbits.RC0
#define R1 PORTBbits.RB0
#define R2 PORTBbits.RB1
#define R3 PORTBbits.RB2
#define R4 PORTBbits.RB3
#define C1 PORTBbits.RB4
#define C2 PORTBbits.RB5
#define C3 PORTBbits.RB6
#define MOTOR1_RE0 PORTEbits.RE0 // Define L298N control for motor1 (RE0)
#define MOTOR2_RE1 PORTEbits.RE1 // Define L298N control for motor2 (RE1)
void main();
void LCD_config(void);
void LCD_command(unsigned char k);
void LCD_display(unsigned char i);
void delay(void);
void keypad(void);
void checkPassword(void);
void unlockSequence(void); // Function to handle unlocking sequence
void testMotor(void); // Motor testing function
unsigned char enteredPassword[5] = {0}; // Store entered password
unsigned char correctPassword[5] = {'4', '4', '4', '4', '\0'}; // Predefined password
unsigned char keyIndex = 0; // To track entered keys
void main() {
// Configure I/O Ports
TRISC = 0x00; // Configure PORTC as output for LCD control
TRISD = 0x00; // Configure PORTD as output for LCD data
TRISB = 0x70; // Configure PORTB as input for keypad columns and output for rows
TRISE = 0x00; // Configure PORTE as output for L298N control pins
MOTOR1_RE0 = 0; // Ensure motor1 is off initially
MOTOR2_RE1 = 0; // Ensure motor2 is off initially
LCD_config(); // Initialize LCD
LCD_command(0x80); // Set cursor to the first line
LCD_display('E'); LCD_display('n'); LCD_display('t'); LCD_display('e'); LCD_display('r');
LCD_display(' '); LCD_display('P'); LCD_display('W'); LCD_display(':');
testMotor(); // Test motor functionality
while (1) {
keypad(); // Scan keypad
}
}
void LCD_config(void) {
LCD_command(0x38); // Configure LCD 2-line mode
LCD_command(0x01); // Clear LCD screen
LCD_command(0x0C); // Turn on display without cursor
LCD_command(0x06); // Increment cursor
}
void LCD_command(unsigned char k) {
PORTD = k;
RS = 0;
RW = 0;
EN = 1;
delay();
EN = 0;
}
void LCD_display(unsigned char i) {
PORTD = i;
RS = 1;
RW = 0;
EN = 1;
delay();
EN = 0;
}
void delay(void) {
unsigned int p;
for (p = 0; p <= 2048; p++); // Basic delay
}
void keypad(void) {
// Scan keypad rows and columns
R1 = 1; R2 = 0; R3 = 0; R4 = 0;
if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = '1'; LCD_display('1'); }
if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '2'; LCD_display('2'); }
if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '3'; LCD_display('3'); }
R1 = 0; R2 = 1; R3 = 0; R4 = 0;
if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = '4'; LCD_display('4'); }
if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '5'; LCD_display('5'); }
if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '6'; LCD_display('6'); }
R1 = 0; R2 = 0; R3 = 1; R4 = 0;
if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = '7'; LCD_display('7'); }
if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '8'; LCD_display('8'); }
if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '9'; LCD_display('9'); }
R1 = 0; R2 = 0; R3 = 0; R4 = 1;
if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = ''; LCD_display(''); }
if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '0'; LCD_display('0'); }
if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '#'; LCD_display('#'); }
// Check password only after 4 digits have been entered
if (keyIndex == 4) {
enteredPassword[keyIndex] = '\0'; // Null-terminate the entered password
checkPassword(); // Check the entered password
}
}
void checkPassword(void) {
// If entered password matches the correct password
if (strcmp(enteredPassword
, correctPassword
) == 0) { unlockSequence(); // Handle unlock sequence
} else {
LCD_command(0x01); // Clear LCD
LCD_command(0x80); // Set cursor to the first line
LCD_display('W'); LCD_display('r'); LCD_display('o'); LCD_display('n'); LCD_display('g');
LCD_display(' '); LCD_display('P'); LCD_display('W');
delay(); // Show the wrong password message briefly
// Reset password entry process
keyIndex = 0; // Reset key index for new password entry
memset
(enteredPassword
, 0, sizeof(enteredPassword
)); // Clear entered password
// Prompt for password again
LCD_command(0x01);
LCD_command(0x80);
LCD_display('E'); LCD_display('n'); LCD_display('t'); LCD_display('e'); LCD_display('r');
LCD_display(' '); LCD_display('P'); LCD_display('W'); LCD_display(':');
}
}
void unlockSequence(void) {
// Display "Unlocked" message and activate motors (L298N via RE0 and RE1)
LCD_command(0x01); // Clear LCD
LCD_command(0x80); // Set cursor to the first line
LCD_display('U'); LCD_display('n'); LCD_display('l'); LCD_display('o'); LCD_display('c');
LCD_display('k'); LCD_display('e'); LCD_display('d');
MOTOR1_RE0 = 1; // Activate motor1 (or relay for motor control)
MOTOR2_RE1 = 1; // Activate motor2 (or relay for motor control)
delay(); delay(); // Wait for a while before turning off motors
MOTOR1_RE0 = 0; // Deactivate motor1
MOTOR2_RE1 = 0; // Deactivate motor2
}
void testMotor(void) {
MOTOR1_RE0 = 1; // Turn on motor1
MOTOR2_RE1 = 1; // Turn on motor2
delay();
MOTOR1_RE0 = 0; // Turn off motor1
MOTOR2_RE1 = 0; // Turn off motor2
delay();
}