Monday, August 26, 2013

Microcontroller Based Digital Lock - Engineering Project



The digital lock with password handling capacity has replaced the old locks for it has many advantages. A programmable code lock can be used for numerous applications in which access to an article/gadget is to be restricted to a limited number of persons. The main advantage is the security and safety these lock provides. The circuit is simpler in design and easy to implement. These have attracted many possibilities for manufacture of the digital locks as per customer’s convenience. The locks have password facility which ensures that the trusted person/persons can only access these. The password facility helps in complete safety and security of the customer. The amount of theft is thus reduced greatly. The microcontroller based digital lock we have used has 89c51 controller at its heart. The use of microcontroller has greatly reduced the circuit complexity. The keypad has been interfaced to the controller as an input whereas the LCD display is used as an output. The key pressed will tell whether the entered password is correct or not. The message is displayed on the LCD screen. The digital locks, in future, will effectively replace the old key-locks. They can be modified with the help of retina scans or image processing devices which ensures no need even to remember the password! The size of the locks can be as small as the pocket calculators to larger sizes as per the choice. The banks in particular have started opting for digital locking system. Many new ultra modern housing societies have digital security system nowadays.
 4x3 Keypad
The keypad comprises of 4 rows and 3 columns. The keypad design is in the matrix form with transistors used as switches. The rows are forcefully grounded,i.e., they are made zero by the programmer and the columns are at high potential. The key pressed is detected by sending a zero signal to the processor. This determines the exact key pressed. Key should be pressed more than 20ns for detection.
89c51 Microprocessor
89c51 is a 40 pin IC. It has 4 I/O ports of which 3 I/O ports are used here. Port 3 is a multifunction port with control signals embedded on it. When 1s are written to port 0 pins, the pins can be used as high impedance inputs. When 1s are written to Port 1 pins they are pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 1 pins that are externally being pulled low with source current (IIL) because of the internal pull-ups. Port 2 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 2 output buffers can sink/source four TTL inputs. When 1s are written to Port 2 pins they are pulled high by the internal pull-ups and can be used as inputs.
16x2 LCD
It is a 16x2 character LCD. It has provisions for backlight LEDs. The common features of this LCD are
5 x 8 dots with cursor
Built-in controller (KS 0066 or Equivalent)
+ 5V power supply (Also available for + 3V)
1/16 duty cycle
B/L to be driven by pin 1, pin 2 or pin 15, pin 16 or A.K (LED)

4x3 KEYPAD
Scanning and identifying the key
               
4*3 matrix is connected to two ports. The rows are connected to an output port and the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1s for all columns since they are all connected to high (Vcc) If all the rows are grounded and a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed.

Grounding rows and reading columns
               
To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, and then it reads the columns. If the data read from the columns is D3-D0=1111, no key has been pressed and the process continues until a key press is detected. However, if one of the column bits has a zero, this means that a key press has occurred. For example, if D3-D0=1101, this means that a key in the D1 column has been pressed. After a key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row D0 only; then it reads the columns. If the data read is all 1s, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. After identification of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed.


To make sure that the preceding key has been released, 0s are output to all rows at once, and the columns are read and checked repeatedly until all the columns are high. When all columns are found to be high, the program waits for a short amount of time before it goes to the next stage of waiting for a key to be pressed.

To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it. Remember that the output latches connected to rows still have their initial zeros, making them grounded. After the key press detection, it waits 20ms for the bounce and then scans the columns again. This serves two functions: (a) it ensures that the first key press detection was not an erroneous one due to spike noise, and(b) the 20ms delay prevents the same key press from being interpreted as a multiple key press. If after the 20-ms delay the key is still pressed, it goes to the next stage to detect which row it belongs to; otherwise, it goes back into the loop to detect a real key press. To detect which row the key press belongs to, it grounds one row at a time, reading the columns each time. If it finds that all columns are high, this means that the key press cannot belong to that row; therefore, it grounds the next row and continues until it finds the row the key press belongs to. Upon finding the row that the key press belongs to, it sets up the starting address for the look-up table holding the scan codes (or the ASCII value) for that row and goes to the next stage to identify the key. To identify the key press, it rotates the column bits, one bit at a time, into the carry flag and checks to see if it is low. Upon finding the zero, it pulls out the ASCII code for that key from the look-up table; Otherwise, it increments the pointer to point to the next element of the look-up table.
Keypad interfaced with microcontroller
The microcontroller accesses both rows and columns through the port
1.    Make all rows of port P0 high so that it gives high signal when key is pressed.
2.    See if any key is pressed by scanning the port P0 by checking all columns for non zero condition.
3.    If any key is pressed, to identify which key is pressed make one row high at a time.
4.    Initiate a counter to hold the count so that each key is counted.
5.    Check port P0 for nonzero condition. If any nonzero number is there in [accumulator], start column scanning by following step 9.
6.    Otherwise make next row high in port P0.
7.    Add a count of 08h to the counter to move to the next row by repeating steps from step 6.
8.    If any key pressed is found, the [accumulator] content is rotated right through the carry until carry bit sets, while doing this increment the count in the counter till carry is found.
9.    Move the content in the counter to display in data field or to memory location
10.  To repeat the procedures go to step 2. 
LCD interfacing with microcontroller
LCD module has 8-bit data interface and control pins. One can send data as 8-bit or in pair of two 4-bit nibbles. To display any character on LCD micro controller has to send its ASCII value to the data bus of LCD. For e.g. to display 'AB' microcontroller has to send two hex bytes 41h and 42h respectively.LCD display used here is having 16x2 size. It means 2 lines each with 16 characters.

