fork download
  1. #include "p18f452.h"
  2. #include <string.h>
  3.  
  4. #define _XTAL_FREQ 20000000 // Adjust according to your oscillator configuration
  5.  
  6. // LCD control pins
  7. #define RS PORTBbits.RB0
  8. #define EN PORTBbits.RB1
  9. #define D4 PORTBbits.RB4
  10. #define D5 PORTBbits.RB5
  11. #define D6 PORTBbits.RB6
  12. #define D7 PORTBbits.RB7
  13.  
  14. // Keypad rows
  15. #define row1port PORTCbits.RC0
  16. #define row2port PORTCbits.RC1
  17. #define row3port PORTCbits.RC2
  18. #define row4port PORTCbits.RC3
  19.  
  20. // Keypad columns
  21. #define col1port PORTCbits.RC4
  22. #define col2port PORTCbits.RC5
  23. #define col3port PORTCbits.RC6
  24.  
  25. #define BUZZER PORTDbits.RD2
  26.  
  27. // Motor control pins
  28. #define MOTOR1_RE0 PORTEbits.RE0
  29. #define MOTOR2_RE1 PORTEbits.RE1
  30.  
  31. // Global variables
  32. unsigned char enteredPassword[5] = {0}; // Stores the entered password
  33. unsigned char correctPassword[5] = {'4', '4', '4', '4', '\0'}; // Predefined correct password
  34. unsigned char keyIndex = 0;
  35.  
  36. // Function declarations
  37. void Lcd_Cmd(char cmd);
  38. void Lcd_Set_Cursor(char a, char b);
  39. void Lcd_Print_Char(char data);
  40. void Lcd_Print_String(const char* str);
  41. void Lcd_Init();
  42. void keypadScan();
  43. void checkPassword();
  44. void unlockDoor();
  45. void lockDoor();
  46.  
  47. // Function implementations
  48.  
  49. void Lcd_Cmd(char cmd) {
  50. PORTD = cmd; // Ensure PORTD is configured as output in MCC
  51. RS = 0; // Command mode
  52. EN = 1;
  53. __delay_ms(2);
  54. EN = 0;
  55. }
  56.  
  57. void Lcd_Set_Cursor(char a, char b) {
  58. if (a == 1)
  59. Lcd_Cmd(0x80 + b - 1);
  60. else if (a == 2)
  61. Lcd_Cmd(0xC0 + b - 1);
  62. }
  63.  
  64. void Lcd_Print_Char(char data) {
  65. PORTD = data; // Ensure PORTD is configured as output in MCC
  66. RS = 1; // Data mode
  67. EN = 1;
  68. __delay_ms(2);
  69. EN = 0;
  70. }
  71.  
  72. void Lcd_Print_String(const char* str) {
  73. while (*str) {
  74. Lcd_Print_Char(*str++);
  75. }
  76. }
  77.  
  78. void Lcd_Init() {
  79. Lcd_Cmd(0x38); // 2-line, 5x7 matrix
  80. Lcd_Cmd(0x0C); // Display on, cursor off
  81. Lcd_Cmd(0x01); // Clear display
  82. Lcd_Cmd(0x06); // Increment cursor
  83. }
  84.  
  85. void keypadScan() {
  86. // Set rows as output and columns as input
  87. TRISCbits.TRISC0 = 0; // Row 1
  88. TRISCbits.TRISC1 = 0; // Row 2
  89. TRISCbits.TRISC2 = 0; // Row 3
  90. TRISCbits.TRISC3 = 0; // Row 4
  91. TRISCbits.TRISC4 = 1; // Column 1
  92. TRISCbits.TRISC5 = 1; // Column 2
  93. TRISCbits.TRISC6 = 1; // Column 3
  94.  
  95. // Scan each row
  96. for (int row = 0; row < 4; row++) {
  97. // Activate the row
  98. switch(row) {
  99. case 0: row1port = 0; break;
  100. case 1: row2port = 0; break;
  101. case 2: row3port = 0; break;
  102. case 3: row4port = 0; break;
  103. }
  104.  
  105. // Check columns
  106. if (col1port == 0) {
  107. enteredPassword[keyIndex++] = '1' + row * 3; // '1', '4', '7', '*'
  108. __delay_ms(200); // Debounce delay
  109. } else if (col2port == 0) {
  110. enteredPassword[keyIndex++] = '2' + row * 3; // '2', '5', '8', '0'
  111. __delay_ms(200); // Debounce delay
  112. } else if (col3port == 0) {
  113. enteredPassword[keyIndex++] = '3' + row * 3; // '3', '6', '9', '#'
  114. __delay_ms(200); // Debounce delay
  115. }
  116.  
  117. // Reset the row
  118. switch(row) {
  119. case 0: row1port = 1; break;
  120. case 1: row2port = 1; break;
  121. case 2: row3port = 1; break;
  122. case 3: row4port = 1; break;
  123. }
  124.  
  125. if (keyIndex >= 4) { // If the password length is reached
  126. enteredPassword[4] = '\0'; // Null-terminate the password
  127. keyIndex = 0; // Reset the index for the next input
  128. checkPassword(); // Call function to check the entered password
  129. }
  130. }
  131. }
  132.  
  133. void checkPassword() {
  134. if (strcmp(enteredPassword, correctPassword) == 0) {
  135. Lcd_Set_Cursor(1, 1);
  136. Lcd_Print_String("Access Granted");
  137. unlockDoor();
  138. __delay_ms(1000); // Delay before relocking
  139. lockDoor(); // Call to lock the door
  140. } else {
  141. Lcd_Set_Cursor(1, 1);
  142. Lcd_Print_String("Access Denied");
  143. BUZZER = 1; // Turn on the buzzer
  144. __delay_ms(1000);
  145. BUZZER = 0; // Turn off the buzzer
  146. }
  147.  
  148. __delay_ms(1000); // Display result for a moment
  149. Lcd_Cmd(0x01); // Clear the LCD display
  150. }
  151.  
  152. void unlockDoor() {
  153. // Unlocking sequence
  154. MOTOR1_RE0 = 1; // Motor runs in one direction to unlock
  155. MOTOR2_RE1 = 0; // Set second motor line to 0
  156. __delay_ms(3000); // Door remains unlocked for 3 seconds
  157. MOTOR1_RE0 = 0; // Stop motor
  158. }
  159.  
  160. void lockDoor() {
  161. // Locking sequence
  162. MOTOR1_RE0 = 0; // Set first motor line to 0
  163. MOTOR2_RE1 = 1; // Motor runs in opposite direction to lock
  164. __delay_ms(3000); // Door remains locked for 3 seconds
  165. MOTOR2_RE1 = 0; // Stop motor
  166. }
  167.  
  168. void main() {
  169. TRISB = 0x00; // Set PORTB as output for LCD and control
  170. TRISD = 0x00; // Set PORTD as output for LCD data lines
  171. TRISE = 0x00; // Set PORTE as output for motor control
  172. TRISDbits.TRISD2 = 0; // Set RD2 as output for the buzzer
  173.  
  174. Lcd_Init(); // Initialize the LCD
  175. Lcd_Set_Cursor(1, 1);
  176. Lcd_Print_String("Enter Password:");
  177.  
  178. while (1) {
  179. keypadScan(); // Continuously scan the keypad
  180. }
  181. }
Success #stdin #stdout 0.04s 25800KB
stdin
Standard input is empty
stdout
#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
    }
}