Nothing Special   »   [go: up one dir, main page]

Embedded System Lab Manual

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 67

MICROPROCESSOR BASED EMBEDDED

SYSTEM

ECE3637
LAB MANUAL
Revised Date
April, 2015

DEPARTMENT OF ELECTRICAL ENGINEERING


THE UNIVERSITY OF LAHORE

Revision History
Revised
Date

April 23,2015

Revised By

Dr. Ghulam Abbas


Engr. Shahzad Ahmed

January 20,
2013

DR. Musharraf Ahmed


Hanif
Engr. Shahzad Ahmed

Revised Topics
Lab.01 Using KEIL
Lab.02 Software Delay
Lab.03 8051 IO Ports
Lab.04 Switch De-bounce
Lab.05 Soft Timer
Lab.06 8051 Timers
Lab.07 PWM generation
Lab.08 Interfacing a DC motor
Lab.09 8051 External Interrupt
Lab.10 Serial Communication
Lab.11 8051 Timers and Serial Interrupts
Lab.12 Switch De-bounce using FSM
Lab.13 LCD interfacing
Lab.14 Keypad matrix
Lab.15 Interfacing ADC
Lab.16 Stepper Motor Interfacing
Lab.01 Setup development for STM32
Lab.02 Bare bone project for STM 32
Lab.03 Configure a GPIO pin as output
Lab.04 Create a software delay
Lab.05 Configure a GPIO pin as input
using debugger
Lab.06 De-bounce a witch input and
count its state changes
Lab.07 implement a time cyclic
executive using Timer
Lab.08 Start development and debugging
of course project
Lab.09 Implementing FSM
Lab.10 implement a multistate machine
Lab.11 Implement a multistate cyclic
executive
Lab.12 Using LCD library
Lab.13 Using USART library
Lab.14 Keypad matrix
Lab.15 ADC
Lab.16 Demonstration and Evaluation of
Course project.
2

January 20,
2012

Hassan Bin Ahmed

Lab.01 8051 assembler and simulator


Lab.02 Examining the CY flag and the
stack
Lab.03 Simulating I/O ports
Lab.04 Data transfer
Lab.05 Arithmetic operations-1
Lab.06 Arithmetic operations-11
Lab.07 ASCII and BCD conversion
Lab.08 Testing 8051 I/O ports
Lab.09 Timer programming
Lab.10 Event counter programming
Lab.11 8051 serial port interfacing
Lab.12 Interfacing an LCD to 8051
Lab.13 Interfacing key pad matrix
Lab.14 interfacing a sensor
Lab.15 Interfacing a stepper motor
Lab.16 8051 programming in C

C E RTIF IC ATE OF A PP ROVA L


It is certified that the lab manual titled Microprocessor based embedded
system, in scope and in quality, covers the objectives and topics defined in
the course outline.

Remarks by Mentor:

---------------------------------------------Dr. Ghulam Abbas


Assistant Professor
Department of Electrical Engineering
The University of Lahore

Remarks by HOD:

------------------------------------------------Dr. Asrar ul Haq Sheikh


Professor
Department of Electrical Engineering
The University of Lahore

TABLE OF CONTENTS

S.
No

TOPICS

PAGE
NO.

Using KEIL

Software Delay

10

8051 IO Ports

14

Switch De-bounce

19

Soft Timer

23

8051 Timers

26

PWM generation

51

Interfacing a DC motor to 8051

55

8051 External Interrupt

59

8
9

10 Serial Communication

42

11 8051 Timers and Serial Interrupts

45

12 Implementing Switch De-bounce using FSM

48

13 LCD interfacing

51

14 Keypad matrix

55

15 Interfacing ADC

59

16 Stepper Motor Interfacing

63

Lab. 01
Experiment title:
Using KEIL to write an 8051 Assembly language program and burning its hex file to the
controller.
5

Introduction and Theory:


Keil is basically an Integrated development environment (IDE) for developing embedded
projects supporting a wide range of microcontrollers based on ARM, Cortex-M and Cortex-R.
The IDE integrates the source editor, compiler, assembler, linker, debugger and a very strong
simulator all in one tool. It is easy to use.
GeniusPro540/840 is used to burn, program and erase the ROM of a wide range of
microcontrollers.

Pre Lab Preparation:


Download and install KEILl Vision5 from www.keil.com

Equipment:

Personal computer (Hardware).


AT89S52 (Hardware)
G540 / G840 programmer. (Hardware)
KEIL Vision5/4 installed. (Software)
GeniusPro540 / GeniusPro840

