Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.55 KB | None | 0 0
  1. #include <ioavr.h>
  2. #include <intrinsics.h>
  3. #include <string.h>
  4. #define E 0            
  5. #define RS 1            
  6. #define LCDPORT PORTC  
  7. #define LCDCTRL PORTB
  8. #define delayTime 3000
  9. #define TRUE 1
  10. #define FALSE 0
  11.  
  12. //lcd ctrl portb, lcd data porte, buttons portd,leds porta
  13.  
  14. //global variables declarations
  15. unsigned char savedPassword[4] = {'2','2','4','1'};
  16. unsigned char password[4]={ '0','0','0','0'};
  17. int userPasswordSize =0;
  18. int isDisplayCleared = 0;
  19. int pasnum =0;
  20. volatile int state =0;
  21. int isMessageSent = TRUE;
  22. int wasButtonPressed = FALSE;
  23. int time =0;
  24. int unlockTime = 0;
  25. int isDoorUnlocked = FALSE;
  26. int buttonTimer;
  27.  
  28.  
  29. //functions declarations
  30. void initialize();
  31. void send(unsigned char data);
  32. void displayWelcomeMessage();
  33. void displayString(unsigned char* data);
  34. void setpassword(unsigned char passdata);
  35. void checkCorrectness();
  36. void resetSettings();
  37. void isMessageOnScreen();
  38. void initSettingsReset();
  39.  
  40.  
  41.  
  42. int main( void )
  43. {
  44.   initialize();
  45.   initSettingsReset();
  46.  
  47.  
  48.  
  49.   while(1)
  50.   {
  51.      
  52.   }
  53.  
  54.   return 0;
  55. }
  56. void initialize()
  57. {
  58.   //Ports initialization
  59.   DDRB =  255;  
  60.   DDRC =  255;
  61.   DDRD = 0x00;
  62.   DDRA = 0xFF;
  63.  
  64.   //Lcd initialization
  65.   LCDCTRL &= ~(1<<RS);
  66.   send(0x38);
  67.   send(0x06);
  68.   send(0x0f);
  69.   send(0x01);
  70.   __delay_cycles(120000);  
  71.   LCDCTRL |= (1<<RS);
  72.  
  73.   //Counters initialization
  74.   TCCR1B |= (1<< CS12) | (0 << CS11) | (0<< CS10);
  75.   TIMSK |= (1 << TOIE0)| (1 << TOIE1);
  76.  
  77.   TCCR0 |= (1 << CS00);
  78.   TCNT0 = 0;
  79.  
  80.  
  81.  
  82.   // Interrupts initialization -->
  83.   __enable_interrupt();
  84.  
  85.   EICRA |= (1<<ISC01);              
  86.   EICRA |= (1<<ISC11);
  87.   EICRA |= (1<<ISC21);
  88.   EICRA |= (1<<ISC31);
  89.  
  90.   EICRB |= (1<<ISC41);                
  91.   EICRB |= (1<<ISC51);
  92.   EICRB |= (1<<ISC61);
  93.   EICRB |= (1<<ISC71);
  94.  
  95.   EIMSK |= (1<<INT0);                    
  96.   EIMSK |= (1<<INT1);  
  97.   EIMSK |= (1<<INT2);
  98.   EIMSK |= (1<<INT3);
  99.   EIMSK |= (1<<INT4);
  100.   EIMSK |= (1<<INT5);
  101.   EIMSK |= (1<<INT6);
  102.   EIMSK |= (1<<INT7);
  103.  
  104.   // <--
  105.  
  106.  
  107.   displayWelcomeMessage();
  108. }
  109. void clearDisplay()       //fucntion for clearing the display
  110. {
  111.   LCDCTRL &= ~(1<<RS);
  112.   __delay_cycles(4000);
  113.  
  114.   send(0x01);
  115.   __delay_cycles(12000);
  116.  
  117.   LCDCTRL |= (1<<RS);
  118.   __delay_cycles(4000);
  119.  
  120.   isMessageSent = FALSE;
  121. }
  122.  
  123. //TIMERS -->
  124.  
  125. #pragma vector = TIMER0_OVF_vect        //8 bit timer0, used for eliminations of button vibrations which genetrates unwatned input
  126. __interrupt void buttonReset()
  127. {
  128.   if(buttonTimer <= delayTime)
  129.   {
  130.       buttonTimer++;
  131.   }
  132. }
  133.  
  134. #pragma vector = TIMER1_OVF_vect
  135.  __interrupt void settingsResetAfterTime()    //16 nit timer1, one second cycle
  136.  {
  137.       TCNT1=36735;
  138.      
  139.       if(wasButtonPressed == TRUE)           //condition if button was pressed or not
  140.       {                                      //if yes resets all seting after 3 seconds of inactivity
  141.         time = time+1;
  142.         if(time == 3)
  143.         {
  144.           resetSettings();
  145.         }
  146.       }
  147.       if(isDoorUnlocked == TRUE)             //condition if user unclocked door or not
  148.       {                                      //if yes door unlocked for 5 seconds (time set for faster testing)
  149.                                              //after that time door are locked again        
  150.         unlockTime = unlockTime +1;          
  151.         if(unlockTime == 5)
  152.         {
  153.            resetSettings();
  154.            displayString("DoorLocked");
  155.            unlockTime = 0;
  156.            isDoorUnlocked = FALSE;
  157.            
  158.         }
  159.    
  160.      }
  161.      
  162.  }
  163.  
  164. // <--
  165.  
  166.  
  167. void send(unsigned char data)   //sending character on LCD
  168. {
  169.   LCDCTRL |= (1<<E);
  170.   LCDPORT = data;
  171.   __delay_cycles(400);
  172.   LCDCTRL &= ~(1<<E);
  173.   __delay_cycles(500);
  174. }
  175. void displayWelcomeMessage()
  176. {
  177.   displayString("Enter Password");
  178. }
  179. void displayString(unsigned char* data)          //sending whole string on display using previous function send
  180. {
  181.   for(int i = 0; i<(unsigned)strlen(data); i++)
  182.   {
  183.     send(data[i]);
  184.   }
  185.   isMessageSent = TRUE;
  186. }
  187. void setpassword(unsigned char passdata)        //function which saves password given by a user
  188. {
  189.  
  190.   password[pasnum]= passdata;
  191.   pasnum = pasnum +1;
  192.   userPasswordSize = userPasswordSize +1;
  193. }
  194.  
  195.  
  196. void isMessageOnScreen()                        //checks if there is a message on screeen, if there is it clear display after pressing a button
  197. {
  198.     if(isMessageSent == TRUE)
  199.       {
  200.         clearDisplay();
  201.         resetSettings();
  202.       }
  203. }
  204.  
  205. //Buttons declaration using inrerupts -->
  206.  
  207. #pragma vector = INT0_vect
  208. __interrupt void button0(){     //button 0
  209.   if(buttonTimer >= delayTime)
  210.   {
  211.     setpassword('0');            //call function which assings proper charcter to an array for further chech with  password
  212.     isMessageOnScreen();         //function which checks if there is any message on screen
  213.     wasButtonPressed = TRUE;     //condition if button was pressed or not
  214.     time =0;                     //reset timer
  215.     send('0');                  //send '0' on the screen
  216.     buttonTimer = 0;
  217.   }
  218. }
  219. #pragma vector = INT1_vect
  220. __interrupt void button1(){     //button 1
  221.    if(buttonTimer >= delayTime)
  222.   {
  223.   setpassword('1');
  224.   isMessageOnScreen();
  225.   wasButtonPressed = TRUE;
  226.   time =0;
  227.   send('1');                            //send '1' on the screen
  228.   buttonTimer=0;
  229.   }
  230.  
  231. }
  232. #pragma vector = INT2_vect      //button 2
  233. __interrupt void button2(){
  234.   if(buttonTimer >= delayTime)
  235.   {
  236.   setpassword('2');
  237.   isMessageOnScreen();
  238.   wasButtonPressed = TRUE;
  239.   time =0;
  240.   state =1;
  241.   send('2');                            //send '2' on the screen
  242.  // password(3);
  243.   buttonTimer =0;
  244.   }
  245. }
  246. #pragma vector = INT3_vect      //button 3
  247. __interrupt void button3(){
  248.   if(buttonTimer >= delayTime)
  249.   {
  250.   setpassword('3');
  251.   isMessageOnScreen();
  252.   wasButtonPressed = TRUE;
  253.   time =0;
  254.   state =1;
  255.   send('3');                            //send '3' on the screen
  256.   buttonTimer =0;
  257.   }
  258.  
  259.  // password(4);
  260. }
  261. #pragma vector = INT4_vect      //button 4
  262. __interrupt void button4(){
  263.   if(buttonTimer >= delayTime)
  264.   {
  265.   setpassword('4');
  266.   isMessageOnScreen();
  267.   wasButtonPressed = TRUE;
  268.   time =0;
  269.   state =1;
  270.   send('4');                            //send '4' on the screen
  271.   buttonTimer =0;
  272.   }
  273.  
  274. }
  275. #pragma vector = INT5_vect      //button 5
  276. __interrupt void button5(){
  277.   if(buttonTimer >= delayTime)
  278.   {
  279.   setpassword('5');
  280.    isMessageOnScreen();
  281.    wasButtonPressed = TRUE;
  282.    time =0;
  283.   send('5');                            //send '5' on the screen
  284.   buttonTimer =0;
  285.   }
  286.  
  287. }
  288. #pragma vector = INT6_vect      //button ENTER
  289. __interrupt void Enter(){
  290.   if(buttonTimer >= delayTime)
  291.   {
  292.   clearDisplay();
  293.   //displayString("Applay");
  294.   wasButtonPressed = FALSE;
  295.   checkCorrectness();
  296.   buttonTimer =0;
  297.   }
  298.  
  299. }
  300. #pragma vector = INT7_vect      //button  RESET
  301. __interrupt void reset(){
  302.   if(buttonTimer >= delayTime)
  303.   {
  304.   wasButtonPressed = TRUE;
  305.    resetSettings();                     //call function which reset password
  306.   buttonTimer =0;
  307.   }
  308. }
  309.  
  310. //<--
  311.  
  312. void checkCorrectness()                 //checking is password is correct
  313. {
  314.   int wrongPassword = FALSE;
  315.   if ( userPasswordSize != sizeof(savedPassword))    //if length of password given by user =/= length of saved password, then password is false
  316.   {
  317.       clearDisplay();
  318.       PORTA= 0x00;
  319.       wrongPassword = TRUE;
  320.       displayString("Wrong Password");
  321.   }
  322.   else{
  323.       for (int i = 0; i< sizeof(password);i++)
  324.       {
  325.             if(password[i] != savedPassword[i])      //if password given by user is not the same as saved one, then password is false
  326.             {
  327.               wrongPassword = TRUE;
  328.             }
  329.       }
  330.   }
  331.   if (wrongPassword == TRUE)                        //if password is false
  332.   {                                                
  333.       clearDisplay();
  334.       PORTA =0x00;                                  //all leds are turned on
  335.       displayString("Wrong Password");              //Message that password was not correct
  336.       //display attempts left;
  337.   }
  338.   else                                              //if password was correct
  339.   {
  340.     clearDisplay();    
  341.     PORTA = 0xAA;                                  //every second led is turned on
  342.     displayString("Access Granted");               //message that we granted access beacuse password was correct
  343.     isDoorUnlocked = TRUE;
  344.   }
  345.  
  346. }
  347. void initSettingsReset()                          //function reseting all settings when program starts
  348. {
  349.    pasnum = 0;  //help variable which incremetns by pressing buttons, position in array
  350.    PORTA = ~0x00; //Turn off all leds
  351.    userPasswordSize = 0; //length of password given by user
  352.    time = 0;              //timer 1 reset
  353.    wasButtonPressed = FALSE;  //conditions if button was pressend or not
  354.  
  355. }
  356. void resetSettings()  //resseting settings
  357. {
  358.  
  359.   for(int i=0;i <sizeof(savedPassword);i++) //reseting whole array with password given by user
  360.   {
  361.       password[i]= '0';
  362.      
  363.      
  364.   }
  365.       isDoorUnlocked = FALSE;             //condition if door are unlocker or not
  366.       clearDisplay();                     //clearing the display
  367.       pasnum = 0;                         //help variable which incremetns by pressing buttons, position in array
  368.       PORTA = ~0x00;                      //turn off all leds
  369.       userPasswordSize = 0;               //number of characters given by user while entering the password
  370.       if(wasButtonPressed == TRUE)
  371.       {
  372.         clearDisplay();                
  373.         wasButtonPressed = FALSE;
  374.       }
  375.       time = 0;                           //timer 1 reset
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement