Password Keypad LCD
Password Keypad LCD
b. Buttom Layout
3. Program
a. Coding AVR
1. #include <mega16a.h>
2. #include <alcd.h>
3. #include <stdint.h>
4. #include <delay.h>
5. #include <stdio.h>
6.
7. #define READY 0
8. #define TRUEPASS 1
9. #define WRONGPASS 2
10. #define BINTANG 13
11. #define PAGAR 15
12.
13. int8_t pollingKeypad();
14. void shortPress(int8_t key);
15. int8_t globalKey = -1;
16. int8_t state = 0;
17. void updateLCD();
18. void getText();
19. uint32_t nilaiKey = 0;
20. char line1[20], line2[20];
21. uint32_t PASS = 2164;
22.
23. void main(void)
24. {
25.
26. int8_t key = -1;
27.
28. DDRA = 0XFF;
29. DDRB.3=1;
30. DDRC = 0X0F;
31.
32. PORTA = 0X00;
33. PORTB.3=0;
34. PORTB.3=1;
35. PORTC = 0XFF;
36.
37. lcd_init(16);
38.
39. while (1)
40. {
41. globalKey = key = pollingKeypad();
42. if (key != -1)
43. {
44.
45. shortPress(key); lcd_clear(); delay_ms(20);
46. while (pollingKeypad() != -1) {}
47. }
48. updateLCD();
49. }
50. }
51. int8_t pollingKeypad()
52. {
53. int8_t key = -1;
54. PORTC = 0b11111110;
55. if(PINC.4 == 0)key = 13; //*
56. else if(PINC.5 == 0)key = 12; //C
57. else if(PINC.6 == 0)key = 11; //B
58. else if(PINC.7 == 0)key = 10; //A
59.
60. PORTC = 0b11111101;
61. if(PINC.4 == 0)key = 14; //D
62. else if(PINC.5 == 0)key = 9;
63. else if(PINC.6 == 0)key = 6;
64. else if(PINC.7 == 0)key = 3;
65.
66. PORTC = 0b11111011;
67. if(PINC.4 == 0)key = 15; //#
68. else if(PINC.5 == 0)key = 8;
69. else if(PINC.6 == 0)key = 5;
70. else if(PINC.7 == 0)key = 2;
71.
72. PORTC = 0b11110111;
73. if(PINC.4 == 0) key = 0;
74. else if(PINC.5 == 0)key = 7;
75. else if(PINC.6 == 0)key = 4;
76. else if(PINC.7 == 0)key = 1;
77. return key;
78. }
79.
80. void shortPress(int8_t key)
81. {
82. if ( state == READY ){
83. if ( key < 10 ){
84. nilaiKey = nilaiKey *10 + key;
85. }
86.
87. else if ( key == BINTANG ){
88. nilaiKey = 0; state = READY;
89. }
90. else if ( key == PAGAR ){
91. if ( nilaiKey == PASS){
92. nilaiKey = 0; state = TRUEPASS;
93. }
94. else{
95. nilaiKey = 0; state = WRONGPASS;
96. }
97. }
98. }
99.
100. else if ( state == TRUEPASS){
101. if ( key == BINTANG ){
102. state = READY;
103. }
104. }
105.
106. else if ( state == WRONGPASS){
107. if ( key == BINTANG ){
108. state = READY;
109. }
110. }
111. }
112. void updateLCD()
113. {
114. getText();
115. lcd_gotoxy(0,0);
116. lcd_puts(line1);
117. lcd_gotoxy(0,1);
118. lcd_puts(line2);
119.
120. }
121. void getText()
122. {
123. if ( state == READY ){
124. sprintf(line1,"INPUT PASSWORD ");
125. sprintf(line2,"PASSWORD: %u",nilaiKey);
126. PORTA=0b00000000;
127. }
128.
129. else if (state == TRUEPASS){
130. sprintf(line1,"PASSWORD BENAR");
131. sprintf(line2,"");
132. PORTA=0b00000001;
133. delay_ms(50);
134. PORTA=0b00000010;
135. delay_ms(50);
136. PORTA=0b00000100;
137. delay_ms(50);
138. PORTA=0b00001000;
139. delay_ms(50);
140. PORTA=0b00010000;
141. delay_ms(50);
142. PORTA=0b00100000;
143. delay_ms(50);
144. PORTA=0b01000000;
145. delay_ms(50);
146. PORTA=0b10000000;
147. delay_ms(50);
148. }
149.
150. else if (state == WRONGPASS){
151. sprintf(line1,"PASSWORD SALAH");
152. sprintf(line2,"");
153.
154. }
155. }