Procedure:
Run the KEIL software.
Click on project from the menu and then select new uVision project.
Name the project toggle and save it in a new folder with the name My First Project.
Copy a startup file.asm for the 8051 in My First Project if not copied automatically.
A window will open to select the target device. Select ATMEL and then AT89S52 and
click on ok.
Select new from the menu to create a source file.
A text window will open to write the code.
Write the code given below in asssembly language to toggle ports 1 & 2 after a delay.

Save the source file with the extension .a51 or.asm.


Add this file to your project.
Go to target options and change the Xtal to 11.0592 MHz from 33MHz.
Go to Output tab and click on Hex file.
Build and rebuild the project for zero errors and warnings.
Select debugger. Start the debug session and run it.
Select peripherals. Select port 1 &2 for simulation results.
Plug the 8051 controller to the Genius programmer and make a successful connection.
A window showing the memory with addresses and its contents is displayed.
Load the hex file of your project.
Program the 8051 which is now ready to use.

Observations and Results:


Once the program is assembled, go to the project folder count and enlist the number of
new files with their extensions.
Run the debugger & simulator to see the results.
Analyze the memory of 8051 before and after erasing the chip
Ports 1 & 2 will toggle with alternate bit pattern after a delay.

Learning outcomes:
The students must be able to develop a project written in an assembly language.
Get to know how to use the source editor, compiler/debugger, simulator in an integrated
development environment and creating the hex file.
Learn to get the hex file burnt to the microcontroller using a universal programmer.
7

Questions:
What is an IDE? What is the name and version of the IDE used in this lab?
What is an assembler? What are its input and output files? Which assembler is used in
this lab?
What is a linker? What are its input and output files? Which linker is used in this lab?

What is a simulator? Which simulator is used in this lab?


Open the folder My First Project and find out how many files are contained in it after
completing all the steps?
Write down the names of the files in folder My First Project that contain software code
in them for the target device.
What is the name and extension of uVision project file?
What is the purpose of startup (.s) file?
What is the purpose of linker script (.lst) file?
What is the purpose of G840 / G540?

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

4 = Very good

5=

Questions

Rating

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any)

Lab. 02
Experiment title:
Implementing a software delay
9

Introduction and Theory:


All I/O ports in 8051 by default when powered up are pulled up internally except for Port 0
which is pulled up externally. All I/O ports are by default inputs but once zero written to a port it
is an output. To use it as an input reprogram it by writing 0 to it. A good practice is to write a 1
for an input and a zero for an output. Three of the ports are multiplexed. Port 3 is having serial
com, external interrupts and timers. Two of the ports are used for addressing mode for accessing
external memory. For C language programming a C Kiel compiler is required. (GCC Sourcery)

Pre Lab Preparation:


Study the I/O ports programming in C from the book (8051 by M. Ali Mazidi). Study the pin
configuration of the 8051 trainer.

Equipment:

8051 trainer available in the lab


Universal programmer
DMM
Computer
KEIL uVision4
G540

Procedure:
To test the operation of the ports of your 8051 Trainer in the lab you
can download the C program for Toggling LEDs on P2 given below:

10

For you Compiler, make sure the hex option in the Link is checked so the hex
file for the above program is produced.

The source file will be saved with .c extension.


Now, follow the steps given in Lab 1 to download the hex file into the
8051 Trainer.
The above test program toggles the P2 of the 8051. Use a logic probe
or the LEDs of the 8051 Trainer to watch the bits of the ports toggle on
and off. Change the time delay in between the "on" and "off" states
but make sure the time delay is long enough that you can observe the
LED going on and off.

Observations and Results:


To create a loop of 10,000 iterations we need data type of (unsigned
char, unsigned int).
To create a loop of 200 iterations we need data type of (unsigned char,
unsigned int).
In the toggle program reduce the size of the delay to point in which the LEDs looks to be on
all the time (no longer toggling). How many millisecond is that?

Learning outcomes:
To toggle and test the 8051 Trainer and its ports using C program

11

Questions:

What is the opposite of 0x55?


What is the opposite of 0xAA?
List all ports of 8051
In this Lab, which ports were tested?
Give the pins of P3 used for TxD and RxD.
Which ports of the 8051 have internal pull-up resistors?
Which ports of the 8051 require the connection of external pull-up
resistors in order to be used for I/O?

Each student is required to perform the lab and submit the following
Performa along with the lab report.

12

1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

13

Lab. 03
Experiment title:
Programming the I/O ports

a) Port mirror
b) Implementing sliding LEDs

Introduction and Theory:


