1. Laboratory Manual
s
m
MikroDes MD51
te
8051 trainer kit ys
sS
De
ro
ik
MikroDes Systems
M
Web: www.mikrodes.com
Email: mikrodes@gmail.com
Phone: 09960933595, 02332305904
All Rights Reserved c 2010
2. ii
MikroDes Systems gives no warranty of any kind, expressed or implied, with regard to the
documentation in this manual. MikroDes Systems will not be liable for incidental or consequential
damages in connection with, or arising out of the use of this documentation. The trainer kit MD51
is to be used for educational purpose only and not as a part of any life saving device. This writeup
is a property of MikroDes Systems. It may be used for academic purpose by customers of Mikrodes
Systems. Any part of this document should not be used for any commercial purpose.
All rights reserved.
For comments and suggestions contact: mikrodes@gmail.com
s
t em
ys
sS
De
ro
ik
M
MikroDes Systems (www.mikrodes.com)
3. List of Experiments
1 Keil microVision IDE 3
s
2 Implementing flashing LED 11
m
3 Implementing combinational logic 15
te
4 Sensing keys and activating relays 19
5 Reading code memory using MOVC instruction 25
6
7 Study of Timer0 in timer mode ys
BCD to 7-segment decoder using MOVC instruction 29
35
sS
8 Study of Timer0 in counter mode 39
9 Study of LCD display 43
10 Sensing key in matrix keyboard 53
De
11 Study of external interrupts 61
12 Study of timer-0 interrupt 73
ro
A Program Downloading Procedure 77
ik
M
MikroDes Systems (www.mikrodes.com)
4. 2 List of Experiments
s
m
te
ys
sS
De
ro
ik
M
MikroDes Systems (www.mikrodes.com)
5. Experiment 1
Keil microVision IDE
1.1 Objectives
The objectives of this experiment are,
s
m
1. To understand the various steps for creating a new project in Keil uVision Integrated
Development Environment.
2. Build the project and Simulate the program.
te
1.2 Prerequisites
1. Understanding of 8051 architecture
2. MOV, SJMP instructions
1.3 Apparatus ys
sS
1. PC with Windows XP and Keil uVision installed
2. 8051/89V51RD2 data sheet
1.4 Theory
De
For developing 8051 based programs, we need following softwares
1. An editor: To type and edit the program
2. An assembler: To convert an assembly program into object files
3. A compiler: To convert a C program into object files
ro
4. A linker: To link all object files into a single object file
5. Object to Hex converter: To convert the object file to hex file
6. Debugger: For step-wise execution of the user program and understand the bugs
7. Simulator: Imitates the functionality of on-chip peripherals such as timer, IO ports
ik
8. Downloader: To download the hex file into the microcontroller
The softwares (sr. 1 to sr. no 7) are available in a single integrated package. Such package
M
is typically called as Integrated Development Environment (IDE). An excellent IDE is available
from a company called Keil. The IDE is named as uVision (Micro Vision). Various versions of
uVision are available. You may download a demo copy of uVision latest version and install it
in your PC for all experiments in this manual. All the programs given here are tested for Keil
uVision-4. In addition to Vision-4 software (IDE), we also need software for downloading the
user program into the microcontroller. For 8051, a software called as FlashMagic is available.
You may download the demo copy of it and install it in your PC for downloading hex files in
MD51 kit. In this experiment, we are going to understand the uVision4 IDE.
1.5 Procedure
We will have to carry out following three activities.
1. Activity 1: Creating a new uVision project
6. 4 Keil microVision IDE
2. Activity 2: Building the created project
3. Activity 3: Simulating/Debugging the program
IMP: While doing any activity, read each point in the activity completely, and then do it.
1.5.1 Activity 1
1. Create a folder where all the files of your project will be stored. To do this, double
click on my computer icon. Open D: drive or any other available drive. For explanation
purpose, we consider here that, D: drive is available. After opening the drive, create a
new folder “8051”. Open it and create one more folder. Name it as 01-FirstProj. We
will keep all the files of our project in this folder. Remember.. Further in these activities,
s
you need to create and save some files. Every time you do that, you MUST ensure that
m
the file is saved in the SAME folder that you created just now. So the path of the folder
for this project is D: 8051 01-FirstProj.
2. Double click on the uVision4 icon on the desktop.
3. This will start the uVision4 software. Close the open project (if any), by selecting the
te
Project menu and clicking on close. If there are no open projects, go to next step.
4. Select the Project menu and click on New uVision Project (or New Project).
5. Type the project file name. Do not provide any extension to the file name. The extension
ys
of the file is automatically taken as .uvproj. For this exercise, give the name FirstProj
to the project. Select the folder D:805101-FirstProj. Then, click on Save.
6. The software now, prompts to select the microcontroller (device). The Mikrodes MD51
sS
kit uses P89V51RD2 microcontroller, Therefore, select the company as Philips (or NXP).
Expand the device tree (i.e) click on the small + symbol on the left side of the com-
pany name; and then select the device as P89V51RD2. Click on OK. (Do not select
P89LV51RD2 or similar other device. Select only P89V51RD2.) If you did not find this
device (i.e.) IC, then select Intel 8031 or 8051.
De
7. After this, the software prompts you to add the startup file. We do not need the startup
file now. Therefore click on the button No.
8. Now that we have created a new project, we need to create a source file (program file)
(i.e.) the file which has the actual program. To do this, select the File menu and click
on New. A new file will be created. Now, select the File menu and click on Save
ro
as. Provide the filename as main.asm. Remember this time, you must provide the file
extension. Since we are writing assembly program, give the .asm extension.
9. Select the folder D:805101-FirstProj. Click on the Save button. The file is now saved
ik
with the name main.asm. Now, type the program given in section 1.6 in this .asm file
and save the file. Save the file. (File→Save)
10. Now, open the project folder. (i.e.) D:805101-FirstProj. Observe that a project file
M
with name “FirstProj.uvproj” and a program file with name “FirstProj.asm” are present.
There are other files also; which are created by uVision4 software. Leave them as they
are.
11. Though the new source file is saved, it is not yet added to the project. To add the
file, expand Target1 in the project window. To see where is the project window refer
to figure 1.1. You will find it in the left portion of the figure. When you expand the
Target1, you will see Source Group 1 folder icon. Now, right click on the Source
Group 1 and then click on the submenu Add files to the project. A dialog box will
appear, prompting you to select the .c file. Since we have not created any .c file, and
since we have created .asm file, we have to select .asm file. For this, select the file type as
ASM Source file (*.s*, *.asm*, *.a*). When you do this, you will see FirstProj.asm
file. Click on it to select it, and click on the Add button ONLY ONCE. Click on close
button to close the dialog box
MikroDes Systems (www.mikrodes.com)
7. Sect. 1.6 Program 5
1.6 Program
;*****************************************
; MikroDes Systems (www.mikrodes.com)
;
; Program for demonstating simple instructions
; bank switching and stack operation
; input: none
; output: none
s
; Procedure:
; build the project and simulate using Keil uVision4
em
; Single step the execution and observe the registers
; in memory window
;
; Copyright (c) MikroDes Systems Aug 2009
st
;*****************************************
; ORG is an assemble directive
ORG 0000H ; starting code address
SJMP START
ORG 0030H
; main program loop
Sy
START:
MOV A, #45H ; loads hex 45
MOV A, #45 ; loads decimal 45
CLR A ; clears ACC
es
; default bank is bank 0. So the value
; gets loaded at addr. 00
MOV R0, #55H
oD
SETB RS0 ; select bank1
; this value gets loaded at 08 since
; R0 in bank1 has addr. 08
; observe location 08h in meory window.
MOV R0, #56H
kr
; initialize SP before calling any subroutine
MOV SP, #35H
; observe SP(Stack Pointer) before LCALL,
Mi
; within the subroutine and after it
; Also observe the actual STACK
; (i.e.) location 35H onwards
PUSH ACC
PUSH ACC
POP ACC
POP ACC
LCALL subroutine
; infinite loop
MikroDes Systems (www.mikrodes.com)
8. 6 Keil microVision IDE
SJMP $ ;$
; subroutine
subroutine:
MOV A, #98H
RET
END; END is an assembly directive and not an instruction
1.6.1 Activity 2: Building the created project
s
1. Building a project means, converting the source files (.c or .asm) files into a hex file
that can be downloaded in the micro controller. Before we do this, we need to do some
m
settings for the project. For this, Select the Project menu and select Target options. You
may also click on the target options icon. Refer to figure 1.1.
te
ys
sS
De
Figure 1.1: Setting target options
2. Set the crystal frequency as 11.0592 MHz, since the MD51 board uses this frequency.
Refer to figure 1.2. After setting frequency, DONOT click OK, since we need to do
some more settings.
ro
ik
M
Figure 1.2: Setting clock frequency
3. Now, Click on the output tab. Click to select the option Create hex file. Refer to
figure 1.3. Click on OK button.
4. Select the Project menu and click on build target or press F7 key or click on build
icon on build tool-bar.
5. The project will be built (i.e.) the source file will be assembled. If there are no errors,
the output window will show the message that there are 0 errors.
MikroDes Systems (www.mikrodes.com)
9. Sect. 1.6 Program 7
s
Figure 1.3: Enabling Hex file Creation
m
te
ys
sS
Figure 1.4: Building Keil Project
De
ro
ik
Figure 1.5: Output after building the project
M
6. If there are any errors, they will be due to some mistake in typing the program. To locate
the error, double click on the first error. Check for the error and remove it. Repeat this
till all errors are removed. The output window will also show the name of the hex file.
In our case it will be FirstProj.hex. (Observe that the hex file name is the SAME as
the project name and NOT the same as the .asm file) Save the program file and press
F7 to build the project again.
1.6.2 Activity 3: Simulating and debugging the program
Simulation means imitating the behavior of the processor in the software. Debugging means
to find bugs in your program by doing stepwise execution of your program. Once the project
is built without any errors, we can test the program by executing it step by step. This can be
done by entering in the debug mode. The screen will be as shown in the figure 1.6.
MikroDes Systems (www.mikrodes.com)
10. 8 Keil microVision IDE
1. To enter in the debug mode, press the debug icon, or press control+F5.
s
Figure 1.6: Debug mode
m
2. As soon as we enter the debug mode, the source code window shows an arrow on the left
side of the first instruction to be executed. Also the peripherals menu becomes active
te
and if we click on it, we can see various peripheral submenus.
3. If you see the disassembly window, then close it, since in this experiment we do not need
it. Now, you will see only asm program in source code window.
ys
4. Now, to execute the program in single step, click the step over button.
sS
Figure 1.7: Program execution methods
De
5. The instruction at the arrow will be executed (simulated) and the arrow will move to
the next instruction.
6. Observe ACC and the registers R0, R1, ... R7 while executing the program. Note
ro
that, the ACC and the registers get loaded by appropriate values. When RS0 bit is set,
observe it by expanding the PSW register. Instead of separate RS0 and RS1 bits, there
is a RS. It indicates directly the bank selected. Observe that, when bank 1 is selected,
R0 corresponds to memory location 08H, R1 (09H) and so on.
ik
IMP: If you have not studied the instructions given in this program, then skip next two
points. The purpose of this experiments is mainly to get familier with uVision4 IDE.
7. Observe how SP gets initialized and how it is used to PUSH and POP the accumulator.
M
After pushing observe internal data memory 35H onwards. Also observe that, PC is
pushed on the stack while entering the subroutine and popped while exiting from it.
8. At the end of the program there is a statement for infinite loop. When this is executed,
the program loops there itself. The instruction used for this is SJMP $.
1.7 Observations
Write the observations which you made, while single stepping the program.
1.8 Conclusions
Write conclusion about utility of simulator for debugging the programs.
MikroDes Systems (www.mikrodes.com)
11. Sect. 1.9 Homework 9
1.9 Homework
1. Read the help of uVision software and try to learn various facilities in it.
2. Do enough practice of creating project, creating .asm file, building the project and sim-
ulating the program.
s
m
te
ys
sS
De
ro
ik
M
MikroDes Systems (www.mikrodes.com)
12. 10 Keil microVision IDE
s
m
te
ys
sS
De
ro
ik
M
MikroDes Systems (www.mikrodes.com)
13. Experiment 2
Implementing flashing LED
2.1 Objectives
s
The objectives of this experiment are,
m
1. To understand the Input-output operations in 8051.
2. To understand delay subroutine using DJNZ instruction.
3. Debugging in uVision4.
te
4. Downloading the program in MD51 kit.
2.2 Prerequisites
ys
1. Experiment 1, (Keil microVision IDE) should have been performed.
2. DJNZ instruction
sS
2.3 Apparatus
1. PC with Windows XP
2. Keil uVision and FlashMagic softwares installed
3. 8051/89V51RD2 data sheet
De
4. MD51 kit with power adapter and serial cable
2.4 Theory
ro
8051 micro-controller is a 40 pin chip. It has four ports. A port is nothing but a set of 8 pins
which can used for data input-output(IO). The ports are named as P0, P1, P2 and P3. These
ports are brought onto pins of 8051. Since there are 8 pins per port, total 32 pins of 8051 can
be used as port lines. Each line can be individually made LOW or HIGH. Also the complete
ik
port (8 pins) can be written by an 8 bit data (byte). If the data written in the port is 00 then,
the pins will become LOW and if the data written is FF, then, the pins will become HIGH. If
this is done repeatedly, it is possible to toggle the port pins; and if LEDs are connected to the
M
port pins, the LEDs will flash.
This experiment explains how to perform port Output operations in 8051 and test it in
MD51 kit. For this, we need to understand some instructions. So do the following activity.
2.4.1 Activity
Copy the syntax and description of following instructions from 8051 instruction set, (provided
in the CD) and understand the operation of each instruction.
• MOV Addr,# Immediate Value
• MOV Addr, Rn
• MOV Rn, Addr
• SETB, CLR instructions
14. 12 Implementing flashing LED
2.4.2 Outputting a byte on an 8-bit port
MOV Px, #<8 bit data> instruction can be used to write any value to any port. For example,
for writing a value 35H in port P1, we should use MOV P1, # 35H. Here, # indicates an
immediate value. (If # is not written, then 35H will be treated as an address). We can make
the micro-controller to wait by using a delay routine. After the delay routine we can again
change the port value and have another delay. If this is repeated, the port pins will toggle and
the LEDs connected to that port willl flash.
In MD51 kit, 8 LEDs are connected to port P1 in a pulled up fashion; (i.e.), if 00H is output
on P1, all LEDs will glow and if FFH is output on P1, all LEDs will be off. Refer to figure
2.1. Observe that the cathodes of LEDs are connected to port P1 pins (P1.0, P1.1, ... P1.7).
The DIP switch1 (DS4) is provided to disconnect the LEDs if required. In this experiment,
s
you need to use the LEDs. So keep the DS4 switch ON. Also study the flowchart in figure 2.2
m
and then the program given in section 2.7.
2.5 Circuit Diagram
te
+5V
ys
DS4
2
1
2 1
ON
sS 3
4
2-POS DIP Switch
470E,
470E,
470E,
470E,
470E,
470E,
470E,
470E,
R18
R19
R21
R22
R23
R27
R29
R30
De
D16
D18
D20
D21
D22
D23
D24
D25
ro P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
ik
P1.[0..7]
M
Figure 2.1: LED Connections
1
switches in Dual In-line Plastic package. These switches can fit in IC sockets
MikroDes Systems (www.mikrodes.com)
15. Sect. 2.6 Flowchart 13
2.6 Flowchart
Start
Make all port lines
of P1 LOW
s
Wait for 1 sec
em
Make all port lines
of P1 HIGH
st
Wait for 1 sec
Sy
Figure 2.2: Flowchart for Flash-LED
2.7 Program for flashing LED on P1
;*****************************************
es
; LED Flashing program for 8051
; Tested with MD51 kit
; Copyright (c)
oD
; MikroDes Systems
; http://www.mikrodes.com
; Aug 2009
;*****************************************
kr
; ORG is an assemble directive to set
;the code memory address
ORG 0000H ; starting code address
SJMP BACK
Mi
ORG 0030H
; main program loop
BACK:
MOV P1, #00H
LCALL DELAY_1_SEC
MOV P1, #0FFH
LCALL DELAY_1_SEC
SJMP BACK
; subroutine for delay
MikroDes Systems (www.mikrodes.com)
16. 14 Implementing flashing LED
DELAY_1_SEC:
MOV R0, #55H
DLY2:
MOV R1, #55H
DLY1:
MOV R2, #55H
DJNZ R2, $ ;%$
DJNZ R1, DLY1
DJNZ R0, DLY2
RET
;*****************************************
s
m
2.8 Procedure
1. Create a folder “8051” in D: and open it.
2. Copy the folder “LED-flashing” from the “Programs” folder in the CD, to D:8051.
te
3. Open the folder D:8051LED-flashing. Double click to open the µVision project “LED-
flashing.uvproj”. The project will open in the µVision software.
4. Build the project. This will crate a .hex file with name LED-flashing.hex
the micro-controller.
ys
5. Follow the instructions given in appendix A on page 77, and download the .hex file in
6. After the downloading is complete, the user program starts executing. Observe that, the
LEDs connected to P1 start flashing. (Make sure that the DIP switch near LEDs is ON).
sS
2.9 Exercise
1. Change the immediate data 00h and 0FFH, in the given program, to some other value,
build the project, download the file, reset the kit and observe the output.
De
2. In the program try to increase the delay. (Copy-paste the instruction, LCALL DE-
LAY 1 SEC)
2.10 Conclusion
ro
1. It is easy to write any desired byte into a micro-controller port using MOV instruction.
2. By repeatedly changing the data on a port with some delay, it is possible to flash LEDs.
The delay can easily be changed by changing the program.
ik
3. Micro-controller based logic gates are very useful in low frequency applications as there
is flexibility in functioning of logic gates.
M
2.11 Homework
1. Modify the program to create 1KHz waveform on all port pins of P1. Download it in the
MD51 kit and observe the output. You will find that all the LEDs are always glowing.
How will you test that they are flashing?
2. Use SETB and CLR instructions to flash only one LED connected to P1.0 after every 1
second. Test the program using MD51 kit.
MikroDes Systems (www.mikrodes.com)
17. Experiment 3
Implementing combinational logic
3.1 Objectives
s
The objectives of this experiment are,
m
1. To understand how to implement any combinational logic using assembly program in
8051 microcontroller.
2. Implement an AND gate and verify it in MD51 kit.
te
3.2 Prerequisites
1.
2.
3.
8051 architecture
Port structure of 8051
MOV, SJMP instructions ys
sS
4. JB, JNB instructions
3.3 Apparatus
1. PC with Windows XP,Keil uVision and FlashMagic installed.
De
2. 8051 instruction sheet
3.4 Theory
ro
3.4.1 What is a combinational logic?
A combinational logic is a logic whose output depends totally on the present state of inputs.
(i.e.) the output does not depend on the previous states of input. This kind of logic circuit
ik
does not contain any memory elements such as flip-flops. They contain only simple gates
without feedback. The gates AND, OR, NOR, NAND, EX-OR, EX-NOR etc. are examples of
combinational logic. Also combination of the basic gates to implement a Boolean expression
M
results in a combinational logic.
3.4.2 Why to implement a combinational logic in microcontroller?
Many times, in industrial processes, the output state depends on the states of various inputs.
In such case, we need to implement a combinational logic in a microcontroller. If we use a
dedicated circuit based on TTL or CMOS ICs, then the logic cannot be easily changed. (i.e.),
this approach is not flexible. However, if the same logic is implemented using a microcontroller,
then by just changing the assembly program, we can change the implemented logic. (i.e.)
implementing a combinational logic in a microcontroller is a highly flexible way.
So wherever, flexibility is more important, in such applications, it is better to implement
combinational logic in a microcontroller.
18. 16 Implementing combinational logic
3.4.3 How to implement it?
To implement any combinational logic in a microcontroller, we need to configure some port
pins as input and some as output. For example, if we want to implement a simple 2-input
AND gate, then we need to use two port pins as input and one port pin as output. In this
experiment, we are going to use P3.2 and P3.3 as input lines and P1.0 as output. This is
done because, P3.2 and P3.3 port pins are connected to push to on switches which are placed
in the right bottom corner of the MD51 kit. The output is taken from P1.0 since; LEDs are
connected to port P1.
Let s now try to implement an AND gate in 8051, using an assembly program. For this,
s
the input-1 (P3.2) is first checked if it is logical 0 or 1. If it is 0, then the output P1.0 is made
logical 0. If it is 1, then next input, input2 (P3.3) is checked if it is logical 0 or 1. If it is 0,
em
the output is made 0. If it is also 1, then it means both the inputs are 1. Hence the output is
made 1.
This way the logic of AND gate is implemented by continuously checking the inputs (called
as polling) and adjusting the output to either logic 1 or logic 0. The flowchart is shown in
st
figure 3.1
Sy
es
oD
kr
Figure 3.1: AND gate flowchart
3.5 Program
Mi
;*****************************************
; MikroDes Systems (www.mikrodes.com)
;
; Program for implementing 2 input
; AND gate in 8051
;
; input: keys S1(P3.2) and S2(P3.3) on the
; right bottom corner of MD51 kit
; output: LED connected to P1.0
; NOTE: Please note that the when the switch
; is pressed then, the logic level of input
MikroDes Systems (www.mikrodes.com)
19. Sect. 3.6 Procedure 17
; is LOW and when released the logic level
; is HIGH.
; Also, the logic level of output is LOW when
; LED is glowing and it is HIGH when
; LED is OFF. This is because, the LEDs
; are connected with common anode mode.
; NOTE:
; (SWITCH=Pressed, pin=LOW)
; (SWITCH=Released, pin=HIGH)
; (LED=ON, pin=LOW) (LED=OFF, pin=HIGH)
s
; Procedure:
m
; Download the and_gate.hex using FlashMagic
; Press S1. Observe the LED (P1.0).
; It will not glow
te
; Press S2. Observe the LED (P1.0)
; It will not glow
; Press S1 & S2. Observe the LED (P1.0)
;
;
;
It will glow now.
Prepare the truth table and verify that
the operation is same as AND gate. ys
sS
; Copyright (c) MikroDes Systems Aug 2009
; www.mikrodes.com
;*****************************************
De
ORG 0000H ; starting code address
START:
JB P3.2, CLR_BIT ; jump to CLR_BIT if P3.2 is 0
JB P3.3, CLR_BIT ; jump to CLR_BIT if P3.2 is 0
ro
CLR P1.0 ; set P1.0 if both P3.2 and P3.3 are 1
SJMP START
ik
CLR_BIT:
SETB P1.0
SJMP START ; Repeat it
M
END
;*****************************************
3.6 Procedure
We will have to carry out following three activities.
1. Activity 1: Copy the folder of the experiment from the CD given. Open the project and
study it.
2. Activity 2: Build the project, download the hex file in MD51 and verify the operation.
3. Activity 3: Modify the program and verify functionality.
MikroDes Systems (www.mikrodes.com)
20. 18 Implementing combinational logic
3.6.1 Activity 1:
1. Create a folder “micro-controller” in D: drive (or in any local drive) and Open the created
folder.
2. Insert the MikroDes MD51 CD in the CD drive and open it. Open the 8051 programs
folder. Copy the folder “03 AND-Gate” to the D:micro-controller local folder. After
copying, open it.
3. Double click on “and gate.uvproj” file. This will start the uVision4 software and open
the project. Study the asm program in the file “AND-Gate.asm”. Build the project by
pressing F7 key. The hex file will be created. Press Cntrol+F5 to start the simulation
(debugging).
4. Click on Peripherals→IO port and then click on Port-1. This will show the port 1 state.
s
(Refer to figure 3.2). Also open Port-3. Execute the program step-wise by typing F10,
m
repeatedly. Set the port P3.2 and P3.3 and observe P1.0. Observe that, only when both
the inputs are 1 , the putput becomes 1 . This proves that, the program implements and
AND gate in 8051.
te
ys
sS
Figure 3.2: Viewing ports in simulator
De
3.6.2 Activity 2:
1. Follow the instructions given in appendix A on page 77, and download the .hex file in
the micro-controller.
2. Now press the “push-to-on” switches S1 and S2 (near the right bottom corner of MD51
ro
kit) and observe P1.0 LED. You will find that, when both the switches are released, (i.e),
when both inputs are logic 1 , the output becomes 1 . (i.e.) The LED becomes off. For
all other input combinations, the LED remains ON (P1.0 is logic 0 ). This proves that,
ik
the AND gate is implemented in 8051.
3.6.3 Activity 3:
M
1. Now, modify the program to implement OR gate by modifying the instructions. Repeat
all steps of activity 2 and verify the operation in MD51 kit.
3.7 Observations
Write the observations which you made, while performing the activities mentioned.
3.8 Conclusions
Write conclusion based on your observations.
3.9 Homework
Implement EX-OR gate and verify its operation.
MikroDes Systems (www.mikrodes.com)
21. Experiment 4
Sensing keys and activating relays
4.1 Objectives
The objectives of this experiment are,
s
m
1. To understand how to sense the state of a key connected to a port pin using assembly
program in 8051 micro-controller.
2. To understand how to activate relay based on the key state.
te
4.2 Prerequisites
1. 8051 architecture
2. Port structure of 8051
3. JB, JNB, SETB, CLR instructions
4.3 Apparatus
ys
sS
1. PC with Windows XP, Keil uVision and FlashMagic installed.
2. 8051 instruction sheet
3. MD51 micro-controller kit
De
4.4 Theory
In most of the electronic systems, the user input is taken through keys. The keys can be
connected to a micro-controller in two ways. 1. Single key per pin and 2. Key matrix.
In this experiment, we are going to understand the first method of connecting keys. There
ro
are two keys S1 and S2 on the MD51 kit and they are connected to P3.2 and P3.3. These keys
are connected such that, when a key is pressed, the logic level on the corresponding pin goes
low and when the key is released, it goes high. A key can be sensed using the instructions JB
ik
or JNB. The syntax of JB is JB <bit address>, label. If we want to sense P3.2, and if we
write an instruction JB P3.2, Next; then it will sense the port pin P3.2 and if the pin is high,
then the program will jump to Next. Otherwise the program will go to the next instruction.
M
JNB instruction works the same way except that it makes the program branch if the level on
the pin is low. Using these instructions it is possible to sense a key press event and based on
it activate some devices.
In this experiment, we are going to activate/deactivate relays on the MD51 kit based on
the key state. There are 2 relays on the kit. They are Relay-0 and Relay-1. Relay-0 is driven
by pin P0.2 and Relay-1 is driven by the pin P0.3. The program given senses the keys S1 and
S2 (near the right bottom corner) and activates Relay-0 is key INT-0 is pressed. If key S2
(INT-1) is pressed, the Relay-1 is activated. The figure 4.1 shows the inputs and outputs. The
8051 micro-controller senses the keys and based on the key state, activates the relays.
1. JB (Jump on bit):
This instruction checks the specified bit and jumps to specified label if that bit is high.
The syntax of the instruction is, JB bit, label.
22. 20 Sensing keys and activating relays
Figure 4.1: Block diagram
s
Key S1 (INT0) P3.2
Key S2 (INT1) P3.3
m
Relay-0 P0.2
Relay-1 P0.3
te
Table 4.1: Pin Connections
2. JNB (Jump on no bit):
ys
This instruction checks the specified bit and jumps to specified label if that bit is low.
The syntax of the instruction is, JB bit, label.
sS
A relay can be activated by making LOW, the port pin, that is connected to relay driver.
To deactivate the relay the port pin needs to be made HIGH. A port pin can be made HIGH
or LOW, using following instructions.
1. SETB (Set the bit):
De
This instruction sets the specified bit. If the bit is a port bit(for example, P1.0, P1.2
etc), then, it sets the corresponding port pin (i.e. makes it HIGH). The syntax of the
instruction is, SETB bit.
2. CLR (Clear the bit):
This instruction makes the specified bit low. If the bit is a port bit(for example, P1.0,
ro
P1.2 etc), then, it resets the corresponding port pin (i.e. makes it LOW). The syntax of
the instruction is, CLR bit.
ik
4.5 Circuit diagram
4.5.1 Key sensing circuit
M
The key sensing circuit is placed at the right bottom corner of the MD51 kit as shown in figure
4.3(a). The actual connections are shown in figure 4.3(b). The circuit shows that, there are
two push button keys S1 and S2. When a key is in released (not pressed) position, the upper
point of that key goes to VCC potential. (i.e.) the logic level is HIGH. When a key is pressed,
the upper point of that key goes to GND potential. (i.e.) the logic level is LOW. Key S1 is
connected to P3.2 and key S2 to P3.3 through the DIP switch DS8. Therefore, while doing
this experiment, both the switches in DS8 must be in ON position.
4.5.2 Relay control circuit
The relays, connectors for relay contacts and relay driver circuit are placed near top side of
the MD51 kit as shown in figure 4.4(a). The actual circuit is shown in part (b) of the same
figure. The circuit shows that, there are two relays Relay-0 and Relay-1. They are driven
MikroDes Systems (www.mikrodes.com)
23. Sect. 4.5 Circuit diagram 21
s
m
te
Figure 4.2: Relay Circuit
ys
sS
De
ro
ik
M
Figure 4.3: Key sensing section on MD51 kit
by the driver transistors Q1 and Q2 . The transistors are controlled by the port lines P0.2
and P0.3 respectively. If P0.2 is made low then the relay-0 is activated else it is deactivated.
Similarly P0.2 controls relay-1.
MikroDes Systems (www.mikrodes.com)
24. 22 Sensing keys and activating relays
s
m
te
4.6 Flow chart
ys
Figure 4.4: Relay section in MD51 kit
sS
The flowchart is shown in figure 4.5. In this flowchart, initially a key S1 is sensed. If it
is pressed, then the relay-0 is activated, else the relay-0 is deactivated. Then the key S2 is
sensed. If it is pressed, then the relay-1 is activated, else the relay-1 is deactivated. The
complete process is repeated continuously.
De
ro
ik
M
Figure 4.5: Flowchart for relay control
MikroDes Systems (www.mikrodes.com)
25. Sect. 4.7 Program 23
4.7 Program
;*****************************************
; MikroDes Systems (www.mikrodes.com)
;
; THIS PROGRAM DEMONSTRATES RELAY CONTROL
; USING KEY INPUT
; input: Key press on S1 or S2
s
; output: activation of relay-0 / relay-1
em
; Procedure:
; Switch ON the DIP switches DS8 (bottom
; middle place) and also DS3 (near relay)
st
; Download the relay-control.hex file
; using FlashMagic
; *RESET* the kit.
; Press S1 or S2 and Sy
; observe the relay state.
; Copyright (c) MikroDes Systems Aug 2009
; www.mikrodes.com
;****************************************
es
; equate used for making the program
; easy to read/understand
RLY_0 EQU P0.2
oD
RLY_1 EQU P0.3
SW1 EQU P3.2
SW2 EQU P3.3
kr
ORG 0000
SJMP START
START: ; This is a label
Mi
; DEACTIVATE BOTH RELAYS
SETB RLY_0
SETB RLY_1
; MAKE KEY PINS INPUT
SETB SW1
SETB SW2
BACK:
JNB SW1, RLY_0_ACTIVTE ; KEY NOT PRESSED
SETB RLY_0 ; DEACTIVATE
MikroDes Systems (www.mikrodes.com)
26. 24 Sensing keys and activating relays
CHK_RLY_1:
JNB SW2, RLY_1_ACTIVTE ; KEY NOT PRESSED
SETB RLY_1 ; DEACTIVATE
SJMP BACK
RLY_0_ACTIVTE:
CLR RLY_0
SJMP CHK_RLY_1
RLY_1_ACTIVTE:
s
CLR RLY_1
SJMP BACK
m
END
;****************************************
te
4.8 Procedure
1. Create a folder “8051” in D: and open it.
ys
2. Copy the folder “Relay” from the “Programs” folder in the CD, to D: Micro-controller.
3. Open the folder D: Micro-controllerRelay. Double click to open the µVision project
“Relay.uvproj”. The project will open in the µVision software.
sS
4. Build the project. This will crate a .hex file with name Relay.hex
5. Follow the instructions given in appendix A on page 77, and download the .hex file in
the micro-controller.
6. Press key S1 and observe that, relay-0 activates. If you release S1, the relay-0 is deacti-
vated. Also S2 controls relay-1 in similar way.
De
4.9 Observation
When key S1 is pressed, it is observed that, relay-0 activates. If S1 is released, the relay-0 is
ro
deactivates. Also S2 controls relay-1 in similar way.
4.10 Conclusion
It is possible to sense a key connected to any port pin using JB and JNB instructions. It is
ik
possible to activate any device such as relay, LED etc. using SETB and CLR instructions.
4.11 Exercise
M
There are 8 LEDs on MD51 board. They are connected to P1.0 to P1.7. Write a program to
activate LED connected to P1.0 when key S1 is pressed and activate LED connected to P1.7
when key S2 is pressed. Assemble the program, download and test it.
MikroDes Systems (www.mikrodes.com)
27. Experiment 5
Reading code memory using MOVC
instruction
s
5.1 Objectives
m
The objectives of this experiment are,
1. To understand MOVC instruction
te
2. To observe the 8051 code memory contents on LEDs
5.2 Prerequisites
1. Understanding of 8051 architecture
2. MOV, CPL, SJMP instructions
ys
sS
5.3 Apparatus
1. PC with Windows XP and Keil uVision installed.
2. 8051/89V51RD2 data sheet
3. MD51 educational trainer kit, serial cable and power adapter
De
5.4 Theory
The code memory of a microcontroller holds the op-codes of user program. The code memory
is a non-volatile memory. When we download the program in the actual microcontroller, it is
ro
loaded in the code memory. Through user program we can, however, read the code memory if
required. For this MOVC instruction is available in 8051. The syntax of the MOVC instruction
is MOVC A, @A+DPTR.
In this, the DPTR holds the base address and ACC (accumulator) holds the offset. This
ik
instruction adds the A and DPTR to find the actual code memory address and the data at
that location is read and loaded in A. For example, the following program loads data from
address (0030H+04H = 0034H) and loads the data from this address from code memory into
M
Accumulator.
MOV A, #04H
MOV DPTR, #0030H
MOVC A, @A+DPTR
5.5 Procedure
We will have to carry out following three activities.
1. Activity 1: Creating a new uVision project
2. Activity 2: Building the created project
3. Activity 3: Downloading and testing the program
28. 26 Reading code memory using MOVC instruction
5.5.1 Activity 1:
Understand and write the syntax and operation of MOVC instruction.
5.6 Program
In, MD51 kit; the LEDs are connected to port P1. These LEDs can be used to observe the
contents of the code memory itself. If we want to observe the contents of location 0033H in
code memory, then we can set the DPTR to 0030H and Accumulator to 03H. With these values
in DPTR and ACC, if MOVC instruction is executed, then the accumulator will have the data
s
at location 0033H from code memory.
em
;*****************************************
; MikroDes Systems (www.mikrodes.com)
; This program reads the code memory and
; displays the op-codes of the program on Port 1
st
; Input: none
; Output: value on port P1.
; Procedure:
; Download the hex file CodeMemory.hex Sy
; Reset the kit if required.
; The LEDs will show the value 90H. (1001 0000)
; This is the first byte of the instruction MOV DPTR, #00.
; To verify, enter the debug mode in uVision and observe
; the code memory by typing C:0x0000 in memory window
es
; modify the second instruction (MOV A, #00) to
; MOV A, #03 and save the file.
; Build the project. Download the hex file.
; The LEDs will show 74H. This is the byte at address
oD
; 0003H in the code memory.
; Thus by setting the value of A or DPTR or both, we
; can read the byte at the desired code memory address
kr
; (c) Copyright 2009 MikroDes Systems
; www.mikrodes.com
;********************************************************
Mi
; The program always starts from the location 0000H
ORG 0000H
; set the pointer to desired address
MOV DPTR, #0000H
; set the offset to the desired value
MOV A, #00H
; read the code memory from address A+DPTR
MOVC A, @A+DPTR ; Read the table
MikroDes Systems (www.mikrodes.com)
29. Sect. 5.7 Observations 27
; output the 7 segment code on P1 LEDs
CPL A ; IMP: since the LEDs are connected
; as common anode, we need to complement the data
; Output the value in Acc to port P1.
MOV P1, A
; Infinite loop (equivalent to halt instruction)
SJMP $
END ;********************************************************
s
m
5.6.1 Activity 2:
1. Create a folder “8051” in D: and open it. If it already exists, open it.
te
2. Copy the folder “05 Reading Code Memory” from the “8051 Programs” folder in the
CD, to D:8051.
3. Open the folder D:8051 05 Reading Code Memory. Double click to open the Vision
project.
ys
4. “CodeMemory.uvproj”. The project will open in the Vision software.
5. Build the project. This will create the .hex file in the project folder. Verify that it is
created.
sS
5.6.2 Activity 3:
1. Follow the instructions given in appendix A on page 77, and download the .hex file in
De
the micro-controller.
2. Observe that, the LEDs connected to P1 show the pattern 10010000 (90H). This is
because; the first byte of the instruction MOV DPTR, #0000H is 90H.
3. To observe this in simulator, click on view and then on memory window. Then type
C:0x0000 in the top text box of memory window and press “enter” key. This will show
ro
the code memory. Observe that the value at location 0000 is 90H. Refer to figure 5.1.
4. In the program, DPTR and A are both set to 0000H and then the MOVC is executed.
Therefore, the value in accumulator after MOVC is executed is value at 0000H (i.e.) 90H.
5. Change the value loaded in DPTR and/or accumulator. Build the project, then download
ik
the program and observe the LEDs.
M
5.7 Observations
Write the observations which you made, while single stepping the program. Also write your
observations after downloading the program in the MD51 kit.
5.8 Conclusions
Write conclusion about using MOVC for reading the code memory.
5.9 Homework
Write a program to seqencially display first 4 bytes of code memory on LEDs connected to P1.
MikroDes Systems (www.mikrodes.com)
30. 28 Reading code memory using MOVC instruction
s
m
te
ys
sS
De
ro
Figure 5.1: Viewing code memory in simulator
ik
M
MikroDes Systems (www.mikrodes.com)
31. Experiment 6
BCD to 7-segment decoder using
MOVC instruction
s
6.1 Objectives
m
The objectives of this experiment are,
1. To understand how to prepare 7 segment codes.
2. To understand how to store and access BCD to 7-segment table in 8051.
te
6.2 Prerequisites
1. Understanding of 8051 architecture
2. MOV, MOVC, ANL, CPL, SJMP instructions
3. Experiment 5 should have been performed
ys
sS
6.3 Apparatus
1. PC with Windows XP and Keil uVision installed.
2. 8051/89V51RD2 data sheet
3. MD51 educational trainer kit, serial cable and power adapter
De
6.4 Theory
BCD stands for Binary Coded Decimal. This is a way of representing numbers in 4-bit binary
value for numbers between 0 and 9. Seven segment code is used to show a number on a
ro
7-segment LED display. The conversion from BCD to 7-segment is many times required to
display a number on a seven segment display. The BCD to 7 segment conversion process is
illustrated in figure 6.1. It shows that the input is 4 bit BCD value and the output is 7 segment
data and also decimal point. (total 8 bit output).
ik
M
Figure 6.1: Block diagram of BCD to 7 segment conversion
A seven segment display has 7 segments (a, b, c, d, e, f, g) and a decimal point (dp).
Therefore it requires total 8 bit data. The 8 bit data is usually connected to 7 segment display
such that the bit 0 (D0) is connected to segment a, D1 is connected to segment b and so on.
32. 30 BCD to 7-segment decoder using MOVC instruction
There are two kinds of 7-segment displays; Common anode (CA) and Common cathode
(CC) type. The following table shows codes for both types. In this experiment, we assume
that we are using a common anode type display. Observe that, the 7-segment code for CC
type is just complement of that of CA type. Also observe that, the BCD value does not decide
the state of dp. This is because; whether decimal point is to be displayed or not; is decided
by the user depending on the number of decimal places in the displayed number. Figure 6.2
shows the BCD to 7 segment table.
s
m
te
ys
Figure 6.2: BCD to 7 segment table
sS
6.4.1 Activity 1:
1. Write the BCD codes for numbers 0 to 9.
2. Understand the concept of common anode and common cathode connections. Draw the
internal connections of 7-segment display.
De
3. Understand the codes given in the figure 6.2.
6.4.2 How to write the program?
Look at figure 6.2. Is there any fixed relationship between BCD input and 7-segment output?
ro
We do not find such a relation from the table. Such input output relation can be easily
implemented with a microcontroller program using a Look-up table. A look-up table is nothing
but a table which can be accessed using the base address and an offset.
ik
Look-up table is usually stored in code memory since the code memory is non-volatile. To
store the table in the code memory, we need to use the DB assembler directive. This directive
tells the assembler to put the given data at appropriate code memory address. Lets understand
M
by an example.
ORG 0423H
MY_TABLE:
DB 0C0H, 0F9H, 0A4H
The above program first sets the code memory address to 0423H. Therefore the value of
the label MY TABLE will be 0423H. The assembler will prepare the .hex file such that, the
value C0H will get stored at address 0423H, value F9H at 0424H and so on, in code memory.
6.4.3 Activity 2
1. Prepare a look-up table for common anode 7-segment display.
MikroDes Systems (www.mikrodes.com)
33. Sect. 6.5 Procedure 31
2. Study MOVC instruction of 8051. The syntax is MOVC A, @A+DPTR.
MOVC instruction reads the data at address A+DPTR, from the code memory. While
using MOVC, we need to set DPTR to the starting address (base address) of the table. This
can be dome using following instruction.
MOV DPTR, #MY_TABLE
Then we have to initialize A with any value between 0 and 9. (MOV A, #0H) and then we
s
have to access the table using the instruction MOVC A, @A+DPTR. If A=0, then after this
instruction A will have value C0H. If A=1, then after this instruction A will have value F9H
and so on.
em
The following program takes the BCD data from lower nibble of P2 (P2.0, P2.1, P2.2 and
P2.3), converts it to appropriate 7-segment code and displays the code on LEDs connected to
port P1. This program actually can take values from 0-F. So it can display 7-segment codes
for all numbers 0 to F.
st
IMP: The program uses the leftmost column of the matrix keyboard for inputting the input
data. Thus we want to use the matrix keyboard as simple 4 keys. We can do a simple trick
for this. In the matrix keyboard, keep the leftmost column line (P2.7) always low. This will
Sy
make the leftmost 4 column keys as simple 4 keys connected to lower nibble of P2. Therefore,
in the program you can see the CLR P2.7 instruction.
6.5 Procedure
We will have to carry out following three activities.
1. Activity 1: Creating a new uVision project
es
2. Activity 2: Building the created project
3. Activity 3: Simulating/Debugging the program
6.6 Program
oD
;*******************************************************
; MikroDes Systems (www.mikrodes.com)
; This program converts BCD value to 7 Segment code
kr
; Input: P2.0-P2.3
; The input is to be given using leftmost column
; of the matrix keyboard. In this case,
; Uppermost key works as highest significant input
Mi
; Lowermost key works as lowest significant input
; Output: P1.0-P1.7
; The 7 segment code is displayed on LEDs connected
; to P1.
; Procedure:
; Download the bcd7seg.hex file using FlashMagic
; Reset the kit if required.
; You will observe pattern 1 0 0 0 1 1 1 0 on
; the LEDs. This pattern is corresponding to the
; BCD input F (refer to the table at the end of this
MikroDes Systems (www.mikrodes.com)
34. 32 BCD to 7-segment decoder using MOVC instruction
; program)
; Now press keys on leftmost column of the keyboard
; and observe the LED pattern
; Correlate your observations with the table given
; at the end of the program.
; IMP:
; When switch is pressed, the logic level is LOW
s
; Copyright (c) MikroDes Systems Aug 2009
; www.mikrodes.com
em
;********************************************************
ORG 00H
SJMP START
ORG 30H
st
START:
; set P0.7 Low for making Matrix keyboard appear
; as simple 4 key keyboard
CLR P0.7 Sy
; Now use 4 the keys in column 0 to input BCD data.
; DPTR is used as base register. So it must be initialized by
; the start address of 7 segment table
MOV DPTR, #TABLE
LOOP:
; read the keys
es
MOV A,P2 ; Read the data from port 1
ANL A, #0FH ; Keep only lower nibble since the
;rows of the keyboard are connected to P2.0 to P2.3
MOVC A, @A+DPTR ; Read the table
oD
; output the 7 segment code on P1 LEDs
CPL A ; imp: since the LEDs are connected to VCC
MOV P1, A
SJMP LOOP
kr
TABLE:
DB 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8
DB 0x80,0x90,0x88,0x80,0xC6,0xC0,0x86,0x8E
Mi
END
;********************************************************
; Seven segment table data
; The 7 seg display is assumed to be common anode type,
; directly connected to the port pins
; The segnment will glow if the connected pin is made LOW
; DP G F E D C B A
; 1 1 0 0 0 0 0 0 = 0xC0; for 0000 (0) all keys pressed
; 1 1 1 1 1 0 0 1 = 0xF9; for 0001 (1) upper three keys pressed
; 1 0 1 0 0 1 0 0 = 0xA4; for 0010 (2)
; 1 0 1 1 0 0 0 0 = 0xB0; for 0011 (3)
; 1 0 0 1 1 0 0 1 = 0x99; for 0100 (4)
MikroDes Systems (www.mikrodes.com)
35. Sect. 6.7 Observations 33
; 1 0 0 1 0 0 1 0 = 0x92; for 0101 (5)
; 1 0 0 0 0 0 1 0 = 0x82; for 0110 (6)
; 1 1 1 1 1 0 0 0 = 0xF8; for 0111 (7)
; 1 0 0 0 0 0 0 0 = 0x80; for 1000 (8)
; 1 0 0 1 0 0 0 0 = 0x90; for 1001 (9)
; 1 0 0 0 1 0 0 0 = 0x88; for 1010 (A)
; 1 0 0 0 0 0 0 0 = 0x80; for 1011 (B)
; 1 1 0 0 0 1 1 0 = 0xc6; for 1100 (C)
; 1 1 0 0 0 0 0 0 = 0xCO; for 1101 (D)
; 1 0 0 0 0 1 1 0 = 0x86; for 1110 (E) lowest key pressed
; 1 0 0 0 1 1 1 0 = 0x8E; for 1111 (F) all keys released
s
;*********************************************************
m
6.6.1 Procedure
1. Create a folder “8051” in D: and open it. If it already exists, open it.
te
2. Copy the folder “05 BCD-7SEG” from the “8051 Programs” folder in the CD, to D:8051.
3. Open the folder D:805105 BCD-7SEG. Double click to open the Vision project “05
BCD-7SEG.uvproj”. The project will open in the Vision software.
created.
ys
4. Build the project. This will create the .hex file in the project folder. Verify that it is
5. Connect the male side of serial cable to serial connector (near right top corner) on MD51
kit and female side of the serial cable to COM1 of PC.
sS
6. Follow the instructions given in appendix A on page 77, and download the .hex file in
the micro-controller.
7. Observe that, the LEDs connected to P1 show the pattern 10001110. This is because;
all the keys in the leftmost column are in released state. (i.e.) the lower nibble of P2
De
is 1111 (hex F). In the look-up table (figure 6.2), the output pattern for 1111 input is
10001110.
8. Now press the lower key in the leftmost column of keyboard. The input pattern will be
0111 (E). The output pattern on the LEDs will be 10000110.
9. Now, press the four keys of the left most column with any combination, and observe
ro
the LED pattern. Remember that, when a key is pressed the level on corresponding pin
is low. Verify that the pattern on LEDs is the same as shown in look-up table in the
program.
ik
6.7 Observations
Write the observations that you made while doing this experiment.
M
6.8 Conclusions
Write conclusion based on the observations.
6.9 Homework
Implement a 3 to 8 decoder using look-up table method.
MikroDes Systems (www.mikrodes.com)
36. 34 BCD to 7-segment decoder using MOVC instruction
s
m
te
ys
sS
De
ro
ik
M
MikroDes Systems (www.mikrodes.com)
37. Experiment 7
Study of Timer0 in timer mode
7.1 Objectives
s
The objectives of this experiment are,
m
1. To understand Timer in 8051
2. To create time delay using timer
3. To display incrementing number on LEDs
te
7.2 Prerequisites
1. Understanding of 8051 architecture
2. MOV, CPL, SJMP instructions
ys
3. Experiments 1 to 4 should have been performed
sS
7.3 Apparatus
1. PC with Windows XP and Keil uVision installed.
2. 8051/89V51RD2 data sheet
3. MD51 educational trainer kit, serial cable and power adapter
De
7.4 Theory
The 8051 microcontroller has two timers; Timer0 and Timer1. The timers are implemented
in hardware; hence they can do the work of counting input clock pulses even when the micro-
ro
controller is doing some other work. When the timer is working with internal clock then it
is said to work in “Timer” mode. This mode is useful for measuring time. When the timer
is used with external clock, it is said to be in “counter” mode. This mode is useful to count
number of external events; hence the counter mode can be used to implement event counter.
ik
The timers in Intel 8051 are incrementing type. They cannot be used in decrementing mode.
There are total 4 modes of operation of timer in 8051.
M
1. Mode 0: 13 bit timer/counter
2. Mode 1: 16 bit timer/counter
3. Mode 2: 8 bit auto-reload mode
4. Mode 3: implements three 8 bit timers using two 16 bit timers
In this experiment, we are going to configure the timer0 as timer in mode 1. In this mode,
we will create a time delay of 1 second and, this time delay will be further used to show
incrementing value on port P1.
The mode 1 of timer0 is a 16 bit mode. Hence it uses TH0 and TL0 together as 16 bit
timer register. These registers are to be loaded separately as 8-bit registers.
To create a one second delay, we actually create a 50mSec delay and repeat it 20 times,
thus creating 1 sec delay. To create, the basic 50 mS delay, we need to find the count to be
loaded in the timer.
38. 36 Study of Timer0 in timer mode
The MD51 kit uses a 11.0592MHz crystal. This is internally divided by 12 and then that
frequency is fed to timer as clock. So timer gets the clock of (11.0592MHz)/12 = 0.9216 MHz
(nearly 1 MHz). At this clock, the timer must increment by 50000 x 0.9216 pulses to get delay
of 50mS. So the count comes to be 50000 x 0.9216 = 46080. We want the timer to overflow
after these many pulses. Therefore we have to load the timer by a value (FFFFh − 46080).
Higher byte of this value is to be loaded in TH0 and lower byte in TL0. Once this is done
the timer is started and the timer flag bit TF0 is continuously checked till TF0 becomes 1.
After this the timer is reloaded by (FFFFh − 46080). This is repeated 20 times. After timer
overflows 20 times, the time delay of 1 sec will be over.
s
7.4.1 Activity 1
em
Draw the block diagram of timer0 in mode 1, in figure 8.1 in your notebook.
st
Sy
es
Figure 7.1: Timer0 block diagram
From the above block diagram, we can understand that, to use the timer0 as timer in mode
1, we have to clear the C/T bit. Then we have to clear GATE bit. Also we have to set the
oD
TR0 bit.
7.4.2 Activity 2
kr
Read the program, comments and understand the program.
7.5 Program
;****************************************
Mi
; THIS PROGRAM DEMONSTRATES TIMER-0 USED AS
; TIMER: Tested on MD51 V1.0 kit
; input: No input required
; output: binary count on LEDs (P1)
; Procedure:
; Download the hex file and observe the output
; on LEDs. The LEDs show incrementing count
; upto 10 and resets
; Copyright (c) Aug 2009
MikroDes Systems (www.mikrodes.com)
39. Sect. 7.6 Procedure 37
; MikroDes Systems, Sangli
;*************************************************
ORG 0000
SJMP START
START:
MOV P1, #0FFH; ALL LEDs OFF
; 30H is used as count value to be displayed on LEDs
MOV 30H, #0
s
; Timer overflows 20 times
MOV 31H, #20
m
; Timer 0 set in mode 1
; and works as timer
te
MOV TMOD, #01; 0000 0001
MOV TL0, #LOW(0xFFFF-46080) ; count for 50mS delay
ys
MOV TH0, #HIGH(0xFFFF-46080) ; count for 50mS delay
SETB TR0
sS
BACK:
JNB TF0, $ ; wait till timer overflows
CLR TF0 ; clear the flag
; reload the timer
De
MOV TL0, #LOW(0xFFFF-46080) ; count for 50mS delay
MOV TH0, #HIGH(0xFFFF-46080) ; count for 50mS delay
DJNZ 31H, BACK ; repeat 20 times
MOV 31H, #20
ro
INC 30H
MOV A, 30H ; copy the count in ACC
CJNE A, #11, NO_CLR ; check if count > 10
ik
MOV 30H, #0 ; clear the count
CLR A
NO_CLR:
M
CPL A ; because LEDs are active low.
MOV P1, A ; display count on LEDs
SJMP BACK
END
;****************************************************
7.6 Procedure
1. Create a folder “8051” in D: and open it. If it already exists, open it.
2. Copy the folder “07 Timer0 as Timer” from the “8051 Programs” folder in the CD, to
D:8051.
MikroDes Systems (www.mikrodes.com)
40. 38 Study of Timer0 in timer mode
3. Open the folder D:805107 Timer0 as Timer. Double click to open the Vision project
“Timer0 as Timer.uvproj”. The project will open in the Vision software.
4. Build the project. This will create the .hex file in the project folder. Verify that it is
created.
5. Follow the instructions given in appendix A on page 77, and download the .hex file in
the micro-controller.
6. Observe that, the LEDs connected to P1 show incrementing binary number.
7.7 Observations
Write the observations which you made after downloading the program in MD51 kit.
s
7.8 Conclusions
m
Write conclusion based on your observations.
7.9 Homework
te
Modify the program to increase the time delay to 2 sec.
ys
sS
De
ro
ik
M
MikroDes Systems (www.mikrodes.com)
41. Experiment 8
Study of Timer0 in counter mode
8.1 Objectives
s
The objectives of this experiment are,
m
1. To understand Timer in 8051
2. To create time delay using timer
3. To display incrementing number on LEDs
te
8.2 Prerequisites
1. Understanding of 8051 architecture
2. MOV, CPL, SJMP instructions
ys
3. Experiments 1 to 4 should have been performed
sS
8.3 Apparatus
1. PC with Windows XP and Keil uVision installed.
2. 8051/89V51RD2 data sheet
3. MD51 educational trainer kit, serial cable, jumper wire and power adapter
De
8.4 Theory
In the experiment on timer as Timer, we studied that when the timer is working with internal
clock then it is said to work in Timer mode and when the timer is used with external clock, it
ro
is said to be in counter mode. This mode is useful to count number of external events; hence
the counter mode can be used to implement event counter.
In this experiment, we are going to configure the timer0 as Counter in mode 1. In this
ik
mode, we will count the external events (such as key press) and the count of number of events
occurred will be shown on port P1.
M
8.4.1 Activity 1
Draw the block diagram of timer0 in mode 1, in figure 8.1 in your notebook.
From the block diagram, we can understand that, to use the timer0 as Counter in mode 1,
¯
we have to SET the C/T bit. Then we have to clear GATE bit. Also we have to set the TR0
bit. After this we must provide the external clock (pulses) on T0 pin. (P3.4).
In MD51 kit, we can easily verify the counter operation, by using the push button switches
placed in right bottom corner. These are named as INT0 and INT1; however they can be
also used for giving pulses to timer input. For this, you need to connect the INT0 pin on the
connector to P3.4 on the 10 pin box header (on the left side of the kit.). After this connection,
if we press the INT0 key, then P3.4 will go low and if we release the INT0 key, P3.4 will go
high. You can verify this by connecting logic probe to P3.4. Thus, the INT0 key can be used
to provide the pulses (clock) to the timer (used as counter).
42. 40 Study of Timer0 in counter mode
s
em
Figure 8.1: Timer0 block diagram
st
8.4.2 Activity 2
Read the program, comments and understand the program given in section 8.5.
8.5 Program Sy
;****************************************
; THIS PROGRAM DEMONSTRATES TIMER USED AS
; COUNTER: Tested on MD51 V1.0 kit
; input: Key press (S1)(INT0)
; output: count on LEDs
es
; Procedure:
; Disconnect the DS8 dip switch and
; connect the pin 1 (int0) of X11 to P3.4
oD
; on the box header. Then press S1 (int0 SWITCH) and
; observe LEDs
; IMP: Due to switch-bouncing, there may be
; multiple increments. You can remove bouncing (de-bounce)
; with an external 33uF capacitor between
kr
; P3.4 and GND (Use SIP connector near box
; type connector and use the jumpers provided).
; Copyright (c) Aug 2009
Mi
; MikroDes Systems, Sangli
;****************************************
ORG 0000
SJMP START
START:
MOV P1, #0FFH; ALL LEDs OFF
; Timer 0 set in mode 1
; and works as counter
MOV TMOD, #05; 0000 0110
MOV TL0, #0
MOV TH0, #0
MikroDes Systems (www.mikrodes.com)
43. Sect. 8.6 Procedure 41
SETB TR0
BACK:
MOV A, TL0 ; Complement it
CPL A ; because LEDs are active low.
MOV P1, A ; copy timer value to port
SJMP BACK
END
;****************************************
s
8.6 Procedure
m
1. Create a folder “8051” in D: and open it. If it already exists, open it.
2. Copy the folder “07 Timer0 as counter” from the “8051 Programs” folder in the CD, to
D:8051.
te
3. Open the folder D:805107 Timer0 as counter. Double click to open the Vision project
“Timer0 as counter.uvproj”. The project will open in the Vision software.
4. Build the project. This will create the .hex file in the project folder. Verify that it is
created.
ys
5. Connect the INT0 pin on the connector X11 (near INT0 key in the right bottom corner
of the kit) to P3.4 on the 10 pin box header (on the left side of the kit.). After this
connection, if we press the INT0 key, then P3.4 will go low and if we release the INT0
sS
key, P3.4 will go high. You can verify this by connecting logic probe to P3.4.
6. Follow the instructions given in appendix A on page 77, and download the .hex file in
the micro-controller.
7. You will observe that all the LEDs are off. This is because, the counter value is 00000000.
Now press the key (INT0) near the right bottom corner of the kit, to give pulse input to
De
the counter. Release the key. Observe that, the LEDs connected to P1 show 00000001.
Go on pressing the key multiple times. You will observe that, incrementing binary
number. The binary number on LEDs will increment after each key press.
ro
8.7 Observations
Write the observations which you made after downloading the program in MD51 kit.
ik
8.8 Conclusions
Write conclusion based on your observations.
M
8.9 Homework
Connect the pin P3.4 to output of 555 timer on the board. Observe the output. You will
find that the count on LEDs changes very fast. Find the reason for it. Change the program
suitably to see the incrementing count on LEDs.
MikroDes Systems (www.mikrodes.com)
44. 42 Study of Timer0 in counter mode
s
m
te
ys
sS
De
ro
ik
M
MikroDes Systems (www.mikrodes.com)
45. Experiment 9
Study of LCD display
9.1 Aim
1. To study the connections of LCD module
s
2. To study the commands to control LCD module
m
3. writing a program to Display a string on LCD display
9.2 Theory
te
LCD (Liquid Crystal Display ) modules are very popular because of the follwing advantages.
1. Low cost
2. Can display alphanumeric text
3.
4.
It has built in display controller
ys
Available with 1, 2, 4, lines and 16, 20, 40 characters in a line
The MD51 kit has a on-board LCD module. This allows us to display alphanumeric characters
sS
such as numbers, text messages etc. The block diagram of it is shown on figure 9.1.
An LCD module has a display as well as display controller IC. This IC needs to be given
certain commands to use the display. The controller also has its own display RAM. The user
is expected to write into this RAM. The rest of the functions are done by the controller IC
to take the RAM data and display it on LCD. The LCD controller also supports various
De
commands such as Clear Display, Blink Cursor, Move Cursor etc. There are various types of
LCD modules.
ro
ik
M
Figure 9.1: LCD block diagram
9.3 Activity
Surf the Internet to get links to tutorials on LCD modules. Follow those links to know more
about them. Prepare a note on LCD modules available in market.
9.4 LCD pins
The LCD module available on MD51 kit is a of 16x2 type. (i.e.), it can display 16 characters
on each of the 2 lines. The display has total 14 pins for various signals and two pins(Anode-
Cathode) for the backlit LEDs. The pin description and connections of LCD are shown in
figure 9.2.