Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. /**
  2.  * main.c
  3.  */
  4. void main(void)
  5. {
  6.     WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;     // stop watchdog timer
  7.     set_DCO(CURRENT_FREQ);
  8.     LCD_INIT();
  9.     init_keypad();
  10.     write_string("LOCKED", 0x00);
  11.     write_string("ENTER KEY", 0x40);
  12.     char key, index = 0;
  13.     char key_str[4];
  14.     char answer[4]="1234";
  15.  
  16.     while (1){
  17.         //check initial key presses
  18.         key=keypad_check();
  19.         if (key != '\0'){
  20.             if (key=='*'){ //clear screen and restart if * pressed
  21.                 index=0;
  22.                 clear_LCD();
  23.                 write_string("LOCKED", 0x00);
  24.                 write_string("ENTER KEY", 0x40);
  25.             }
  26.             else{ //otherwise print the character pressed and add to the answer string
  27.                 write_char_LCD(0x4A+index, key);
  28.                 key_str[index]=key;
  29.                 index++;
  30.                 delay_ms(500, CURRENT_FREQ);
  31.             }
  32.         }
  33.         //once four characters have been entered check against answer string
  34.         if (index== 4){
  35.             index=0;
  36.             //if code is correct, print hello world and wait for next input
  37.             if (key_str[0]==answer[0] && key_str[1]==answer[1] && key_str[2]==answer[2] && key_str[3]==answer[3]) {
  38.                 clear_LCD();
  39.                 write_string("HELLO WORLD", 0x00);
  40.                 //once correct entered, wait for next key
  41.                 while (1){
  42.                     key=keypad_check();
  43.                     if (key != '\0'){
  44.                         //if next key is *, clear and go back to locked screen
  45.                         if (key=='*'){
  46.                             index=0;
  47.                             clear_LCD();
  48.                             write_string("LOCKED", 0x00);
  49.                             write_string("ENTER KEY", 0x40);
  50.                             break;
  51.                         }
  52.                         //if next key is #, prompt for new code and
  53.                         else if (key == '#'){
  54.                             clear_LCD();
  55.                             write_string("ENTER NEW CODE", 0x00);
  56.                             while (1) {
  57.                                 key=keypad_check();
  58.                                 if (key != '\0'){
  59.                                     //if * is pressed, clear and restart new code entry
  60.                                     if (key=='*'){
  61.                                         index=0;
  62.                                         clear_LCD();
  63.                                         write_string("ENTER NEW CODE", 0x00);
  64.                                     }
  65.                                     //change code to next key pressed and exit once 4 digits entered
  66.                                     else{
  67.                                         write_char_LCD(0x40+index, key);
  68.                                         answer[index]=key;
  69.                                         index++;
  70.                                         delay_ms(500, CURRENT_FREQ);
  71.                                         if (index == 4) {
  72.                                             index = 0;
  73.                                             clear_LCD();
  74.                                             write_string("LOCKED", 0x00);
  75.                                             write_string("ENTER KEY", 0x40);
  76.                                             break;
  77.                                         }
  78.                                     }
  79.                                 }
  80.                             }
  81.                             break;
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.             //if code is incorrect return to locked screen
  87.             else {
  88.                 clear_LCD();
  89.                 write_string("LOCKED", 0x00);
  90.                 write_string("ENTER KEY", 0x40);
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement