0% found this document useful (0 votes)
23 views

Source Code For Password Based Circuit Breaker

this source code is to solve circuit system
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Source Code For Password Based Circuit Breaker

this source code is to solve circuit system
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <xc.

h>
#include <stdio.h>
#include <string.h>

#define _XTAL_FREQ 20000000 // Define the oscillator frequency in Hz (20 MHz in


this case)

// Configuration settings for PIC16F877 (adjust as per your setup)


#pragma config FOSC = HS // Oscillator Selection bits (High-speed
oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage ICSP disabled
#pragma config CPD = OFF // Data EEPROM Code Protection (Code protection
off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off)

// LCD and Keypad connections


#define RS RD0
#define EN RD1
#define LCD_PORT PORTB
#define KEYPAD_PORT PORTC

// Function prototypes
void initialize();
void lcd_command(unsigned char);
void lcd_data(unsigned char);
void lcd_initialize();
void lcd_print(const char *);
void check_password();
char get_keypad_key();
void activate_relay();
void deactivate_relay();

// Password and input variables


const char correct_password[] = "1234";
char entered_password[5];
unsigned char password_index = 0;

void main(void) {
initialize();
lcd_initialize();

lcd_print("Enter Password:");
__delay_ms(1000);

while(1) {
check_password();
}
}

void initialize() {
// Set LCD and Keypad ports as output/input
TRISB = 0x00; // LCD as output
TRISD = 0x00; // Control pins as output
TRISC = 0xF0; // Keypad (upper 4 bits input, lower 4 bits output)

// Set relay pins as output (Assuming relay connected to RD2 and RD3)
TRISD2 = 0;
TRISD3 = 0;

deactivate_relay();
}

void lcd_initialize() {
lcd_command(0x38); // 8-bit mode, 2-line display, 5x8 font
lcd_command(0x0C); // Display on, cursor off
lcd_command(0x06); // Increment cursor
lcd_command(0x01); // Clear display
}

void lcd_command(unsigned char cmd) {


LCD_PORT = cmd;
RS = 0; // Command mode
EN = 1;
__delay_ms(1);
EN = 0;
}

void lcd_data(unsigned char data) {


LCD_PORT = data;
RS = 1; // Data mode
EN = 1;
__delay_ms(1);
EN = 0;
}

void lcd_print(const char *str) {


while(*str) {
lcd_data(*str++);
}
}

void check_password() {
char key = get_keypad_key();

if(key != '\0') { // If a key is pressed


if(key == '#') { // '#' acts as Enter key
entered_password[password_index] = '\0'; // Null-terminate the input

// Check if the entered password matches the correct password


if(strcmp(entered_password, correct_password) == 0) {
lcd_command(0x01); // Clear display
lcd_print("Access Granted");
activate_relay();
} else {
lcd_command(0x01); // Clear display
lcd_print("Incorrect Password");
__delay_ms(1000);
deactivate_relay();
}

// Reset the input


password_index = 0;
lcd_command(0x01);
lcd_print("Enter Password:");
} else if(key == '*') { // '*' acts as Clear key
password_index = 0;
lcd_command(0x01);
lcd_print("Enter Password:");
} else {
// Store key and show '*' for each input
entered_password[password_index++] = key;
lcd_data('*');
}
}
}

char get_keypad_key() {
// Keypad scanning logic
KEYPAD_PORT = 0x0E; // Column 1
if (KEYPAD_PORT & 0x10) return '1';
if (KEYPAD_PORT & 0x20) return '4';
if (KEYPAD_PORT & 0x40) return '7';
if (KEYPAD_PORT & 0x80) return '*';

KEYPAD_PORT = 0x0D; // Column 2


if (KEYPAD_PORT & 0x10) return '2';
if (KEYPAD_PORT & 0x20) return '5';
if (KEYPAD_PORT & 0x40) return '8';
if (KEYPAD_PORT & 0x80) return '0';

KEYPAD_PORT = 0x0B; // Column 3


if (KEYPAD_PORT & 0x10) return '3';
if (KEYPAD_PORT & 0x20) return '6';
if (KEYPAD_PORT & 0x40) return '9';
if (KEYPAD_PORT & 0x80) return '#';

KEYPAD_PORT = 0x07; // Column 4


if (KEYPAD_PORT & 0x10) return 'A';
if (KEYPAD_PORT & 0x20) return 'B';
if (KEYPAD_PORT & 0x40) return 'C';
if (KEYPAD_PORT & 0x80) return 'D';

return '\0'; // No key pressed


}

void activate_relay() {
RD2 = 1; // Activate relay 1
RD3 = 1; // Activate relay 2
}

void deactivate_relay() {
RD2 = 0; // Deactivate relay 1
RD3 = 0; // Deactivate relay 2
}

You might also like