march 2014 - site.iugaza.edu.pssite.iugaza.edu.ps/wp-content/uploads/lab3_keypad... · 4 embedded...
TRANSCRIPT
March 2014
3
2 Embedded Systems Lab
Objectives
- To be familiar with interfacing Keypads to PIC18F4550 as input/output
devices.
- To deal with new components such as BCD 7-segment and sounder.
- To be able to start building real useful applications.
Keypads
Matrix keypads are simply an extension to the simple tact switch inputs. They
consist of keys interconnected in the shape of a matrix. Each key is a simple
mechanical switch located at the crossing between the matrix rows and
columns. When a key is pressed, its row and column form an electrical contact.
The rows and columns can be connected to the pins of microcontroller ports. The
big advantage of using a matrix keypad is that it allows interfacing a large
number of keys with a relatively small number of microcontroller pins. For
example, a 12-key keypad requires only 7 (instead of 12, if interfaced
individually) I/O pins of the microcontroller if organized into a 4 rows and 3
columns matrix.
Keypad
3 Embedded Systems Lab
Let's consider the case of a 12 keys matrix as the one shown in the figure and we
want to interface this keypad to PIC18F4550 microcontroller. In order to detect
that one key is pressed we will use the following strategy:
1. The 12 keys are arranged in 3 columns and 4 rows. Each column and row is
connected by a separated wire.
2. Pushing a key merely connects the corresponding row to the corresponding
column.
3. We connect the columns to 3 output pins of the PIC and the rows to other 4
input pins.
4. For each of the columns we output logic '1' holding other columns to logic '0'
and scan the values of the rows, if one of the rows is logic '1', this means that
the key located at the intersection of the current column and the current row
is pressed. If no row is one, the logic '1' should be moved to the next column
and the rows then rescanned.
5. The PIC should keep repeating this process every time it's required to detect
a key press.
4 Embedded Systems Lab
Notice
In order to start, you must know some important notes:
- Keypad at Proteus: write keypad << choose Keypad-Phone (2nd choice).
- It is preferred to use ports efficiently by reserving just one port for keypad.
- For all next parts, you will deal with some new components and new codes,
all of that will mainly depend on the main concept of keypad’s work.
So, think .. then start working
In case the microcontroller does not have the internal pull-down
feature on its I/O port, the pull-down resistors on the input lines
must be connected externally. With our keypad, resistors must be
connected with rows.
1. Write an assembly and basic programs that displays the
value of the pressed key on a 7-segment.
2. Simulate the circuit using Proteus ISIS program.
3. Connect the circuit at the hardware kit.
Lab Work 1
5 Embedded Systems Lab
Proteus
How to think …
- Strategy of checking pressed number will depend on the previous
description of keypad structure and work.
- Pins that are connected to the columns are configured as outputs
and to the rows as inputs.
- Checking will be by:
Enabling the first column by entering 1 0 0 for the pins that are
connected with columns.
Check the four cells (pins) at the first column.
If any of rows pins is pressed, load the 7-seg representation value
of pressed number to the connected port of 7-seg.
Repeat the previous steps for the second and the third columns
with changing enabling way at the first step (0 1 0 .. then .. 0 0 1).
- With Proteus, remember the order of columns and rows: 1-3 from
left to right for columns, 1-4 from top to bottom for rows.
6 Embedded Systems Lab
Mikrobasic
7 Embedded Systems Lab
Assembly
How to think …
- The same work will be here with some differences: Enable column <<
test rows, but with external loop that contains the repeated test
operation.
- The operation of loading numbers into 7-seg will be done as an
external codes (out of loop) using goto instruction (just for code
management).
- Every pressed number is displayed at 7-seg by loading its value and
calling delay subroutine.
8 Embedded Systems Lab
New instructions are used:
Byte-oriented File Register Operations
- Clrf portx: clear f: portx = 0x00
Bit-oriented File Register Operations
- Bsf portx,b: bit set f: sets bit ‘b’ of portx (bit=1).
- Bcf portx,b: bit clear f: clears bit ‘b’ of portx (bit=0).
- Btfsc portx,b: bit test file, skip if clear:
If bit ‘b’ in register ‘f’ is 0, then the next instruction is
skipped
Start the loop
9 Embedded Systems Lab
Zero label denotes to the code of
loading the 7-seg representation of
zero to the port with delay << then
goto x to continue checking pressed
numbers.
To make a continues loop
10 Embedded Systems Lab
1. Edit your programs and simulation in order to display the
value of the pressed key on a BCD 7-segment.
Lab Work 2
- Binary coded decimal 7-segment display takes the numbers
from 0-9 and displays them normally. Its internal work
converts the entered decimal to binary by representing the
number with four digits then views it as normal 7-seg.
- At Proteus: bcd display
- It has four pins 1-4 from right to left << connected to the pins
of your port.
11 Embedded Systems Lab
Proteus
One of the edited parts at Assembly (the same for other numbers)
At Mikrobasic, just change the loaded value of numbers to the ports for all
columns and rows.
12 Embedded Systems Lab
For more of scientific interest and to be more integrated in the practical
applications of programming PIC (Mikrobasic) …
Starting from this lab, Application part will be allocated at the work; it will
be divided into parts through the labs. For its end, we will get a useful
application similar to one of important application in the world that is
possible for you to develop or expand it for wider use for the future.
Access control System
(Secure Electronic Lock)
I It has a distinct evaluation of the lab report, so try to reserve a
place of your work for our new task, which is .. Application.
Note
1. Write a Mikrobasic program that enable the user to enter his
password then press “check” button, if the password matches
the stored one, the LED will shine.
Application - Part 1
13 Embedded Systems Lab
Proteus
How to think …
- Checking the pressed numbers will be the same of the previous
parts, but instead of loading the value into the port when the
number is pressed, we will make the following steps:
Password will be 3-digits << the original password will be entered
into the code as an array of integers.
The user must press 3 numbers, every time the pressed number is
stored in another array with different index.
Index will be determined as a counter of pressed numbers: ex:
If the user pressed 1: counter will start from 0: array[0] will store
1, then counter is incremented, and the same work happened for
2nd and 3rd numbers.
- After the checking of the pressed numbers code, we must check if
the button is pressed and if the entered password matches the
stored one. If the both conditions are achieved, LED will shine.
14 Embedded Systems Lab
Array of password digits:
declared as a constant
with integer type.
Array of the entered
password by the user.
Counter of pressed
numbers by user, as an
index of pass counter
array
We can use while ‘condition’ instead of using
‘delay_ms’ in order to be sure for getting one
press during long press. This happened using
empty while loop that make the PIC remains at
this state through a long press.
15 Embedded Systems Lab
16 Embedded Systems Lab
Index of array starts with 0,
condition check is done by ‘and’
operation.
17 Embedded Systems Lab
1. Construct the circuit shown in the figure below and
write a mikrobasic program that will generate a
different sound for each keypad key.
- Sound will be generated by “sounder” device that
is connected to the pin and to the ground by its
two sides.
- Sounder has two functions that will be used:
Sound_init
- Sound_init(port, pin) ..ex: sound_init(portb,0):
configure the pin (0 here) of port (b here) for sound
generation.
- It is placed before „while true‟ as a part of configuration section with tris configuration, .. etc.
Homework
18 Embedded Systems Lab
… Good Luck …
Sound_play
- Sound_play(frequence_in_hz, duration_in_ms).
ex: sound_play(1900,400): generates a sound with
1900hz freq. and 400ms duration.
- It is placed instead of loading numbers into 7-seg
port with the same code.
- In order to generate a mixed series of tones, you
can repeat this function for several times for
different frequencies and durations.
Feel free to build your own tones
2. Develop your Application using:
- Sounder, by making different tunes for warning or
welcoming for the user (for in/correct password).
- Green led shining for correct password (3-digits
only) and red led shining for incorrect password.
- Use red led shining also to alarm the error for
entering more than or less than 3 digits for
password (1234 is incorrect!).
3. Include your labwork at the report (all work of
labwork1 and just edited parts at the other labworks
or application with their simulation results).