You are on page 1of 11

Microprocessor and Interfacing (ELE-301)

LABORATORY MANUAL
ELE-301 MICROPROCESSOR AND INTERFACING

STUDENT NAME

STUDENT ID

BATCH

LAB INSTRUCTOR:
Engr. Muhammad Aamir

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

Lab Rubrics
Assessment Criteria for Lab
Outcomes Assessment:

Lab Rubrics (Hardware based)

Attributes Exceeds Expectation Meets Expectation Does not meet Marks


(3) (2) expectation (1)
1. Coding The program was The program was The program was
(Cognitive C4) exceptionally well readable only by not able to be
(CLO 4) organized and very easy someone with complied and was
to follow. experience in coding. difficult to
The code fulfilled all the The program fulfilled understand.
specification of the the specification of
problem. the problem.
The program compiles The program
with out error. compiled with minor
Program does not error such as missing
contain unnecessary syntax.
repetition Program does
contain unnecessary
repetition
2. Analysis using Accurately calculate the Calculate the output Did not perform a
Computer output by applying or required mathematical
simulation theoretical knowledge parameter but with calculation or
(Cognitive C4) gained through lecture some minor computer
(CLO 4) and demonstration. mistakes. simulation.
Used Proteus to Used proteus to
validate it with validate it with the
theoretical values. theoretical values.
Correlates the Correlate the
experimental results experimental result
with both methods and with theoretical
comments on any error values but did
or differences between comment on any
them difference or error
3. Interfacing/ Able to select the Required minor Unable to select the
implementation correct tools and guidance in selecting appropriate tools or
in hardware equipment, check the the right tools/types equipment to
(Psychomotor- calibration of the of equipment, conduct the
P3) equipment and connect calibration of the experiment and
(CLO 5) the different equipment, or wiring required guidance
components of the different components throughout the
trainer module without of the trainer experiment.
any guidance to module.
perform the experiment
4. Lab Report/ Student submitted a Student submitted a Student submitted a
Presentation genuinely original genuinely original genuinely original
(Affective- A3) manual without any manual, however, did manual but did not
(CLO-6) grammatical error or not addressed all the adequately address
formatting error and questions sufficiently. the questions.

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

lastly it is not Have minor Have many


plagiarized. grammatical errors or grammatical and
formatting errors. formatting errors.
Able to explain Entire Lastly, it is not lastly, it is not
program design plagiarized. plagiarized
correctly as it is
Materials are highly Able to explain some Unable to explain
organized with required part of the program the program design
information design. Material are not
Demonstrate Materials are organized properly
cooperation through all partially organized and missing
the group. with required information
information Zero cooperation
Demonstrate between the group
cooperation after member
intervention
CLO4/PLO5 CLO4/PLO5 Total Marks
A + B+C
Marks Obtained = _____ out of 6 A) Marks Obtained = ______ x 0.5 /6=
CLO5/PLO3 CLO5/PLO3

Marks Obtained = _____ out of 3 B) Marks Obtained = ______ x 0.25 /3=


CLO6/PLO10 CLO6/PLO10

Marks Obtained = _____ out of 3 B) Marks Obtained = ______ x 0.25 /3=


*Plagiarized manual will mark ZERO.

Lab Instructor Signature: ___________________

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

2 LAB EXPERIMENT

OBJECT:
Interfacing keypad and joystick with microcontroller.

SOFTWARE & HARDWARE


1. AVR Studio 7
2. Microcontroller Trainer

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

Figure 1 Switches arrangement for 4x4 matrix Keypad

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

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

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).

Figure 2 Normal Mode operation. Switch S9 is pressed

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.

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

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.

4x3 and 4x4 Keypad with Pinout

Figure 3 4X3 keypad (left) and 4X4 keypad (right). Where R and C represent the rows and columns respectively

SCHEMATIC DIAGRAM

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

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>

#define KEY_PRT PORTA


#define KEY_DDR DDRA
#define KEY_PIN PINA

unsigned char keypad[4][4] = { {0x07,0x08,0x09, 0x0d},


{0x04,0x05,0x06,0x0c},
{0x01,0x02,0x03,0x0e},
{0x0A,0x00,0x0B,0x1c}};

unsigned char colloc, rowloc;

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 */

_delay_ms (20); /* 20 ms key debounce


time */
colloc = (KEY_PIN & 0x0F);
}
while(colloc == 0x0F);
{

/* now check for rows */


KEY_PRT = 0xEF; /* check for pressed key in 1st row
*/
_delay_ms(2);
colloc = (KEY_PIN & 0x0F);
if(colloc != 0x0F)
{
rowloc = 0;

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

break;
}

KEY_PRT = 0xDF; /* check for pressed key in 2nd row


*/
_delay_ms(2);
colloc = (KEY_PIN & 0x0F);
if(colloc != 0x0F)
{
rowloc = 1;
break;
}

KEY_PRT = 0xBF; /* check for pressed key in 3rd row


*/
_delay_ms(2);
colloc = (KEY_PIN & 0x0F);
if(colloc != 0x0F)
{
rowloc = 2;
break;
}

KEY_PRT = 0x7F; /* check for pressed key in 4th row


*/
_delay_ms(2);
colloc = (KEY_PIN & 0x0F);
if(colloc != 0x0F)
{
rowloc = 3;
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

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

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.

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

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.

#define F_CPU 8000000UL

#include <avr/io.h>

int main (void)

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

Department of Biomedical Engineering-SHU


Microprocessor and Interfacing (ELE-301)

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.

DISCUSSION AND CONCLUSION:


In this lab, we learned how to connect a microcontroller to a keypad. How to program a device that
will receive a user-inputted number and show it on 7-segment LEDs. The ATMEGA 32 IC's PORTA will
be used to connect the 4x4 keypad, which has 4 rows and 4 columns, and PORTD will be used to
show the output number in BCD format. The 74LS47, a BCD to 7-segment decoder, is attached to the
PORTD, which is subsequently connected to two common anodes 7-segment displays.

Department of Biomedical Engineering-SHU

You might also like