Вы находитесь на странице: 1из 10

PRACTICE 2 (CS-22XX)

Objectives

• Practice control transfer instructions (jump, loop, and call instructions) in Keil μVision IDE

• Practice 8051 I/O port programming with Keil μVision IDE

• Creating various simulation projects with LEDs in Multisim

Related textbook chapters:

• Chapter 3: Jump, Loop, and Call Instructions (Week 2)

• Chapter 4: I/O Port Programming (Week 3)

EXERCISE 1: LED BLINKING

In the first exercise, we will practice writing an Assembly language program to blink an LED and
simulate it in Multisim. Blinking LED is the “Hello, world!” type of exercise in the world of
microcontroller programming.

Note that some of the very basic steps on how to create a project, select components in
Multisim, etc., are omitted here as we have covered them in Practice 1. If you do not remember
how to do it, please find, and repeat the relevant steps from the previous manual.

1. Launch Keil μVision IDE.

2. Create a new project and name it “Lab2Ex1” (create an empty Lab2Ex1 folder first and then
save your project there). Select AT89S52 (a version of the 8051 MCU) as the microcontroller for
your project. Modify options for Target “Target 1” by changing the crystal (Xtal) value to 11
MHz on the Target tab, and by checking the create HEX File option on the Output tab.

1
3. Create an assembly program named “Lab2Ex1.asm”, and write down this code:

Notice that it is a combination of two examples from the course textbook. The main part of the
program is taken from Example 4-2 with the small modification of the port bit used (P2.7
instead of P1.0), and the delay subroutine is taken from Example 3-8. The code given to you
above has no comments. The code that you submit must contain comments. In your
comments include:

• Your name (first and last) and group on top.


• Calculation of the time it takes to execute the delay subroutine given that you have
11.0592 MHz crystal (Lecture on Chapter 3).

4. Save your source (assembly) file, and don’t forget to add it to the project tree on the left-
hand side of Keil IDE.

5. Assemble and build your program. Make sure that you have 0 Errors, and 0 Warnings.

6. Now, we are ready to debug the code and see the status of the I/O port’s pins (P2.7 to be
exact). To do so, start the debugger. Then, make sure that View – Periodic Window Update is
checked. Next, click Peripherals (top menu bar) → I/O-Ports → Port 2 to view P2.

7. To have a better understanding of what is going on with your code, you can add breakpoints
to it, such that you can see the status of specific registers, memory locations, ports, right before
the line of code with a breakpoint is executed.

To add/remove a breakpoint, left-click on the left dark-grey area in the zone that matches the
line you want your code to stop before. For example, let’s add two breakpoints right before
LCALL DELAY instructions as shown on the picture below:

2
8. To start execution of your code and have it stopped at the first breakpoint, click on the Run
button on top as shown below, or you can click F5.

Notice that when you click it one more time, it will advance and stop before the next
breakpoint, and the status of P2.7 bit will change (shown below). Click the Run button (or F5)
several times and try to make sense of what is going on.

Make a full-screen screenshot of your debugging stage with the Parallel Port 2 window
shown. You will have to submit it to receive a grade. One screenshot at any of the output
states is enough.

3
9. If everything works as expected, and your P2.7 changes its status at each breakpoint, you are
ready to test your code on the microcontroller circuit in Multisim electronics simulator. Launch
Multisim. Create the “Lab2Ex1Multisim” folder inside the previously created “Lab2Ex1” folder.
Save your Multisim project in the “Lab2Ex1Multisim” folder.

10. Build the schematic circuit shown below. Notice how it is partially similar to the one you
have created in Practice 1, but this time it contains an LED and a resistor.

4
To place an LED into your schematic, search for LED_red (you can use other colors as well) in
the component list as shown below.

From the courses on the electrical circuits, you should remember that LED is a polar
component, meaning that the direction with which you connect LED to the circuit matters.
Connect the Anode (positive) side with the Vcc source through a resistor, and the Cathode
(negative) side with pin 28, which is the pin corresponding to P2.7 from the code that you have
previously tested in Keil. Don’t forget about a resistor. Put 200 Ω resistor by searching for
RESISTOR component. When you do not include a resistor in your circuit, it can burn and
destroy your LED. In simulation, you will not experience such issues, but it is always a good
exercise to create a more realistic schematic circuit resembling how would you build a real one.

5
11. Upload the program (hex file) that you have created with Keil to your microcontroller by
double-clicking on it and specifying the hex file directory. Click on the Play icon on top to start
the simulation. You should see how your LED blinks by changing the colors of two arrows near
the LED symbol.

12. Now, let’s learn how to connect an oscilloscope (waveform measuring device) and see what
happens on pin 28. Select the Oscilloscope button (4th from the top) on the right-hand side
menu as shown below:

13. Place the oscilloscope on your schematic and connect it as follows: channel A’s positive (+)
side is connected to pin 28, and channel A’s negative (-) side is connected to ground GND.

