Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Page |1

University of Sindh Jamshoro


Faculty of Engineering and Technology
University of Sindh Jamshoro

EMBEDDED SYSTEM DESIGN


Lab NO.3
LAB SUBJECT DATA ABILITY TO
PERFOR OBSERVATION
KNOWLED ANALYSIS AND CONDUCT
MANCE PRESENTATION CALCULATION /RESULTS score
GE INTERPRETATION EXPERIMENT
INDICAT AND CODING
OR
1 1 1 1 1 5
Name

Name ____________________________ Roll No: _______________________ Score: __________


Signature of Tutor: _______________ Date: ______________

EdSim51 Simulator
OBJECTIVES:
Upon successful completion of this experiment, students will be able to:
 Know about the EdSim51 Simulator, Development tools for all 8051 devices
 Create a new project using edSim51
EQUIPMENTS REQUIRED:
 Personal Computer
 EdSim51 Tool
INTRODUCTION:
The EdSim51 Simulator was written in Java (http://www.java.com). To run the EdSim51 Simulator you
must first install the J2RE (Java 2 Runtime Environment). Of course, your computer may already have
the J2RE installed. To find out if you have the J2RE, or to install it if you don't, click on the following
button in the site where you get this software online:

There are three steps to ensuring your computer has the latest Java installed:
On the page that opens when you click on the above GET IT NOW button, click on the Download
Now! Arrow.
 Click on the Download button on the new page that opens. There are two options - online and offline.
They both do the same thing but online tends to be faster. For more information, click on
the Instructions link on that page.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |2

 When installation is complete, click on the Verify Installation button. This will test your computer to
see if Java was successfully installed.

EdSim51 Simulator Installation

The simulator is packaged in one file called edsim51di.zip. You can get that file using the download
button.
Keep Up-to-Date with the Latest Version of the EdSim51 Simulator

Features are sometimes added to the simulator (and the odd bug removed). You should return to this
page periodically to see if a newer version is available. To get the newer version simply follow the
same download and install procedure (as described below) and replace the old simulator with the new.
Save the zip file anywhere on your computer.
Unzip

Once you have downloaded the zip file, extract it. On most operating systems, you can do this by
double-clicking the zip file.

Edsim51di.zip contains a folder called lib and a JAR file called edsim51di.jar.
lib and edsim51di.jar must be kept together in the same location.

Launching the EdSim51 Simulator

Note: You cannot launch the simulator from within the zip file. If you try that, you will probably get
a Java Exception error.

 Windows: To launch the simulator, double-click on edsim51di.jar.


 Launching: From then on, double-clicking on edsim51di.jar will launch the simulator.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |3

Introduction:

A virtual 8051 is interfaced with virtual peripherals such as a keypad,


motor, display, UART, etc. The student can write 8051 assembly code, step through the code and
observe the effects each line has on the internal memory and the external peripherals.

EdSim51

 The top left box gives the user access to all the 8051's registers, data memory and code
memory.
 In the centre is a textbox where the user either loads an assembly program or writes the
code directly. Shown above is a program being single-stepped (execution is currently at
location 0034H in code memory - hence that line is highlighted).
 On the right is a list of the 32 port pins and what each one is connected to. The current
value of the port pin is displayed here.
 The bottom panel shows all the peripherals that are connected to the 8051.

The Virtual Peripherals:

 Analogue-to-Digital Converter (ADC)


 Comparator
 UART
 4 Multiplexed 7-segment Displays

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |4

 4 X 3 Keypad
 8 LEDs
 DC Motor
 8 Switches
 Digital-to-Analogue Converter (DAC) - displayed on oscilloscope

EdSim51's Simulator and not some of the many other simulators that are available

Many of the simulators for the 8051 that you will find are industry-standard. Professional 8051-based
embedded systems designers use them. While they show the state of the registers, memory and the
port pins while code is being debugged, they do not have graphical representations of peripherals that
can be used interactively to communicate with the 8051. EdSim51 have filled that need. The student
can learn how to scan a keypad, multiplex 7-segment displays, control a motor and count its
revolutions, etc. this simulator will help students learn to write programs to exercise the 8051.
Students can begin by ignoring the peripherals. In this way they can first become accustomed to the
many different move, arithmetic and branch instructions that make up the 8051 instruction set. As the
students gain experience and confidence they can then write code to scan a keypad, count the motor's
revolutions, or multiplex 7-segment displays, etc.
Some of the peripherals share the same port pins. For example, the 7-segment displays' data lines and
the DAC are connected to port 1. This means that, if the student wishes to use the display and the
DAC together, he/she must learn how to disable one to access the other. Or the peripherals can be
moved - ie: connected to other port pins.

Syntax Highlighting

Now, assembly code written in EdSim51 is automatically syntax highlighted. Instructions are coloured
blue, assembler directives (such as ORG, USING, etc) are coloured purple, aliases (for example, the
assembler replaces TMOD with the address of TMOD) are coloured orange and comments are
coloured green.

Switching off syntax highlighting: if you wish to turn off syntax highlighting, simply right-click
anywhere in the assembly code window and respond to the prompt.

Change the System Clock Frequency

Originally, the simulator ran with a system clock frequency of 12


MHz. Now the user can enter a value for the system clock frequency in MHz.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |5

UART Transmitting HEX Data

Up until now, the external UART only transmitted text - whatever the user typed in the Tx field was
transmitted to the 8051. Now, a list of 8-bit numbers (written in HEX) can be transmitted. To do so,
the user encloses the list in curly braces, each number separated by a comma, as shown in the image
opposite. When text is transmitted, it is terminated by 0DH. This is not the case with a list of numbers.
In the example shown opposite, the four numbers 56, 3a, 23 and e7 are transmitted, nothing more. If
the user wishes to send {56, 3a, 23, e7} as text rather than a list of 8-bit numbers, the text is escaped
using the \ character. Therefore, \{56, 3a, 23, e7} in the Tx field would result in {56, 3a, 23, e7}
followed by 0DH being transmitted to the 8051.

LCD Module

A simulation of the popular Hitachi HD44780 LCD module


has been implemented for the EdSim51 Simulator.
Zoom

For high resolution monitors, click on the zoom button. The zoom button is located
below the red Exit button.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |6

Keypad Modes

Now the user can select from three modes of operation:

 Standard - any number of keys can be


closed at the same time.
 Pulse - once the mouse button is released
the key reopens.
 Radio - in radio mode only one key at a
time can be closed.

Keypad And External 1 Interrupt

Until now the keypad could only be implemented using


busy-waiting. It can still be implemented in that
manner, but it can also be used together with the 8051
external 1 interrupt pin, P3.3.

Note: this pin, P3.3, is also used by the display-select decoder, therefore multiplexing of the 7-
segment displays cannot be implemented together with the keypad interrupt. To multiplex the displays
and use the keypad at the same time, the keypad must be implemented using busy waiting.

Intel HEX Reader/Writer

You can now save your source code in Intel HEX format. Or you can write C code for the 8051 using
one of many available 8051 C compilers, then import the HEX code into the EdSim51 Simulator.

Simulator Window Update

As with many microcontroller simulators, EdSim51 allows the user to either step through a program,
executing a single instruction per step, or to run the program continuously. In the original EdSim51
design, when running a program, the simulator would execute one instruction, update the screen, pause
for a quarter second, then proceed with the next instruction and so on. This allowed the programmer to
observe changes in the hardware and registers for each executed instruction. However, while this is
very useful for debugging, it meant the user would need to wait a long time for things to happen
(examples: data transmitted on the serial port to appear on the UART, an LED flashing at half second
intervals, etc).

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |7

The best of both: now the user has the choice. Using the Update
Freq. drop down menu (shown opposite) the programmer can choose how
often the screen should be updated. The choices are: update the screen
after every instruction execution (the default), after 100, 1000, 10,000 or
50,000 executed instructions.

Type in a value: the options available from the menu may not be
appropriate. Therefore, the user can now enter a number rather than select
a value from the list. For the entered number to take effect, the user must
hit Enter on the computer keyboard.

The Microcontroller Panel

A screenshot of the microcontroller panel: This gives the user access to all the 8051's registers and
data memory. Boxes that are white can be edited directly. Those that are grey cannot. For example, the
port latch bits can be edited directly by the user, but the port pins are controlled by the external
peripherals and the port latches and cannot be edited.
When the mouse pointer is left to hover over one of the register labels, the register's
address appears, as shown opposite for the PCON register.

The Bitfield

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |8

In the above image, the individual bits for the accumulator are shown (ACC). The user can enter any
address or SFR name in the blue box (replacing ACC) and the bits for that given address will then be
displayed. Also, if you let the mouse pointer hover over one of the bits momentarily, the bit's
description is displayed, as shown below:

The bit field for the TMOD SFR is shown. Notice bit 2 is
the counter/timer bit. Also notice the background is grey. This is because the TMOD register is not bit-
addressable - the user cannot alter these bits directly.

The PSW is bit-addressable, therefore the background of each bit is


white and the user can change any of the bits directly.

The bitfield can be used to see the bit pattern of any address in RAM (0
to 7FH) by typing the address in the blue box. If the location is bit-addressable, the bit backgrounds
are white and the user can alter any of the eight bits.
As with SFRs, if the location is not bit-addressable the backgrounds are
grey.

Data and Code Memory

By default, data memory is displayed. Any address in RAM (00H to 7FH) can be altered by entering
the address in the blue box (labelled addr) and then entering the desired value in the box to the right
(labelled value).

Displaying Code Memory

Code memory can also be examined and edited, as shown in the image below. To switch between data
memory and code memory the user clicks on the button that is labelled Data Memory when data
memory is displayed and Code Memory when code memory is displayed.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
Page |9

The first 127 bytes of code memory are displayed. To view another area of code memory, enter the
start address in the blue box. The 127 bytes from the start address onwards will then be displayed.
Again, like data memory, the address specified in the blue addr box can be altered by entering a value
in the value box. However, it should be noted that this will result in the machine code and the
assembly program being different. In the image above, the assembly code that generated the machine
code (as displayed in code memory) can be seen on the right.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 10

The Assembly Code Panel


A simple assembly program is shown in the
assembly code panel to the left. This program runs
in a continuous loop, displaying the numbers 0 to 9
and back to 0 on the first 7-segment display. A
snapshot of its execution is shown to the right.

When the background of the assembly code text


area is white is it editable. The programmer can
write code directly here, or can load a program
from file using the Load button (dealt with in the
next section). When the program is ready for
testing, the user can either click on
the Assm button to execute instructions one at a
time, or on the Run button to run the program
continuously. Either way, the program will first be
assembled. If an error in the code is discovered, a
message is displayed in the message box above the
assembly code (with a red background) and the
line with the error is highlighted within the code in red.
If the code assembles without errors, Assm is replaced by Step, the text area's background changes to
light grey. The code cannot be edited at this point. If you want to go back to editing your code, simply
click on the Reset button. Source code window a bit small? Read about the source pane unlocker
below

Source Pane Unlocker

The simulator was designed so that everything is visible on the screen at the same time - the internal
registers, the source code, the peripherals, etc. However, this means the code pane is quite small. But it
can be expanded.
Load and Save

The user can write code directly into the text box when it is in edit mode, or an existing program can
be loaded from a file using the Load button. Similarly, code in the text box can be saved to file using
the Save button.
There are two file types handled. The first is plain-text. Assembly programs are saved as regular
plain-text files (often called text-only) and usually with .a or .asm file extension. By default, this is the
format used when saving your source code in the EdSim51 Simulator. The other file type is Intel
HEX. The user can select to save a file in HEX format by choosing Intel HEX

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 11

Files from the


Files of Type: menu in the Save dialog box.

To make the simulator more user-friendly, the last directory accessed (either through loading a file or
saving a file) will be remembered. Therefore, the next time the user opens a file dialog box by clicking
on either the Load or Savebuttons, the dialog box automatically opens in the last directory visited.
Copy and Paste

You can select code in the assembly text area and copy it to the system clipboard using
the Copy button, the same as you do in your word processing package. This can then be pasted
elsewhere in the assembly text area, using the Paste button (if the text area is editable - white
background - if it is not, click Reset). Or you can paste the selected text into some other application
(such as your word processor). Similarly, you can copy text from another application and paste it into
the assembly text area.

A Few Notes on the Assembler

The 2-pass assembler with the EdSim51 Simulator is not a full-blown assembler. It does not link
multiple files and only some of the directives you might expect are implemented. However, I feel it is
adequate for the beginner. Below is a list of its features:

 All of the 8051 instructions are implemented, except for MOVX instructions, as the
simulator does not handle external memory.
 JMP rel equates to either SJMP rel or AJMP rel. LJMP rel must be programmed explicitly.
 Similarly, CALL equates to ACALL. LCALL must be programmed explicitly.
 SET and EQU directives are implemented.
 ORG is implemented.
 USING directive (states which register bank is being used) is implemented.
 ARn equates to the register address, as specified by USING (if the register bank is not
specified prior to ARn's use, register bank 0 is assumed).
 SFR names and SFR bit names equate to the appropriate address.
 HIGH followed by an operand in brackets equates to the high byte of the operand.
 LOW followed by an operand in brackets equates to the low byte of the operand.
 Labels are followed by a colon.
 The default for numerical values is decimal. Hex values can be entered by
appending H after the number, or placing 0x before it. If H is used, the number cannot
begin with a letter (example: F5H must be written as 0F5H). Binary values are entered by
appending B after the number (as shown in the image below).
 The assembler is not case-sensitive.

Debugging
Regardless of whether the code is running or being stepped, once the code assembles without errors,
the address of each instruction is displayed to the left.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 12

When stepping through the code, the instruction that was just executed is shown in the grey box at the
top, together with the instruction's address
(example shown: Executed 0x2A: MOV 90H,
A). The next instruction to be executed, its
address is highlighted. (002CH).

Breakpoints

A breakpoint can be set by double-clicking the


instruction's address, as shown in the images
below.

Setting a breakpoint:

When stepping through the code, move the mouse over the address of the instruction and double-click.
When the breakpoint is set the vertical bar (|) immediately to the right of the address is replaced by a
star (*), as shown in the image below.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 13

Removing a breakpoint:

A breakpoint is indicated by a star (*). To remove the breakpoint, move the mouse over the
instruction's address and double-click. The * is replaced by |. Alternatively, you can remove all
breakpoints with one click of: . When the program is running and a
breakpoint is encountered execution halts just before that instruction. In other words, the next
instruction to be executed will be the breakpoint instruction. The programmer can then step through
the code, or run the program, from that point.
The Peripherals
Note: The logic diagrams in this section were drawn for the default peripheral interface. You may
wish to alter this interface, which you can do by clicking on the DI button (see the image below). You
can also view the logic diagram for the new interface by clicking on the LD button (again, see the
image below). Also, there are instances in the following section where the sharing of port pins is
pointed out. For example, the motor and the UART share the same port pins. This of course may not
be true in your case, if, for example, you move the motor to other pins.

The Dynamic Interface Panel - remap the peripherals

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 14

An Instance of the Dynamic Logic Diagram


The Peripherals:

 ADC
 Comparator
 Four 7-segment LED Displays
 LCD Module
 UART
 Keypad
 LED Bank
 Bi-directional Motor
 Switch Bank
 DAC (output displayed on oscilloscope)

These image on the left shows what is connected to each of the 32 port pins. If a pin's description is
too long to fit, you can see the full description by hovering the mouse over it, or by clicking on the
zoom button (see below).

Zoom (Port Pin Connections)

Click on one of the buttons marked + to display that port's connections in a separate window. An
example for port 2 is shown below.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 15

The logic diagram below gives details of the peripheral interfacing.

EdSim51 Simulator Peripheral Logic Diagram

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 16

The LED Bank, DAC and the 7-segment Displays

As can be seen in the diagram below, the LED bank, the DAC inputs and the 7-segment display data
lines all share port 1. The selection of which of the four displays is enabled is done via P3.3 and P3.4.
These port pins are applied to a 2-to-4 line decoder, the outputs of which are applied to the base of
transistors that enable/disable the displays.

Logic diagram showing the LED bank, DAC and 7-segment display connections only.

The decoder is enabled via a logic 1 on P0.7. This pin is also applied to the DAC WR input, which is
active-low. Therefore, to write data to the DAC the programmer disables the displays, which also has
the effect of enabling the DAC's data lines. Data can then be written to the DAC. Whatever is on the
inputs when the WR line is taken low is stored in (or written to) the DAC's internal register and
remains there after the WR line is taken high. The analogue output for this data will be displayed on
the scope until the next time WR is taken low and a new value is placed in the internal register.

The keypad can be implemented using the external interrupt 1 line (see keypad), but not while also
multiplexing the 7-segment displays. This is because the external interrupt 1 line (P3.3) is also used
by the display decoder (see logic diagram above). Therefore, keypad interrupt and multiplexing the 7-
segment display cannot be used at the same time - the keypad AND gate should be disabled.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 17

An example of the DAC in action is shown below:

Code for Ramp Generation

LED Bank and the


7-segment Displays Panel

Ramp Generated via DAC Appears on Scope

The code that generated the above ramp is also shown (above middle). The DAC's WR input is taken
low permanently (CLR P0.7) while the value in the accumulator is sent to the DAC's inputs,
increased by 8 and then sent to the inputs again, and so on.

When the mouse is placed over the scope screen, a tooltip appears, stating the scope's vertical scale is
one volt per division and the horizontal scale is, in this case, 25 microseconds per division. The
scope's horizontal scale is tied in with the 8051 system clock frequency (this is unrealistic, but I feel
it's adequate for simulation purposes). If, for example, the system clock frequency is changed to
11.059 MHz, then the scope's horizontal scale is 17.36 microseconds per division.

The LCD Module

4-bit Mode: The LCD module is a simulation of the Hitachi


HD44780 and is interfaced to the 8051 in 4-bit mode. P1.7 through P1.4 are connected to DB7
through DB4, while P1.3 is connected to the register-select pin and P1.2 is connected to the enable
pin. Notice the read-write pin is connected to ground - the module can only be written to.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 18

Logic diagram showing the LED bank, DAC and LCD module connections only.

Switch Bank and the ADC Panel

The outputs of the ADC are tri-state: the RD line, which is connected to P3.7, must be low for the
ADC reading to appear on the outputs.
The WR line (connected to P3.6) is used to initiate a conversion. As it is positive edge triggered, it
must be taken low and then high to start a conversion. When the conversion is complete the INTR line
goes low (and remains low until another conversion is initiated). This line is applied to the external 0
interrupt line, INT0. In this way, the 8051 can be interrupted by the completion of an ADC conversion.
Note the tip that appears when the mouse is placed over the ADC button. Clicking on this button
disables the ADC and enabled the comparator. See the Comparator for more information.
The function of the ADC pins are summarised in the following table:

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 19

The Comparator and the DAC

Many lecturers like to get their students to develop their own ADC using a DAC and a comparator. To
meet this need, the output of the DAC is also applied to the inverting pin of a comparator, as shown in
the logic diagram extract below. The analogue input that is applied to the ADC is also applied to the
non-inverting pin of the comparator, as can be seen above (in this extract,
since the ADC is disabled, the analogue voltage connection to the ADC is
omitted). When the comparator is enabled, the button's label
says Comparator, as can be seen in the image opposite. Hovering over
the button displays a tip - click the button to disable the comparator and
enable the ADC. The button corresponds to the switch at the ADC chip
select and the switch between the comparator output and P3.7 in the logic
diagram extract above. You can see the switch between the comparator
output and P3.7 is closed while at the same time the ADC CS line is
switched to +V, disabling the ADC. When the comparator is enabled it can
then be used together with the DAC to implement an ADC. There are
many techniques that can be used.
Learn about comparators and DACs used as ADCs (An external site that is not affiliated with
EdSim51.)
P3.0 and P3.1 are applied to a dual bridge
driver, the outputs of which are applied to
a bi-directional DC motor. The truth table
for the bridge and its effect on the motor
is:
Motor Panel
In the image on the left, the motor's shaft

is in the default position (pointing at 3 o'clock). Notice

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 20

the sensor (depicted by a vertical line at the top of the motor) is black. When the motor's shaft lines up
with the sensor, the sensor changes from black to red, as shown in the image on the right.
The motor sensor, which is applied to P3.5, goes low once every revolution (in the simulator,
whenever the motor shaft lines up with the sensor, the sensor changes from black to red and P3.5 goes
to logic 0). P3.5 is the external clock source for timer 1. Therefore, code can be written that, using
timer 1, counts the motor's revolutions. The speed of the motor can be varied manually (using the
slider to the right of the motor - take a look at the hardware screenshots above). This will make the
rev. counting programs more interesting. Note: The motor control lines share the TXD and RXD lines
for the 8051's internal serial port. As can be seen in the logic diagram extract above, these lines are
also connected to the external UART. Therefore, when exercising the motor, garbage messages may
appear in the UART's receiver window.

PROGRAM-1

Multiplexing the 7-segment Displays - logic diagram extract

Notes on Multiplexing

<- get the source code


; This program multiplexes the number 1234
; on the four 7-segment displays.

; Note: a logic 0 lights a display segment.

start:
SETB P3.3 ; |
SETB P3.4 ; | enable display 3
MOV P1, #11111001B ; put pattern for 1 on display
CALL delay
CLR P3.3 ; enable display 2
MOV P1, #10100100B ; put pattern for 2 on display
CALL delay
CLR P3.4 ; |
SETB P3.3 ; | enable display 1
MOV P1, #10110000B ; put pattern for 3 on display
CALL delay
CLR P3.3 ; enable display 0
MOV P1, #10011001B ; put pattern for 4 on display
CALL delay
JMP start ; jump back to start

; a crude delay
delay:
MOV R0, #200
DJNZ R0, $
RET

When running this program, best viewed with Update Freq. set to 100.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 21

Fig3.1 Display 1 Fig3.2 Display 2

Fig3.3 Display 3 Fig3.4 Display 4

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 22

PROGRAM-2

LCD Module Assembly Program Example


The example below sends the text ABC to the display.
<- get the source code
; put data in RAM
MOV 30H, #'A'
MOV 31H, #'B'
MOV 32H, #'C'
MOV 33H, #0 ; end of data marker

; initialise the display


; see instruction set for details

CLR P1.3 ; clear RS - indicates that instructions are being sent to the
module

; function set
CLR P1.7 ;|
CLR P1.6 ;|
SETB P1.5 ;|
CLR P1.4 ; | high nibble set

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E

CALL delay ; wait for BF to clear


; function set sent for first time - tells module to go into 4-
bit mode
; Why is function set high nibble sent twice? See 4-bit operation on pages 39 and 42 of
HD44780.pdf.

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
; same function set high nibble sent a second time

SETB P1.7 ; low nibble set (only P1.7 needed to be changed)

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E
; function set low nibble sent
CALL delay ; wait for BF to clear

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 23

; entry mode set


; set to increment with no shift
CLR P1.7 ;|
CLR P1.6 ;|
CLR P1.5 ;|
CLR P1.4 ; | high nibble set

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E

SETB P1.6 ;|
SETB P1.5 ; |low nibble set

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E

CALL delay ; wait for BF to clear

; display on/off control


; the display is turned on, the cursor is turned on and blinking is turned on
CLR P1.7 ;|
CLR P1.6 ;|
CLR P1.5 ;|
CLR P1.4 ; | high nibble set

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E

SETB P1.7 ;|
SETB P1.6 ;|
SETB P1.5 ;|
SETB P1.4 ; | low nibble set

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E

CALL delay ; wait for BF to clear

; send data
SETB P1.3 ; clear RS - indicates that data is being sent to module
MOV R1, #30H ; data to be sent to LCD is stored in 8051 RAM, starting
at location 30H
loop:

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 24

MOV A, @R1 ; move data pointed to by R1 to A


JZ finish ; if A is 0, then end of data has been reached - jump out of loop
CALL sendCharacter ; send data in A to LCD module
INC R1 ; point to next piece of data
JMP loop ; repeat

finish:
JMP $

sendCharacter:
MOV C, ACC.7 ;|
MOV P1.7, C ;|
MOV C, ACC.6 ;|
MOV P1.6, C ;|
MOV C, ACC.5 ;|
MOV P1.5, C ;|
MOV C, ACC.4 ;|
MOV P1.4, C ; | high nibble set

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E

MOV C, ACC.3 ;|
MOV P1.7, C ;|
MOV C, ACC.2 ;|
MOV P1.6, C ;|
MOV C, ACC.1 ;|
MOV P1.5, C ;|
MOV C, ACC.0 ;|
MOV P1.4, C ; | low nibble set

SETB P1.2 ;|
CLR P1.2 ; | negative edge on E

CALL delay ; wait for BF to clear

delay:
MOV R0, #50
DJNZ R0, $
RET

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 25

Fig 3.5 Displaying ABC on KEYBOARD


PROGRAM-3
Ramp Signal on the DAC Output - logic diagram extract
<- get the source code
; This program generates a ramp on the DAC
; output.

; You can try adding values other than 8


; to the accumulator to see what this does
; to the ramp signal.

CLR P0.7 ; enable the DAC WR line


loop:
MOV P1, A ; move data in the accumulator to the ADC inputs (on P1)
ADD A, #4 ; increase accumulator by 4
JMP loop ; jump back to loop

When running this program, best viewed with Update Freq. set to 1.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 26

Fig 3.6 Ramp Signal

PROGRAM-4
Scanning the Keypad - logic diagram extract

Notes on Keypads

<- get the source code

The following program shows how to scan the keypad. The program halts (to be precise, sits in an
endless loop) once a key is pressed.
; This program scans the keypad.

; While no key is pressed the program


; scans row0, row1, row2, row3 and back to
; row0, continuously.

; When a key is pressed the key number


; is placed in R0.

; For this program, the keys are numbered


; as:

; +----+----+----+
; | 11 | 10 | 9 | row3

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 27

; +----+----+----+
; | 8 | 7 | 6 | row2
; +----+----|----+
; | 5 | 4 | 3 | row1
; +----+----+----+
; | 2 | 1 | 0 | row0
; +----+----+----+
; col2 col1 col0

; The pressed key number will be stored in


; R0. Therefore, R0 is initially cleared.
; Each key is scanned, and if it is not
; pressed R0 is incremented. In that way,
; when the pressed key is found, R0 will
; contain the key's number.

; The general purpose flag, F0, is used


; by the column-scan subroutine to indicate
; whether or not a pressed key was found
; in that column.
; If, after returning from colScan, F0 is
; set, this means the key was found.

start:

MOV R0, #0 ; clear R0 - the first key is key0

; scan row0
SETB P0.3 ; set row3
CLR P0.0 ; clear row0
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program
; | (because the pressed key was found and its number is in R0)

; scan row1
SETB P0.0 ; set row0
CLR P0.1 ; clear row1
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program
; | (because the pressed key was found and its number is in R0)

; scan row2
SETB P0.1 ; set row1
CLR P0.2 ; clear row2
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 28

; | (because the pressed key was found and its number is in R0)

; scan row3
SETB P0.2 ; set row2
CLR P0.3 ; clear row3
CALL colScan ; call column-scan subroutine
JB F0, finish ; | if F0 is set, jump to end of program
; | (because the pressed key was found and its number is in R0)

JMP start ; | go back to scan row 0


; | (this is why row3 is set at the start of the program
; | - when the program jumps back to start, row3 has just been
scanned)

finish:
JMP $ ; program execution arrives here when key is found - do nothing

; column-scan subroutine
colScan:
JNB P0.4, gotKey ; if col0 is cleared - key found
INC R0 ; otherwise move to next key
JNB P0.5, gotKey ; if col1 is cleared - key found
INC R0 ; otherwise move to next key
JNB P0.6, gotKey ; if col2 is cleared - key found
INC R0 ; otherwise move to next key
RET ; return from subroutine - key not found
gotKey:
SETB F0 ; key found - set F0
RET ; and return from subroutine

It may appear as if this program does nothing. Remember, the program simply scans the keypad
until a key is pressed. It then places the key number in R0 and enters an endless loop, doing nothing.
Therefore, to see that it has performed the task correctly, examine the contents of R0 after a key is
pressed. Also remember that, if the update frequency is set to 1, it will take a short amount of time
for they pressed key's number to appear in R0.

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 29

Fig3.7 LCD displaying R0 Value

PROGRAM-6

10. The Motor - logic diagram extract

Notes on DC Motors

<- get the source code


; This program exercises the motor.
; The motor is rotated in a clockwise
; direction and the number of revolutions
; is displayed on Display 0 (the 7-segment
; display). The display only shows up to
; nine revolutions and then resets.

; The motor sensor is connected to P3.5,


; which is the external clock source for
; timer 1. Therefore, timer 1 is put into

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 30

; event counting mode. In this way, the


; the timer increments once every motor
; revolution.

; The value in timer 1 low byte is moved


; to A and this value together with the
; data pointer (DPH and DPL) are used to
; get the 7-segment code from program memory.
; The code is then sent to P1 to put the
; appropriate number on the Display 0.

; The motor can be changed from clockwise


; to anti-clockwise by pressing SW0 (on P2.0).
; The motor direction is stored in F0 (1 for
; clockwise, 0 for anti-clockwise). The
; value in F0 is sent to Display 0's decimal
; point (P1.7). This indicates the motor's
; direction - if the decimal point is lit,
; the motor is rotating anti-clockwise, while
; if it is not lit the motor is rotating
; clockwise.

; The value in F0 is compared with the


; value of SW0. If they are the same the
; motor direction does not need to be
; changed. If they are not the same it means
; the user has pressed SW0 and the motor
; direction must be reversed. When this
; happens the new motor direction is then
; stored in F0.

MOV TMOD, #50H ; put timer 1 in event counting mode


SETB TR1 ; start timer 1

; | 7-segment code table into DPL

CLR P3.4 ;|
CLR P3.3 ; | enable Display 0
again:
CALL setDirection ; set the motor's direction

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 31

MOV A, TL1 ; move timer 1 low byte to A


CJNE A, #10, skip ; if the number of revolutions is not 10 skip next instruction
CALL clearTimer ; if the number of revolutions is 10, reset timer 1
skip:
MOVC A, @A+DPTR ; | get 7-segment code from code table - the
index into the table is
; | decided by the value in A
; | (example: the data pointer points to the start of the
; | table - if there are two revolutions, then A will contain
two,
; | therefore the second code in the table will be copied to
A)

MOV C, F0 ; move motor direction value to the carry


MOV ACC.7, C ; and from there to ACC.7 (this will ensure Display
0's decimal point
; will indicate the motor's direction)

MOV P1, A ; | move (7-seg code for) number of revolutions and motor
direction
; | indicator to Display 0

JMP again ; do it all again

setDirection:
PUSH ACC ; save value of A on stack
PUSH 20H ; save value of location 20H (first bit-addressable
; location in RAM) on stack
CLR A ; clear A
MOV 20H, #0 ; clear location 20H
MOV C, P2.0 ; put SW0 value in carry
MOV ACC.0, C ; then move to ACC.0
MOV C, F0 ; move current motor direction in carry
MOV 0, C ; and move to LSB of location 20H (which has bit address
0)

CJNE A, 20H, changeDir ; | compare SW0 (LSB of A) with F0 (LSB of 20H)


; | - if they are not the same, the motor's direction needs to
be reversed

JMP finish ; if they are the same, motor's direction does not need to be
changed
changeDir:
CLR P3.0 ;|
CLR P3.1 ; | stop motor

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq
P a g e | 32

CALL clearTimer ; reset timer 1 (revolution count restarts when motor


direction changes)
MOV C, P2.0 ; move SW0 value to carry
MOV F0, C ; and then to F0 - this is the new motor direction
MOV P3.0, C ; move SW0 value (in carry) to motor control bit 1
CPL C ; invert the carry

MOV P3.1, C ; | and move it to motor control bit 0 (it will therefore have
the opposite
; | value to control bit 1 and the motor will start
; | again in the new direction)
finish:
POP 20H ; get original value for location 20H from the stack
POP ACC ; get original value for A from the stack
RET ; return from subroutine

clearTimer:
CLR A ; reset revolution count in A to zero
CLR TR1 ; stop timer 1
MOV TL1, #0 ; reset timer 1 low byte to zero
SETB TR1 ; start timer 1
RET ; return from subroutine

Fig3.8 Motor Rotation

EMBEDDED SYSTEM DESIGN


Dr. Zain Anwar Engr. Fozia Attiq

You might also like