All ports of 8051 are pulled up internally except the port 0 which is pulled up externally.
All ports are input by default when powered up. When first 0 is written to a port it becomes an
output. In order to use it as an input it must be reprogrammed by writing 1 to it. All these ports
are bit accessible and byte accessible.

Pre Lab Preparation:


Students must perform lab 2 before performing this lab.

Equipment:
KEIL simulator
8051 trainer/AT 89s52

Pin configuration of AT 89s52 is given below:


14

Procedure:
In this Lab we test the 8051's port for input operation.
Download the C program for Sending P1 Switch Data to P2 LED given
below.

Then, compile, download and run the program.


15

This program gets data from P1 and sends it to P2.


Implementing sliding LEDs
Implement the following code to implement sliding led on P1.

Observations and Results:


Any change of status of the switches connected to P1 will be instantly
reflected on LEDs connected to P2.
Write and run a program to get data from P1 and after adding a fixed
value of 5 send it to P2. Set the switches and examine the LEDs and
verify the result.
Use sbit keyword to access bit 5 of port 1 and make it an input and bit
2 of port 2 connected to an led.

Learning outcomes:
Configuring ports as inputs and outputs

Questions:

On the 8051 Trainer, which port is connected to the DIP switches?


On the Trainer, which port is connected to the LED 7segment?
In this Lab, which port of the 8051 Trainer did we use for outputting?
In the 8051, explain why we must write "1" to a port in order for it to
be used for input.
16

The Maximum value we can input from P! DIP switches is (in


binary), .hex, and .decimal.
Upon reset, all the ports of the 8051 are configured as ______ (input,
output).
To make all the bits of P1 an input port we must write ____ hex to
register______.
Write a simple short program to get a byte of data from P1 and after
incrementing it once sends it to P2.

Assignment:
Write a code such that P1.0 is an input connected to a switch and P1.7 is an output
connected to a led and P2.0 is connected to the oscilloscope
a) If switch = 0 then a square wave of 1Hz IS generated at P2.7 and the led blinks
with the same frequency.
b) If switch = 1 then a square wave of 2Hz IS generated at P2.7 and the led blinks with the same
frequency.

Write a code to continuously monitor the input pin connected to TTL


mode square wave generator and send it to the output pin connected
to an LED.

17

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):
18

Lab. 04
Experiment title:
Implementing an input counter using switch de-bounce

Introduction and Theory:


In many systems we need input counters to keep track of an action. Switch
debounce tales place between 100ms to 500ms on both at press as well on release.
Due to the phenomena of switch debounce has to be handled in software such that
a single push results in a single input.

Pre Lab Preparation:


Lab-3 and lab-4 must be performed before implementing this lab. A delay can be generated by
writing a software code. For example by implementing loops. A grip on the structures of loops in
C is important.

Equipment:
KEIL uVision4/5 compiler
8051 trainer/AT 89s52
Universal programmer G540/G840

Procedure:
Write and test a C program to count up in binary on your 8051 Trainer.
The counter should start counting from 0 and go up to 255 after a
delay of 1 second.

Change the time delay in between the counts. Make sure the time
delay is long enough that you can observe the LED counting up.
In the above step,the maximum count was FFH(or 255). Modify the
above program to set maximum count to 10 (00001010).
Change the maximum count to the value of your age and observe the
LED counting up to that number.
19

The value of the counter is sent to port 1.


Now modify the above code such that P2.0 is the input connected to
the push button. Each time when the push button connected to P2.0 is
pressed the counter increments and is sent to P1.
Also test your program to verify the counter variable overflow. You will
have to write
Use the code for switch de bouncing to implement a push button.

Test your program on the Keil simulator as well as 8051 trainer.

Observations and Results:


Verify the above scenario using KEIL simulator.

Learning outcomes:
Be able to write a software delay to implement a counter.
Developing a portable system.
.

20

Questions:
If variable is 8 bit in length used in a counter, what would be the value in the counter
after 256 iterations if it started from 0.

21

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?

22

8. Comment whether your work in the lab has an


impact on the society.
Additional Comments (if any):

Lab. 05
Experiment title:
Implementing a soft timer on 8051 trainer using C

Introduction and Theory:


In many systems such as microwave oven we use timers to keep track of an action.

Pre Lab Preparation:


Lab-3 and lab-4 must be performed before implementing this lab. A delay can be generated by
writing a software code. For example by implementing loops. A grip on the structures of loops in
C is important.

Equipment:
Keil uVision4/5 compiler
8051 trainer
Universal programmer

