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

HIGHER COLLEGE OF TECHNOLOGY

DEPARTMENT OF ENGINEERING
ELECTRICAL POWER, BIOMEDICAL,
COMPUTER AND TELECOMMUNICATION
ENGINEERING

HANDOUTS FOR THE COURSE:

MICROPROCESSORS SYSTEMS AND


INTERFACING (EECP3171)

Prepared by: FALAH H. JAWAD

1 | Page

Chapter One
Introduction to Microprocessors, Microprocessor-Based
Systems, Microcomputers, and Microcontrollers
1.1 THE MICROPROCESSOR: [1]
The microprocessor is a semiconductor digital integrated circuit. It has
the same structure of a Central Processing Unit (CPU) for a computer. It
consists of a number of digital computing circuits (which constitute the
Arithmetic Logic Unit ALU), a number of control and timing circuits
(which constitute the Control Unit) as well as a number of storage
elements (called the registers).
This structure enables the microprocessor to perform the same
computing and control functions of a CPU. The microprocessor can
execute programs and can control most of the parts of a computer. Any
program consists of a number of microprocessor instructions which are
stored as binary codes (called machine language). These instructions
enable the microprocessor to do different arithmetic, logical, dataprocessing, compute and control operations. Also, the microprocessor
can interact with and control external devices that are connected with
it.
Usually the microprocessor is connected to a main external memory,
external I/O interfaces (ports) and devices, a clock generator circuit and
a dc supply to form a microprocessor-based system.

1.2 THE MICROPROCESSOR-BASED SYSTEM: [2]


A microprocessor-based system is a system in which a microprocessor is
used as the main part that does most operations of the system through
the programs that it is executing.
Most old systems were hardware-based systems in which hardware
parts and circuits are designed and used to perform the functions of the
system. Any changes or additions to these functions require changing or
adding more hardware to the system. So, hardware-based systems are
complex and less flexible systems.
On the contrast, a microprocessor-based system is a much more flexible
system, where one hardware is used to perform a large number of
functions just by executing different software. Also microprocessor-

2 | Page

based systems are less expensive, much reliable, easily maintained,


consume low power and usually have small sizes.
Now, microprocessor-based systems are used everywhere for many
applications in all areas. Some areas and applications of these systems
are:
a. Computers: All CPUs of current computers (desktops, servers and
embedded computers) are microprocessors. Also, computer hardware
parts like printers, scanners, network switches ..etc are controlled
by microprocessors or microcontrollers.
b. Telecommunications: Mobiles, digital telephones and exchanges,
networks devices, satellites .etc.
c. Medical Equipment: ECG, EEG, Patient Monitoring Systems, Sonar
Systems, Medical Lab Systems.etc.
d. Electrical Power Systems: Power stations control, machine drives,
power protection and control systems, UPSs (Uninterruptable
Power Supplies) etc.
e. Military and Defense Systems: Guided missiles, radars, .etc.
f. Control: From simple systems like microwaves, TVs, washing
machines to complex systems like robots and factory control.
g. Consumer and Entertainment: Toys, Play stations .etc.
h. Office Automation, Publications and Media: Photocopy
machines, printers, publication and media equipment ..etc.
i. Instrumentation: Function generators, Spectrum Analyzers, Logic
Analyzers, .etc.

1.3 THE FEATURES OF DIFFERENT MICROPROCESSORS: [2]


Since Intel introduced its first 4-bit microprocessor (Intel 4004) in 1970,
there have been great advances in microprocessors. Now, hundreds
types of microprocessors are manufactured by different manufacturers
with different capabilities and features. Some of the features that
differentiate types of microprocessors are:
a. The Instruction Set Architecture (ISA) of the microprocessor. It is the
actual programmer visible instruction set, addressing modes, size of
operands, I/O mechanism and types of operations performed by the
microprocessor.
b. The class of the ISA. Microprocessors are now designed under two
classes of ISAs. These are CISC (Complex Instruction Set Computer)
and RISC (Reduced Instruction Set Computer) architectures.
c. The microprocessor organization (Microarchitecture). It is how the
hardware implements the ISA. This includes the logic design of the
3 | Page

microprocessor, how many cores (CPUs) the microprocessor chip


contains, its functional units, the size and design of cache memory
and physical registers inside the microprocessor, as well as the
number and design of pipelines inside the microprocessors.
d. The processor word length. It is the maximum length of data word
that internally the microprocessor can process at the same time (8bit, 16-bit, 32-bit or 64-bit). It is usually reflected by the design of its
ALUs and by the size of its internal registers.
e. The size of the external main memory that can be connected with the
microprocessor (1Mbyte, 100Mbyte, 1Gbyte, 4Gbyte, 64Gbyte). Also,
the number of I/O ports (interfaces) that can be connected with the
microprocessor.
f. The maximum frequency of the clock that drives the microprocessor
(10MHz, 20MHz, 500MHz, 4GHz).
g. The dc voltage needed to operate the microprocessor and its
consumed power.
Some of manufactures of microprocessors with some types are:
a.
b.
c.
d.
e.
f.

Intel. Like Pentium 4, Core i3, Core i5, Core i7, Xeon, Itanium 2.
AMD. Like Phenom, Opteron.
Motorola. Like MC6800, MC68020.
IBM. Like PowerPC, POWER 5.
Hitachi. Like SuperH.
ARM. Like SC100, Cortex-A.

1.4 CASE STUDY OF A MICROPROCESSOR-BASED SYSTEM: THE


MICROCOMPUTER: [2]
The microcomputer is a computer which its CPU is a microprocessor. The
basic parts of any microcomputer are shown by the block diagram in
figure 1.1.

4 | Page

Figure 1.1 The Basic Architecture of a Microcomputer.


As well as the microprocessor, the microcomputer has a dc supply, a
clock generator circuit, a main external memory, a number of
input/output devices with their interfaces. These parts are connected
together by a system of three busses: The address, data and control
busses. We will explain the specifications and functions of these parts.

1.4.1

The CPU (The Microprocessor): [2]

The microprocessor is considered as the CPU of the microcomputer


which performs the following main functions:
a. Fetching program instructions one after another from memory then
executing them and doing the arithmetic, logical, data-processing and
other operations as specified by these instructions.
b. Controlling almost all the parts of the microcomputer system.
As we have mentioned, the internal structure of any microprocessor
includes the following basic parts:
a. A number of control and timing circuits which constitute the Control
Unit. This unit decodes the binary codes of instructions and gives the
control signals needed to execute these instructions by the
microprocessor internal parts. This unit also gives control signals to
external parts.
5 | Page

b. A number of digital computing circuits which constitute the


Arithmetic Logic Unit ALU. In ALU the microprocessor performs
arithmetic, logical and other data processing operations.
c. A number of storage elements (called the registers). Registers
represent the working space (temporary storage) in the
microprocessor. The number and functions of registers vary between
different microprocessor designs.
The external lines of any microprocessor are classified to: DC supply
input lines, Ground lines, a clock input line (or direct crystal connection
lines), an address bus (output), a data bus (input/output) and a control
bus (input/output) as shown in figure 1.2.

Figure 1.2 The Classification of the Microprocessor External Lines.

1.4.2

The DC Supply: [2]

It is used to supply DC voltages to the microprocessor and other parts of


the microcomputer like +5v, -5V, +12v & -12v depending on their
specifications.

1.4.3

The Clock Generator Circuit: [2]

It supplies the microprocessor with the clock which synchronizes all its
internal and external operations. Without a clock, the microprocessor
will remain at halt. Also, the clock determines the speed of the
microprocessor operations.

6 | Page

A crystal is connected with the clock generator circuit and it determines


its frequency. In some microprocessors the clock generator circuit is
inside the microprocessor, so, the crystal is directly connected with the
microprocessor.

1.4.4

The Main Memory: [2]

It is an external memory used to store the binary codes of program


instructions and data and results of operations. It consists of a number
of semiconductor ICs, each IC has a certain storage size (1 Kbyte, 2
Kbyte..1 Megabyte). The main memory consists of two parts:
RAM- (Random Access Memory): It is a read/write memory used by the
system to store temporary programs, data and results.
ROM- (Read Only Memory): It used to store the permanent programs of
the microcomputer by the manufacturer of the system like monitor
programs, Power-On Self-Test (POST) programs and BIOS.

1.4.5

The Input / Output Devices and I/O Interfaces: [2]

A number of I/O devices can be connected in a microcomputer like


displays, printers, keyboards These devices are connected with the
microprocessor through interfaces.
An interface is a circuit which connects an I/O device with the
microprocessor and makes it easier for the microprocessor to read or
write data from/to the I/O device.
These interfaces are usually called I/O ports.
Interfaces can also perform some other functions like:
a. Providing the status of I/O device to the microprocessor (on, off,
ready, ..).
b. Providing a buffered storage for data sent from an input device to the
microprocessor.
c. Providing a latched storage for data sent from the microprocessor to
an output device.
d. Changing transferred data from serial-to-parallel or from parallel-toserial.

1.4.6

The System External Bus: [2]

The microprocessor is connected with memory and I/O ports through the
system bus. A bus consists of a number of lines of the same function
over which data or information is transferred from a source to a
destination.
7 | Page

The system bus consists of 3 buses: the address, data & control buses.
Through these buses the microprocessor sends and receives data,
instructions and control signals.

1.4.6.1

The Address Bus: [2]

It is a one-directional (unidirectional) bus, always output from the


microprocessor and input to memory and I/O ports. This bus carries
the address produced by the microprocessor in order to access
memory locations or I/O ports for read/write operations. Each
memory location or I/O port has one address.
The number of lines in the address bus is different from one
microprocessor to another.
Ex: Intel 8086/8088 microprocessors have an address bus of 20 lines
(A0- A19). So, the maximum size of memory that can be connected
with these microprocessors is 220 =1 Megabyte). The address range
of these locations is from 00000H to FFFFFH.
Problem: Intel Pentium 4 microprocessor has an address bus of 36
lines. Name these lines and calculate the maximum memory size that
can be connected with this microprocessor.

1.4.6.2

The Data Bus: [2]

It is a two-directional (bidirectional) bus, input/output, to/from the


microprocessor. It carries data to/from the microprocessor and
memory and I/O ports.
The number of lines in data bus is different from one microprocessor
to another.
Ex: Intel 8086 microprocessor has a data bus of 16 lines (D 0- D15).
Ex: Intel 8088 microprocessor has a data bus of 8 lines (D 0- D7).
Ex: Intel Pentium 4 microprocessor has a data bus of 64 lines (D 0D63).

1.4.6.3

The Control Bus: [2]

It is a two-directional (bidirectional) bus, input/output, to/from the


microprocessor. Output control lines carry control signals from the
microprocessor to external parts. These signals indicate the types of
operations done by the microprocessor and synchronize the
operations of the microprocessor with the external parts.
Ex: RD, WR, IO/M
8 | Page

Input control lines carry request signals from external parts to the
microprocessor to do some operations them.
Ex: INTR, RESET, BUS REQEST.

1.5 MAIN OPERATIONS PERFORMED BY MICROPROCESSORS IN


SYSTEMS: [3]
In a microprocessor-based system, the microprocessor performs the
following main types of operations.
a. Program-Initiated Operations. These are the operations done by
the microprocessor due to the program instructions that it is
executing.
From the moment when a DC supply and a clock are connected to the
microprocessor, it will enter a continuous state of fetching
instructions from memory then executing them. According to
instructions, most microprocessors can do the following types of
operations:
- Internal data operations like arithmetic, logical, test for conditions,
data-processing and control operations.
- Memory read- both for instructions and data.
- Memory write- for data only.
- I/O read- from an input port.
- I/O write- to an output port.
For memory or I/O read or write operations, the microprocessor
performs each operation in the following sequence of steps:
i. First it puts the address of memory location or the I/O port on the
address bus. This address will enable that location.
ii. Then, it gives a control signal that specify the type of operation it is
doing, read or write, with memory or an I/O port. This signal makes the
memory or I/O port responds correctly and the operation will be
synchronized properly.
iii. Then data will transfer to or from the microprocessor through the data
bus.
b. Peripheral (Externally) -Initiated Operations. These
operations done by the microprocessor as a respond to
requests coming to the microprocessor through some of
control lines from external devices (peripherals) connected
microprocessor.

are the
external
its input
with the

9 | Page

The microprocessor will respond to some of these requests directly


and unconditionally. For other requests, the microprocessor will not
respond to them unless certain conditions are satisfied. If these
conditions are not satisfied, the microprocessor will neglect these
requests. So, in the microprocessor there is an output acknowledge
line corresponding to each input conditional request line. Through
this line the microprocessor indicates if it has accepted the request or
not.
Some of the important peripheral-initiated operations are: Reset,
Interrupts, Bus Request and Wait.
RESET:
When Reset request is activated, the microprocessor will stop all its
operations and return back to the state when it is first turned on. It
will return to the start of the permanent program in the ROM memory
(usually the Monitor or POST program).
Ex: When Intel 8086/8088 RESET line is activated, the microprocessor
will restart its program execution from the address FFFF0H.
INTURRUPTS:
External
devices
can
interrupt
the
microprocessor.
The
microprocessor will suspend the execution of the current program
and transfer to execute another program called Interrupt Service
Routine. After completing this routine, the microprocessor will return
to complete the original program.
Some interrupts are Non-Maskable interrupts. The microprocessor will
respond unconditionally to them. Other interrupts are Maskable
interrupts. The microprocessor will respond to them when certain
conditions are satisfied.
BUS REQUEST:
Some external devices can request from the microprocessor to
suspend for some time its control on external buses( address, data
and part of control bus), so these devices can take control of these
buses and use them to transfer data in a large quantity between
memory and some I/O interfaces( like hard disk drives).
This process is called Direct Memory Access (DMA) and devices that
request bus control are called DMA Controllers. The microprocessor
will respond unconditionally to this request. After DMA process
complete, the DMA controller will inform the microprocessor to return
to its control of the buses.
10 | P a g e