Algorithm to send data to LCD:
1. Make R/W low
2. Make RS=0; if data byte is command
RS=1; if data byte is data (ASCII value)
3. Place data byte on data register
4. Pulse E (HIGH to LOW)
5. Repeat the steps to send another data byte



LCD Initialization
This is the pit fall for beginners. Proper working of LCD depend on the how the LCD is initialized. We have to send few command bytes to initialize the lcd. Simple steps to initialize the LCD are
  1. Specify function set:
                Send 38H for 8-bit, double line and 5x7 dot character format.
  1. Display On-Off control:
                Send 0FH for display and blink cursor on.
  1. Entry mode set:
                Send 06H for cursor in increment position and shift is invisible.
  1. Clear display:
          Send 01H to clear display and return cursor to home position.

ASSEMBLY LANGUAGE PROGRAM
         

 ORG     0000H
           
            MOV     P0,#00H
                        MOV   R2,#04H
                        CLR     P3.5

            MOV   A,#30H
            ACALL            COMMAND
            MOV   A,#38H
            ACALL            COMMAND
            MOV   A,#08H
            ACALL            COMMAND
            MOV   A,#01H
      ACALL      COMMAND
            MOV   A,#06H
            ACALL            COMMAND
            MOV   A,#0DH
            ACALL            COMMAND
            MOV   A,#01H
            ACALL            COMMAND
            MOV   A,#80H
            ACALL            COMMAND
                        MOV   A,#'E'
            ACALL            DISPLAY
            MOV   A,#'N'
            ACALL            DISPLAY
            MOV   A,#'T'
            ACALL            DISPLAY
      MOV         A,#'E'
            ACALL            DISPLAY
            MOV   A,#'R'
            ACALL            DISPLAY
            MOV   A,#' '
                        ACALL            DISPLAY
                        MOV   A,#'P'
                        ACALL            DISPLAY
                        MOV   A,#'A'
                        ACALL            DISPLAY
                        MOV   A,#'S'
                        ACALL            DISPLAY
                        MOV   A,#'S'
                        ACALL            DISPLAY
                        MOV   A,#'W'
                        ACALL            DISPLAY
                        MOV   A,#'O'
                        ACALL            DISPLAY
                        MOV   A,#'R'
                        ACALL            DISPLAY
                        MOV   A,#'D'
                        ACALL            DISPLAY
                        MOV   A,#':'
            ACALL            DISPLAY
                        MOV   A,#0C2H
                        ACALL            COMMAND

K1:                   MOV   P2,#0FH
                        MOV   A,P2
                        ANL     A,#00001111B
                        CJNE  A,#00001111B,K1

K2:                   ACALL            DELAY
                        MOV   A,P2
                        ANL     A,#00001111B
                        CJNE  A,#00001111B,OVER
                        SJMP  K2

OVER:                        ACALL            DELAY
                        MOV   A,P2
                        ANL     A,#00001111B
                        CJNE  A,#00001111B,OVER1
                        SJMP K2

OVER1:          CLR     P2.7
                        SETB  P2.6
                        SETB  P2.5
                        SETB  P2.4
                        MOV   A,P2
                        ANL     A,#00001111B
                        CJNE  A,#00001111B,ROW_0
           
                        CLR     P2.6
                        SETB  P2.7
                        MOV     A,P2
                        ANL     A,#00001111B
                        CJNE  A,#00001111B,ROW_1

                        CLR     P2.5
                        SETB  P2.6
                        MOV   A,P2
                        ANL     A,#00001111B
                        CJNE  A,#00001111B,ROW_2

                        CLR     P2.4
                        SETB  P2.5
                        MOV   A,P2
                        ANL     A,#00001111B
                        CJNE  A,#00001111B,ROW_3
ABCD:             LJMP    K1

ROW_0:          MOV   DPTR,#KCODE0
                        SJMP  FIND
ROW_1:          MOV   DPTR,#KCODE1
                        SJMP  FIND
ROW_2:          MOV   DPTR,#KCODE2
                        SJMP  FIND
ROW_3:          MOV   DPTR,#KCODE3
                        SJMP  FIND

FIND:              RRC    A
FIND1:            RRC    A         
                        JNC     MATCH
                        INC      DPTR
                        SJMP  FIND1

