SMART CLEO Smart Doorlock

Committer:
SMART_CLEO
Date:
Wed Dec 06 08:53:21 2017 +0000
Revision:
0:b5bd2de438b7
SMART_CLEO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SMART_CLEO 0:b5bd2de438b7 1 #include "mbed.h"
SMART_CLEO 0:b5bd2de438b7 2 #include "TextLCD.h"
SMART_CLEO 0:b5bd2de438b7 3
SMART_CLEO 0:b5bd2de438b7 4 // C1, C2, C3, C4
SMART_CLEO 0:b5bd2de438b7 5 PinName pin_KEYPAD_C[4] = {PC_4, PC_5, PC_6, PC_7};
SMART_CLEO 0:b5bd2de438b7 6 // R1, R2, R3, R4
SMART_CLEO 0:b5bd2de438b7 7 PinName pin_KEYPAD_R[4] = {PC_8, PC_9, PC_10, PC_11};
SMART_CLEO 0:b5bd2de438b7 8 // 1A, 1B, 2A, 2B
SMART_CLEO 0:b5bd2de438b7 9 PinName pin_STEP[4] = {PA_5, PA_6, PA_7, PA_8};
SMART_CLEO 0:b5bd2de438b7 10
SMART_CLEO 0:b5bd2de438b7 11 PinName pin_BUZZER = PD_2;
SMART_CLEO 0:b5bd2de438b7 12
SMART_CLEO 0:b5bd2de438b7 13 DigitalOut Buzzer(pin_BUZZER);
SMART_CLEO 0:b5bd2de438b7 14
SMART_CLEO 0:b5bd2de438b7 15 DigitalInOut Keypad_C1(pin_KEYPAD_C[0]);
SMART_CLEO 0:b5bd2de438b7 16 DigitalInOut Keypad_C2(pin_KEYPAD_C[1]);
SMART_CLEO 0:b5bd2de438b7 17 DigitalInOut Keypad_C3(pin_KEYPAD_C[2]);
SMART_CLEO 0:b5bd2de438b7 18 DigitalInOut Keypad_C4(pin_KEYPAD_C[3]);
SMART_CLEO 0:b5bd2de438b7 19 BusOut Step(pin_STEP[0], pin_STEP[1], pin_STEP[2], pin_STEP[3]);
SMART_CLEO 0:b5bd2de438b7 20
SMART_CLEO 0:b5bd2de438b7 21 // R1, R2, R3, R4
SMART_CLEO 0:b5bd2de438b7 22 BusIn Keypad_R(pin_KEYPAD_R[0], pin_KEYPAD_R[1], pin_KEYPAD_R[2], pin_KEYPAD_R[3]);
SMART_CLEO 0:b5bd2de438b7 23 // rs, rw, e, d0-d3
SMART_CLEO 0:b5bd2de438b7 24 TextLCD lcd(PB_12, PB_13, PB_14, PB_15, PA_9, PA_10, PA_11);
SMART_CLEO 0:b5bd2de438b7 25
SMART_CLEO 0:b5bd2de438b7 26 enum{OPEN, CLOSE};
SMART_CLEO 0:b5bd2de438b7 27
SMART_CLEO 0:b5bd2de438b7 28 uint8_t Phase_2[4] = {0x03, 0x06, 0x0C, 0x09};
SMART_CLEO 0:b5bd2de438b7 29 char Key_value[16] = {'1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D'};
SMART_CLEO 0:b5bd2de438b7 30 char Password[4] = {'0', '0', '0', '0'};
SMART_CLEO 0:b5bd2de438b7 31 char NowPassword[4] = {'0', '0', '0', '0'};
SMART_CLEO 0:b5bd2de438b7 32 volatile uint8_t Password_setting = 0, Password_check = 0, Doorlock_flag = 1;
SMART_CLEO 0:b5bd2de438b7 33
SMART_CLEO 0:b5bd2de438b7 34 volatile uint16_t Keypad = 0, Keypad_pre = 0, i;
SMART_CLEO 0:b5bd2de438b7 35
SMART_CLEO 0:b5bd2de438b7 36 uint16_t Keypad_Read(void);
SMART_CLEO 0:b5bd2de438b7 37 void Doorlock(uint8_t mode);
SMART_CLEO 0:b5bd2de438b7 38 void buzzer_ctrl(uint8_t count);
SMART_CLEO 0:b5bd2de438b7 39 void key_save(char key);
SMART_CLEO 0:b5bd2de438b7 40
SMART_CLEO 0:b5bd2de438b7 41 int main() {
SMART_CLEO 0:b5bd2de438b7 42
SMART_CLEO 0:b5bd2de438b7 43 char Key_Now_value;
SMART_CLEO 0:b5bd2de438b7 44
SMART_CLEO 0:b5bd2de438b7 45 lcd.printf("Smart Doorlock\n");
SMART_CLEO 0:b5bd2de438b7 46 lcd.printf("Doorlock CLOSE");
SMART_CLEO 0:b5bd2de438b7 47
SMART_CLEO 0:b5bd2de438b7 48 while(1) {
SMART_CLEO 0:b5bd2de438b7 49 Keypad = Keypad_Read();
SMART_CLEO 0:b5bd2de438b7 50 if(Keypad != 0)
SMART_CLEO 0:b5bd2de438b7 51 {
SMART_CLEO 0:b5bd2de438b7 52 if(Keypad_pre != Keypad)
SMART_CLEO 0:b5bd2de438b7 53 {
SMART_CLEO 0:b5bd2de438b7 54 Keypad_pre = Keypad;
SMART_CLEO 0:b5bd2de438b7 55 buzzer_ctrl(1);
SMART_CLEO 0:b5bd2de438b7 56 for(i=0; i<16; i++)
SMART_CLEO 0:b5bd2de438b7 57 {
SMART_CLEO 0:b5bd2de438b7 58 if(Keypad & (0x0001 << i))
SMART_CLEO 0:b5bd2de438b7 59 {
SMART_CLEO 0:b5bd2de438b7 60 Key_Now_value = Key_value[i];
SMART_CLEO 0:b5bd2de438b7 61 break;
SMART_CLEO 0:b5bd2de438b7 62 }
SMART_CLEO 0:b5bd2de438b7 63 }
SMART_CLEO 0:b5bd2de438b7 64 if((Password_setting == 0) && (Password_check == 0))
SMART_CLEO 0:b5bd2de438b7 65 {
SMART_CLEO 0:b5bd2de438b7 66 if(Key_Now_value == '*')
SMART_CLEO 0:b5bd2de438b7 67 {
SMART_CLEO 0:b5bd2de438b7 68 if(Doorlock_flag == 0)
SMART_CLEO 0:b5bd2de438b7 69 {
SMART_CLEO 0:b5bd2de438b7 70 Doorlock_flag = 1;
SMART_CLEO 0:b5bd2de438b7 71 Doorlock(CLOSE);
SMART_CLEO 0:b5bd2de438b7 72 buzzer_ctrl(2);
SMART_CLEO 0:b5bd2de438b7 73 lcd.locate(0, 1);
SMART_CLEO 0:b5bd2de438b7 74 lcd.printf("Doorlock CLOSE");
SMART_CLEO 0:b5bd2de438b7 75 }
SMART_CLEO 0:b5bd2de438b7 76 else
SMART_CLEO 0:b5bd2de438b7 77 {
SMART_CLEO 0:b5bd2de438b7 78 Password_check++;
SMART_CLEO 0:b5bd2de438b7 79 }
SMART_CLEO 0:b5bd2de438b7 80 }
SMART_CLEO 0:b5bd2de438b7 81 else if(Key_Now_value == '#')
SMART_CLEO 0:b5bd2de438b7 82 {
SMART_CLEO 0:b5bd2de438b7 83 Password_setting++;
SMART_CLEO 0:b5bd2de438b7 84 }
SMART_CLEO 0:b5bd2de438b7 85 }
SMART_CLEO 0:b5bd2de438b7 86 else
SMART_CLEO 0:b5bd2de438b7 87 {
SMART_CLEO 0:b5bd2de438b7 88 if((Key_Now_value >= '0') && (Key_Now_value <= '9'))
SMART_CLEO 0:b5bd2de438b7 89 {
SMART_CLEO 0:b5bd2de438b7 90 key_save(Key_Now_value);
SMART_CLEO 0:b5bd2de438b7 91 if(Password_setting)
SMART_CLEO 0:b5bd2de438b7 92 {
SMART_CLEO 0:b5bd2de438b7 93 Password_setting++;
SMART_CLEO 0:b5bd2de438b7 94 }
SMART_CLEO 0:b5bd2de438b7 95 else if(Password_check)
SMART_CLEO 0:b5bd2de438b7 96 {
SMART_CLEO 0:b5bd2de438b7 97 Password_check++;
SMART_CLEO 0:b5bd2de438b7 98 }
SMART_CLEO 0:b5bd2de438b7 99 }
SMART_CLEO 0:b5bd2de438b7 100 else if(Password_setting)
SMART_CLEO 0:b5bd2de438b7 101 {
SMART_CLEO 0:b5bd2de438b7 102 if(Key_Now_value == '#')
SMART_CLEO 0:b5bd2de438b7 103 {
SMART_CLEO 0:b5bd2de438b7 104 if(Password_setting >= 5)
SMART_CLEO 0:b5bd2de438b7 105 {
SMART_CLEO 0:b5bd2de438b7 106 for(i=0; i<4; i++)
SMART_CLEO 0:b5bd2de438b7 107 Password[i] = NowPassword[i];
SMART_CLEO 0:b5bd2de438b7 108 buzzer_ctrl(3);
SMART_CLEO 0:b5bd2de438b7 109 }
SMART_CLEO 0:b5bd2de438b7 110 Password_setting = 0;
SMART_CLEO 0:b5bd2de438b7 111 }
SMART_CLEO 0:b5bd2de438b7 112 }
SMART_CLEO 0:b5bd2de438b7 113 else if(Password_check)
SMART_CLEO 0:b5bd2de438b7 114 {
SMART_CLEO 0:b5bd2de438b7 115 if(Key_Now_value == '*')
SMART_CLEO 0:b5bd2de438b7 116 {
SMART_CLEO 0:b5bd2de438b7 117 if(Password_check >= 5)
SMART_CLEO 0:b5bd2de438b7 118 {
SMART_CLEO 0:b5bd2de438b7 119 Password_check = 0;
SMART_CLEO 0:b5bd2de438b7 120 for(i=0; i<4; i++)
SMART_CLEO 0:b5bd2de438b7 121 {
SMART_CLEO 0:b5bd2de438b7 122 if(Password[i] != NowPassword[i])
SMART_CLEO 0:b5bd2de438b7 123 {
SMART_CLEO 0:b5bd2de438b7 124 Password_check++;
SMART_CLEO 0:b5bd2de438b7 125 }
SMART_CLEO 0:b5bd2de438b7 126 }
SMART_CLEO 0:b5bd2de438b7 127 if(Password_check == 0)
SMART_CLEO 0:b5bd2de438b7 128 {
SMART_CLEO 0:b5bd2de438b7 129 Doorlock(OPEN);
SMART_CLEO 0:b5bd2de438b7 130 lcd.locate(0, 1);
SMART_CLEO 0:b5bd2de438b7 131 lcd.printf("Doorlock OPEN ");
SMART_CLEO 0:b5bd2de438b7 132 buzzer_ctrl(3);
SMART_CLEO 0:b5bd2de438b7 133 Doorlock_flag = 0;
SMART_CLEO 0:b5bd2de438b7 134 }
SMART_CLEO 0:b5bd2de438b7 135 else
SMART_CLEO 0:b5bd2de438b7 136 {
SMART_CLEO 0:b5bd2de438b7 137 lcd.locate(0, 1);
SMART_CLEO 0:b5bd2de438b7 138 lcd.printf("Password Error");
SMART_CLEO 0:b5bd2de438b7 139 buzzer_ctrl(2);
SMART_CLEO 0:b5bd2de438b7 140 wait(2);
SMART_CLEO 0:b5bd2de438b7 141 lcd.locate(0, 1);
SMART_CLEO 0:b5bd2de438b7 142 lcd.printf("Doorlock CLOSE");
SMART_CLEO 0:b5bd2de438b7 143 }
SMART_CLEO 0:b5bd2de438b7 144 }
SMART_CLEO 0:b5bd2de438b7 145 Password_check = 0;
SMART_CLEO 0:b5bd2de438b7 146 }
SMART_CLEO 0:b5bd2de438b7 147 }
SMART_CLEO 0:b5bd2de438b7 148 }
SMART_CLEO 0:b5bd2de438b7 149 }
SMART_CLEO 0:b5bd2de438b7 150 }
SMART_CLEO 0:b5bd2de438b7 151 else
SMART_CLEO 0:b5bd2de438b7 152 {
SMART_CLEO 0:b5bd2de438b7 153 Keypad_pre = 0;
SMART_CLEO 0:b5bd2de438b7 154 }
SMART_CLEO 0:b5bd2de438b7 155 }
SMART_CLEO 0:b5bd2de438b7 156 }
SMART_CLEO 0:b5bd2de438b7 157
SMART_CLEO 0:b5bd2de438b7 158 //
SMART_CLEO 0:b5bd2de438b7 159 // bit | 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SMART_CLEO 0:b5bd2de438b7 160 // -------------------------------------------------------
SMART_CLEO 0:b5bd2de438b7 161 // value | D # 0 * C 9 8 7 B 6 5 4 A 3 2 1
SMART_CLEO 0:b5bd2de438b7 162 //
SMART_CLEO 0:b5bd2de438b7 163 // 1 2 3 A
SMART_CLEO 0:b5bd2de438b7 164 // 4 5 6 B
SMART_CLEO 0:b5bd2de438b7 165 // 7 8 9 C
SMART_CLEO 0:b5bd2de438b7 166 // * 0 # D
SMART_CLEO 0:b5bd2de438b7 167 //
SMART_CLEO 0:b5bd2de438b7 168 uint16_t Keypad_Read(void)
SMART_CLEO 0:b5bd2de438b7 169 {
SMART_CLEO 0:b5bd2de438b7 170 uint16_t keypad_tmp, Read_data = 0, i;
SMART_CLEO 0:b5bd2de438b7 171 for(i=0; i<4; i++)
SMART_CLEO 0:b5bd2de438b7 172 {
SMART_CLEO 0:b5bd2de438b7 173 switch(i)
SMART_CLEO 0:b5bd2de438b7 174 {
SMART_CLEO 0:b5bd2de438b7 175 case 0:
SMART_CLEO 0:b5bd2de438b7 176 Keypad_C1.output();
SMART_CLEO 0:b5bd2de438b7 177 Keypad_C1 = 1;
SMART_CLEO 0:b5bd2de438b7 178 break;
SMART_CLEO 0:b5bd2de438b7 179 case 1:
SMART_CLEO 0:b5bd2de438b7 180 Keypad_C2.output();
SMART_CLEO 0:b5bd2de438b7 181 Keypad_C2 = 1;
SMART_CLEO 0:b5bd2de438b7 182 break;
SMART_CLEO 0:b5bd2de438b7 183 case 2:
SMART_CLEO 0:b5bd2de438b7 184 Keypad_C3.output();
SMART_CLEO 0:b5bd2de438b7 185 Keypad_C3 = 1;
SMART_CLEO 0:b5bd2de438b7 186 break;
SMART_CLEO 0:b5bd2de438b7 187 case 3:
SMART_CLEO 0:b5bd2de438b7 188 Keypad_C4.output();
SMART_CLEO 0:b5bd2de438b7 189 Keypad_C4 = 1;
SMART_CLEO 0:b5bd2de438b7 190 break;
SMART_CLEO 0:b5bd2de438b7 191 }
SMART_CLEO 0:b5bd2de438b7 192
SMART_CLEO 0:b5bd2de438b7 193 wait(0.001);
SMART_CLEO 0:b5bd2de438b7 194 keypad_tmp = Keypad_R;
SMART_CLEO 0:b5bd2de438b7 195 Read_data |= (keypad_tmp << (i*4));
SMART_CLEO 0:b5bd2de438b7 196 switch(i)
SMART_CLEO 0:b5bd2de438b7 197 {
SMART_CLEO 0:b5bd2de438b7 198 case 0:
SMART_CLEO 0:b5bd2de438b7 199 Keypad_C1 = 0;
SMART_CLEO 0:b5bd2de438b7 200 Keypad_C1.input();
SMART_CLEO 0:b5bd2de438b7 201 break;
SMART_CLEO 0:b5bd2de438b7 202 case 1:
SMART_CLEO 0:b5bd2de438b7 203 Keypad_C2 = 0;
SMART_CLEO 0:b5bd2de438b7 204 Keypad_C2.input();
SMART_CLEO 0:b5bd2de438b7 205 break;
SMART_CLEO 0:b5bd2de438b7 206 case 2:
SMART_CLEO 0:b5bd2de438b7 207 Keypad_C3 = 0;
SMART_CLEO 0:b5bd2de438b7 208 Keypad_C3.input();
SMART_CLEO 0:b5bd2de438b7 209 break;
SMART_CLEO 0:b5bd2de438b7 210 case 3:
SMART_CLEO 0:b5bd2de438b7 211 Keypad_C4 = 0;
SMART_CLEO 0:b5bd2de438b7 212 Keypad_C4.input();
SMART_CLEO 0:b5bd2de438b7 213 break;
SMART_CLEO 0:b5bd2de438b7 214 }
SMART_CLEO 0:b5bd2de438b7 215 }
SMART_CLEO 0:b5bd2de438b7 216 return Read_data;
SMART_CLEO 0:b5bd2de438b7 217 }
SMART_CLEO 0:b5bd2de438b7 218
SMART_CLEO 0:b5bd2de438b7 219 void Doorlock(uint8_t mode)
SMART_CLEO 0:b5bd2de438b7 220 {
SMART_CLEO 0:b5bd2de438b7 221 int16_t i;
SMART_CLEO 0:b5bd2de438b7 222
SMART_CLEO 0:b5bd2de438b7 223 if(mode == OPEN)
SMART_CLEO 0:b5bd2de438b7 224 {
SMART_CLEO 0:b5bd2de438b7 225 for(i=0; i<200; i++)
SMART_CLEO 0:b5bd2de438b7 226 {
SMART_CLEO 0:b5bd2de438b7 227 Step = Phase_2[i%4];
SMART_CLEO 0:b5bd2de438b7 228 wait(0.005);
SMART_CLEO 0:b5bd2de438b7 229 }
SMART_CLEO 0:b5bd2de438b7 230 }
SMART_CLEO 0:b5bd2de438b7 231 else
SMART_CLEO 0:b5bd2de438b7 232 {
SMART_CLEO 0:b5bd2de438b7 233 for(i=200; i>=0; i--)
SMART_CLEO 0:b5bd2de438b7 234 {
SMART_CLEO 0:b5bd2de438b7 235 Step = Phase_2[i%4];
SMART_CLEO 0:b5bd2de438b7 236 wait(0.005);
SMART_CLEO 0:b5bd2de438b7 237 }
SMART_CLEO 0:b5bd2de438b7 238 }
SMART_CLEO 0:b5bd2de438b7 239 }
SMART_CLEO 0:b5bd2de438b7 240
SMART_CLEO 0:b5bd2de438b7 241 void buzzer_ctrl(uint8_t count)
SMART_CLEO 0:b5bd2de438b7 242 {
SMART_CLEO 0:b5bd2de438b7 243 int i;
SMART_CLEO 0:b5bd2de438b7 244 for(i=0; i<count; i++)
SMART_CLEO 0:b5bd2de438b7 245 {
SMART_CLEO 0:b5bd2de438b7 246 Buzzer = 1;
SMART_CLEO 0:b5bd2de438b7 247 wait(0.1);
SMART_CLEO 0:b5bd2de438b7 248 Buzzer = 0;
SMART_CLEO 0:b5bd2de438b7 249 if(i != (count-1))
SMART_CLEO 0:b5bd2de438b7 250 wait(0.1);
SMART_CLEO 0:b5bd2de438b7 251 }
SMART_CLEO 0:b5bd2de438b7 252 }
SMART_CLEO 0:b5bd2de438b7 253
SMART_CLEO 0:b5bd2de438b7 254 void key_save(char key)
SMART_CLEO 0:b5bd2de438b7 255 {
SMART_CLEO 0:b5bd2de438b7 256 int i;
SMART_CLEO 0:b5bd2de438b7 257 for(i=0; i<3; i++)
SMART_CLEO 0:b5bd2de438b7 258 {
SMART_CLEO 0:b5bd2de438b7 259 NowPassword[i] = NowPassword[i+1];
SMART_CLEO 0:b5bd2de438b7 260 }
SMART_CLEO 0:b5bd2de438b7 261 NowPassword[3] = key;
SMART_CLEO 0:b5bd2de438b7 262 }