WAIT:
This request causes the microprocessor to delay its operations not
stopping them. This request used by slow I/O devices and memory in
order to synchronize their operations with the microprocessor.

1.6 THE MICROCONTOLLER: [4]


Many
manufacturers
are
now
designing
and
implementing
microcontrollers. Microcontrollers are now finding large number of
applications with a growing market.
A microcontroller is also a digital semiconductor integrated circuit that
has a structure that enables it to perform computing and control
functions. But, microcontrollers differ from microprocessors in many
aspects:
a. Microcontrollers are also actually processors that are designed to be
used for dedicated applications (mainly control applications) like
mobiles, automotive applications, home appliances, security
systems, video games, printers, photocopiers, network switches,
military and medical equipmentetc. On the other hand,
microprocessors are considered as general purpose processors.
b. RAM, ROM, I/O ports, and timers must be added externally to a
microprocessor to enable it to perform its functions. On the other
hand, a microcontroller is designed to be all of that in one. As well as
the CPU (the processing module of the microcontroller), internally a
microcontroller has a fixed amount of ROM and RAM, I/O parallel
ports, a serial port, a timer (to allow the microcontroller to perform
tasks for certain time periods), an Analog-to-Digital Converter (ADC)
and may be other parts. Figure 1.3 shows the internal parts of a
single chip microcontroller.

11 | P a g e

Figure 1.3 The internal parts of a single chip microcontroller.


c. The system in which a microcontroller is used is usually called an
Embedded System, because in this system the microcontroller
executes special application software that is typically burned into the
internal microcontroller ROM.
On contrast, microprocessors are usually used in Personal Computers
that run a variety of software for many applications. The operating
system loads a variety of applications into the external RAM and lets
the microprocessor run them.
d. But, microprocessors have the advantage of versatility on the
amount of RAM, ROM, and I/O ports. For microcontrollers, the fixed
amount of on-chip ROM, RAM, and number of I/O ports makes them
ideal for applications in which cost and space are critical. In many
applications, the space it takes, the power it consumes, and the price
per unit are much more critical considerations than the computing
power.
For microcontrollers, the CPU core are now ranging from simple 4-bit
processors to sophisticated 32/64-bit processors. The following is a
list of some available microcontrollers according to their
manufacturer:

Atmel
AT89 series (Intel 8051 architecture)
AT90, ATtiny, ATmega, ATxmega series (AVR architecture)
AT91SAM (ARM architecture)
12 | P a g e

AVR32 (32-bit AVR architecture) MARC4


Intel

8-bit
o
MCS-51 (8051 family)

16-bit
o
MCS-96 (8096 family)
Microchip Technology
Microchip produces microcontrollers with 3 very different
architectures:
a. 8-bit (8-bit data bus) PIC microcontrollers:

PIC10 and PIC12 series: 12-bit instruction words.

PIC16 series: 14-bit instruction words.

PIC18 series: 16-bit instruction words.


b. 16-bit (16-bit data bus) microcontrollers:
PIC24: 24-bit instruction words.

o
o

c. 32-bit (32-bit data bus) microcontrollers:


PIC32MX series. Uses the MIPS architecture
NXP Semiconductors
8-bit
80C51
16-bit
XA
32-bit
ARM9
ARM Cortex-M4
Texas Instruments
TMS370 (8-bit)
MSP430 Ultra-low-power 16-bit microcontroller
TMS320F28xx (32-bit)

Chapter Two
Assembly Language Programming of Intel 8086/8088
Microprocessors
2.1 THE PROGRAMMING MODEL OF INTEL 8086/8088
MICROPROCESSORS: [1]

13 | P a g e

The programming module of a microprocessor consists of the


microprocessor registers that are used and affected directly by its
instructions. They are considered as the program visible registers.
Both Intel 8086/8088 microprocessors has the programming model shown
in figure 2.1.The word length of these microprocessors is 16-bit and their
internal structure is approximately the same. The difference between them
is in their external buses. Intel 8086 has a data bus 16-bit while 8088 has
an 8-bit data bus.
AX (16-bit)
AH (8-bit)
AL (8-bit)
BX (16-bit)
BH (8-bit)
BL (8-bit)
CX (16-bit)
CH (8-bit)
CL (8-bit)
DX (16-bit)
DH (8-bit)
DL (8-bit)

Accumulator Register
Base Index Register
Count Register
Data Register

SP (16-bit)

Stack Pointer Register

BP (16-bit)

Base Pointer Register

SI (16-bit)

Source Index Register

DI (16-bit)

Destination Index
Register

IP (16-bit)

Instruction Pointer
Register

FLAGS (16-bit)

Flags Register

CS (16-bit)

Code Segment Register

DS (16-bit)

Data Segment Register

SS (16-bit)

Stack Segment
Register

ES (16-bit)

Extra Segment Register

General
Purpose
Registers

Special
Purpose
Registers

Figure 2.1The Programming Model of Intel 8086/8088 Microprocessors

2.1.1

The General Purpose (Multipurpose) Registers: [1]

These are eight registers: AX (Accumulator Register), BX (Base Index


Register), CX (Count Register), DX (Data Register), SP (Stack Pointer
14 | P a g e

Register), BP (Base Pointer Register), SI (Source Index Register) and DI


(Destination Index Register). Each register has a size 16-bit. Registers
AX, BX, CX, & DX can be used as 8-bit registers (AL, AH, BL, BH, CL, CH,
DL & DH).
Mainly, these registers are used in programs to hold data on which the
instructions are performed as well as the results of these instructions.
Some of them are used for addressing and others have special uses in
programs as we will see later.

2.1.2

The Special Purpose Registers: [1]

These are six registers: IP (Instruction Pointer), FLAGS (Flags Register),


CS (Code Segment Register), DS (Data Segment Register), SS (Stack
Segment Register) and ES (Extra Segment Register). Each register has a
size 16-bit. Each of these registers has a special use in programs and it
cannot be used other than that.
a. IP (Instruction Pointer): This register gives the offset address of the
next instruction in the program memory to be executed by the
Execution Unit inside the microprocessor. When the Execution Unit
receives an instruction for execution, IP will automatically change to
point to the next instruction in the program.
When the microprocessor is first powered ON or in case of its Reset, IP
will hold the address 0000H. This register can only be modified using
Jump or Call instructions.
b. FLAGS (Flags Register): Each flip-flop in this register will be Set or
Reset to indicate a certain case that may happen after the
microprocessor executes some instructions like arithmetic and logical
instructions. These flags can be tested by other instructions in the
program in order that the microprocessor takes some decisions
according to the states of Flags. Some of these flags that we will use in
our programs are:
-

CF (Carry Flag). It sets (CF=1) when a carry out of the MSB (Most
Significant Bit) results from addition instruction or borrow from
subtraction. Otherwise it will reset (CF=0).
Ex:

10111101
+ 11101001

Carry (CF=1)

1 10100110
15 | P a g e

ZF (Zero Flag). It sets (ZF=1) if the result from an instruction is equal to


zero, otherwise it will reset (ZF=0).
SF (Sign Flag). It sets (SF=1) if the result is a negative number (Its
MSB=1). It will reset (SF=0) if the result is a positive number (Its
MSB=0).
IF (Interrupt Flag). Used to control microprocessor maskable interrupt
requests.
c. Segment Registers (CS, DS, SS & ES). Used by the microprocessor to
get the segment addresses of program memory and data memory. The
microprocessor combines segment addresses from these registers with
the offset addresses to get the actual (physical) addresses of different
memory segments.

2.2 MEMORY SEGMENTATION IN INTEL 8086/8088


MICROPROCESSORS: [1]
Intel 8086/8088 microprocessors have an address bus of 20 lines (A 0- A19),
so they address a total memory of 1MByte with the address range 00000HFFFFFH. This memory is segmented by these microprocessors to 4 types of
segments. One segment will be used store programs instructions (it
represents the program memory) while the other segments are used to
store data (they represent the data memory).
Each segment has a segment address. Also, each memory byte in each
segment has an offset address. So, in programs each storage location
(byte) in the memory is specified by a segment address (16-bit) and an
offset address (16-bit) usually written in programs in the form
segment:offset for example 5000H:2BA0H.
The microprocessor uses these two addresses to get an actual (physical)
address of 20-bit, which will be put on the address bus to access the
memory storage location. For each segment, the segment address is taken
by the microprocessor from a specified segment register, and the offset
address is taken from other registers or written directly in the program.
The 4 memory segments are listed in table 2.1:
Memory
Segment
a. Code
Segment.
(Program
memory)

Default Use
To store program
instructions.

Segment
address
In CS
register.

Offset Address
In IP register.

16 | P a g e

b. Data
Segment.
(A data
memory)
c. Stack
Segment
(A data
memory)

d. Extra
Segment
(A data
memory)

To store data (variables)


used by the program.

In DS
register.

Used as a stack. Stack is


a data memory in which
data is stored in the
form of Last-In-First-Out.
When the
microprocessor transfers
to execute some
subroutines it stores
some data temporarily in
the stack.
As additional data
segment for some string
instructions.

In SS
register.

In ES
register.

A direct 16-bit
address or an
address in BX or
SI or DI registers.
In SP or BP
registers.

In DI register.

Table 2.1The four Memory Segments of Intel 8086/8088 Microprocessors.


2.3 ASSEMBLY LANGUAGE, MACHINE LANGUAGE, ASSEMBLER

AND ASSEMBLER DIRECTIVES: [1]


Each microprocessor executes a number of instructions. Each instruction is
given a name called "mnemonic". For example, for Intel8086/8088 MOV
instruction is used to transfer (copy) data and ADD instruction is used for
addition.
The language used to write these instructions is called assembly language.
The general format for instructions in assembly language is:
LABEL
EX:

Next:

OPCODE
MOV

OPERANDS
AX , BX

Destination Operand

COMMENTS
; Copy BX to AX

Source Operand

Both the LABEL and COMMENTS are optional, used in some cases, but each
instruction must have its opcode which specify the type of operation. The
OPERANDS are register names, memory addresses or values of data on
which the instruction will operate. Some 8086/8088 instructions have no
operand, others have one or two operands.

17 | P a g e

Programs written in assembly language must be translated to machine