MATCH:          CLR     A
                        MOVC A,@A+DPTR
           
                        CJNE  R2,#4H,PWL1
            MOV     31H,A
                        ACALL            DISPLAY
                        DEC    R2
                        SJMP  ABCD

PWL1:             CJNE  R2,#3H,PWL2
            MOV     32H,A
                        ACALL            DISPLAY
                        DEC    R2
                        SJMP  ABCD 
           
PWL2:             CJNE    R2,#2H,PWL3
            MOV     33H,A
                        ACALL            DISPLAY
                        DEC    R2
                        SJMP  ABCD
                       
PWL3: 
            MOV     34H,A
                        ACALL            DISPLAY
      
                        SJMP  D3

D3:                  MOV   DPTR,#MASTER
                        CLR     A
                        MOVC A,@A+DPTR

            CJNE    A,31H,WRONG
                        INC      DPTR
                        CLR     A
                        MOVC A,@A+DPTR

            CJNE    A,32H,WRONG
                        INC      DPTR
                        CLR     A
                        MOVC A,@A+DPTR

            CJNE    A,33H,WRONG
                        INC      DPTR
                        CLR     A
                        MOVC A,@A+DPTR

            CJNE    A,34H,WRONG
                        SJMP  RIGHT

                        LJMP  K1

WRONG: 
                        MOV   A,#01H
            ACALL            COMMAND
                        MOV   A,#'P'
            ACALL            DISPLAY
            MOV   A,#'A'
            ACALL            DISPLAY
            MOV   A,#'S'
            ACALL            DISPLAY
            MOV   A,#'S'
            ACALL            DISPLAY
            MOV   A,#'W'
            ACALL            DISPLAY
            MOV   A,#'O'
                        ACALL            DISPLAY
                        MOV   A,#'R'
                        ACALL            DISPLAY
                        MOV   A,#'D'
                        ACALL            DISPLAY
                        MOV   A,#' '
                        ACALL            DISPLAY
                        MOV   A,#'W'
                        ACALL            DISPLAY
                        MOV   A,#'R'
                        ACALL            DISPLAY
                        MOV   A,#'O'
                        ACALL            DISPLAY
                        MOV   A,#'N'
                        ACALL            DISPLAY
                        MOV   A,#'G'
                        ACALL            DISPLAY
                        MOV   A,#'!'
                        ACALL            DISPLAY
                        SETB  P3.5
                                   
STAY:             SJMP  STAY
                        RET

RIGHT: 
                        MOV   A,#01H
            ACALL            COMMAND
                        MOV   A,#'P'
            ACALL            DISPLAY
            MOV   A,#'A'
            ACALL            DISPLAY
            MOV   A,#'S'
            ACALL            DISPLAY
            MOV   A,#'S'
            ACALL            DISPLAY
            MOV   A,#'W'
            ACALL            DISPLAY
            MOV   A,#'O'
                        ACALL            DISPLAY
                        MOV   A,#'R'
                        ACALL            DISPLAY
                        MOV   A,#'D'
                        ACALL            DISPLAY
                        MOV   A,#' '
                        ACALL            DISPLAY
                        MOV   A,#'R'
                        ACALL            DISPLAY
                        MOV   A,#'I'
                        ACALL            DISPLAY
                        MOV   A,#'G'
                        ACALL            DISPLAY
                        MOV   A,#'H'
                        ACALL            DISPLAY
                        MOV   A,#'T'
                        ACALL            DISPLAY
                        MOV   A,#'!'
                        ACALL            DISPLAY
                       
                       
                        MOV   P0,#0FFH
STAY1:           SJMP    STAY1
                        RET


COMMAND:           
                        ACALL            READY
                        MOV   P1,#00H
                        MOV   P1,A
            CLR     P3.2
            CLR     P3.1
            SETB    P3.0
                        ACALL            DELAY1         
            CLR     P3.0
                        RET    

READY:          PUSH A
                        MOV   P1,#0FFH
            CLR     P3.0
            CLR     P3.2
            SETB    P3.1
WAIT:              CLR     P3.0
                        MOV   A,P1
            SETB    P3.0
                        MOV   A,P1
            CLR     P3.0
            JB      ACC.7,WAIT
                        POP    A
                        RET    

DISPLAY:       ACALL            READY
                        MOV   P1,#00H
                        MOV   P1,A
            SETB    P3.2
            CLR     P3.1
            SETB    P3.0
            ACALL   DELAY1
            CLR     P3.0
                        RET    

DELAY:           MOV   R0,#0FFH
H1:      MOV   R1,#0FFH
H:        DJNZ R1,H
                        DJNZ R0,H1
                        RET    

DELAY1:         MOV   R3,#0FFH
HERE3:           DJNZ R3,HERE3
            RET



            ORG   300H
KCODE0:        DB       '1','2','3'
KCODE1:        DB       '4','5','6'
KCODE2:        DB       '7','8','9'
KCODE3:        DB       '*','0','#'


MASTER:       DB      '1','2','3','4'
                        END





0 comments:

Post a Comment