Advertisement
Guest User

Untitled

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