language before executing them by the microprocessor. Instructions in
machine language are binary codes that can be executed by the
microprocessor. For each instruction in assembly language there is a
corresponding instruction in machine language, usually given by the
microprocessor manufacturer as a hexadecimal code. In most
microcomputers, a translator program called "The Assembler" is used to
automatically perform the translation operation and store the machine
language program in memory then run it. For example the Line Assembler
used with the microcomputer trainer MC8088/EV system.
Some of the statements written in assembly language programs are called
Assembler Directives. They arent instructions to the microprocessor, but
they represent directives to the assembler to do some operations while it
assembles the program to machine language. Two examples of the
directives used with the Line Assembler of the microcomputer trainer
MC8088/EV system are:
a. DB (Define Byte): Used to book a number of locations in the data
memory, each location is one byte and put data in each location. Ex:
0090:20 DB 23, 6C, EB, A , B , C
b. DW (Define Word): Used to book a number of locations in the data
memory, each location is a word (two bytes0 and put data in each
location. Ex:
0090:50 DW 18B0, AAFF
Note: All the numbers written in the programs for the Line Assembler of
the microcomputer trainer MC8088/EV must be hexadecimal values.
2.4 ADDRESSING MODES USED WITH INTEL 8086/8088

INSTRUCTIONS: [1]
The addressing mode is the method used in the instruction to specify the
location of data (the operands) on which the instruction will operate.
The followings are some of the addressing modes used with 8086/8088
instructions.

2.4.1

Register Addressing Mode. [1]

In this mode, the operands are one or two of the microprocessor


registers. Ex:
18 | P a g e

MOV SI , BX
MUL CX
MOV AH , BL
Source operand and destination operand registers must be of the same
size.

2.4.2

Immediate Addressing Mode. [1]

Source operand only is an immediate data byte or data word. Ex:


MOV AX, 34BC
MOV DL, 68

2.4.3

; Store the number 34BCH in AX


; Store the number 68H in DL

Direct Data Memory Addressing Mode. [1]

One of the operands is a memory location which contains the data that
will be used by the instruction. The offset address of this memory
location is written directly in the instruction. By default data is stored in
data segment memory. Ex:
MOV AX, [0070]
register AX. The
MOV [002B], CL
memory. The

;Copy one data word from memory to


;offset address of the memory is 0070H.
;Copy one data byte from register CL to
;offset address of the memory is 002BH.

Note: the brackets [ ] represent a directive to the assembler that the


number represent an offset address of a data memory.

2.4.4

Register Indirect Data Memory Addressing Mode. [1]

One of the operands is a memory location which contains the data that
will be used by the instruction. The offset address of this memory
location will be in a register. The registers
that will be used to hold the offset address are BX, SI or DI only. By
default data is stored in data segment memory. Ex:
MOV BX, 0070
; First the offset address of memory must be
stored in a register.
MOV AX, [BX] ;Copy one data word from memory to register AX.
The
;offset address of the memory is taken from
register BX.
MOV DI, 002B
stored in a register.

; First the offset address of memory must be


19 | P a g e

MOV [DI], CL

;Copy one data byte from register CL to memory.

The
;offset address of the memory is taken from
register DI.
Note: Register indirect addressing is very useful in case the program
deals with a large quantity of data.

2.4.5

Register Relative Data Memory Addressing Mode. [1]

One of the operands is a memory location which contains the data that
will be used by the instruction. The offset address of this memory
location will be in a register. Also, a displacement will be added to or
subtracted from the offset address. The registers
that will be used to hold the offset address are BX, SI or DI only. By
default data is stored in data segment memory. Ex:
MOV BX, 0070
stored in a register.
MOV AX, [BX-20]
AX. Offset

; First the offset address of memory must be


;Copy one data word from memory to register

; address of the memory is in register BX


displaced by -20H.
MOV DI, 002B
; First the offset address of memory must be
stored in a register.
MOV [DI+3C0], CL
;Copy one data byte from register CL to
memory. Offset
;address of the memory is in register DI
displaced by +3C0H.
Note: The original address in registers BX or SI or DI will not change
because the displacement will be added temporarily. This enables the
program to get data from different areas in memory starting from a
fixed address.

2.4.6

Direct Program Memory Addressing Mode. [1]

This mode is used with instructions JMP, LOOP and CALL which cause the
jump in the program from one part to another in the Code Segment. The
offset jump address is written directly in the instruction. This address
will replace the current address of IP to cause the jump. Ex:

20 | P a g e

JMP
address 0020H.

2.4.7

0020

; Jump in the program memory to the offset

Indirect Program Memory Addressing Mode. [1]

This mode is used with JMP instruction which cause the jump in the
program from one part to another in the Code Segment. The offset jump
address will be in any 16-bit register (AX, BX, CX, DX, SP, BP, SI, DI).
When the microprocessor executes JMP instruction it replaces the
current address of IP by the address specified by the register and jump.
Ex:
MOV

CX, 0020

;First store the offset jump address in CX

register.
JMP
CX
address which is

; Jump in the program memory to the offset


;the number stored in register CX.

2.4.8

Fixed-Port Addressing Mode. [1]

This mode is used with instructions IN and OUT which are used to
transfer data between the microprocessor and Input / Output ports. The
port address is written directly in the instruction and it must be 8-bit
only. Ex:
IN AL,50 ; Input one byte from the input port to AL. The port
address is 50H.
OUT 70,AX ; Output one word (two bytes) from AX to the output
port. The
; port address is 70H.

2.4.9

Variable-Port Addressing Mode. [1]

This mode is used with instructions IN and OUT which are used to
transfer data between the microprocessor and Input / Output ports. The
port address will be 16-bt in register DX only. Ex:
MOV DX, 1F00
IN AX, DX
to register AX.
OUT DX,AL
port address

;First store the port address in DX register.


; Input one word (two bytes) from input port
;The port address is in register DX.
; Output one byte from AL to the output port. The
21 | P a g e

;is in register DX.

2.5 INTEL 8086/8088 MICROPROCESSORS GROUPS OF


INSTRUCTIONS: [1]
All the instructions of Intel 8086/8088 microprocessors can be classified to
the following Groups:
1.
2.
3.
4.
5.
6.

Data Transfer Group.


Arithmetic Group.
Logical Group.
Control Transfer Group.
String Manipulation Group.
Processor Control Group.

2.5.1

Data Transfer Group: [1]

Data transfer instructions are used in the program to move (copy) data
from one location to another. The data may be in groups of 8-bits or 16bits. All these instructions do not affect the FLAGS. We will study some
instructions in this group.

2.5.1.1

MOV Instruction: [1]

It is used to copy a byte or a word of data from a source to a


destination.
a. From a register to a register:
MOV SP, SI
MOV CL, BH
MOV DS, BX
b. From a data memory to a register and from a register to a data memory:
MOV AX,[0400]
MOV [SI], CH
MOV [BX+60], SI
c. Immediate data to a register or a data memory:
MOV BX,1C00
MOV DH,56
MOV [1000], FFCC
Note: Immediate data cannot be moved to a segment register. Move
the data to a general purpose register then to the segment register.
MOV CX, 0090
MOV DS, CX

2.5.1.2

PUSH Instruction: [1]


22 | P a g e

It is used to save a word only from a register or data segment


memory into the top of the stack segment memory.
PUSH DX
PUSH [2000]
memory with offset

; Store a data word from the data segment

;address 2000H in the top of the stack segment


memory.

2.5.1.3

POP Instruction: [1]

It is used to return a word only from the top of the stack segment
memory to a register or a data segment memory.
POP [SI]
memory to the data

; Return a data word from the top of stack segment


; segment memory. Offset address of data memory is in

register SI.
POP BX

Note: In any program usually the sequence of POP instructions is


opposite to that of PUSH instructions, because data is stored in stack
in the form of Last-In-First-Out.
PUSH AX
PUSH BP
.
POP BP
POP AX

2.5.1.4

IN Instruction: [1]

It is used to input a byte or a word from an input port (input


interface) to registers AL or AX only.
IN AL,50
IN AX,F0
MOV DX,5000
IN AL,DX
IN AX,DX

2.5.1.5

OUT Instruction: [1]

It is used to output a byte or a word from registers AL or AX only to


an output port (output interface).
OUT B0, AL
OUT 70, AX
MOV DX,B000
23 | P a g e

OUT DX, AL
IN DX, AX

2.5.2

Arithmetic Group: [1]

2.5.2.1

ADD, ADC, SUB, SBB and CMP Instructions: [1]

Byte or word addition, subtraction and comparison instructions can


be executed on signed or unsigned binary numbers. After the
microprocessor executes any of these instructions the Flags CF, ZF,
OF, AF, PF and SF will change according to the result.
a. ADD (add):
ADD
destination, source
; destination + source
destination
Result
ADD BX, CX
; BX + CX
BX

Result

b. ADC (add with carry):


ADC
destination, source
; destination + source + CF
destination
Result
ADC DL, AL
; DL + AL + CF
DL
c. SUB (subtract)
SUB
destination, source
destination
SUB CH, BL
; CH - BL Result
d. SBB (subtract with borrow):
SBB destination, source
destination
SBB SP, DI
; SP - DI - CF
e. CMP (compare)

Result

Result

destination - source
CH

Result

; destination - source - CF
Result

SP

CMP destination, source


; destination - source
CMP AX, SI
; AX - SI
Comparison is a subtraction operation without a result, i.e. both the
source and destination operands will remain the same. Only the
FLAGS will change.
In all these instructions the source and destination operands are:
1.

Register, Register

2.

ADD CX,AX
CMP SI,DI
Register, Data in Memory or Data in Memory, Register
24 | P a g e

3.

ADC CL, [0030]


SUB [SI], AX
Register, Immediate Data or Data in Memory, Immediate Data
SBB AL,45
ADD [0900], F000

2.5.2.2

INC and DEC Instructions: [1]

A byte or a word, a signed or an unsigned number can be


incremented by 1 using INC instruction or decremented by 1 using
DEC instruction. Flags ZF, OF, AF, PF and SF will change.
INC and DEC instructions has one operand, either a register or a
memory location.
INC BX ; BX + 1
BX
DEC CL ; CL - 1
CL
INC BYTE PTR [DI]
; Increment by 1 a byte of data in data
segment memory.
;The offset address of that memory is in
register DI.
DEC WORD PTR [2000] ; Decrement by 1 a word of data in data
segment
;memory. The offset address of that memory
is 2000H.
Note: BYTE PTR and WORD PTR are directives to the assembler used
to clarify if the operation is on a byte or a word.
Programming Example 1: The data words F0C0H, CAB6H, 2D50H
and FF56H are stored in data memory starting with address 0090:0H.
Write a program that adds all these numbers and stores the result in
data memory starting with 0090:10H.
Solution:
The sum of these words will be a word. The carry must be taken and
it will be a byte.
MOV
MOV
MOV
MOV
ADD
ADD
ADC
ADD

DX,0090
DS,DX
AX,0
; AX will be used to store the sum.
CL,0
; CL will be used to store the carry.
AX,[0] ; add the first word.
AX,[2] ; add the second word.
CL,0
; take the first carry.
AX,[4] ; add the third word.
25 | P a g e

ADC CL,0
; take the second carry.
ADD AX,[6] ; add the fourth word.
ADC CL,0
; take the third carry.
MOV [10],AX ; store the sum in data memory 0010H.
MOV [12],CL ; store the total carry in data memory 0012H.
INT 7
0090:0
DW F0C0, CAB6, 2D50, FF56

2.5.2.3

MUL, IMUL, DIV and IDIV Instructions: [1]

Unsigned bytes and words can be multiplied and divided using MUL
and DIV instructions. The results will be unsigned values. Signed
bytes and words can be multiplied and divided using IMUL and IDIV
instructions. The results will be signed values in this case.
a. Byte Multiplication:
The multiplicand must be a byte in AL register only. The multiplier is a
byte either in an 8-bit register or in memory (the multiplier cannot be
an immediate byte). The result will be a word in AX (The high byte in
AH and the low byte in AL).
Byte in
in AX
AL

Byte in
*

AL
AH
BL
BH
CL
CH

Result (Word)

AH

AL

High Byte

Low Byte

DL
DH
memo
ry

MUL BL

; AL * BL

AX

MUL AL

; AL * AL

AX

MUL BYTE PTR [0020] ; AL* [0020]

AX

b. Word Multiplication:
The multiplicand must be a word in AX only. The multiplier is a word
either in a 16-bit register or in memory (the multiplier cannot be an
26 | P a g e

immediate word). The result will be a double word in DX and AX


(The high word in DX and low word in AX).
Word in
Word) in DX AX
AX

Word in

MUL SI
word)

AX
BX
CX
DX
SI
DI
SP
BP
memo
ry

DX
High

; AX *SI

MUL WORD PTR [BX]


low word)

