Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ioavr.h>
- #include <intrinsics.h>
- #include <string.h>
- #define E 0
- #define RS 1
- #define LCDPORT PORTC
- #define LCDCTRL PORTB
- #define delayTime 3000
- #define TRUE 1
- #define FALSE 0
- //lcd ctrl portb, lcd data porte, buttons portd,leds porta
- //global variables declarations
- unsigned char savedPassword[4] = {'2','2','4','1'};
- unsigned char password[4]={ '0','0','0','0'};
- int userPasswordSize =0;
- int isDisplayCleared = 0;
- int pasnum =0;
- volatile int state =0;
- int isMessageSent = TRUE;
- int wasButtonPressed = FALSE;
- int time =0;
- int unlockTime = 0;
- int isDoorUnlocked = FALSE;
- int buttonTimer;
- //functions declarations
- void initialize();
- void send(unsigned char data);
- void displayWelcomeMessage();
- void displayString(unsigned char* data);
- void setpassword(unsigned char passdata);
- void checkCorrectness();
- void resetSettings();
- void isMessageOnScreen();
- void initSettingsReset();
- int main( void )
- {
- initialize();
- initSettingsReset();
- while(1)
- {
- }
- return 0;
- }
- void initialize()
- {
- //Ports initialization
- DDRB = 255;
- DDRC = 255;
- DDRD = 0x00;
- DDRA = 0xFF;
- //Lcd initialization
- LCDCTRL &= ~(1<<RS);
- send(0x38);
- send(0x06);
- send(0x0f);
- send(0x01);
- __delay_cycles(120000);
- LCDCTRL |= (1<<RS);
- //Counters initialization
- TCCR1B |= (1<< CS12) | (0 << CS11) | (0<< CS10);
- TIMSK |= (1 << TOIE0)| (1 << TOIE1);
- TCCR0 |= (1 << CS00);
- TCNT0 = 0;
- // Interrupts initialization -->
- __enable_interrupt();
- EICRA |= (1<<ISC01);
- EICRA |= (1<<ISC11);
- EICRA |= (1<<ISC21);
- EICRA |= (1<<ISC31);
- EICRB |= (1<<ISC41);
- EICRB |= (1<<ISC51);
- EICRB |= (1<<ISC61);
- EICRB |= (1<<ISC71);
- EIMSK |= (1<<INT0);
- EIMSK |= (1<<INT1);
- EIMSK |= (1<<INT2);
- EIMSK |= (1<<INT3);
- EIMSK |= (1<<INT4);
- EIMSK |= (1<<INT5);
- EIMSK |= (1<<INT6);
- EIMSK |= (1<<INT7);
- // <--
- displayWelcomeMessage();
- }
- void clearDisplay() //fucntion for clearing the display
- {
- LCDCTRL &= ~(1<<RS);
- __delay_cycles(4000);
- send(0x01);
- __delay_cycles(12000);
- LCDCTRL |= (1<<RS);
- __delay_cycles(4000);
- isMessageSent = FALSE;
- }
- //TIMERS -->
- #pragma vector = TIMER0_OVF_vect //8 bit timer0, used for eliminations of button vibrations which genetrates unwatned input
- __interrupt void buttonReset()
- {
- if(buttonTimer <= delayTime)
- {
- buttonTimer++;
- }
- }
- #pragma vector = TIMER1_OVF_vect
- __interrupt void settingsResetAfterTime() //16 nit timer1, one second cycle
- {
- TCNT1=36735;
- if(wasButtonPressed == TRUE) //condition if button was pressed or not
- { //if yes resets all seting after 3 seconds of inactivity
- time = time+1;
- if(time == 3)
- {
- resetSettings();
- }
- }
- if(isDoorUnlocked == TRUE) //condition if user unclocked door or not
- { //if yes door unlocked for 5 seconds (time set for faster testing)
- //after that time door are locked again
- unlockTime = unlockTime +1;
- if(unlockTime == 5)
- {
- resetSettings();
- displayString("DoorLocked");
- unlockTime = 0;
- isDoorUnlocked = FALSE;
- }
- }
- }
- // <--
- void send(unsigned char data) //sending character on LCD
- {
- LCDCTRL |= (1<<E);
- LCDPORT = data;
- __delay_cycles(400);
- LCDCTRL &= ~(1<<E);
- __delay_cycles(500);
- }
- void displayWelcomeMessage()
- {
- displayString("Enter Password");
- }
- void displayString(unsigned char* data) //sending whole string on display using previous function send
- {
- for(int i = 0; i<(unsigned)strlen(data); i++)
- {
- send(data[i]);
- }
- isMessageSent = TRUE;
- }
- void setpassword(unsigned char passdata) //function which saves password given by a user
- {
- password[pasnum]= passdata;
- pasnum = pasnum +1;
- userPasswordSize = userPasswordSize +1;
- }
- void isMessageOnScreen() //checks if there is a message on screeen, if there is it clear display after pressing a button
- {
- if(isMessageSent == TRUE)
- {
- clearDisplay();
- resetSettings();
- }
- }
- //Buttons declaration using inrerupts -->
- #pragma vector = INT0_vect
- __interrupt void button0(){ //button 0
- if(buttonTimer >= delayTime)
- {
- setpassword('0'); //call function which assings proper charcter to an array for further chech with password
- isMessageOnScreen(); //function which checks if there is any message on screen
- wasButtonPressed = TRUE; //condition if button was pressed or not
- time =0; //reset timer
- send('0'); //send '0' on the screen
- buttonTimer = 0;
- }
- }
- #pragma vector = INT1_vect
- __interrupt void button1(){ //button 1
- if(buttonTimer >= delayTime)
- {
- setpassword('1');
- isMessageOnScreen();
- wasButtonPressed = TRUE;
- time =0;
- send('1'); //send '1' on the screen
- buttonTimer=0;
- }
- }
- #pragma vector = INT2_vect //button 2
- __interrupt void button2(){
- if(buttonTimer >= delayTime)
- {
- setpassword('2');
- isMessageOnScreen();
- wasButtonPressed = TRUE;
- time =0;
- state =1;
- send('2'); //send '2' on the screen
- // password(3);
- buttonTimer =0;
- }
- }
- #pragma vector = INT3_vect //button 3
- __interrupt void button3(){
- if(buttonTimer >= delayTime)
- {
- setpassword('3');
- isMessageOnScreen();
- wasButtonPressed = TRUE;
- time =0;
- state =1;
- send('3'); //send '3' on the screen
- buttonTimer =0;
- }
- // password(4);
- }
- #pragma vector = INT4_vect //button 4
- __interrupt void button4(){
- if(buttonTimer >= delayTime)
- {
- setpassword('4');
- isMessageOnScreen();
- wasButtonPressed = TRUE;
- time =0;
- state =1;
- send('4'); //send '4' on the screen
- buttonTimer =0;
- }
- }
- #pragma vector = INT5_vect //button 5
- __interrupt void button5(){
- if(buttonTimer >= delayTime)
- {
- setpassword('5');
- isMessageOnScreen();
- wasButtonPressed = TRUE;
- time =0;
- send('5'); //send '5' on the screen
- buttonTimer =0;
- }
- }
- #pragma vector = INT6_vect //button ENTER
- __interrupt void Enter(){
- if(buttonTimer >= delayTime)
- {
- clearDisplay();
- //displayString("Applay");
- wasButtonPressed = FALSE;
- checkCorrectness();
- buttonTimer =0;
- }
- }
- #pragma vector = INT7_vect //button RESET
- __interrupt void reset(){
- if(buttonTimer >= delayTime)
- {
- wasButtonPressed = TRUE;
- resetSettings(); //call function which reset password
- buttonTimer =0;
- }
- }
- //<--
- void checkCorrectness() //checking is password is correct
- {
- int wrongPassword = FALSE;
- if ( userPasswordSize != sizeof(savedPassword)) //if length of password given by user =/= length of saved password, then password is false
- {
- clearDisplay();
- PORTA= 0x00;
- wrongPassword = TRUE;
- displayString("Wrong Password");
- }
- else{
- for (int i = 0; i< sizeof(password);i++)
- {
- if(password[i] != savedPassword[i]) //if password given by user is not the same as saved one, then password is false
- {
- wrongPassword = TRUE;
- }
- }
- }
- if (wrongPassword == TRUE) //if password is false
- {
- clearDisplay();
- PORTA =0x00; //all leds are turned on
- displayString("Wrong Password"); //Message that password was not correct
- //display attempts left;
- }
- else //if password was correct
- {
- clearDisplay();
- PORTA = 0xAA; //every second led is turned on
- displayString("Access Granted"); //message that we granted access beacuse password was correct
- isDoorUnlocked = TRUE;
- }
- }
- void initSettingsReset() //function reseting all settings when program starts
- {
- pasnum = 0; //help variable which incremetns by pressing buttons, position in array
- PORTA = ~0x00; //Turn off all leds
- userPasswordSize = 0; //length of password given by user
- time = 0; //timer 1 reset
- wasButtonPressed = FALSE; //conditions if button was pressend or not
- }
- void resetSettings() //resseting settings
- {
- for(int i=0;i <sizeof(savedPassword);i++) //reseting whole array with password given by user
- {
- password[i]= '0';
- }
- isDoorUnlocked = FALSE; //condition if door are unlocker or not
- clearDisplay(); //clearing the display
- pasnum = 0; //help variable which incremetns by pressing buttons, position in array
- PORTA = ~0x00; //turn off all leds
- userPasswordSize = 0; //number of characters given by user while entering the password
- if(wasButtonPressed == TRUE)
- {
- clearDisplay();
- wasButtonPressed = FALSE;
- }
- time = 0; //timer 1 reset
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement