fork download
  1. #include <P18F452.h>
  2. #include <string.h> // Include for strcmp function
  3.  
  4. #define RS PORTCbits.RC2
  5. #define RW PORTCbits.RC1
  6. #define EN PORTCbits.RC0
  7. #define R1 PORTBbits.RB0
  8. #define R2 PORTBbits.RB1
  9. #define R3 PORTBbits.RB2
  10. #define R4 PORTBbits.RB3
  11. #define C1 PORTBbits.RB4
  12. #define C2 PORTBbits.RB5
  13. #define C3 PORTBbits.RB6
  14. #define MOTOR1_RE0 PORTEbits.RE0 // Define L298N control for motor1 (RE0)
  15. #define MOTOR2_RE1 PORTEbits.RE1 // Define L298N control for motor2 (RE1)
  16.  
  17. void main();
  18. void LCD_config(void);
  19. void LCD_command(unsigned char k);
  20. void LCD_display(unsigned char i);
  21. void delay(void);
  22. void keypad(void);
  23. void checkPassword(void);
  24. void unlockSequence(void); // Function to handle unlocking sequence
  25. void testMotor(void); // Motor testing function
  26.  
  27. unsigned char enteredPassword[5] = {0}; // Store entered password
  28. unsigned char correctPassword[5] = {'4', '4', '4', '4', '\0'}; // Predefined password
  29. unsigned char keyIndex = 0; // To track entered keys
  30.  
  31. void main() {
  32. // Configure I/O Ports
  33. TRISC = 0x00; // Configure PORTC as output for LCD control
  34. TRISD = 0x00; // Configure PORTD as output for LCD data
  35. TRISB = 0x70; // Configure PORTB as input for keypad columns and output for rows
  36. TRISE = 0x00; // Configure PORTE as output for L298N control pins
  37.  
  38. MOTOR1_RE0 = 0; // Ensure motor1 is off initially
  39. MOTOR2_RE1 = 0; // Ensure motor2 is off initially
  40.  
  41. LCD_config(); // Initialize LCD
  42.  
  43. LCD_command(0x80); // Set cursor to the first line
  44. LCD_display('E'); LCD_display('n'); LCD_display('t'); LCD_display('e'); LCD_display('r');
  45. LCD_display(' '); LCD_display('P'); LCD_display('W'); LCD_display(':');
  46.  
  47. testMotor(); // Test motor functionality
  48.  
  49. while (1) {
  50. keypad(); // Scan keypad
  51. }
  52. }
  53.  
  54. void LCD_config(void) {
  55. LCD_command(0x38); // Configure LCD 2-line mode
  56. LCD_command(0x01); // Clear LCD screen
  57. LCD_command(0x0C); // Turn on display without cursor
  58. LCD_command(0x06); // Increment cursor
  59. }
  60.  
  61. void LCD_command(unsigned char k) {
  62. PORTD = k;
  63. RS = 0;
  64. RW = 0;
  65. EN = 1;
  66. delay();
  67. EN = 0;
  68. }
  69.  
  70. void LCD_display(unsigned char i) {
  71. PORTD = i;
  72. RS = 1;
  73. RW = 0;
  74. EN = 1;
  75. delay();
  76. EN = 0;
  77. }
  78.  
  79. void delay(void) {
  80. unsigned int p;
  81. for (p = 0; p <= 2048; p++); // Basic delay
  82. }
  83.  
  84. void keypad(void) {
  85. // Scan keypad rows and columns
  86. R1 = 1; R2 = 0; R3 = 0; R4 = 0;
  87. if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = '1'; LCD_display('1'); }
  88. if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '2'; LCD_display('2'); }
  89. if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '3'; LCD_display('3'); }
  90.  
  91. R1 = 0; R2 = 1; R3 = 0; R4 = 0;
  92. if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = '4'; LCD_display('4'); }
  93. if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '5'; LCD_display('5'); }
  94. if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '6'; LCD_display('6'); }
  95.  
  96. R1 = 0; R2 = 0; R3 = 1; R4 = 0;
  97. if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = '7'; LCD_display('7'); }
  98. if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '8'; LCD_display('8'); }
  99. if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '9'; LCD_display('9'); }
  100.  
  101. R1 = 0; R2 = 0; R3 = 0; R4 = 1;
  102. if (C1 == 1) { while (C1 == 1); enteredPassword[keyIndex++] = ''; LCD_display(''); }
  103. if (C2 == 1) { while (C2 == 1); enteredPassword[keyIndex++] = '0'; LCD_display('0'); }
  104. if (C3 == 1) { while (C3 == 1); enteredPassword[keyIndex++] = '#'; LCD_display('#'); }
  105.  
  106. // Check password only after 4 digits have been entered
  107. if (keyIndex == 4) {
  108. enteredPassword[keyIndex] = '\0'; // Null-terminate the entered password
  109. checkPassword(); // Check the entered password
  110. }
  111. }
  112.  
  113. void checkPassword(void) {
  114. // If entered password matches the correct password
  115. if (strcmp(enteredPassword, correctPassword) == 0) {
  116. unlockSequence(); // Handle unlock sequence
  117. } else {
  118. LCD_command(0x01); // Clear LCD
  119. LCD_command(0x80); // Set cursor to the first line
  120. LCD_display('W'); LCD_display('r'); LCD_display('o'); LCD_display('n'); LCD_display('g');
  121. LCD_display(' '); LCD_display('P'); LCD_display('W');
  122. delay(); // Show the wrong password message briefly
  123.  
  124. // Reset password entry process
  125. keyIndex = 0; // Reset key index for new password entry
  126. memset(enteredPassword, 0, sizeof(enteredPassword)); // Clear entered password
  127.  
  128. // Prompt for password again
  129. LCD_command(0x01);
  130. LCD_command(0x80);
  131. LCD_display('E'); LCD_display('n'); LCD_display('t'); LCD_display('e'); LCD_display('r');
  132. LCD_display(' '); LCD_display('P'); LCD_display('W'); LCD_display(':');
  133. }
  134. }
  135.  
  136. void unlockSequence(void) {
  137. // Display "Unlocked" message and activate motors (L298N via RE0 and RE1)
  138. LCD_command(0x01); // Clear LCD
  139. LCD_command(0x80); // Set cursor to the first line
  140. LCD_display('U'); LCD_display('n'); LCD_display('l'); LCD_display('o'); LCD_display('c');
  141. LCD_display('k'); LCD_display('e'); LCD_display('d');
  142.  
  143. MOTOR1_RE0 = 1; // Activate motor1 (or relay for motor control)
  144. MOTOR2_RE1 = 1; // Activate motor2 (or relay for motor control)
  145. delay(); delay(); // Wait for a while before turning off motors
  146. MOTOR1_RE0 = 0; // Deactivate motor1
  147. MOTOR2_RE1 = 0; // Deactivate motor2
  148. }
  149.  
  150. void testMotor(void) {
  151.  
  152.  
  153. MOTOR1_RE0 = 1; // Turn on motor1
  154. MOTOR2_RE1 = 1; // Turn on motor2
  155. delay();
  156. MOTOR1_RE0 = 0; // Turn off motor1
  157. MOTOR2_RE1 = 0; // Turn off motor2
  158. delay();
  159. }
Success #stdin #stdout 0.02s 25660KB
stdin
Standard input is empty
stdout
#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();
}