Result (Double

AX
Low Word

DX(The high word) AX(The low

;AX * [BX]

DX(The high word) AX(The

c. Byte Division:
The dividend must be a word in register AX only. The divider is a
byte either in an 8-bit register or in a memory. The result (the
quotient) will be a byte in AL and the remainder will be a byte in AH.
Word in
Result (quotient)
AX

DIV BH

Byte in
AL
AH
BL
BH
CL
CH
DL
DH
memo
ry

; AX BH

DIV BYTE PTR [DI]


remainder)

Remainder

AH

AL

AL (The quotient) AH (The remainder).


; AX [DI]

AL (The quotient) AH (The

d. Word Division:
27 | P a g e

The dividend must be a double word in registers DX (The high word)


and AX (The low word) only. The divider is a word either in a 16-bit
register or in a memory (the divider cannot be an immediate word).
The result (the quotient) will be a word in AX and the remainder will
be a word in DX registers.
Double Word in
Result (quotient)
DX
High

AX
Low Word

Word in
AX
BX
CX
DX
SI
DI
SP
BP
memo
ry

Remainder

DX

AX

DIV CX
; DX AX CX
AX (The quotient) DX (The
remainder).
DIV WORD PTR[30] ; DX AX[0030]
AX(The quotient) DX(The
remainder)
Programming Example 2: Assume that the variables A, B, and C are
unsigned byte valued numbers stored in data memory starting with address 0090:10.
The assigned values for these variables are:
A= 7AH , B=3DH, C=4EH
Write a program that calculates the value of the variable X from the following
equation:
X= (B2 -2FC0 + AC)/ (C+ 65)
Assume that no carry will result from the addition operation and no borrow will result
from the subtraction operations. The program stores the result and the remainder in the
data memory starting with address 0090:20.
Solution:
MOV DX,0090
MOV DS,DX
MOV AL, [0011]
MUL AL
; Calculate B2
MOV DX, AX
SUB DX, 2FC0
; Calculate B2-2FC0
MOV AL,[0010]
MUL BYTE PTR [0012]
; Calculate AC
ADD DX, AX
;Calculate B2-2FC0+AC
28 | P a g e

MOV BL,[0012]
ADD BL,65
MOV AX,DX
DIV BL
MOV [0020], AX
INT 7
0090:10 DB 7A
0090:11 DB 3D
0090:12 DB 4E

2.5.3

; Calculate C+65
; Calculate X
; Store result and remaider
;A
;B
;C

Logical Group: [1]

Logical instructions regard their operands as strings of bits. They


operate on bytes and words. The Flags CF, OF, PF, SF and ZF change
after the microprocessor executes most of logical instructions.

2.5.3.1

AND, OR and XOR Instructions: [1]

a. AND (logical AND):


AND DH, CL

; DH . CL

b. OR (Logical OR):
OR DX, AX

; DX + AX

c. XOR (Logical Exclusive OR)


XOR CH, BL
; CH + BL Result

Result

DH

Result

DX

CH

In all these instructions the source and destination operands are:


1. Register, Register
XOR CX,AX
OR DL,AH
2. Register, Data in Memory or Data in Memory, Register
AND CL, [0030]
XOR [SI], AX
3. Register, Immediate Data or Data in Memory, Immediate Data
OR AL,45
AND [0900], F000

2.5.3.2

NOT Instruction: [1]

It is used to get the ones complement of its operand which is a byte


or word in a register or memory.
NOT BX
NOT DH

; BX
; DH

BX
DH
29 | P a g e

NOT BYTE PTR [DI]


; [DI]
[DI]
NOT WORD PTR [0050]
; [0050]

[0050]

Programming Example 3: The number N1=CF8DH and the number


N2= DCA7H are stored in data memory starting with location 0090:0.
Write a program the finds the value of the number N which is
specified by the following Boolean expression and stores it in data
memory 0090:10 where:
N= N1+ N2 + N1 . N2
Solution: MOV DX,0090
MOV DS,DX
MOV AX, [0000]
; move N1 to AX
MOV BX, [0002] ; move N2 to BX
NOT BX
; N2
OR AX,BX
; N1+N2
MOV CX,[0000] ; move N1 to CX
MOV DX,[0002] ; move N2 to DX
AND CX, DX
; N1.N2
NOT CX
; N1.N2
XOR AX, CX
; N1+N2 + N1.N2
NOT AX
; Find N
MOV [0010],AX ; Store N
INT 7
0090:0
DW CF8D, DCA7

2.5.4

Control Transfer Group: [1]

This group of instructions is used to jump (branch) in the program from


one part to another. In this case the sequential execution of instructions
will be changed. These instructions will change the address in IP register
(or addresses of both IP and CS registers), so, they will cause the jump
in the program.
The Control Transfer Instructions include: the unconditional jump, the
conditional jump, loop instructions, CALL, RET, INT, INTO and IRET
instructions.

2.5.4.1

The Unconditional Jump using JMP Instruction: [1]

It is without any condition. The jump will occur always when the
microprocessor executes JMP instruction. JMP instruction is used with
two addressing modes:
30 | P a g e

a. Direct Program Memory Addressing Mode: The offset jump address is


written
directly with instruction. Ex:
001B
001E

MOV BX,FFFF
DEC BX
0020
JMP 001E

Note: In modern assemblers, the programmer will label the


destination of the jump and use this label with JMP instruction. The
assembler will determine the value of the direct address and
substitute it in JMP instruction. Ex:
MOV BX,FFFF
Next: DEC BX
JMP Next
b. Indirect Program Memory Addressing Mode: The jump address will be
put in a 16-bit register (AX, BX, CX, DX, SP, BP, SI, DI). When the
microprocessor executes such instruction it replaces the address in IP
by the address stored in the register and
jump. Ex:

2.5.4.2

JMP

AX

The Conditional Jump: [1]

Each conditional jump instruction has a condition to jump. The


microprocessor jumps only if the condition specified in the instruction
is met during program execution. Otherwise, the microprocessor will
not jump and it will continue to the next instruction in the same
program sequence.
All the conditions are dependent on the status of the flags.
Note: All conditional jump instructions are direct jump, so we must
use labels or write the value of the jump address.
Conditional jump instructions of Intel 8086/8088 microprocessors are
listed in table 2.2.
Conditional Jump
JZ
JNZ
JC
JNC
JO
JNO
JS

Jump if:
Zero
Not Zero
Carry
No Carry
Overflow
No Overflow
Sign is Set ( sign negative)

Remark
Jump if ZF=1
Jump if ZF=0
Jump if CF=1
Jump if CF=0
Jump if OF=1
Jump if OF=0
Jump if SF=1
31 | P a g e

JNS
JP/JPE
JNP/JPO

Sign is Reset ( sign Jump if SF=0


positive)
Parity/ Parity Even
Jump if PF=1
No Parity / Parity Odd
Jump if PF=0

JA
JAE
JB
JBE
JE
JNE

Above
Above or Equal
Below
Below or Equal
Equal
Not Equal

Used
when
operations
are
performed
on
unsigned
numbers.

JG
JGE
JL
JLE
JE

Greater
Greater or Equal
Less
Less or Equal
Equal

Used
when
operations
are
performed
on
signed numbers.

Table 2.2The
microprocessors.

conditional

jump

instructions

of

Intel

8086/8088

Programming Example 4: The unsigned data words 34FDH and


C694H are stored in data memory starting with address 0090:0. Write
a program that displays the maximum number on the LCD display.
Solution:

MOV CX,0090
MOV DS,CX
MOV AX,[0000]
CMP AX,[0002]
JA DISP
MOV AX,[0002]
DISP:
INT 10
INT 7
0090:0 DW 34FD, C694

2.5.4.3

LOOP Instruction: [1]

LOOP is an unconditional instruction used to repeat part of the


program a number of times. By default, LOOP uses register CX as a
counter. So, the number of required repetitions must be put in CX. Ex:

SUM:

MOV AX,0
MOV BX,4
MOV CX,200
ADD AX,BX
32 | P a g e

LOOP SUM
LOOP will decrement CX by 1 each time it repeats the loop. If CX 0,
it will jump again to the specified label. If CX=0, it will not jump and
the program continues from the next instruction.
The above part of the program can be written as shown without using
LOOP:
MOV AX,0
MOV BX,4
MOV CX,200
SUM:
ADD AX,BX
DEC CX
JNZ SUM
Note: All loop instructions require direct address, so we must use
labels or write the value of the address in practical exercises.
Programming Example 5: The data words 45FDH, CD08H, 7FF0H,
6CB8H, AA22H, 9945H, E0FAH, 9034H, 0F10H, ADBCH, 2189H,
CF94H, BD5FH, 8832H and 7A1BH are stored in data memory starting
with address 0090:0. Write a program that calculates the sum of
these words and stores the result in data memory starting with
0090:40.
Solution: The sum of all these words will be a word, but the total
carry will be a byte.
MOV DX,0090
MOV DS,DX
MOV SI,0000
; starting offset address of data.
MOV CX, 0F
; Counter to the number of
additions.
MOV AX,0
; AX will be used to store the
sum.
MOV BL,0
; BL will be used to store the carry.
SUM:
ADD AX,[SI]
; add a word.
ADC BL,0
;take the carry
INC SI
INC SI
; get the address of next word.
LOOP SUM
; add second word.
MOV [0040], AX
MOV [0042], BL
INT 7
0090:0
DW 45FD, CD08, 7FF0, 6CB8, AA22, 9945, E0FA
0090:E
DW 9034, 0F10, ADBC, 2189, CF94, BD5F, 8832, 7A1B
33 | P a g e

Programming Example 6: The numbers 56H, 77H, 33H,5FH, 78H,


CBH, 99H, 12H, 0H, 6EH, 76H, 90H, ACH, BEH are unsigned bytes
stored in data memory starting with address 0090:30. Write a
program that displays the minimum number on the LCD display.
Solution:
MOV DX,0090
MOV DS,DX
MOV SI,0030
; Starting offset address of data.
MOV AL, [SI]
; First number to AL and consider it
as the minimum.
INC SI
; Get address of second number.
MOV CX, 0D
; Loop Counter to the remaining
numbers.
AGAIN :
CMP AL,[SI]
; Compare next number in memory
with AL.
JB NEXT
; Continue if AL is Less.
MOV AL,[SI]
; If the number in memory is Less,
store it in AL.
NEXT:
INC SI
; Get address of next number in
memory.
LOOP AGAIN
; Compare again.
INT E
INT 7
0090:30
DB 56, 77, 33,5F, 78, CB, 99, 12, 0, 6E, 76, 90, AC, BE
Programming Example 7: The data memory starting with address 0090:00
holds an array A of 20 unsigned words. The data memory starting with address 0090:40
holds an array B of 20 unsigned bytes. Write a program that divides each word from
array A by the corresponding byte from array B. The program stores the results and
remainders OF divisions in data memory starting with address 0090:80.
Solution:
MOV DX,0090
MOV DS,DX
MOV SI,0000
MOV DI,0040
MOV BX,0080
MOV CX, 14
AGAIN:
MOV AX,[SI]
DIV BYTE PTR [DI]
MOV [BX], AX
ADD SI, 2
INC DI
ADD BX, 2
LOOP SUM
34 | P a g e

0090:0
0090:40

2.5.4.4

INT 7
DW 45FD, CD08, .
DB 90, 34,

CALL and RET Instructions: [1]

CALL instruction is used to transfer the flow of the program to a


subroutine. CALL differs from JMP instruction because it saves the
return address (the current address of IP) in the stack. Ex:
MAIN:

FUNCTION:

MOV DX,1500
MOV BX, 2800
CALL FUNCTION
MOV AX,DX
INT 7
ADD DX, BX
RET

Instruction RET is used at end of the subroutine in order to return to


the main program. Actually RET will pop the return address from the
top of the stack to IP.

2.5.4.5

INT and IRET Instructions: [1]

There are two types of interrupts in all Intel microprocessors: Internal


Interrupts and External Interrupts (Hardware Interrupts).
1. Internal Interrupts:
They are sometimes called Exceptions. Also they are of two types:
Software Interrupts and Special Cases Interrupts.
a. Software Interrupts:
They are invoked when either INT or INTO instructions are used in the
program.
Syntax:
Ex:

INT Vector-Number
INT E

INT is an unconditional interrupt instruction. When the


microprocessor executes this
instruction it will directly leave the current program and transfer to
execute the Interrupt Service Routine(ISR). But before the
microprocessor transfers to execute the ISR, it pushes the current
addresses of CS and IP to the stack memory, so they will be the
return addresses to the original program.
The Vector-Number (also called Interrupt Type) is a number of one
byte (from 00 to FF). The microprocessor uses this number to get the
segment address and offset address of the memory in which the ISR
35 | P a g e

is stored. The microprocessor will put these addresses in CS and IP


respectively, so it will transfer to execute the ISR.
At the end of the ISR, the instruction IRET (Interrupt Return) is used
to return to the original program. IRET will return addresses from the
stack to IP and CS registers.
b. Special Cases Interrupts
Each of these interrupts will be automatically executed by the
microprocessor when a special internal microprocessor case occurs.
Each of these interrupts has a default type. An example of these
interrupts is the Divide-by-Zero Error interrupt. It vector type is 0 by
default.
2. External Interrupts (Hardware Interrupts)[3]:
These interrupts are requested by external devices connected with
the microprocessor. The external device request each interrupt by
activating one of the microprocessor input control interrupt lines.
Intel 8086/8088 microprocessor has two hardware interrupt lines:
a. INTR: It is the maskable interrupt line. The Microprocessor will
respond to this interrupt only if the interrupt flag is set (IF=1). If IF=1,
the microprocessor will acknowledge the external device that it is
accepting the interrupt by activating INTA line (Active-Low output
control line). The external device supplies the microprocessor with
the interrupt vector-number through data bus and the microprocessor
will use this number to transfer to the ISR as mentioned before. This
process is shown in figure 2.2.

Intel P
INTR
INTA
7D-0D

External
Device

Figure 2.2 The Maskable Interrupt of Intel 8086/8088


Microprocessors.
b. NMI: It is the non-maskable interrupt line. The Microprocessor will
respond to this interrupt without any condition. The interrupt vectornumber is 2 by default.
Note: Since the interrupt vector number is one byte in all Intel
microprocessors, the total number of ISRs dedicated for internal and
external interrupts is 256 .

Chapter Three
36 | P a g e

Connecting Memories and Interfacing I/O Devices with Intel


8088 Microprocessor
3.1 CONNECTING ROM AND STATIC RAM MEMORIERS WITH
INTEL 8088 MICROPROCESSOR: [3]
The general pins-out for ROMs and SRAMs is shown in figure 3.1 below:

Figure 3.1 The General Pins-Out for ROMs and SRAMs Memories.
Notes:
1. Memory storage capacity (the number of storage locations in the
memory) C= 2n where n is the number of address lines.
Ex: a 1 K memory has 10 address lines (A0-A9) because: 1K=1024=210 .
2. Each memory has a number of data lines equal to the size of each
storage location which can be 1, 2, 4, 8 or 16-bit.
Ex: A memory with each storage location is 4 bits has 4 data lines (D 0-D3).
Usually the total memory size is written like: 1Kx4, 1Kx8, 2Kx8, 16Kx1 ..
etc.
Ex: A memory 4Kx8 has:
4K= 4x1024=4096= 212
12 address lines A 0- A11. It has 8 data lines: D 0D7.
3. A SRAM and a ROM has at least one CS (Chip Select) or CE ( Chip Enable
) input. It enables all the memory chip and it is usually connected to one of
the address decoder outputs.
4. A ROM has one control input OE , usually connected with MEMR line of
the system bus.
5. A SRAM has two control inputs OE (connected to MEMR) and WE
(connected to MEMW) lines of the system bus.
37 | P a g e

3.1.1

Memory Map and the Address Decoder: [3]

We know that Intel 8088 microprocessors can address 1Mbyte of


memory with a total address range 00000H-FFFFFH. So, each memory
that will be connected with the microprocessor must be assigned and
address range according to its size. For examples:
A 1K memory can take an address range 00000 - 003FFH or FF000 FF3FFH.
A 2K memory can take an address range 00000 - 007FFH or 50000 507FFH.
A 4K memory can take an address range 00000 00FFFH or C4000
C4fFFH.
An 8K memory can take an address range 00000 01FFFH or FE000 FFFFFH.
The assignment of addresses to memory chips (ICs) in a system is called
the memory map.
The following points must be considered when we assign addresses to
memories connected with Intel 8088 microprocessor:
1. For ROM memories, one of the connected ROMs must be given an
address range ending with FFFFFH (Ex: FFF00H-FFFFFH for a 4K
ROM) because at startup or after resetting Intel 8088 microprocessor
its registers IP=0000 and CS=FFFFH, so, the microprocessor will
restart running the system startup program from the address FFFF0H.
So, this address should be assigned for a ROM memory, because it
usually holds the startup system program as stored by the system
manufacturer.
2. Also, one of the connected SRAM memories must be given an address
range starting with 00000H because the interrupt vectors for Intel
8088 microprocessor are stored in the lower part of RAM memory
starting with address 00000H.
Note:
For other types of microprocessors the given starting addresses for
ROM and RAM memories may be different.
3. The rest of ROM chips and the SRAM chips can be given any other
address range.
4. After selecting the address range for each memory piece, an address
decoder circuit must be designed and connected. The address
decoder circuit will provide chip select signals to all memory chips in
order to enable each one according to its address range.
38 | P a g e

Simple gates can be used as address decoders. Also, some decoder


ICs can be used like 74LS139 (Dual 2 line-to- 4 line decode), 74LS138
(a 3 line-to-8 line decoder) or 74LS154 (a 4 line-to-16 line decoder).
Memory Connection Example 1: It is required to connect with Intel
8088 microprocessor a SRAM memory with a total storage capacity of
8 Kbyte. If the available SRAM is of type 6116 (2Kx8), select the
required number of SRAM chips and select an address range for each
chip, then design the address decoder circuit. Draw the complete
connection.
Solution:
4 pieces of 6116 SRAM are required.
Each SRAM is 2Kx8, so each SRAM has 8 data lines D0-D7.
2K=2x1024=2048=211. So, each SRAM has 11 address lines A0-A10.
The selected address ranges for the 4 SRAMs are:
SRAM0
(00000-007FFH)
SRAM1
(00800-00FFFH)
SRAM2
(01000-017FFH)
SRAM3
(01800-01FFFH)
The complete connection including the address decoder circuit is shown
in figure 3.2 below:

39 | P a g e

Figure 3.2 The Circuit Connection for Example 1.


Memory Connection Example 2: It is required to connect with Intel
8088 microprocessor a ROM memory with a total storage capacity of 32
Kbyte. If the available ROM is an EPROM of type 2732 (4Kx8), select the
required number of EPROM chips and select an address range for each
chip, then design the address decoder circuit. Draw the complete
connection.
Solution:
8 pieces of 2732 EPROM are required.
Each EPROM is 4Kx8, so each EPROM has 8 data line D0-D7.
4K=4x1024=4096=212. So, each EPROM has 12 address lines A0-A11.
The selected address ranges for the 8 EPROMs are:
EPROM7 (FF000-FFFFFH)
EPROM6 (FE000-FEFFFH)
EPROM5 (FD000-FDFFFH)

40 | P a g e

The complete connection including the address decoder circuit is shown


in figure 3.3 below:

Figure 3.3 The Circuit Connection of Example 2.


Problem1: It is required to connect with Intel 8088 microprocessor a
SRAM memory with a total storage capacity of 128Kbyte. If the available
SRAM is of type 61128 (16Kx8), select the required number of SRAM
chips and select an address range for each chip, then design the
address decoder circuit. Draw the complete connection.
Problem2: It is required to connect with Intel 8088 microprocessor a
ROM memory with a total storage capacity of 128Kbyte. If the available
ROM is an EPROM of type 27256 (32Kx8), select the required number of
EPROM chips and select an address range for each chip, then design the
address decoder circuit. Draw the complete connection.

41 | P a g e

3.2 INTERFACING PERIPHERAL (I/O) DEVICES WITH INTEL 8088


MICROPROCESSOR: [3]
A large number of I/O devices (Peripherals) can be connected with the
microprocessor like keyboards, LEDs, 7-segment displays, ADC and DAC
converters, screens, printers .. These devices are connected with the
microprocessor through interfaces and through these interfaces the
microprocessor transfers data with peripheral devices.
The simplest types of interfaces are called I/O ports.
When I/O ports are connected with the microprocessor, each port must be
given one address called Port Address. Intel 8088 microprocessor can
give isolated addresses for 64Kbytes of input ports and 64kbytes of output
ports other than the addresses given to memory. That is using only
address lines A0-A15. So, the total isolated address range of I/O ports is
0000-FFFFH.
Each port can be given one 8-bit or 16-bit address and a separate address
decoder circuit must be designed and connected to give enable (chip
select) signals to these ports according to their addresses.
Data is transferred between I/O ports and I/O devices in two modes:
1. The Parallel Mode:
The data is exchanged between I/O devices and I/O ports one byte at a
time through 8 data lines. Examples of parallel I/O devices are keyboards,
displays, ADC and DAC converters.
2. The Serial Mode:
Data is transferred serially one bit at a time through one data line. Since
the microprocessor is a parallel device, the output port must have the
ability to change data from parallel-to-serial and give it to the serial output
device. Also, the input port must have the ability to change data from
serial-to-parallel and give it to the microprocessor.
Examples of serial I/O devices are modems, tape recorders, and USB
devices.

Connecting Simple (Non-Programmable) Parallel I/O


Ports With Intel 8088 Microprocessor: [3]

3.2.1

Tri-state buffers ICs are usually used as simple non-programmable input


ports to connect simple input devices like switches, keyboards and ADC
converters. For Examples:
1. IC 74LS244. It contains 8-bit (Octal) Tri-State Buffers. It has two
active-low enable lines G1 and G2.
42 | P a g e

2. IC 74LS126. It contains 4 tri-state buffers. Each buffer has a separate


active-high enable line E.
Latches ICs are usually used as simple non-programmable output ports
to connect simple output devices like LEDs, 7-segment displays and
DAC converters. For examples:
1. IC 74LS75. It contains 4-bit latches. It has two active-high enable
lines G1 and G2.
2. IC 74LS100. It contains 8-bit latches with one common active-high
enable line G.
Note:
IC 74LS373 has 8-bit (Octal) Latches with Tri-State Buffered outputs. The
latches have a common active-high enable line G, and the tri-state
buffers have a common active-low enable line OE. This IC can be used
as an 8-bit input port or an 8-bit output port.
I/O Ports Connection Example 1: It is required to connect with Intel
8088 microprocessor the following devices:
1. One simple parallel 8-bit input port.
2. One simple parallel 4-bit output port.
3. One simple parallel 8-bit output port.
The available parts are:
1. IC 74LS244 (Octal tri-state buffers with two enable lines G1
and G2).
2. IC 74LS75 (4-bit latches with two enable lines G1 and G2).
Assign an isolated address for each port then design the
address decoder circuit. Select the required ports from the ICs
given. Draw the complete circuit connection with the
microprocessor.
Solution:
a. Address
Address
Address
Address

selection:
1000H for the 8-bit input port.
2000H for the 4-bit output port.
3000H for the 8-bit output port.

b. I/O ports selection:


For the 8-bit input port we select one chip 74LS244.
For the 4-bit output port we select one chip 74LS75.
For the 8-bit output port we select two chips 74LS75.
c. The complete circuit design:
43 | P a g e

As shown in figure 3.4.

Figure 3.3 I/O Ports Circuit Connection of Example 1.


Problem3:
It is required to connect with Intel 8088 microprocessor the
following devices:
1. Two simple parallel 8-bit input ports.
2. Two simple parallel 8-bit output ports.
The available part is IC 74LS373 (8-bit Latches with Tri-State
Buffers. It has two enable lines G for the latches and OE for the
tri-state buffers).
44 | P a g e

Assign an isolated address for each port then design the


address decoder circuit. Select the required ports using the
given IC. Draw the complete circuit connection with the
microprocessor.

Connecting Programmable Parallel I/O Ports With Intel


8088 Microprocessor: [3]

3.2.2

There are a large number of programmable devices that can be


connected with the microprocessor. These devices can perform a large
number of functions that help the microprocessor to control the different
parts of the microcomputer or the microprocessor-based system and
free it to execute programs more quickly. These devices are
programmed by the microprocessor to do the required operations.
Examples of such devices are Programmable Interrupt Controllers,
Direct Memory Access Controllers, Keyboards Controllers, Cathode Ray
Tube (CRT) controllers, Programmable Parallel I/O ports and
Programmable Serial I/O ports.
Programmable Parallel I/O ports (also called Programmable Parallel
Interface Devices) are integrated circuits, each one contains a number
(2 , 3 or more) of I/O ports that can be programmed to be either input or
output ports and in different data transfer conditions ( unconditional I/O,
interrupt-driven
I/O,
I/O
with
handshaking).
Internally
each
Programmable Parallel I/O ports IC consists of:
1. A number of latches and tri-state buffers used as I/O ports.
2. A control register used by the microprocessor to program the ports,
which one is input and which one is output and the data transfer
conditions.
3. A status register that can be read by the microprocessor to know the
status of I/O ports.
An example of these ICs
Peripheral Interface (PPI).

is

Intel

8255A

Programmable
D0 D7

The external lines of this IC are shown in figure 3.5.


Microprocessor Side
Side

RESET

Peripherals
PA0 PA7
A0

RD

PC0 PC3 A1
PC4 PC7 WR
PB0 PB7

Intel 8255A
45 | P a g e
PPI
CS

Figure 3.5 The external lines of Intel 8255A PPI.


Externally the microprocessor side contains the data, address and
control lines that will be connected with the microprocessor. The
peripheral side contains the lines of three 8-bit I/O ports A, B and C.
Port A can be programmed as an 8-bit input or output or a bidirectional
port.
Port B can be programmed as an 8-bit input or output port.
Port C is divided into two 4-bit sections, each section can be
programmed as a 4-bit input or output port.
Internally each port consists of some latches and tri-state buffers. But,
when PPI is connected with the microprocessor, the microprocessor will
consider it as an IC with four 8-bit registers:
- Three registers are the ports A, B and C and they are read/write
registers.
- The fourth register is the control register which is a write only
register.
Each register will have a separate address when connected with the
microprocessor. The address is determined by the logic level of address
lines A0 and A1 as shown in table 3.1:

Table 3.1

A1
0
0
1
1

A0
0
1
0
1

The Register
Port A
Port B
Port C
Control Register

Addressing of Intel

8255A PPI Registers.


If, for example, PPI is connected with Intel 8088 microprocessor, and it is
given an isolated address 2000H, then port A address will be 2000H,
port B address will be 2001H, port C address will be 2002H and the
control register will be 2003H and PPI will have a total address range
2000H-2003H.

46 | P a g e

1.
2.

I/O Ports Connection Example 2: It is required to connect with Intel


8088 microprocessor the following devices:
Three parallel programmable 8-bit ports.
One simple parallel 8-bit input port.
The available parts are:
1. Intel 8255A PPI.
2. IC 74LS373 (8-bit Latches with Tri-State Buffers. It has two enable
lines G for the latches and OE for the tri-state buffers).
Select the required ports. Assign isolated I/O addresses to these
ports. Design the address decoder circuit. Draw the complete circuit
connection with the microprocessor.
Solution:
a. I/O ports selection:
For the three programmable 8-bit ports one chip of 8255A PPI
will be enough.
For the 8-bit parallel simple input port one chip 74LS373 will be
used.
b. Address Selection:
PPI can be given the address range 1000H-1003H.
The simple input port can be given the address 2000H.
c. The complete circuit design:
As shown in figure 3.6.

47 | P a g e

Figure 3.6 I/O Ports Circuit Connection of Example 2.


Problem 4: A number of I/O ports have to be added to a
microprocessor-based system
which uses Intel 8088 microprocessor. These ports are:
i. Three programmable parallel 8-bit ports.
ii. One simple parallel 8-bit output port.
iii. One simple parallel 8-bit input port.
-

The available parts are:


IC 74LS244 (Octal Tri-state buffers with two enable lines G1 and G2).
IC 74LS100 (8-bit Latches with one enable line G).
Intel 8255A PPI.
Design the required interfaces by selecting the I/O ports. Assign isolated
I/O addresses to these ports. Design the address decoder circuit. Draw
the complete circuit connection with the microprocessor system bus.

Connection of Some Parallel I/O Devices With Intel


8088 Microprocessor: [3]

3.2.3

48 | P a g e

3.2.3.1

Connection of LED 7-Segment Displays: [3]

Both common-anode and common-cathode LED 7-segment displays


can be connected with the microprocessor through output ports
which have enough current driving ability like 74LS100, 74LS75 and
743LS73. Intel 8255A PPI can be used and the needed ports must be
programmed as output ports.
I/O Devices Connection Example 1: Connecting two LED commonanode displays using two 8-bit output ports which are two ICs of type
74LS373 (8-bit Latches with Tri-State Buffers). The complete
connection is shown in figure 3.7.

Figure 3.7 Connection of two LED common-anode displays using two


8-bit output ports.
I/O Devices Connection Example 2: Connecting eight LED
common-cathode 7-segment displays using two 8-bit output ports
which are two ICs of type 74LS373 (8-bit Latches with Tri-State
Buffers).
The connection method is called Multiplexed Scanned Displays
and it shown in figure 3.8.
49 | P a g e

Figure 3.8 Multiplexed Scanned Displays Connection.


50 | P a g e

3.2.3.2

Connection of Keyboards and Switches: [3]

I/O Devices Connection Example 3: Connection of an array of 8


switches or keys through an 8-bit input port which is IC 74LS244 as
shown in figure 3.9 below:

Figure 3.9 Connection of an array of 8 switches or keys through an 8bit input port.
I/O Devices Connection Example 4: Connection of a matrix of 4x4
keys using a 4-bit input port and a 4-bit output port. In this
connection the microprocessor uses a method called "Matrix Scan"
to detect which keys are pressed. The rows are connected through
the input port and the columns through the output port. In this
examples lines PC0-PC3 of port C of Intel 8255A are programmed as
inputs, while PC4-PC7 line are programmed as output shown in figure
3.10.

51 | P a g e

Figure 3.10 Connection of a matrix of 4x4 keys through PPI.

3.2.3.3

Connection of Digital-to-Analog Converters (DACs): [3]

A DAC is connected with the microprocessor using an output port.


I/O Devices Connection Example 5: Connection of an 8-bit DAC
using IC 74LS100 ( 8-bit latches) as shown in figure 3.11 below:

Figure 3.11 Connection of an 8-bit DAC using an 8-bit output port.

3.2.3.4

Connection of Analog-to-Digital Converters (ADCs): [3]

An ADC is connected with the microprocessor using an input port.


I/O Devices Connection Example 6: Connection of an 8-bit ADC
using port A and some lines of port C of Intel 8255A PPI. The circuit
connection is shown in figure 3.12.
52 | P a g e

Figure 3.12 Connection of an 8-bit ADC through PPI.


Problem 5: Through only one piece of IC type Intel 8255A PPI,
connect of the following I/O devices with Intel 8088 microprocessor:
1. Two Common Cathode LED 7-Segment Display.
2. One 8-bit ADC converter.
3. An array of 3 keys.
Assign an isolated I/O address to PPI and draw complete connection of
this IC with the microprocessor.

3.3 SERIAL COMMUNICATION INTERFACES: [3]


A serial communications interface (SCI) is a device that enables the serial
(one bit at a time) exchange of data between a microprocessor and
peripherals such as printers, external drives, scanners, or mice. The SCI
contains a parallel-to-serial converter that serves as a data transmitter,
and a serial-to-parallel converter that serves as a data receiver.
The most significant advantage of a SCI is simpler wiring. In addition, serial
interface cables can be longer than parallel interface cables, because
there is much less interaction (crosstalk) among the conductors in the
cable.

3.3.1

What is a UART Device? [3]

UART is a Universal Asynchronous Receiver/Transmitter device which is


the key component of the serial communications interface of a
computer. The UART is usually a programmable IC that has the internal
53 | P a g e

structure which enables it to provide the serial communication between


the microprocessor and serial peripheral devices. The UART takes
parallel bytes of data and transmits the individual bits in a sequential
fashion. At the destination, a second UART re-assembles the serial bits
into complete parallel bytes. One example of such devices is Intel 8250
UART.

Chapter Four
Introduction to PIC Microcontrollers
4.1 THE PIC MICROCONTROLLER FAMILY: [4]
The PIC microcontroller family of microcontrollers is manufactured by Microchip Technology
Inc. Currently they are one of the most popular microcontrollers used in many commercial
and industrial applications. The PIC microcontroller architecture is based on a modified
Harvard RISC (Reduced Instruction Set Computer) instruction set from only 6 pins to 80 pins,
and from 384 bytes to 128 Kbytes of program memory.
PIC microcontrollers are available with many different specifications
depending on:
Memory Type
Flash
OTP (One-time-programmable)
ROM (Read-only-memory)
ROMless
InputOutput (I/O) Pin Count
418 pins
2028 pins
3244 pins
45 and above pins
Memory Size
54 | P a g e

0.51K
24K
816K
2432K
4864K
96128K
Special Features
CAN
USB
LCD
Motor Control
Radio Frequency
The models of PIC microcontrollers are upward compatible with each other
and a program developed for one model can very easily, and in many
cases with no modifications, be run on other models of the family. This is
because the basic assembler instruction set of PIC microcontrollers
consists of only 33 instructions and most of the family members (except
the newly developed devices) use the same instruction set.
All PIC microcontrollers offer the following features:
RISC instruction set.
Digital I/O ports
On-chip timer with 8-bit prescaler
Power-on reset
Watchdog timer
Power saving SLEEP mode
High source and sink current
Direct, indirect, and relative addressing modes
External clock interface
RAM data memory
EPROM or Flash program memory
Some devices offer the following additional features:
Analogue input channels
Analogue comparators
Additional timer circuits
EEPROM data memory
External and internal interrupts
Internal oscillator
Pulse-width modulated (PWM) output
USART serial interface
Some even more complex devices in the family offer the following
additional features:
CAN bus interface
55 | P a g e

I2C bus interface


SPI bus interface
Direct LCD interface
USB interface
Motor control

Although there are several hundred models of PIC microcontrollers,


choosing a microcontroller for an application is not a difficult task and
requires taking into account these factors:
Number of I/O pins required
Required peripherals (e.g. USART, USB)
The minimum size of program memory
The minimum size of RAM
Whether or not EEPROM non-volatile data memory is required
Speed
Physical size
Cost.
You should always try to find the model which satisfies your minimum
requirements and the one which does not offer more than you may need.
For example, if you require a microcontroller with only 8 I/O pins and if
there are two identical microcontrollers, one with 8 and the other one with
16 I/O pins, you should select the one with 8 I/O pins.
Although there are several hundred models of PIC microcontrollers, the
family can be broken
down into three main groups, which are:
12-bit instruction word (e.g. 12C5XX, 16C5X)
Table 4.1 lists some of the devices in this group. Figure 4.1 shows the pin configuration of the
PIC16C56 microcontroller.

Table 4.1 Some PIC Microcontrollers of 12-bit instruction word group.

56 | P a g e

Figure 4.1 Pin configuration of the PIC16C56 microcontroller.


14-bit instruction word (e.g. 16F8X, 16F87X)
Table 4.2 lists some of the devices in this group. Figure 4.2 shows the pin configuration of the
PIC16F877 microcontroller.

Table 4.2 Some PIC Microcontrollers of 14-bit instruction word group.

57 | P a g e

Figure 4.2 Pin configuration of the PIC16F877 microcontroller.


16-bit instruction word (e.g. 17C7XX, 18C2XX)
16-bit microcontrollers are at the high-end of the PIC microcontroller family. Table 4.3 lists some
of the devices in this group.

Table 4.3 Some PIC Microcontrollers of 16-bit instruction word group.


Instructions occupy only one word in memory, thus increasing the code
efficiency and reducing the required program memory. Instructions and
data are transferred on separate buses, thus the overall system
performance is increased. All memory for the PIC microcontroller family is internal
and it is usually not very easy to extend this memory externally. The program memory is
usually sufficient for small to medium size projects. But the data memory is generally small
and may not be enough for medium to large projects unless a bigger and more expensive
58 | P a g e

member of the family is chosen. For some large projects even this may not be enough and the
designer may have to sacrifice the I/O ports to interface
an external data memory, or to choose a microcontroller from a different manufacturer.

4.2 INSIDE A PIC MICROCONTROLLER: [4]


4.2.1

Program Memory (Flash): [4]

The program memory is where your PicBasic or MikroBasic Pro or MikroC Pro program
resides. PIC microcontrollers are based on the flash technology where the memory chip
can be erased or re-programmed using a programmer device. Most PIC microcontrollers
can also be programmed without removing them from their circuits. This process (called
in-circuit serial programming or ISP). Although the program memory is mainly used to
store a program, there is no reason why it cannot be used to store constant data used in
programs. PIC microcontrollers can have program memories from 0.5 to over 16 K.
It is interesting to note that PICs are known as 8-bit microcontrollers. This is actually true
as far as the width of the data memory is concerned, which is 8-bits wide.
When power is applied to the microcontroller or when the MCLR input is lowered to logic
0, execution start from the Reset Vector, which is the first word located at address 0 the
program memory.

4.2.2

Data Memory (RAM): [4]

The data memory is used to store all of the program variables. All data will be lost when
power is removed. The width of the data memory is 8-bits wide. The data memory in a PIC
microcontroller consists of banks where some models may have only 2 banks, some
models 4 banks, and so on.

4.2.3

Register File Map and Special Function Registers: [4]

Register File Map (RFM) is a layout of all the registers available in a


microcontroller and this is extremely useful when programming the
device, especially when using an assembly language.
The RFM is divided into two parts: the Special Function Registers (SFR),
and the General Purpose Registers (GPR). For example, on a PIC16F84
microcontroller there are 68 GPR registers used to store temporary data.
SFR is a collection of registers used by the microcontroller to control the
internal operations of the device. Depending upon the complexity of the
devices the number of registers in the SFR varies. It is important that
the programmer understands the functions of the SFR registers fully
since they are used both in assembly language and in high-level
languages.
Depending on the model of PIC microcontroller used there could be
other registers. You need not know the operation of some of the
registers since MikroBasic Pro or MikroC Pro compiler loads these
59 | P a g e

registers automatically. For example, writing and reading from the


EEPROM are controlled by SFR registers EECON1, EECON2, EEADR, and
EEDATA. But fortunately, MikroBasic or MikroC Pro compilers provide
simple high-level instructions for writing to and reading from the
EEPROM and thus you do not need to know how to load these registers.
Some of the important SFR registers that you may need to configure
while programming using a high-level language are
OPTION register
I/O registers
Timer registers
INTCON register
A/D converter registers
The functions and the bit definitions of these registers are described in
detail in the following sections.

4.2.3.1

OPTION register: [4]

This register is used to setup various internal features of the


microcontroller and is named as OPTION_REG. This is a readable and
writable register which contains various control bits to configure the
on-chip timer and the watchdog timer. This register is at address 81
(hexadecimal). The OPTION REG register is also used to control the
external interrupt pin RB0.

4.2.3.2

I/O registers: [4]

These registers are used for the I/O control. Every I/O port in the PIC
microcontroller has two registers: port data register and port
direction control register.
Port data register has the same name as the port it controls. For
example, PIC16F84 microcontroller has two port data registers PORTA
and PORTB. A PIC16F877 microcontroller has 5 port data registers
PORTA, PORTB, PORTC, PORTD, and PORTE. An 8-bit data can be sent
to any port, or an 8-bit data can be read from the ports. It is also
possible to read or write to individual port pins. For example, any bit
of a given port can be set or cleared, or data can be read from one or
more port pins at the same time.
Ports in a PIC microcontroller are bi-directional. Thus, each pin of a port can be used as
an input or an output pin. Port direction control register configures the port pins as
either inputs or outputs. This register is called the TRIS register and every port has a
TRIS register named after its port name. For example, TRISA is the direction control
register for PORTA. Similarly, TRISB is the direction control register for PORTB and
so on.
Setting a bit in the TRIS register makes the corresponding port register pin an input.
Clearing a bit in the TRIS register makes the corresponding port pin an output. For
60 | P a g e

example, to make bits 0 and 1 of PORTB input and the other bits output, we have to
load the TRISB register with the bit pattern:
00000011
Figure 4.3 shows the TRISB register and the direction of PORTB pins.

Figure 4.3 TRISB register and the direction of PORTB pins.


Port data register and port direction control registers can be accessed directly using the
MikroBasic Pro or MikroC Pro compiler as we shall see in a later chapter.

4.2.3.3

Timer registers: [4]


Depending on the model used, some PIC microcontrollers have only one timer, and
some may have up to 3 timers. For example PIC16F84 microcontroller which has only
one timer which is an 8-bit register (called TMR0) which can be used as a timer or a
counter. When used as a counter, the register increments each time a clock pulse is
applied to pin T0CK1 of the microcontroller. When used as a timer, the register
increments at a rate determined by the system clock frequency and a prescaler selected
by register OPTION_REG. A timer interrupt is generated when the timer overflows
from 255 to 0. This interrupt can be enabled or disabled by our program.
Most microcontrollers have at least one watchdog facility. The watchdog is basically a
timer which is refreshed by the user program and a reset occurs if the program fails to
refresh the watchdog. A watchdog is a safety feature that prevents runaway software
and stops the microcontroller from executing meaningless and unwanted code.
Watchdog facilities are commonly used in real-time systems where it is required to
regularly check the successful termination of one or more activities. The watchdog
timers oscillator is independent from the CPU clock and the time-out is 18 ms.
Some PIC Microcontrollers have additional timers like TMR1 and TMR2.

4.2.3.4

INTCON register: [4]


This is the interrupt control register. This register is at address 0 and 8BH of the
microcontroller RAM. It is used to enable the interrupts like the external interrupts
from pin INT (RB0) and timer interrupts.

4.2.3.5

A/D Converter registers: [4]

61 | P a g e

The A/D converter is used to interface analog signals to the


microcontroller. The A/D converts analog signals (e.g. voltage) into
digital form so that they can be processed. A/D converter registers
are used to control the A/D converter ports.
On most PIC microcontrollers equipped with A/D, PORTA pins are used
for analog input and these port pins are shared between digital and
analogue functions. For example PIC16F877 includes 8 A/D
converters. There is actually only one A/D converter and the inputs
are multiplexed and they share the same converter. The width of the
A/D converter can be 8-bits or 10-bits. PIC16F877 has a 10-bit
converter. An A/D converter requires a reference voltage to operate.
This reference voltage is chosen by programming the A/D converter
registers and is typically +5V. Thus, if it is a 10-bit converter, then
analog voltages with a resolution of 4.88mV can be measured. For
example, if the measured analog input voltage is 4.88mV we get the
10-bit digital number 0000000001.
In PIC16F877 the A/D converter is controlled by registers ADCON0
and ADCON1.
The bits of ADCON0 are used to select the conversion clock (internal
RC oscillator or external clock) and to select which input pin is routed
to the A/D converter and to turn on the A/D converter circuitry.
ADRESH and ADRESL are the A/D converter result registers.
ADCON1 is the second A/D control register. This register controls the
format of converted data and mode of the PORTA inputs. The format
of converted data can be either left justified or right justified. The
mode of the PORTA inputs can be programmed to be a digital pin or
an analogue pin. For example, if we set Bits 0-3 of ADCON1 to 0110
then all PORTA pins will be digital I/O pins. Bits 0-3 bits of ADCON1
can also be used to define the reference voltage for the A/D
converter.

4.2.4

Oscillator Circuits: [4]

An Oscillator circuit is used to provide a microcontroller with a clock. A


clock is needed so that the microcontroller can execute a program. PIC
microcontrollers have built-in oscillator circuits and this oscillator can be
operated in one of five modes.
LP Low-power crystal
XT Crystal/resonator
HS High-speed crystal/resonator
RC resistor capacitor
Internal Oscillator (No external components, only in some PIC
microcontrollers)
62 | P a g e

In LP, XT, or HS modes, an external oscillator can be connected to the


OSC1 input. This can be a crystal-based oscillator, or simple logic gates
can be used to design an oscillator circuit.
The external crystal connection is shown in figure 4.4. The two
capacitors connected to OSC1 and OSC2 inputs should be chosen as in
Table 4.4.

Figure 4.4 Crystal Oscillator Circuit.

Table 4.4 Capacitor Selection for Crystal Operation.


Resonators are available from 4 to about 8 MHz. They are not as
accurate as crystal-based oscillators. Resonators are usually 3-pin
devices and the two pins at either sides are connected to OSC1 and
OSC2 inputs of the microcontroller. The middle pin is connected to the
ground.
RC oscillator is used for applications where the timing accuracy is not
important. An external resistor and a capacitor are connected to the
OSC1 input of the microcontroller as The oscillator frequency depends
upon the values of the resistor and capacitor, the supply voltage, and to
the temperature. For most applications, using a 5 K resistor with a 20 pF

63 | P a g e

capacitor gives about 4 MHz and this may be acceptable in non-time


critical applications.
Some PIC microcontrollers (e.g. PIC12C672 and PIC16F628) have built-in
oscillator circuits and they do not require any external timing
components. The built-in oscillator is usually set to operate at 4 MHz
and is selected during the programming of the device.

4.2.5

Reset Circuit: [4]

Reset is used to put the microcontroller into a known state. Normally


when a PIC microcontroller is reset execution starts from address 0 of
the program memory. This is where the first executable user program
resides. The reset action also initializes various SFR registers inside the
microcontroller. PIC microcontrollers can be reset when one of the
following conditions occur:
Reset during power on (POR Power ON Reset)
Reset by lowering MCLR input to logic 0
Reset when the watchdog overflows.
The Power ON reset is shown in figure 4.5. A simple circuit used to get a
forced external reset is shown in figure 4.6.

Figure 4.5 Using Power ON Reset.

Figure 4.6 Using an external Reset button.


64 | P a g e

4.2.6

The Configuration Word: [4]

PIC microcontrollers have a special register called the Configuration


Word. This is a 14-bit register. This cannot be directly accessed in a
program. This register can be accessed during the programming of the
microcontroller. The configuration word stores the following information
about a PIC microcontroller:
Code protection bits: These bits are used to protect blocks of memory
so that they cannot be read.
Power-on timer enable bit.
Watchdog (WDT) timer enable bit.
Oscillator selection bits: The oscillator can be selected as XT, HS, LP,
RC, or internal (if supported by the microcontroller).
For example, in a typical application we can have the following
configuration word selection during the programming of the
microcontroller:
Code protection OFF
XT oscillator selection
WDT disabled
Power-up timer enables.

4.2.7

I/O Interfacing: [4]

A PIC microcontroller port can source and sink 25 mA of current. When sourcing current,
the current is flowing out of the port pin, and when sinking current, the current is flowing
into the pin. When the pin is sourcing current, one pin of the load is connected to the
microcontroller port and the other pin to the ground (see figure 4.7a). The load is then
energized when the port output is at logic 1. When the pin is sinking current, one pin of the
load is connected to the supply voltage and the other pin to the output of the port (see
figure 4.7b). The load is then energized when the port output is at logic 0.

Figure 4.7 Current Sourcing and Sinking of a PIC.


So, some external devices like LEDs and displays can be connected with output port lines
through suitable current limiting resistors. Other devices like relays, bulbs and stepper
motor require suitable driving circuits when connected with output port lines.
65 | P a g e

Chapter Five
Programming PIC Microcontrollers using mikroBasic Pro
Language
5.1 MIKROBASIC PRO PROGRAMMING LANGUAGE: [5]
The mikroBasic PRO for PIC is a powerful, feature-rich development tool for
PIC microcontrollers. It is designed to provide the programmer with the
easiest possible solution to developing applications for embedded
systems.

5.1.1

Program Organization: [5]

Any program is constructed of one or more modules. One of the


modules must be the main module. The main module should as the following:
program <program name>
include <include other modules>
'********************************************************
'* Declarations (globals):
'********************************************************
' symbols declarations
symbol ...
' constants declarations
const ...
' structures declarations
structure ...
' variables declarations
dim Name[, Name2...] as [^]type [absolute 0x123] [external] [volatile]
[register] [sfr]
' procedures declarations
sub procedure procedure_name(...)
<local declarations>
...
end sub
' functions declarations
sub function function_name(...) as return_type
<local declarations>
...
end sub
'********************************************************
'* Program body:
'********************************************************
main:
66 | P a g e

' write your code here


end.

The global variables are visible throughout the whole module, from the place of
declaration. Local variables, parameters, function results are within the block in which
they are declared.

5.1.2

Variables: [5]

A variable is an object whose value can be changed during the runtime.


Every variable is declared before using it by giving it a name which
must be a valid identifier. This name is used for accessing the memory
location occupied by a variable. Specifying a data type for each variable
is mandatory. Syntax for variable declaration is:
dim identifier_list as type
Here are a few examples:
dim i, j, k as byte
dim counter, temp as word
Every declared variable consumes part of RAM memory. There is no
need to declare SFRs explicitly, as mikroBasic PRO for PIC automatically
declares relevant registers as global variables of word. For example:
PORTA, TMR0, etc.

5.1.3

Constants: [5]

Constant is a data whose value cannot be changed during the runtime. Constants are
declared in the declaration part of a program or routine. You can declare any number of
constants after the keyword const:
const constant_name [as type] = value
Every constant is declared under unique constant_name which must be a valid
identifier. It is a tradition to write constant names in uppercase. Constant requires you to
specify value, which is a literal appropriate for the given type. type is optional. type
must be used only when declaring a constant array.
Here are a few examples:
const
const
const
const
const

5.1.4

MAX as longint = 10000


MIN = 1000
' compiler will assume word type
SWITCH = "n"
' compiler will assume char type
MSG = "Hello"
' compiler will assume string type
MONTHS as byte[12] = (31,28,31,30,31,30,31,31,30,31,30,31)

Identifiers: [5]

Identifiers are arbitrary names of any length given to functions,


variables, symbolic constants, user-defined data types and labels.
67 | P a g e

Identifiers can contain letters from a to z and A to Z, the underscore


character _ and digits from 0 to 9. First character must be a letter or
an underscore, i.e. identifier cannot begin with a numeral.
Note: mikroBasic Pro language is not a case sensitive. For example
identifiers teacher, TEACHER and TeaCher are related to only one
constant or variable in the program.
Note: Keywords (reserved words) cannot be used as identifiers such as
if, while, integer, bit, org.
Here are some valid identifiers:
Pressure
no_hit
dat2string
_vtext
Here are some invalid identifiers:
5sum
' NO -- because begin with a numeral
%high
' NO -- because contain special characters
xor
' NO -- because match reserved word
j23.62
' NO because contain special characters

5.1.5

Data Types: [5]

5.1.5.1

Simple Types: [5]

Simple types represent types that cannot be divided into more basic. Simple types in
mikroBasic PRO for PIC are listed in table 5.1.

Type
bit
sbit
byte, char
short
word
integer
longword
longint
float

Size
1bit
1bit
8bit
8bit
16bit
16bit
32bit
32bit
32bit

Range
0 or 1
0 or 1
0 .. 255
-127 .. 128
0 .. 65535
-32768 .. 32767
0 .. 4294967295
-2147483648 .. 2147483647
1.17549435082 * 10-38 .. 6.80564774407 * 1038

Table 5.1 Simple types in mikroBasic PRO for PIC.

5.1.5.2

Derived Types: [5]


68 | P a g e

These are the types that are constructed from the simple data types.
We will study only two of these types which are arrays and strings.
a. Arrays:
An array represents an indexed collection of elements of the same
type. Each element has a unique index. The array can be one
dimensional or multidimensional array. Variable array declaration has
the following syntax:
dim identifier as type [array_length1] [array_length2]
Indexing is zero based. So, each of the elements of an array is
numbered from 0 through array_length - 1. Every element of an
array is of type and can be accessed by specifying array name
followed by elements index within brackets. Here are a few examples
of array declaration:
dim weekdays as byte[7]
dim samples as word[50]
dim m as integer[5][10] ' 2-dimensional array of size 5x10
main:
' Now we can access elements of array variables, for example:
samples[0] = 1
weekdays[5] = 4
m[0][4]=-150
Constant array is initialized by assigning it a comma-delimited
sequence of values within parentheses. For example:
const
MONTHS
as
(31,28,31,30,31,30,31,31,30,31,30,31)

byte[12]

b. Strings:
A string represents a sequence of characters equivalent to an array of char. It is
declared like this:
string[string_length]
The specifier string_length is a number of characters a string consists of. The
string is stored internally as the given sequence of characters plus a final null
character (zero). A null string ("") is stored as a single null character. You can assign
string literals or other strings to string variables. The string on the right side of an
assignment operator has to be shorter than another one, or of equal length. For
example:
dim msg1 as string[20]
dim msg2 as string[19]
69 | P a g e

main:
msg1 = "This is some message"
msg2 = "Yet another message"
msg1 = msg2 ' this is ok, but vice versa would be illegal
Alternately, you can handle strings elementbyelement. For example:
dim s as string[5]
' ...
s = "mik"
' s[0] is char literal "m"
' s[1] is char literal "i"
' s[2] is char literal "k"
' s[3] is zero
' s[4] is undefined
' s[5] is undefined

5.1.6

Literals: [5]

Literals are tokens representing fixed numeric or character values.

5.1.6.1

Integer Literals: [5]

Integral values can be represented in decimal, hexadecimal or binary notation.


In decimal notation, numerals are represented as a sequence of digits (without commas,
spaces or dots), with optional prefix + or - operator to indicate the sign. Values default
to positive (6258 is equivalent to +6258). The dollar-sign prefix ($) or the prefix 0x
indicates a hexadecimal numeral (for example, $8F or 0x8F). The percent-sign prefix
(%) indicates a binary numeral (for example, %01010000).
Here are some examples:
11
' decimal literal
$11
' hex literal, equals decimal 17
0x11
' hex literal, equals decimal 17
%11
' binary literal, equals decimal 3

5.1.6.2

Floating Point Literals: [5]

A floating-point value consists of:


Decimal integer
Decimal point
Decimal fraction
e or E and a signed integer exponent (optional)
70 | P a g e

You can omit either decimal integer or decimal fraction (but not both).
Here are some examples:
0.
-1.23
23.45e6
2e-5
3E+10
.09E34

5.1.6.3

'
'
'
'
'
'

=
=
=
=
=
=

0.0
-1.23
23.45 * 10^6
2.0 * 10^-5
3.0 * 10^10
0.09 * 10^34

Character Literals: [5]

Character literal is one character from the extended ASCII character set, enclosed with
quotes (for example, "A"). Character literal can be assigned to variables of byte and
char type (variable of byte will be assigned the ASCII value of the character). Also,
you can assign character literal to a string variable.

5.1.6.4

String Literals: [5]

String literal is a sequence of characters from the extended ASCII character set,
enclosed with quotes. Length of string literal is a number of characters it consists of.
String is stored internally as the given sequence of characters plus a final null
character. String literal with nothing in between the quotes (null string) is stored as a
single null character. You can assign string literal to a string variable or to an array of
char. Here are several string literals:
"Hello world!"
"Temperature is stable"
" "
"C"
""

5.1.7

'
'
'
'
'

message, 12 chars long


message, 21 chars long
two spaces, 2 chars long
letter, 1 char long
null string, 0 chars long

Operators: [5]

Operators are tokens that trigger some computation when being applied to variables and
other objects in an expression.
There are four types of operators in mikroBasic PRO for PIC:
Arithmetic Operators
Relational Operators
Boolean Operators
Bitwise Operators

5.1.7.1

Arithmetic Operators: [5]

71 | P a g e

Arithmetic
operators
are
used
to
perform
mathematical
computations. They have numerical operands and return numerical
results. Since the char operators are technically bytes, they can be
also used as unsigned operands in arithmetic operations.
All arithmetic operators associate from left to right. Arithmetic
operators used in mikroBasic PRO for PIC are listed in table 5.2.
Operator
+
*
/
div

mod

Operation

Operands
byte, short, word, integer,
addition
longint, longword, float
byte, short, word, integer,
subtraction
longint, longword, float
byte, short, word, integer,
multiplication
longint, longword, float
byte, short, word, integer,
division, floating-point
longint, longword, float
division, rounds down to byte, short, word, integer,
nearest integer
longint, longword
modulus, returns the
remainder of integer
byte, short, word, integer,
division (cannot be used longint, longword
with floating points)

Result
byte, short, word, integer,
longint, longword, float
byte, short, word, integer,
longint, longword, float
word, integer, longint,
longword, float
float
byte, short, word, integer,
longint, longword
byte, short, word, integer,
longint, longword

Table 5.2 Arithmetic operators used in mikroBasic PRO for PIC.

5.1.7.2

Relational Operators: [5]

Use relational operators to test equality or inequality of expressions. All relational


operators return TRUE or FALSE. Relational operators used in mikroBasic PRO
for PIC are listed in table 5.3.
Operator
=
<>
>
<
>=
<=

Operation
equal
not equal
greater than
less than
greater than or equal
less than or equal

Table 5.3 Relational operators used in mikroBasic PRO for PIC.


The equal sign (=) can also be an assignment operator, depending on context.

5.1.7.3

Boolean Operators: [5]

Although mikroBasic PRO for PIC does not support boolean type, Boolean operators
are used for building complex conditional expressions. These operators conform to
72 | P a g e

standard Boolean logic and return either TRUE or FALSE. Boolean operators used
in mikroBasic PRO for PIC are listed in table 5.4.
Operator
and
or
xor
not

Operation
logical AND
logical OR
logical exclusive OR (XOR)
logical negation

Table 5.4 Boolean operators used in mikroBasic PRO for PIC.

5.1.7.4

Bitwise Operators: [5]

Bitwise operators are used to modify individual bits of their numerical


operands . These operators are and, or, xor, not, << (shift left),
>> (shift right). For example:
0x1234 and 0x5678
' equals 0x1230
' because ..
' 0x1234 : 0001 0010 0011 0100
' 0x5678 : 0101 0110 0111 1000
' ---------------------------'
and : 0001 0010 0011 0000

5.1.8

Statements: [5]

Statements define algorithmic actions within a program. In the absence of specific jump
and selection statements, statements are executed sequentially in the order of appearance in
the source code.

5.1.8.1

Assignment Statements: [5]

Assignment statements have the following form:


variable = expression
The statement evaluates expression and assigns its value to variable.
All the rules of implicit conversion are applied. Variable can be any
declared variable or array element, and expression can be any
expression. Examples:
a=b*5
x= 0xFFAB or 0x36C0

5.1.8.2

If Statement: [5]

The syntax of the if statement has the following form:


if expression then
73 | P a g e

statements
[else
other statements]
end if
When expression evaluates to true, statements execute. If expression is
false, other statements execute. The expression must convert to a boolean
type; otherwise, the condition is ill-formed. The else keyword with an alternate block
of statements (other statements) is optional. Examples:
if x<>y then
x= y/2
y= x*3
else
x= y*3
y=x/2
end if
if (a>7) and (a<1) then
a= a + 4
end if

5.1.8.3

Nested If Statements: [5]

Nested if statements require additional attention. A general rule is that the nested
conditionals are parsed starting from the innermost conditional, with each else bound
to the nearest available if on its left:
if expression1 then
if expression2 then
statement1
else
statement2
end if
end if
The compiler treats the construction in this way:
if expression1 then
if expression2 then
statement1
else
statement2
end if
74 | P a g e

end if
In order to force the compiler to interpret our example the other way around, we have
to write it explicitly:
if expression1 then
if expression2 then
statement1
end if
else
statement2
end if

5.1.8.4

Select Case Statement: [5]

Use the select case statement to pass control to a specific program branch, based
on a certain condition. The select case statement consists of selector expression
(condition) and list of possible values. The syntax of the select case statement is:
select case selector
case value_1
statements_1
...
case value_n
statements_n
[case else
default_statements]
end select
selector is an expression which should evaluate as integral value. values can be
literals, constants or expressions and statements can be any statements. The case
else clause is optional.
First, the selector expression (condition) is evaluated. The select case
statement then compares it against all available values. If the match is found, the
statements following the match evaluate, and the select case statement
terminates. In case there are multiple matches, the first matching statement will be
executed. If none of the values matches the selector, then
default_statements in the case else clause (if there is one) are executed.
Here is a simple example of the select case statement:
select case operator
case "*"
75 | P a g e

res = n1 * n2
case "/"
res = n1 / n2
case "+"
res = n1 + n2
case else
res = 0
end select
Also, you can group values together for a match. Simply separate the items by commas:
select case reg
case 0
opmode = 0
case 1,2,3,4
opmode = 1
case 5,6,7
opmode = 2
end select

5.1.8.5

For Statement: [5]

The for statement implements an iterative loop and requires you to specify the
number of iterations. The syntax of the for statement is:
for counter = initial_value to final_value [step
step_value]
statement_list
next counter
Note that the parameter step_value may be negative, allowing you to create a
countdown. Examples:
s = 0
for i = 0 to 9
s = s + a[i] * b[i]
next i
sum=0
for J = 10 to 50 step 2
sum = sum + J
next J

5.1.8.6

While Statement: [5]

Use the while keyword to conditionally iterate a statement. The syntax of the while
statement is:
while expression
76 | P a g e

statements
wend
statements are executed repeatedly as long as expression evaluates true. The
test takes place before statements are executed. Thus, if expression evaluates
false on the first pass, the loop does not execute. Example:
s = 0
i = 0
while i < 10
s = s + a[i] * b[i]
i = i + 1
wend
Probably the easiest way to create an endless loop is to use the statement:
while TRUE
' ...
Wend

5.1.8.7

Do Statement: [5]

The do statement executes until the condition becomes true. The syntax of the do
statement is:
do
statements
loop until expression
statements are executed repeatedly until expression evaluates true.
expression is evaluated after each iteration, so the loop will execute statements
at least once.
Here is an example of calculating scalar product of two vectors, using the do
statement:
s = 0
i = 0
do
s = s + a[i] * b[i]
i = i + 1
loop until i = n
77 | P a g e

5.1.8.8

Break Statement: [5]

Break is used to stop (for, while, or do) loop from within its body. Example:
s = 0
i = 0
while TRUE
s = s + a[i] * b[i]
i = i + 1
if i=10 break
wend
s=s+55

5.1.8.9

Goto Statement: [5]

Goto statement is used to unconditionally jump to a local label.


goto label_name
Example:
s = 0
i = 0
while TRUE
s = s + a[i] * b[i]
i = i + 1
if i=10 goto calculate
wend
calculate:
s=s+55
Label and goto statement must belong to the same block. Hence it is not possible to
jump into or out of a procedure or function.
You can use goto to break out from any level of nested control structures. Never jump
into a loop or other structured statement, since this can have unpredictable effects.

5.1.8.10 Asm Statement: [5]


Assembly instructions can be grouped in the program by using asm keyword:

asm
78 | P a g e

block of assembly instructions


end asm
In mikroBasic PRO for PIC comments are not allowed in embedded assembly code.

5.2 MIKROBASIC PROGRAMMING EXAMPLES: [5]


5.2.1
Reading the States of Four Switches and Outputting
Them on Four LEDs:
In this application, four switches are connected to RA0-RA3 and four
LEDs are connected to RB0-RB3 of PIC 16F627. The program is
continuously monitoring the states of the four switches and outputting
them on the LEDs.
program SwitchesLEDs
' Declarations section
Dim x as Byte
main:
' Main program
CMCON=0x07
' Turn comparators off and enable PORTA pins for
digital I/O functions
TRISA=0xFF
' Set all PORTA pins as inputs
TRISB=0x00
' Set all PORTB pins as outputs
PORTB=0
' Turn OFF all LEDs
While TRUE
x=PORTA
' Input the states of switches and save them as variable
x
PORTB=x
' Output the states of switches on LEDs
Wend
end.

5.2.2
Displaying the Numbers 0-9 Continuously on an LED 7Segment Display:
In this application, a Common Cathode LED 7-Segment Display is
connected to RB0-RB7 of PIC 16F627. The program is continuously
displaying the numbers 0-9 on the display with a time delay of one
second between successive numbers.
program DisplayingDigits
' Declarations section
Const
Digits
as
Byte[10]=(0x3F,0x06,0x5B,0X4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F)
Dim x as Byte
79 | P a g e

main:
' Main program
TRISB=0x00
While TRUE
For x=0 To 9
PORTB=Digits[x]
Delay_ms(1000)
Next x
Wend
end.

5.2.3

Displaying Messages on the LCD Display:

In this application, a 16x2 LCD Display is connected to RB0-RB7 of PIC


16F627. The program is continuously displaying the message
Microprocessor Systems on the display.
program LcdDisplay
' Declarations section
' Lcd module connections
Dim LCD_RS as sbit at RB4_bit
LCD_EN as sbit at RB5_bit
LCD_D4 as sbit at RB0_bit
LCD_D5 as sbit at RB1_bit
LCD_D6 as sbit at RB2_bit
LCD_D7 as sbit at RB3_bit
LCD_RS_Direction as sbit at TRISB4_bit
LCD_EN_Direction as sbit at TRISB5_bit
LCD_D4_Direction as sbit at TRISB0_bit
LCD_D5_Direction as sbit at TRISB1_bit
LCD_D6_Direction as sbit at TRISB2_bit
LCD_D7_Direction as sbit at TRISB3_bit
main:
' Main program
LCD_init()
LCD_cmd(_LCD_Cursor_off)
While TRUE
LCD_cmd(_LCD_clear)
delay_ms(2000)
LCD_out(1,2,"Microprocessor")
delay_ms(2000)
LCD_out(2,5,"Systems")
delay_ms(5000)
wend
80 | P a g e

end.

Chapter Six
Some Applications of Microprocessors and Microcontrollers in
Different Fields
6.1 EXAMPLES OF APPLICATIONS OF MICROPROCESSORS AND
MICROCONTROLLERS IN ELECTRICAL POWER SYSTEMS:
6.1.1 Microcontroller-Based Electrical Machine Drive
Systems: [3]
One application is a microcontroller-based variable-frequency power
inverter. This inverter is used to convert the dc voltage supply into a
variable-frequency single-phase or 3-phase ac voltage supply in
order to control the speed of a single-phase or a 3-phase induction
motor. Figure 6.1 shows the block diagram of microcontroller-based
81 | P a g e

variable-frequency power inverter that uses an inverter that uses a


bridge of Isolate Gate Bipolar Transistors (IGBT).
The microcontroller provides the variable frequency pulse width modulation (PWM)
signals that controls the switching of the IGBTs, so that the inverter bridge provides the
required variable frequency ac supply with less harmonic

Figure 6.1 A block diagram of microcontroller-based variable-frequency


power inverter.

6.2 EXAMPLES OF APPLICATIONS OF MICROPROCESSORS AND


MICROCONTROLLERS IN MEDICAL EQUIPMENT:
6.2.1 Using Microprocessors and Microcontrollers in
Electrocardiograph (ECG) Systems: [6]
An electrocardiogram (ECG or EKG) is the measurement and graphic
representation, with respect to time, of the electrical signals
associated with the heart muscles. Applications of an ECG range from
monitoring heart rate to the diagnosis of specific heart conditions.
All ECGs pick up heart signals through electrodes connected
externally to specific locations on the body. The heart signals are
generated by the body and have amplitudes of a few millivolts. The
specific locations of the electrodes allow the heart's electrical activity
to be viewed from different angles, each of which is displayed as a
channel on the ECG printout. The channels are commonly referred to
as "leads," so a 12-lead ECG device has 12 separate channels
displayed graphically. Note that a 12-lead (12-channel) ECG device
requires 10 electrodes (10 wires). The block diagram of a fullfeatured ECG system is shown in figure 6.2.

82 | P a g e

Figure 6.2 A block diagram of a full-featured ECG system.

6.3 EXAMPLES OF APPLICATIONS OF MICROPROCESSORS AND


MICROCONTROLLERS IN TELECOMMUNICATION SYSTEMS:
6.3.1 Using Microprocessors and Microcontrollers in Mobile
Phones: [7]
Mobile (or cellular) phones have become one of the most common
communications devices in everyday use. Apart from simple voice
calls, today's mobile phones are capable of offering many other
services, such as text and multimedia messaging, entertainment
(playback of stored music and FM radio), and photography. High-end
mobile phones, which run on specially designed technology platforms
and contain advanced computing and connectivity features (such as
Internet and email access), are generally known as smartphones.
With advances in all fields of technology, the line between a normal
phone and a smartphone has blurred significantly.
Figure 6.3 shows the block diagram of a typical mobile phone.
83 | P a g e

Figure 6.3 The block diagram of a typical mobile phone.


The primary components of a mobile phone include an application
processor, the memory subsection, the power management and
display driver ICs, the CMOS image sensor, the near-field
communication (NFC) solution, and the SIM card IC. For low power,
battery operated devices, such as mobile phones, ARM is most
widely used and adopted architecture across the world. For example,
Samsung has an extensive line-up of application processors based on
the ARM platform. Samsung's complete range of advanced, low
power application processors are designed for exceptional
performance with the latest features, and are backed by a
comprehensive suite of software development and support tools.

References:
1. The 8086 Microprocessor: Programming & Interfacing The PC,
Kenneth J. Ayala, Delmar Publishers, USA, 1995. ISBN: 978-0-3140-12425.
.
84 | P a g e

2. Microprocessor and Interfacing: Programming and Hardware, 5nd


Edition, Douglas V. Hall, McGRAW-HILL, Singapore, 2004. ISBN:
1849518122.
3. The Intel Microprocessors: 8086/8088, 80186/80188, 80286, 80386,
80486, PENTIUM, PENTIUM Pro PROCESSOR, PENTIUM II, PENTIUM III &
PENTIUM 4 Architecture, Programming and Interfacing, 8th Edition,
Barry B. Brey, Pearson Prentice Hall, USA, 2006.ISBN: 978-0-1350-26458.
4. PIC Microcontrollers: know it all, Lucio Di Jasio, Tim Wilmshurst,
Dogan Ibrahim, John Morton, Newnes, USA, 2008. ISBN: 978-0-75068615-0.
5. http://www.mikroe.com/.
6. www.maximintegrated.com/ECG.
7. www.samsung.com.

85 | P a g e

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