Procedure:
Write a C program to implement a timer which can down count using
8051 trainer.
It should show the binary count on the LEDs connected to one of the
ports for example P2.
A push button is connected to P1.0.
P0 will be configured as an input.
The dip switched connected to this P0 will be used to load the timer maximum value.
Switch de-bounce will be implemented for P1.0.
Write a C program using KEIL such that the timer maximum value is loaded via the dip
switches connected to P0.
When the push button connected to P1.0 is pressed and released the timer starts down
counting from the maximum value loaded by the dip switches to zero.
This down counting binary pattern must be shown on led connected to P2.
Write a delay function such that there is a delay of exactly one second between the
counts.
23

When the down count reaches zero an LED connected to P1.1 is turned on.
One more feature is that whenever the push button connected to P1.0 is pressed and
released during the down counting the timer stops and when it is again pushed the timer
resumes. (optional)

Observations and Results:


Verify the above scenario using KEIL simulator.

Learning outcomes:
Be able to write a software delay to implement a timer.
Developing a portable system.
.

Questions:
How can we implement an infinite loop using for loop?
How can we time out an infinite loop?

24

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
25

8. Comment whether your work in the lab has an


impact on the society.
Additional Comments (if any):

Lab. 06
Experiment title:
Timer programming
a) Generating a Hardware delay using an 8051 timer
b) Implementing a pulse counter using an 8051 timer

Introduction and Theory:


Basic Timer registers:
Timer 0 register (16 bit)
Timer 1 register (16 bit)
TMOD register (8 bits, used to select different timer modes)

Pre Lab Preparation:


Study of 8051 timer and its registers from the book

Equipment:

8051 Trainer
8051 KEIL Compiler
Oscilloscope
Speaker (optional)
74LS04 Inverter (optional)

Universal programmer

Procedure:
Hardware delay:
One can use the 8051 Timer to generate time delay to toggle a bit
using the following program:

26

Compile and run it on the 8051 trainer to make sure it works.


Write a program using a 8051 Timer to generate a 500 Hz square wave
frequency on pin P2.7 Then examine the frequency using the
oscilloscope. Modify the count value to make sure that the frequency
is exactly 500 Hz.
Write a program using a Timer to generate a 1 kHz square wave
frequency on pin of P2.0 Then examine the frequency using the
oscilloscope. Modify the count value to make sure that the frequency is
exactly 1 kHz.

Pulse counter:
Connect the square wave from function generator to pin P3.4 for Timer
0 and set the frequency to 1 Hz.
Write and run a program for counter/timer 0 to count up from 00 to a
maximum of 255 (FFH) as each pulse is fed into the 8051.

27

The count is displayed in binary on the 8 LEDS of 8051 Trainer.


Notice that the binary numbers displayed for the count goes from 00
to FF (00000000- 11111111 in binary or 00 -255 in decimal).

Observations and Results:


Examine the frequency in all steps using oscilloscope. Modify the count value to achieve the
exact value.
Simulate the above steps to see the 8051 timer register values on run time using Keil
simulator.
Use KEIL simulator to simulate all the above steps to analyze the status of the timer registers
working in counter mode.

Learning outcomes:
To program the 8051 timer in different modes.
Learn to use 8051 timers as pulse counters.

Questions:

What is the function of the TMOD register


Is TMOD a bit addressable register?
What is the difference in the timer lengths in modes 0,1 and 2?
In which register do we find the timer start bits and timer rollover bits.
Explain the role of the C/T bit in the TMOD register.
How is the 8051 used as an event counter to count an external event?
28

If timer/counter 0 is used as an event counter, what is the maximum


count for the following modes?
a)
Mode 1
b) Mode 2
Indicate which pin is used for the following.

(a) timer/counter 0

(b) timer/counter 1
If timer/counter 0 is used in mode 1 to count an external event, explain
when TF0 is set to high.
If timer/counter 1 is used in mode 2 to count an external event, explain
when TF0 is set to high.

29

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):
30

Lab. 07
Experiment title:
PWM generation
a) Using software delay
b) Using 8051 timers

Introduction and Theory:


Square wave of a specified frequency is used as a clock in computer systems. PWM
signal is used in modulation. PWM signal of required duty cycle can be generated
using I/O ports of 8051 microcontroller. PWM signals are used in communication
systems. It is also used in photovoltaic battery chargers.

Pre Lab Preparation:


Discuss C data types for 8051

Equipment:

KEIL uVision5 compiler


GeniusPro540 / 840
8051 trainer
Universal programmer
Oscilloscope

Procedure:
a) Using software delay:
Write a C program to generate a square wave frequency on a pin of P2.

31

