#include "p18f452.h" #include <string.h> #define _XTAL_FREQ 20000000 // Adjust according to your oscillator configuration // LCD control pins #define RS PORTBbits.RB0 #define EN PORTBbits.RB1 #define D4 PORTBbits.RB4 #define D5 PORTBbits.RB5 #define D6 PORTBbits.RB6 #define D7 PORTBbits.RB7 // Keypad rows #define row1port PORTCbits.RC0 #define row2port PORTCbits.RC1 #define row3port PORTCbits.RC2 #define row4port PORTCbits.RC3 // Keypad columns #define col1port PORTCbits.RC4 #define col2port PORTCbits.RC5 #define col3port PORTCbits.RC6 #define BUZZER PORTDbits.RD2 // Motor control pins #define MOTOR1_RE0 PORTEbits.RE0 #define MOTOR2_RE1 PORTEbits.RE1 // Global variables unsigned char enteredPassword[5] = {0}; // Stores the entered password unsigned char correctPassword[5] = {'4', '4', '4', '4', '\0'}; // Predefined correct password unsigned char keyIndex = 0; // Function declarations void Lcd_Cmd(char cmd); void Lcd_Set_Cursor(char a, char b); void Lcd_Print_Char(char data); void Lcd_Print_String(const char* str); void Lcd_Init(); void keypadScan(); void checkPassword(); void unlockDoor(); void lockDoor(); // Function implementations void Lcd_Cmd(char cmd) { PORTD = cmd; // Ensure PORTD is configured as output in MCC RS = 0; // Command mode EN = 1; __delay_ms(2); EN = 0; } void Lcd_Set_Cursor(char a, char b) { if (a == 1) Lcd_Cmd(0x80 + b - 1); else if (a == 2) Lcd_Cmd(0xC0 + b - 1); } void Lcd_Print_Char(char data) { PORTD = data; // Ensure PORTD is configured as output in MCC RS = 1; // Data mode EN = 1; __delay_ms(2); EN = 0; } void Lcd_Print_String(const char* str) { while (*str) { Lcd_Print_Char(*str++); } } void Lcd_Init() { Lcd_Cmd(0x38); // 2-line, 5x7 matrix Lcd_Cmd(0x0C); // Display on, cursor off Lcd_Cmd(0x01); // Clear display Lcd_Cmd(0x06); // Increment cursor } void keypadScan() { // Set rows as output and columns as input TRISCbits.TRISC0 = 0; // Row 1 TRISCbits.TRISC1 = 0; // Row 2 TRISCbits.TRISC2 = 0; // Row 3 TRISCbits.TRISC3 = 0; // Row 4 TRISCbits.TRISC4 = 1; // Column 1 TRISCbits.TRISC5 = 1; // Column 2 TRISCbits.TRISC6 = 1; // Column 3 // Scan each row for (int row = 0; row < 4; row++) { // Activate the row switch(row) { case 0: row1port = 0; break; case 1: row2port = 0; break; case 2: row3port = 0; break; case 3: row4port = 0; break; } // Check columns if (col1port == 0) { enteredPassword[keyIndex++] = '1' + row * 3; // '1', '4', '7', '*' __delay_ms(200); // Debounce delay } else if (col2port == 0) { enteredPassword[keyIndex++] = '2' + row * 3; // '2', '5', '8', '0' __delay_ms(200); // Debounce delay } else if (col3port == 0) { enteredPassword[keyIndex++] = '3' + row * 3; // '3', '6', '9', '#' __delay_ms(200); // Debounce delay } // Reset the row switch(row) { case 0: row1port = 1; break; case 1: row2port = 1; break; case 2: row3port = 1; break; case 3: row4port = 1; break; } if (keyIndex >= 4) { // If the password length is reached enteredPassword[4] = '\0'; // Null-terminate the password keyIndex = 0; // Reset the index for the next input checkPassword(); // Call function to check the entered password } } } void checkPassword() { Lcd_Set_Cursor(1, 1); Lcd_Print_String("Access Granted"); unlockDoor(); __delay_ms(1000); // Delay before relocking lockDoor(); // Call to lock the door } else { Lcd_Set_Cursor(1, 1); Lcd_Print_String("Access Denied"); BUZZER = 1; // Turn on the buzzer __delay_ms(1000); BUZZER = 0; // Turn off the buzzer } __delay_ms(1000); // Display result for a moment Lcd_Cmd(0x01); // Clear the LCD display } void unlockDoor() { // Unlocking sequence MOTOR1_RE0 = 1; // Motor runs in one direction to unlock MOTOR2_RE1 = 0; // Set second motor line to 0 __delay_ms(3000); // Door remains unlocked for 3 seconds MOTOR1_RE0 = 0; // Stop motor } void lockDoor() { // Locking sequence MOTOR1_RE0 = 0; // Set first motor line to 0 MOTOR2_RE1 = 1; // Motor runs in opposite direction to lock __delay_ms(3000); // Door remains locked for 3 seconds MOTOR2_RE1 = 0; // Stop motor } void main() { TRISB = 0x00; // Set PORTB as output for LCD and control TRISD = 0x00; // Set PORTD as output for LCD data lines TRISE = 0x00; // Set PORTE as output for motor control TRISDbits.TRISD2 = 0; // Set RD2 as output for the buzzer Lcd_Init(); // Initialize the LCD Lcd_Set_Cursor(1, 1); Lcd_Print_String("Enter Password:"); while (1) { keypadScan(); // Continuously scan the keypad } }
Standard input is empty
#include "p18f452.h" #include <string.h> #define _XTAL_FREQ 20000000 // Adjust according to your oscillator configuration // LCD control pins #define RS PORTBbits.RB0 #define EN PORTBbits.RB1 #define D4 PORTBbits.RB4 #define D5 PORTBbits.RB5 #define D6 PORTBbits.RB6 #define D7 PORTBbits.RB7 // Keypad rows #define row1port PORTCbits.RC0 #define row2port PORTCbits.RC1 #define row3port PORTCbits.RC2 #define row4port PORTCbits.RC3 // Keypad columns #define col1port PORTCbits.RC4 #define col2port PORTCbits.RC5 #define col3port PORTCbits.RC6 #define BUZZER PORTDbits.RD2 // Motor control pins #define MOTOR1_RE0 PORTEbits.RE0 #define MOTOR2_RE1 PORTEbits.RE1 // Global variables unsigned char enteredPassword[5] = {0}; // Stores the entered password unsigned char correctPassword[5] = {'4', '4', '4', '4', '\0'}; // Predefined correct password unsigned char keyIndex = 0; // Function declarations void Lcd_Cmd(char cmd); void Lcd_Set_Cursor(char a, char b); void Lcd_Print_Char(char data); void Lcd_Print_String(const char* str); void Lcd_Init(); void keypadScan(); void checkPassword(); void unlockDoor(); void lockDoor(); // Function implementations void Lcd_Cmd(char cmd) { PORTD = cmd; // Ensure PORTD is configured as output in MCC RS = 0; // Command mode EN = 1; __delay_ms(2); EN = 0; } void Lcd_Set_Cursor(char a, char b) { if (a == 1) Lcd_Cmd(0x80 + b - 1); else if (a == 2) Lcd_Cmd(0xC0 + b - 1); } void Lcd_Print_Char(char data) { PORTD = data; // Ensure PORTD is configured as output in MCC RS = 1; // Data mode EN = 1; __delay_ms(2); EN = 0; } void Lcd_Print_String(const char* str) { while (*str) { Lcd_Print_Char(*str++); } } void Lcd_Init() { Lcd_Cmd(0x38); // 2-line, 5x7 matrix Lcd_Cmd(0x0C); // Display on, cursor off Lcd_Cmd(0x01); // Clear display Lcd_Cmd(0x06); // Increment cursor } void keypadScan() { // Set rows as output and columns as input TRISCbits.TRISC0 = 0; // Row 1 TRISCbits.TRISC1 = 0; // Row 2 TRISCbits.TRISC2 = 0; // Row 3 TRISCbits.TRISC3 = 0; // Row 4 TRISCbits.TRISC4 = 1; // Column 1 TRISCbits.TRISC5 = 1; // Column 2 TRISCbits.TRISC6 = 1; // Column 3 // Scan each row for (int row = 0; row < 4; row++) { // Activate the row switch(row) { case 0: row1port = 0; break; case 1: row2port = 0; break; case 2: row3port = 0; break; case 3: row4port = 0; break; } // Check columns if (col1port == 0) { enteredPassword[keyIndex++] = '1' + row * 3; // '1', '4', '7', '*' __delay_ms(200); // Debounce delay } else if (col2port == 0) { enteredPassword[keyIndex++] = '2' + row * 3; // '2', '5', '8', '0' __delay_ms(200); // Debounce delay } else if (col3port == 0) { enteredPassword[keyIndex++] = '3' + row * 3; // '3', '6', '9', '#' __delay_ms(200); // Debounce delay } // Reset the row switch(row) { case 0: row1port = 1; break; case 1: row2port = 1; break; case 2: row3port = 1; break; case 3: row4port = 1; break; } if (keyIndex >= 4) { // If the password length is reached enteredPassword[4] = '\0'; // Null-terminate the password keyIndex = 0; // Reset the index for the next input checkPassword(); // Call function to check the entered password } } } void checkPassword() { if (strcmp(enteredPassword, correctPassword) == 0) { Lcd_Set_Cursor(1, 1); Lcd_Print_String("Access Granted"); unlockDoor(); __delay_ms(1000); // Delay before relocking lockDoor(); // Call to lock the door } else { Lcd_Set_Cursor(1, 1); Lcd_Print_String("Access Denied"); BUZZER = 1; // Turn on the buzzer __delay_ms(1000); BUZZER = 0; // Turn off the buzzer } __delay_ms(1000); // Display result for a moment Lcd_Cmd(0x01); // Clear the LCD display } void unlockDoor() { // Unlocking sequence MOTOR1_RE0 = 1; // Motor runs in one direction to unlock MOTOR2_RE1 = 0; // Set second motor line to 0 __delay_ms(3000); // Door remains unlocked for 3 seconds MOTOR1_RE0 = 0; // Stop motor } void lockDoor() { // Locking sequence MOTOR1_RE0 = 0; // Set first motor line to 0 MOTOR2_RE1 = 1; // Motor runs in opposite direction to lock __delay_ms(3000); // Door remains locked for 3 seconds MOTOR2_RE1 = 0; // Stop motor } void main() { TRISB = 0x00; // Set PORTB as output for LCD and control TRISD = 0x00; // Set PORTD as output for LCD data lines TRISE = 0x00; // Set PORTE as output for motor control TRISDbits.TRISD2 = 0; // Set RD2 as output for the buzzer Lcd_Init(); // Initialize the LCD Lcd_Set_Cursor(1, 1); Lcd_Print_String("Enter Password:"); while (1) { keypadScan(); // Continuously scan the keypad } }