han back
/
CLEO_SMART_DOORLOCK
SMART CLEO Smart Doorlock
Revision 0:b5bd2de438b7, committed 2017-12-06
- Comitter:
- SMART_CLEO
- Date:
- Wed Dec 06 08:53:21 2017 +0000
- Commit message:
- SMART_CLEO
Changed in this revision
diff -r 000000000000 -r b5bd2de438b7 TextLCD/TextLCD.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD/TextLCD.cpp Wed Dec 06 08:53:21 2017 +0000 @@ -0,0 +1,163 @@ +/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 + * Copyright (c) 2007-2010, sford + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "TextLCD.h" +#include "mbed.h" + +TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, + PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw), + _e(e), _d(d0, d1, d2, d3), + _type(type) { + + _rw = 0; + _e = 1; + _rs = 0; // command mode + _d.output(); + + wait(0.015); // Wait 15ms to ensure powered up + + // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) + for (int i=0; i<3; i++) { + writeByte(0x3); + wait(0.00164); // this command takes 1.64ms, so wait for it + } + writeByte(0x2); // 4-bit mode + wait(0.000040f); // most instructions take 40us + + writeCommand(0x28); // Function set 001 BW N F - - + writeCommand(0x0C); + writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes + cls(); +} + +void TextLCD::character(int column, int row, int c) { + int a = address(column, row); + writeCommand(a); + writeData(c); +} + +void TextLCD::cls() { + writeCommand(0x01); // cls, and set cursor to 0 + wait(0.00164f); // This command takes 1.64 ms + locate(0, 0); +} + +void TextLCD::locate(int column, int row) { + _column = column; + _row = row; +} + +int TextLCD::_putc(int value) { + if (value == '\n') { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } else { + character(_column, _row, value); + _column++; + if (_column >= columns()) { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } + } + return value; +} + +int TextLCD::_getc() { + return -1; +} + +void TextLCD::writeByte(int value) { + _rw = 0; + + _d = value >> 4; + wait(0.000040f); // most instructions take 40us + _e = 0; + wait(0.000040f); + _e = 1; + _d = value >> 0; + wait(0.000040f); + _e = 0; + wait(0.000040f); // most instructions take 40us + _e = 1; +} + +void TextLCD::writeCommand(int command) { + _rs = 0; + writeByte(command); +} + +void TextLCD::writeData(int data) { + _rs = 1; + writeByte(data); +} + +int TextLCD::address(int column, int row) { + switch (_type) { + case LCD20x4: + switch (row) { + case 0: + return 0x80 + column; + case 1: + return 0xc0 + column; + case 2: + return 0x94 + column; + case 3: + return 0xd4 + column; + } + case LCD16x2B: + return 0x80 + (row * 40) + column; + case LCD16x2: + case LCD20x2: + default: + return 0x80 + (row * 0x40) + column; + } +} + +int TextLCD::columns() { + switch (_type) { + case LCD20x4: + case LCD20x2: + return 20; + case LCD16x2: + case LCD16x2B: + default: + return 16; + } +} + +int TextLCD::rows() { + switch (_type) { + case LCD20x4: + return 4; + case LCD16x2: + case LCD16x2B: + case LCD20x2: + default: + return 2; + } +}
diff -r 000000000000 -r b5bd2de438b7 TextLCD/TextLCD.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD/TextLCD.h Wed Dec 06 08:53:21 2017 +0000 @@ -0,0 +1,111 @@ +/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 + * Copyright (c) 2007-2010, sford + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_TEXTLCD_H +#define MBED_TEXTLCD_H + +#include "mbed.h" + +/** A TextLCD interface for driving 4-bit HD44780-based LCDs + * + * Currently supports 16x2, 20x2 and 20x4 panels + * + * @code + * #include "mbed.h" + * #include "TextLCD.h" + * + * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d0-d3 + * + * int main() { + * lcd.printf("Hello World!\n"); + * } + * @endcode + */ +class TextLCD : public Stream { +public: + + /** LCD panel format */ + enum LCDType { + LCD16x2 /**< 16x2 LCD panel (default) */ + , LCD16x2B /**< 16x2 LCD panel alternate addressing */ + , LCD20x2 /**< 20x2 LCD panel */ + , LCD20x4 /**< 20x4 LCD panel */ + }; + + /** Create a TextLCD interface + * + * @param rs Instruction/data control line + * @param e Enable line (clock) + * @param d0-d3 Data lines + * @param type Sets the panel size/addressing mode (default = LCD16x2) + */ + TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, LCDType type = LCD16x2); + +#if DOXYGEN_ONLY + /** Write a character to the LCD + * + * @param c The character to write to the display + */ + int putc(int c); + + /** Write a formated string to the LCD + * + * @param format A printf-style format string, followed by the + * variables to use in formating the string. + */ + int printf(const char* format, ...); +#endif + + /** Locate to a screen column and row + * + * @param column The horizontal position from the left, indexed from 0 + * @param row The vertical position from the top, indexed from 0 + */ + void locate(int column, int row); + + /** Clear the screen and locate to 0,0 */ + void cls(); + + int rows(); + int columns(); + +protected: + + // Stream implementation functions + virtual int _putc(int value); + virtual int _getc(); + + int address(int column, int row); + void character(int column, int row, int c); + void writeByte(int value); + void writeCommand(int command); + void writeData(int data); + + DigitalOut _rs, _rw, _e; + BusInOut _d; + LCDType _type; + + int _column; + int _row; +}; + +#endif
diff -r 000000000000 -r b5bd2de438b7 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 06 08:53:21 2017 +0000 @@ -0,0 +1,262 @@ +#include "mbed.h" +#include "TextLCD.h" + +// C1, C2, C3, C4 +PinName pin_KEYPAD_C[4] = {PC_4, PC_5, PC_6, PC_7}; +// R1, R2, R3, R4 +PinName pin_KEYPAD_R[4] = {PC_8, PC_9, PC_10, PC_11}; + // 1A, 1B, 2A, 2B +PinName pin_STEP[4] = {PA_5, PA_6, PA_7, PA_8}; + +PinName pin_BUZZER = PD_2; + +DigitalOut Buzzer(pin_BUZZER); + +DigitalInOut Keypad_C1(pin_KEYPAD_C[0]); +DigitalInOut Keypad_C2(pin_KEYPAD_C[1]); +DigitalInOut Keypad_C3(pin_KEYPAD_C[2]); +DigitalInOut Keypad_C4(pin_KEYPAD_C[3]); +BusOut Step(pin_STEP[0], pin_STEP[1], pin_STEP[2], pin_STEP[3]); + +// R1, R2, R3, R4 +BusIn Keypad_R(pin_KEYPAD_R[0], pin_KEYPAD_R[1], pin_KEYPAD_R[2], pin_KEYPAD_R[3]); +// rs, rw, e, d0-d3 +TextLCD lcd(PB_12, PB_13, PB_14, PB_15, PA_9, PA_10, PA_11); + +enum{OPEN, CLOSE}; + +uint8_t Phase_2[4] = {0x03, 0x06, 0x0C, 0x09}; +char Key_value[16] = {'1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D'}; +char Password[4] = {'0', '0', '0', '0'}; +char NowPassword[4] = {'0', '0', '0', '0'}; +volatile uint8_t Password_setting = 0, Password_check = 0, Doorlock_flag = 1; + +volatile uint16_t Keypad = 0, Keypad_pre = 0, i; + +uint16_t Keypad_Read(void); +void Doorlock(uint8_t mode); +void buzzer_ctrl(uint8_t count); +void key_save(char key); + +int main() { + + char Key_Now_value; + + lcd.printf("Smart Doorlock\n"); + lcd.printf("Doorlock CLOSE"); + + while(1) { + Keypad = Keypad_Read(); + if(Keypad != 0) + { + if(Keypad_pre != Keypad) + { + Keypad_pre = Keypad; + buzzer_ctrl(1); + for(i=0; i<16; i++) + { + if(Keypad & (0x0001 << i)) + { + Key_Now_value = Key_value[i]; + break; + } + } + if((Password_setting == 0) && (Password_check == 0)) + { + if(Key_Now_value == '*') + { + if(Doorlock_flag == 0) + { + Doorlock_flag = 1; + Doorlock(CLOSE); + buzzer_ctrl(2); + lcd.locate(0, 1); + lcd.printf("Doorlock CLOSE"); + } + else + { + Password_check++; + } + } + else if(Key_Now_value == '#') + { + Password_setting++; + } + } + else + { + if((Key_Now_value >= '0') && (Key_Now_value <= '9')) + { + key_save(Key_Now_value); + if(Password_setting) + { + Password_setting++; + } + else if(Password_check) + { + Password_check++; + } + } + else if(Password_setting) + { + if(Key_Now_value == '#') + { + if(Password_setting >= 5) + { + for(i=0; i<4; i++) + Password[i] = NowPassword[i]; + buzzer_ctrl(3); + } + Password_setting = 0; + } + } + else if(Password_check) + { + if(Key_Now_value == '*') + { + if(Password_check >= 5) + { + Password_check = 0; + for(i=0; i<4; i++) + { + if(Password[i] != NowPassword[i]) + { + Password_check++; + } + } + if(Password_check == 0) + { + Doorlock(OPEN); + lcd.locate(0, 1); + lcd.printf("Doorlock OPEN "); + buzzer_ctrl(3); + Doorlock_flag = 0; + } + else + { + lcd.locate(0, 1); + lcd.printf("Password Error"); + buzzer_ctrl(2); + wait(2); + lcd.locate(0, 1); + lcd.printf("Doorlock CLOSE"); + } + } + Password_check = 0; + } + } + } + } + } + else + { + Keypad_pre = 0; + } + } +} + +// +// bit | 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +// ------------------------------------------------------- +// value | D # 0 * C 9 8 7 B 6 5 4 A 3 2 1 +// +// 1 2 3 A +// 4 5 6 B +// 7 8 9 C +// * 0 # D +// +uint16_t Keypad_Read(void) +{ + uint16_t keypad_tmp, Read_data = 0, i; + for(i=0; i<4; i++) + { + switch(i) + { + case 0: + Keypad_C1.output(); + Keypad_C1 = 1; + break; + case 1: + Keypad_C2.output(); + Keypad_C2 = 1; + break; + case 2: + Keypad_C3.output(); + Keypad_C3 = 1; + break; + case 3: + Keypad_C4.output(); + Keypad_C4 = 1; + break; + } + + wait(0.001); + keypad_tmp = Keypad_R; + Read_data |= (keypad_tmp << (i*4)); + switch(i) + { + case 0: + Keypad_C1 = 0; + Keypad_C1.input(); + break; + case 1: + Keypad_C2 = 0; + Keypad_C2.input(); + break; + case 2: + Keypad_C3 = 0; + Keypad_C3.input(); + break; + case 3: + Keypad_C4 = 0; + Keypad_C4.input(); + break; + } + } + return Read_data; +} + +void Doorlock(uint8_t mode) +{ + int16_t i; + + if(mode == OPEN) + { + for(i=0; i<200; i++) + { + Step = Phase_2[i%4]; + wait(0.005); + } + } + else + { + for(i=200; i>=0; i--) + { + Step = Phase_2[i%4]; + wait(0.005); + } + } +} + +void buzzer_ctrl(uint8_t count) +{ + int i; + for(i=0; i<count; i++) + { + Buzzer = 1; + wait(0.1); + Buzzer = 0; + if(i != (count-1)) + wait(0.1); + } +} + +void key_save(char key) +{ + int i; + for(i=0; i<3; i++) + { + NowPassword[i] = NowPassword[i+1]; + } + NowPassword[3] = key; +} \ No newline at end of file
diff -r 000000000000 -r b5bd2de438b7 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Wed Dec 06 08:53:21 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#ca661f9d28526ca8f874b05432493a489c9671ea