Now examine the frequency using the oscilloscope. Modify the count
value to make it exactly 200Hz frequency.
Modify the above program to make Variable frequencies.
Modify the above program for different duty cycles. (25%, 75%)
Connect the 8 DIP switches on the trainer to P1. Then use the Switches
to change the frequency and examine the Freq. oscilloscope.
If you have access to a Buzzer, connect it to pin P2.7 with a 74LS04
Inverter as a driver and see how the buzzer creates different sounds.
You can also connect this pin to an Led on the trainer through the dip
switch.

b) Using 8051 timers


The following code is given to generate PWM using 8051 timers

32

Observations and Results:


Verify the above scenario using KEIL simulator.

Learning outcomes:
To be able to generate PWM signals of any duty cycle using software delays in 8051.
.

Questions:
Can this PWM signal be used to control the speed of a dc motor?
What is the maximum source sink current of an 8051 port pin.
Can we generate quadrature encoded signal in 8051?

Each student is required to perform the lab and submit the following
Performa along with the lab report.
33

1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

34

Lab. 08
Experiment title:
Interfacing a DC motor to 8051

Introduction and Theory:


Speed of dc motors can be controlled by applying PWM signals of different duty cycles
generated by 8051.PWM is applied to the dc motor through an H bridge. The motor can be
directed to move forward, backwards, neutral e.t.c.

Pre Lab Preparation:


Build a motor driver circuit using LM 298 and test it on a dc motor.

Equipment:

KEIL compiler
Motor driver circuit
Dc motor
Oscilloscope
8051 trainer
Universal programmer
G540

Procedure:
Using your 8051 trainer, build the circuit shown in Figure 17-6 (or 1718 in older edition) of the textbook.

35

Truth table:

Then write and run a program to rotate it clockwise continuously.


After making sure that above steps work, write and run the following:
Connect a buffered switch to P2.1 and use it for clockwise or counter

clockwise. Use
P1.1=0 for CW and P1.1=1 for CCW. If you have set up serial 8051
communication, use
x86 PC to send letters R and L for clockwise and counter clockwise,
respectively.
After making sure that Activity 1 works, write and run the following:
Use P1.7 and P1.6 to choose the speed of rotation.

Observations and Results:


Already mentioned above

Learning outcomes:
To interface a DC motor to the 8051.
To write a program to control the speed and direction of DC motor rotation by the user.
36

Questions:
What is a use of Optoisolator ?
True or false. The DC motor speed is proportional to width of square wave applied to the
DC.
Give the RPM for DC motor used in this Lab
What is the disadvantage of using CPL instruction for turning on and
off the DC motor?
Explain the PWM

37

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?

38

8. Comment whether your work in the lab has an


impact on the society.
Additional Comments (if any):

Lab. 09
Experiment title:
8051 interrupts programming
Programming the external hardware interrupts

Introduction and Theory:


If an interrupt source is triggered the CPU stops its work and run the interrupt service routine
after which it again resumes with its task. If there are more then one interrupt sources CPU
handles them according to the priority assigned. If too many interrupt sources are triggered DMA
may be involved.

Pre Lab Preparation:


Study 8051 interrupts from the book.

Equipment:
8051 trainer
8051 KEIL Compiler
Square wave generator

Procedure:
Write an 8051 program to get data from a single bit of P1.0 (connected
to DIP SW) and send it to P2.7 (connected to LED) continuously while
an interrupt can do the following:
Connect the INT0 (or INT1) pin to a normally high debounce SW and
any time the switch is pressed, a single LED is turned on.
39

Write an 8051 program to get data from a single bit of P1.0 (connected
to DIP SW) and send it to P2.7 (connected to LED) continuously while
an interrupt will do the following:
A square wave is connected to the INT0 (pin 3.2) and any time a H-toL pulse comes in a single LED is turned on. The rate of "On" and "Off" is
the rate of the square wave. In this activity you can also use INT1.

Observations and Results:


Make all observations using an oscilloscope.
Use KEIL simulator to see the results.

Learning outcomes:
Learn to use 8051 external interrupt sources.
Learn to use 8051 timer interrupt sources.

Questions:
Name all of the hardware interrupts in the 8051 and their vector table
addresses.
On reset, INT0 (and INT1) are _________ (edge, level) triggered.
On reset, which interrupt has the highest priority?
True or False. There is only a single interrupt for the serial data
transfer.

40

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

41

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

Lab. 10
Experiment title:
a) Displaying a message using serial com port on hyper terminal
b) Receiving bytes of data at the serial com and sending to the port

Introduction and Theory:


In serial communication data is sent bit by bit. It has wide applications in data communications.
8051 has an on chip serial com port. Serial communication between the 8051 and some other
hardware take place under some protocol.

Pre Lab Preparation:


Study and learn to use serial com registers in 8051.
Study and analyze the code given below to use the serial com:

42

Equipment:

8051 KEIL Compiler.


8051 Trainer.
Computer
Universal programmer.

Procedure:
Transmitting data:

Write a program to send two different strings to the serial port.


Assuming that SW is connected to pin P2.0, monitor its status and
make a decision as follows:
a) SW = 0: send your first name at 4800 baud rate to hyper terminal.
b) SW = 1: send your last name at 9600 baud rate to hyper terminal.
Assume XTAL 11.0592MHz, 8bits, 1Stop bit.
Receiving data:

Write a program to receive bytes of data serially via the serial port and
put them in P1. Set the Baud rate at 9600, 8bit data, and 1 stop bit.
Use timer 1 for baud rate generation.

Observations and Results:


Message is successfully displayed on the hyper terminal. Serial Communication between the
computer and 8051 is not possible without MAX232 in between. Analyze the voltage standards
used by PC and 8051 for communication.

Learning outcomes:
Learn to use serial com in 8051.

Questions:
The 8051 TxD and RxD signals ________ (are, are not) TTL-compatible.
In this lab, what is the role of the MAX233 (MAX232) chip?
With XTAL=11.0592 MHz, what is the maximum baud rate for the
8051?
Show how to achieve the maximum baud rate in Question 3.
What is the role of TI and RI?
True or false. The 8051 can transfer data in full-duplex.
For full duplex, what are the absolute minimum signals needed
between the 8051 Trainer and the x86 PC? Give their names.
43

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?
44

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

Lab. 11
Experiment title:
8051 timer and serial com port interrupt programming

Introduction and Theory:


Study timer interrupts from the book.

Pre Lab Preparation:


Study the register configuration for serial interrupt programming from the book.

Equipment:
8051 trainer
8051 KEIL Compiler

Procedure:
Write an 8051 program to get data from a single bit of P1.0 (connected
to DIP SW) and send it to P2.7 (connected to LED) continuously while
an interrupt will do the following: One of the timers will toggle the PX.Y
bit every 100 microseconds.

Write an 8051 program to get data from a single bit of P1.0 (connected
to DIP SW) and send it to P2.7 (connected to LED) continuously while
an interrupt will do the following: A serial interrupt service routine will
receive data from a PC and display it on one of the ports. (Either
connect LEDs to this port to examine the data coming in or display the
received data on an LCD).

45

Observations and Results:


Verify all steps by using Keil simulator.

Learning outcomes:
To be able to use serial interrupts in 8051

Questions:
Name all the Timer interrupts in the 8051 and their vector table
addresses.
In timer mode 1, indicate when TF0 causes the interrupt.
In timer mode 2, indicate when TF0 causes the interrupt.
True or False. There is only a single interrupt for the both Timer 0 and
1.

46

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
47

6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

Lab. 12
Experiment title:
Implementing switch de-bounce using Finite State Machine

Introduction and Theory:


Upon press or release, all mechanical switch contacts bounce

Bouncing on
press
pressed
The actual switch press has four possible
states
If switch

If switch not
pressed
after
The desired output of the switch status variable should have two states (after
debouncing)
after debounce
debounce period
Switch
period
pressed

Pressed state

If switch pressed
after debounce
period

Switch
not
pressed

Bouncing on
release

Not pressed
state

If switch 48
not
pressed after
debounce period

Pre Lab Preparation:


Study the phenomena of switch debounce and periodic tasking from PONT C. Study FSM from
Morris Mano. Design a finite state machine for the switch debounce by implementing a periodic
task.

Equipment:
KEIL compiler
8051 trainer

Procedure:
Create a new project Lab12 in folder Lab12.
Complete the main.c file yourself. Inside the main() function write the code such that if
button is pressed then LED flashes at 1 Hz, and if it is not pressed then LED flashes at 2 Hz.

Build the project and download Lab12.hex file on 8051.


Rewrite your program such that the de-bouncing feature is added into the button read
function.

Implement a periodic task for reading button input as a state machine using switch-case
construct. Do not use for() or while() loops inside tasks to avoid blocking code.

Observations and Results:


Already defined above

Learning outcomes:
To design a finite state machine.
49

Questions:
What are the three states of a push button?
Write three benefits of Timed Cyclic Executive software architecture over a simple
super-loop?
What are hazards of using for() loop or while() loop inside a periodic task()?

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?

50

6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

Lab. 13
Experiment title:
LCD interfacing with 8051

Introduction and Theory:


A driver is to be written for an LCD as it is run by its own controller usually 8 pins for latching
data, 2 for power supply, 4 pins for control, register selection and enable, 2 pins for contrast e.t.c

Pre Lab Preparation:


Study the data sheet for 2x16 LCD driven by Hitachi controller.

51

52

Equipment:
KEIL compiler
LCD (2X16)
8051 trainer

Procedure:
Connect the LCD to your 8051 trainer as shown in section 12.1 of the
textbook.

53

Then compile and run the LCD driver program from the book or as
provided by the instructor.
Now, modify the above program to display you first name and last
name, all on the same line
Repeat Activity 1 while also putting the year you graduated from high
school on the second line. When you run your program, the LCD should
show (for example):
Khurram Ahmed
Graduated in 2012
Write an LCD program to display your last name on the first line and
the current year on the second line. Both should be in the middle of the
line.

Observations and Results:


Already defined above

Learning outcomes:
To understand the operation modes of an LCD.
To interface and program an LCD.

Questions:
How does the LCD distinguish data from instruction codes when
receiving information at its data pin?
To send the instruction command code 01 to clear the display, we
must make RS = ___.
To send letter 'A' to be displayed on the LCD, we must make RS =
____.
What is the purpose of the E line? Is it an input or an output as far as
the LCD is concerned?
When is the information (command code or data) on the LCD pin
latched into the LCD?
Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

Questions

3= Satisfactory

4 = Very good

Rating

5=

Remarks
54

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

Lab. 14
Experiment title:
Interfacing a keypad to an 8051

Introduction and Theory:


Study the NxM matrix interfacing with 8051 and its driver from the book.
55

Pre Lab Preparation:


The first step is to make a truth table for the keyboard. This truth table
provides the row and column contacts by which a key is produced. Connect the
ohmmeter leads, one to a row and one to a column terminal (lead) of the keyboard,
and press the keys one at a time until you measure zero ohms. Repeat the process
until all the keys are mapped.

Equipment:

8051 trainer
8051 Keil Compiler
4x4 keyboard or any N x M matrix keyboard
8 of 6.8K ohms resistors

Procedure:
After you have mapped your keypad set, connect the keypad to the
8051 as shown in Chapter 12 of the textbook.

Write and run a program that scans your keyboard and displays any
character pressed by the user on the LCD.

56

Your program must display keys 0 - 9 as numbers 0 - 9 on the x86 PC


screen while keys 10, 11, 12, 13, 14, and 15 (or you might say 0A - 0F
in hex) are displayed as letters A, B, C, D, E, and F, respectively. You
can modify and incorporate Program 12-1 of the textbook for your
program.
Repeat Activity 2 for 4x4 keypad and use only a single port for both
rows and columns.

Observations and Results:


Already done above

Learning outcomes:
To interface a 4x4 keyboard (keypad) to the 8051 trainer.

Questions:
What is the purpose of generating the truth table for a given
keyboard?
57

What is the purpose of grounding each row in keyboard interfacing?


What is the input to the microcontroller from column if no key is
pressed?
True or false. In our N x M matrix keypad program we cannot press two
keys at the same time.
In your program in Activity 2, how is the key press detected?
In your program in Activity 2, how is a key press identified?
Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

58

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

Lab. 15
Experiment title:
Interfacing ADC804 to 8051

Introduction and Theory:


In many applications we need to have multiple analog-to-digital converters. The
ADC0804 IC is an 8-bit parallel ADC in the family of the ADC0800 series from
National semiconductor devices. It works with +5volts and has a resolution of 8 bits.
In the ADC0804, the conversion time varies depending on the clocking signals
applied to the CLK IN pin, but it cannot be faster than 110usec.

Pre Lab Preparation:


Students will build and test the interfacing circuit of ADC0804 with AT 89s52 before coming to
lab. Connect the ADC804 to the 8051 Trainer. For the clock source of the ADC804,
you can either use an external clock source (square wave TTL), which is preferable,
or use the XTAL frequency from the 8051 XTAL as shown in Chapter 13. Make sure
that you use at least three or four D flip-flops to bring down the frequency. Notice
the following points when interfacing the 8051 Trainer to the ADC804.

a) The ADC804 clock frequency is approximately 640 kHz at 5V.


b) Make CS =0 and send a low-to-high pulse to pin WR to start the
conversion.
c) Keep monitoring the INTR pin. If INTR is low, the conversion is finished
andwe can go to the next step. If INTR is high, keep polling it goes low.
d) After the INTR has become low, we make CS = 0 and send a high-tolow pulse to the RD pin to get the data out of the adc0804 IC chip.

