03 - MPI - SHU (Usama)
03 - MPI - SHU (Usama)
03 - MPI - SHU (Usama)
LABORATORY MANUAL
ELE-301 MICROPROCESSOR AND INTERFACING
STUDENT NAME
STUDENT ID
BATCH
LAB INSTRUCTOR:
Engr. Muhammad Aamir
Lab Rubrics
Assessment Criteria for Lab
Outcomes Assessment:
2 LAB EXPERIMENT
OBJECT:
Interfacing keypad and joystick with microcontroller.
THEORY
KEYPAD
Keypad or matrix keypad is a matrix of tact switches. It plays very important part in Human Machine Interface.
This enables the user to input values to the embedded system. This value can be a temperature level or a password
for a digital lock. Its construction is fairly simple as shown in below figure. The keypad’s switches are arranged
in Rows and Column. So product of number of rows and column define total number of switches
Mode Of Operation
One of the advantage of using a keypad is that it allow the programmer to reduce the number if pins to used for
interfacing. For example, a 4x4 matrix keypad would have 16 switches and need at least 16 switches and need
16 pins of the microcontroller to read each switch. But it is possible to reduce the 16 pins to 8 pin by using
normal mode of operation.
Normal Mode
In this mode, rows and columns pins are connected to the microcontroller’s port directly. The rows are defined as
output whereas column pins are defined as input in the programming. The controller will read the data or input at
the port and then check all the possible combinations for each switch. For example if we give a high on row and
reads the status of column 1 as high that means switch 9 or (S9) has got pressed. Whenever a switch is pressed
the column will get high (1).
Debouncing
These are spurious spikes generated during keypress events. Normally several spikes get generated during a key
press because of the mechanical contacts in the switch and it causes multiple spikes, which may be detected as
multiple keypress events instead of a single event. Below is an Oscilloscope screenshot of the keypress event.
There you can see, there are multiple spikes during the low to high transition.
We can filter these spikes with the help of either a hardware circuit or a software routine. Hardware methods need
to use low pass filters in each line which increases the number of components in the circuit. Here we use the
easiest way, the software.
We are going to use time window for reading the port status. Introduce a delay between detection and scanning.
By introducing a delay of 100ms between detection and scanning, we can differentiate between spikes and actual
key press.
KEYPAD ENCODER
A keypad encoder is a technique by which the keypad is interfaced to the microcontroller. 74C923 is a keypad
encoder IC with features such as contact debouncing and key auto-repeat which considerably reduces software
overhead in the host microcontroller and lessen the I/O pin requirements to just one from eight. The key press
data is returned through a serial or parallel interface. The best of advantages is that the use of this IC reduces the
work on firmware, which in turn reduces time to market.
LAB EXCERSIE
In this lab, you will program a system that will take user input number and display number on 7 segment LEDs.
The user will give input by using a keypad as shown in figure. The 4x3 keypad, meaning it has 4 rows and 3
columns, will be connected to PORTA of the ATMEGA 32 IC and PORTD will display the output number in
BCD format. The PORTD is connected to 74LS47, which is an BCD to 7 segment decoders, which is then
connected to two common anode 7 segment display. The circuit and code are given below.
Figure 3 4X3 keypad (left) and 4X4 keypad (right). Where R and C represent the rows and columns respectively
SCHEMATIC DIAGRAM
CODE
/*
* Keypad interfacing.cpp
*
* Created: 2/18/2023 4:15:10 PM
* Author : maamir
* PORT C pin 0 to 3 connected to columns while PC4-PC7 connected to rows
*PORT D is connected to 7 segment
*/
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
char keyfind()
{
while(1)
{
KEY_DDR = 0xF0; /* set port direction as input-output */
KEY_PRT = 0xFF;
do
{
KEY_PRT &= 0x0F; /* mask PORT for column read
only */
colloc = (KEY_PIN & 0x0F); /* read status of column */
}while(colloc != 0x0F);
do
{
do
{
_delay_ms(20); /* 20ms key debounce time */
colloc = (KEY_PIN & 0x0F); /* read status of column */
}while(colloc == 0x0F); /* check for any key
press */
break;
}
if(colloc == 0x0E)
return(keypad[rowloc][0]);
else if(colloc == 0x0D)
return(keypad[rowloc][1]);
else if(colloc == 0x0B)
return(keypad[rowloc][2]);
else
return(keypad[rowloc][3]);
}
int main(void)
{
unsigned char x,prenum=0;
DDRD=0xff;
PORTD=0x51;
_delay_ms(100);
while(1)
{
prenum=prenum<<4;
x=keyfind();
PORTD=x|prenum; /* Display which key is pressed */
prenum=x;
}
}
Key Detection
To detect a key pressed, 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-DO =1111, no key has been pressed and the process continues
until a key press is detected. However, if on of the column bit has a zero, this means that a key press is occurred.
For example, if D3- D) =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 microcontroller detect the position of key by using column and row data.
Lab Activity
1. Implement the circuit in proteus by compiling the given program using AVR studio 7
2. Upload the HEX file of the above program in ATMEGA 32 controller using universal programmer.
HEX file is available on Desktop. Place the programmed IC in ATMEGA 32A modules and attached
the modules with the trainer. Connect PORT A with J14 of the trainer and PORT D with J7. Press the
reset switch on the module.
3. Observe the operation of trainer.
LAB TASK
1. Explain how does code display the previous number?
when input is given at Port A, it increases the logic level of all rows of the keyboard. keyfind ()
function is then called to store the column number in the colloc variable and it will be stored and
continuously check the columns until a key is pressed. Then it checks for the pressed row number to
know the exact position of the key. The first row is set low and columns are checked if they go low or
not. then the second row is set to row and columns are checked. this process is repeated until a low
column is detected. Once the column goes low, that respective row is saved in rowloc. then colloc and
rowloc are compared with the value given in the array at that position. In the main() function, this
number is set to at least a significant bit while the previous number is shifted to the most significant bit
by shifting four bits to the left. this number is then sent at port D to display the out-at-seven-segment
display.
2. Remove _ms_delay(2) lines is the code when checking for Keypad’s row. Implement the code in
proteus and also on the trainer. Explain the difference between the result?
The code uses a 2ms delay in the loop that searches for the keyboard row. This delay debounces the key
and ensures that the correct key is recognized.
If you remove the 2ms delay, your code will not be able to debounce the key correctly and the
microcontroller will see multiple keypresses for his single key press. As a result, Trainer and Proteus
behave incorrectly in detecting buttons and displaying incorrect values.
Therefore, it's important to maintain delays in your code to ensure that keys are recognized correctly and
to avoid recognizing multiple keys with a single key press. Debounce time is the time required for an
electrical signal to stabilize after being pressed Key. Electrical signals can fluctuate without debounce
time and can be multiple reads from the same key press. If you remove the debounce time, the program
will Register multiple keypresses for a single physical keypress, which can lead to errors and inaccuracies
result. It is important to note that eliminating the debounce time also improves the measurements.
They are susceptible to electrical noise and interference, which can lead to even more bugs. To
Minimal debounce time is generally recommended to ensure accurate and reliable keyboard reading
few milliseconds.
3. Study 74C923 datasheet and write code in AVR studio, that take user input and display on 7
segments using 74C923 IC. Implement the complete system in proteus.
#include <avr/io.h>
DDRA = 0x00;
DDRD = 0xFF;
while(1)
PORTD=PINA;
}
The function of this code is to read a value from port A and output it to port D. preprocessor directive
#define F CPU 8000000UL sets the microcontroller clock frequency to 8 MHz. This line #include has
AVR input/output (I/O) header file that lists the microcontroller's I/O registers. These two lines set the
DDR for ports A and D: DDRA = 0x00; DDRD = 0xFF. Set all pins of port A as input pins
Set DDRA=0x00 to set all pins of port D as output pins, and set DDRD=0xFF. While(1) starts endless
A loop that repeatedly executes the code in the loop. PORTD=PINA; This line reads the value of port A and
Port D values are the same. Port A values are read using PINA registers and port D values are written using PINA
registers. PORTD register. Matrix-style keyboard and 7-segment LED display Programmable keyboard and
display interface IC known as 74C923. The keyboard has 16 input lines,
The display has four output lines.
4. What is debouncing in the mechanical switch and why is it necessary to remove it when
interfacing with the microcontroller?
Debouncing is a phenomenon where mechanical switches can
transmit multiple electrical impulses to the microcontroller when pressed or released. This can result in
unexpected behavior or programming problems, as a single button click can be interpreted as multiple
presses. To eliminate debouncing, a circuit or software procedure is required to filter out the various
signals and ensure that only one signal is given to the microcontroller for each button press. This is
done by adding a debounce circuit or a software debounce algorithm to the microcontroller application,
which uses filtering techniques or adds a delay to ensure that only the stable signal is recognized and
handled by the microcontroller.