bladabuska

cofre.c v1.0

Aug 22nd, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. /* -----------------------------------------------------------------------------
  2.  * File:            cofre.c
  3.  * Project:         exemplo de cofre
  4.  * Author:          Leandro Schwarz
  5.  * Last modified:   22/08/2014
  6.  * -------------------------------------------------------------------------- */
  7.  
  8. // System definitions
  9. #define F_CPU   16000000UL
  10. #define MCU_8_BITS
  11.  
  12. // Header files
  13. #include "LS_defines.h"
  14. #include "LS_ATmega328.h"
  15. #include "LS_keypad.h"
  16. #include "LS_HD44780.h"
  17.  
  18. // Constant definitions
  19.  
  20. #define STATE_IDLE          0
  21. #define STATE_READ_PASS     1
  22. #define STATE_CHECK_PASS    2
  23.  
  24. // Function declaration
  25. void updateScreen(void);
  26.  
  27. // Global variables
  28. uint8 state = STATE_IDLE;
  29. uint8 passwordIndex = 0;
  30. uint16 entry = 0;
  31.  
  32. // main function
  33. int main()
  34. {
  35.     uint8 key;
  36.     uint16 password = 1802;
  37.  
  38.     usartDisableTransmitter();
  39.     usartDisableReceiver();
  40.    
  41.     // LCD configuration
  42.     lcdInit();
  43.     lcdStdio();
  44.     printf(" Projeto Cofre  \n   Versao 1.0   \n");
  45.     _delay_ms(2000);
  46.     lcdClearScreen();
  47.  
  48.     // Keypad configuration
  49.     keypadInit( 0x07, 0x08, 0x09, 0x0A,
  50.                 0x04, 0x05, 0x06, 0x0B,
  51.                 0x01, 0x02, 0x03, 0x0C,
  52.                 0x0E, 0x00, 0x0F, 0x0D);
  53.    
  54.     state = STATE_READ_PASS;
  55.     updateScreen();
  56.  
  57.     while(1){
  58.    
  59.         // States machine
  60.         switch(state){
  61.  
  62.         // IDLE STATE
  63.         case STATE_IDLE:
  64.             break;
  65.  
  66.         // READ PASS STATE
  67.         case STATE_READ_PASS:
  68.             key = keypadRead();
  69.             if(key != 0xFF){
  70.                 if(key < 0x0A){
  71.                     entry *= 10;
  72.                     entry += key;
  73.                     passwordIndex++;
  74.                     if(passwordIndex == 4)
  75.                         state = STATE_CHECK_PASS;
  76.                     updateScreen();
  77.                 }
  78.             }
  79.             break;
  80.  
  81.         // CHECK PASS STATE
  82.         case STATE_CHECK_PASS:
  83.             if(password == entry){
  84.                 lcdCursorMoveFirstLine();
  85.                 printf("    ACERTOU!    \n\n");
  86.                 _delay_ms(3000);
  87.             }else{
  88.                 lcdCursorMoveFirstLine();
  89.                 printf("     ERROU!     \n\n");
  90.                 _delay_ms(3000);
  91.             }
  92.             entry = 0;
  93.             passwordIndex = 0;
  94.             state = STATE_READ_PASS;
  95.             updateScreen();
  96.             break;
  97.         }
  98.     }
  99.     return 0;
  100. }
  101.  
  102. // Function updateScreen()
  103. void updateScreen(void)
  104. {
  105.     uint8 i;
  106.  
  107.     lcdCursorMoveFirstLine();
  108.     printf(" Senha: ");
  109.     for(i = 0;i < passwordIndex;i++)
  110.         printf("*");
  111.     for(;i < 4;i++)
  112.         printf("-");
  113.     printf("\n%d\n", entry);
  114.  
  115.     return;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment