-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
152 lines (128 loc) · 5.46 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "msp.h"
#include "LCD.h"
#include "keypad.h"
#include "delay.h"
/**
* main.c
*/
void main(void)
{
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
LCD_init();
keypad_init();
int i = 0;
unsigned char program = 0;
unsigned char key= keypad_getkey();
typedef enum {
locked,
unlocked,
set_key
} state;
state current_state;
unsigned char password[4];
unsigned char attempt[4];
current_state = set_key;
while(1){
switch(current_state){
case locked :
LCD_command(CLR_DISP); // clear screen
LCD_command(0x80|0x00);//set cursor top left
LCD_write_word("Locked");
LCD_command(SET_CURSOR | 0x40); // Move cursor to 2nd line
LCD_write_word("Enter Key:"); // when using string/char * use "" instead of ''
while(key == 13){key = keypad_getkey();} //wait for button to be pressed
attempt[0]= key;
LCD_write(key + 0x30); //changes char to ASCII character for number
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
while(key == 13){key = keypad_getkey();}//wait for button press 2
attempt[1] =key;
LCD_write(key + 0x30); // writes number to LCD
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
while(key == 13){key = keypad_getkey();}//wait for button press 3
attempt[2]=key;
LCD_write(key + 0x30); // writes number to LCD
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
while(key == 13){key = keypad_getkey();}//wait for button press 4
attempt[3]= key;
LCD_write(key + 0x30); // writes number to LCD
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
current_state = unlocked;
for (i = 0; i <= 3; i++) {
if (attempt[i] != password[i]) {
current_state = locked;
break;
}
}
break;
case unlocked:
LCD_command(CLR_DISP); // clear screen
LCD_command(0x80|0x00);//set cursor top left
LCD_write_word("Unlocked");
LCD_command(SET_CURSOR | 0x40); // Move cursor to 2nd line
LCD_write_word("Set Key? Press *"); // when using string/char * use "" instead of ''
while(key == 13){key = keypad_getkey();} //wait for button to be pressed
program = key;
LCD_write(key + 0x30); //changes char to ASCII character for number
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
if(program == 10){ // the sTarrrr *
current_state = set_key;
}
else{
current_state = locked;
}
break;
case set_key:
LCD_command(CLR_DISP); // clear screen
LCD_command(0x80|0x00);//set cursor top left
LCD_write_word("Set Key");
LCD_command(SET_CURSOR | 0x40); // Move cursor to 2nd line
LCD_write_word("Enter Key:"); // when using string/char * use "" instead of ''
while(key == 13){key = keypad_getkey();} //wait for button to be pressed
password[0] = key;
LCD_write(key + 0x30); //changes char to ASCII character for number
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
while(key == 13){key = keypad_getkey();}//wait for button press 2
password[1] = key;
LCD_write(key + 0x30); // writes number to LCD
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
while(key == 13){key = keypad_getkey();}//wait for button press 3
password[2] = key;
LCD_write(key + 0x30); // writes number to LCD
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
while(key == 13){key = keypad_getkey();}//wait for button press 4
password[3] = key;
LCD_write(key + 0x30); // writes number to LCD
while(key!=13){// wait for button to be released
delay_ms(1);
key=keypad_getkey();
}
current_state = locked;
break;
default: current_state = locked;
}//switch
}//while
}//main