Equipment:
8051 KEIL Compiler
8051 Trainer
59

ADC804
Potentiometer (10k)
TTL square wave and TTL square wave clock source

Procedure:
Connect a channel to potentiometer and LEDs of trainer to P1 pins.

60

Write a program to display data input from a channel connected to


potentiometer on pins of P0. Examine the data displayed on LEDs. As
the resistance of the potentiometer changes, the output should
change, indicating the value of the analog input in binary.

Notice that the ADC outputs displayed on LEDs is between 0 to FFH


where FFH is for a full-scale input. However, this must be converted to
decimal and then to ASCII in order to be displayed on the LCD.

Observations and Results:


Already mentioned above

Learning outcomes:
To interface an ADC804 to the 8051

Questions:
Indicate the direction of pin RD, WR and INTR from the point of view of
the
ADC804.
Give the steps for converting data and getting the data out of the
ADC804. State the status of the INTR pin in each step.
Give the role of signals INTR in selecting the ADC channel.
In the ADC804 assume that Vref is connected to 2.56 V. Find the
following.

61

a) step size
b) maximum range for Vin
c) D7 - D0 values if Vin = 1.2 V
d) Vin if D7 - D0 = 11111111
e) Vin if D7 - D0 = 10011100
In the ADC804 assume that Vref is connected to 2.7V. Find the
following.
a) step size
b) maximum range for Vin
c) D7 - D0 values if Vin = 1 V
d) Vin if D7 - D0 = 11111111
e) Vin if D7 D0 = 11011101

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
62

2. Are you able to apply knowledge gained in the lab to


similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
5. Can you design a system, component or process to
fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

Lab. 16
Experiment title:
Interfacing a stepper motor to an 8051

Introduction and Theory:


Stepper motors operate with precision with specific bit patterns applied to it as input.

Pre Lab Preparation:

Learn to calculate the required step size for the stepper motor.
Learn different bit patterns applied to the stepper motor for operation
See the pin configuration and the tables for the stepper motor.
Study from any book
63

Using your 8051 trainer, build the circuit shown in Chapter 15 of the
textbook.

Equipment:
8051 KEIL Compiler
8051 Trainer
Stepper motor

ULN2003 or ULN2803 Darlington driver chip


.

Procedure:
Circuit connections:

Write and run a program to rotate it clockwise continuously. Use the


following steps to figure out the stepper motor leads.
64

1. Use an ohmmeter to measure the resistance of leads. This should


identify the COM leads A through D winding leads.
2. The common wires are connected to the positive side of the motor's
power supply. In
many motors, +5V is sufficient.
3. The four leads of the stator winding are controlled by four bits of the
8051 port (in this case P2.0 P2.3). However, since the 8051 lacks
sufficient current to drive the stepper motor windings, we must use a
driver such as the ULN2003 or ULN2803 to energize the stator.
Change the delay to see the speed of rotation.
write and run a program to rotate clockwise 180 degrees, stop for a
second, then go counter clockwise to its original position.
write and run the following:
1. Connect a DIP switch of trainer to P2.1 and use it for choice of
clockwise or counter clockwise. Use P2.1=0 for CW and P2.1=1 for
CCW.
2. After the user has made a choice, the motor will rotate accordingly.
You can also use P2.7 and P2.6 to choose the degree of rotation.

Observations and Results:


Already defined above

Learning outcomes:
To interface a stepper motor to the 8051.
To write a program to control the angle and direction of stepper motor rotation by the user.

Questions:
What is a step angle? Define steps per revolution.
If a given stepper motor has a step angle of 5 degrees, find the
number of steps per rotation.
Give the four sequences for counter clockwise if it starts with
10011001 (binary).

65

Each student is required to perform the lab and submit the following
Performa along with the lab report.
1 = Unsatisfactory
Excellent

2 = Fair

3= Satisfactory

Questions

4 = Very good

Rating

5=

Remarks

(1 to
5)
1. To what extent did you learn on the material in the
lab?
2. Are you able to apply knowledge gained in the lab to
similar problems?
3. Are you able to analyze and interpret data recorded
in the lab?

4. Are you able to identify, formulate and solve


electrical engineering problems based on the
knowledge acquired in the lab.?
66

5. Can you design a system, component or process to


fulfill certain specifications based on the knowledge
acquired in the lab?
6. Were you able to function as a group in the lab?

7. Were you able to interpret effectively the procedures


and questions asked in the lab?
8. Comment whether your work in the lab has an
impact on the society.
Additional Comments (if any):

67

You might also like