14. Next, start your simulation, and double-click on the oscilloscope. You will see a window
showing the square wave that resembles the HIGH and LOW states of pin 28. It means that pin
28 (P2.7) toggles it states between 0 and 1. To adjust the oscilloscope for a better view, click on
Timebase Scale and make it equal to 500 microseconds/division (shown below). You will
observe how the waveform “stretches”. The dotted grid on the background should help you to

6
read the signal. Each dotted grid box corresponds to 500 microseconds on the horizontal axis,
and to 5 Volts on the vertical axis (as indicated to the right of Timebase as Channel A Scale, that
can also be adjusted). In the offline lab, you could measure pin 28’s signal with the real
oscilloscope. It is a very important skill to be able to work with an oscilloscope as once you
create more advanced circuits, it can happen that something goes not as expected, and you
should be able to debug the real circuit and check if you have all pins working as expected. A
typical issue is that sometimes you don’t have a physical connection in your circuit, for
example, one of the wires was not properly connected. Using oscilloscope, you will be able to
identify the source of the problem, and it will save you a lot of time.
1

2 4

Make a full-screen screenshot of your simulation with the oscilloscope window shown. You
will have to submit it to receive a grade. Make sure that the circuit schematic is fully visible
on the screenshot as well.

Congratulations on completing the Blinking LED exercise with Keil and Multisim. Well done!

EXERCISE 2: SWITCH-CONTROLLED LED

In this exercise, we will create a simple system for controlling an LED with a switch. It is similar
to a simple light control system that you have at your home. So, the switch will serve as the
input device, and the LED as the output device. It means that we will have to perform the I/O
port programming once again. Similarly, to the previous exercise we will create the hex file in
Keil first, and then simulate the system in Multisim.

1. Launch Keil μVision IDE.

2. Create a new project and name it “Lab2Ex2”. Select AT89S52 (a version of the 8051 MCU) as
the microcontroller for your project. Modify options for Target “Target 1” by changing the
crystal (Xtal) value to 11 MHz on the Target tab, and by checking the create HEX File option on
the Output tab.

7
3. Create an assembly program named “Lab2Ex2.asm” and write down the code that will get
the status of the switch connected to P1.0 and send it to the LED connected to P2.7. This time
the exact code is not given to you to facilitate your thinking process on how to write the
Assembly language program, but you can start by checking Example 4-7 from the course
textbook. Note that there is typo in the first line of the code there. I will leave it for you to
correct it in your version of the code. Similarly, to the program code from Exercise 1, include
all necessary comments in your code (your name, group).

4. To debug the program code, click Peripherals → I/O-Ports → Port 1 to view P1, and then Port
2 to view P2. Include a breakpoint somewhere between changes in the state of the LED. You
can check and uncheck individual bits in the Parallel Port 1 window, to simulate change of the
input signal.

5. Make a full-screen screenshot of your debugging stage with the Parallel Port 1 and Parallel
Port 2 windows shown. The code must be clearly seen as well. You will have to submit it to
receive a grade. One screenshot at any of the input/output states is enough.

6. Now, we are ready to proceed with a simulation of this exercise in Multisim. We can reutilize
the circuit from the previous exercise and add a switch (DIPSW1 component) with a 10kΩ
resistor there for testing current exercise.

7. Upload the hex file for this exercise to your microcontroller and start the simulation. Check
the readings of the oscilloscope. You can change the switch state simply by clicking it while the
simulation is running. Pay attention to the changes on the oscilloscope screen.

8
5V

0V

Also pay attention to the following: the oscilloscope shows 5 V (HIGH) when the switch is off
and the LED is off, and it shows 0 V (LOW) when the switch is on and the LED is on. It might be
different from what you could have expected. It is because of the way we connected our LED.
We used the so called “Pull-Up” connection, so it has inverse logic. If we use, the “Pull Down”
connection, then we will have the LED off when the switch is off and the oscilloscope will have a
0 V measurement.
Pull Down Pull Up

Make 2 full-screen screenshots of your simulation with the oscilloscope window shown (one
for the switch off case, and another for the switch on case). You will have to submit it to
receive a grade. Make sure that the circuit schematic is fully visible on the screenshot as well.

Congratulations on completing the Blinking LED exercise with Keil and Multisim. Well done!

9
DELIVERABLES FOR GRADING OF PRACTICAL ASSIGNMENT 2:

• Submit your “Lab2” project folder with all internal subfolders “Lab2Ex1”, “Lab2Ex2”.

• It must contain ALL Keil and Multisim related subfolders that you created while doing this
practical assignment. Do not delete folders that were created by Keil of Multisim
software.

• It must contain all screenshots requested in the manual as was explained in each of the
exercises. For the last exercise you need to submit a short video showing your simulation
in operation. You video has to be small in size as the limitation of the files that you can
upload to Moodle is only 40 MB. If your video is larger than 40 Mbytes, please upload it
to YouTube/OneDrive/GoogleDrive and provide a link. Make sure that you give access
right to view for anyone who has the link to your video.

• Zip this folder and submit it to Moodle to a dedicated submission box for Practice 2.

10

Вам также может понравиться