Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -----------------------------------------------------------------------------
- * File: cofre.c
- * Project: exemplo de cofre
- * Author: Leandro Schwarz
- * Last modified: 22/08/2014
- * -------------------------------------------------------------------------- */
- // System definitions
- #define F_CPU 16000000UL
- #define MCU_8_BITS
- // Header files
- #include "LS_defines.h"
- #include "LS_ATmega328.h"
- #include "LS_keypad.h"
- #include "LS_HD44780.h"
- // Constant definitions
- #define STATE_IDLE 0
- #define STATE_READ_PASS 1
- #define STATE_CHECK_PASS 2
- // Function declaration
- void updateScreen(void);
- // Global variables
- uint8 state = STATE_IDLE;
- uint8 passwordIndex = 0;
- uint16 entry = 0;
- // main function
- int main()
- {
- uint8 key;
- uint16 password = 1802;
- usartDisableTransmitter();
- usartDisableReceiver();
- // LCD configuration
- lcdInit();
- lcdStdio();
- printf(" Projeto Cofre \n Versao 1.0 \n");
- _delay_ms(2000);
- lcdClearScreen();
- // Keypad configuration
- keypadInit( 0x07, 0x08, 0x09, 0x0A,
- 0x04, 0x05, 0x06, 0x0B,
- 0x01, 0x02, 0x03, 0x0C,
- 0x0E, 0x00, 0x0F, 0x0D);
- state = STATE_READ_PASS;
- updateScreen();
- while(1){
- // States machine
- switch(state){
- // IDLE STATE
- case STATE_IDLE:
- break;
- // READ PASS STATE
- case STATE_READ_PASS:
- key = keypadRead();
- if(key != 0xFF){
- if(key < 0x0A){
- entry *= 10;
- entry += key;
- passwordIndex++;
- if(passwordIndex == 4)
- state = STATE_CHECK_PASS;
- updateScreen();
- }
- }
- break;
- // CHECK PASS STATE
- case STATE_CHECK_PASS:
- if(password == entry){
- lcdCursorMoveFirstLine();
- printf(" ACERTOU! \n\n");
- _delay_ms(3000);
- }else{
- lcdCursorMoveFirstLine();
- printf(" ERROU! \n\n");
- _delay_ms(3000);
- }
- entry = 0;
- passwordIndex = 0;
- state = STATE_READ_PASS;
- updateScreen();
- break;
- }
- }
- return 0;
- }
- // Function updateScreen()
- void updateScreen(void)
- {
- uint8 i;
- lcdCursorMoveFirstLine();
- printf(" Senha: ");
- for(i = 0;i < passwordIndex;i++)
- printf("*");
- for(;i < 4;i++)
- printf("-");
- printf("\n%d\n", entry);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment