You are on page 1of 66

73

LEARNING GUIDE

Week No.: 6

TOPIC/S

1. Basic Input Output port


2. Design of an address decoder for the input and output ports
3. Introduction of the 8255 Programmable Interface Controller
4. Configuration of the 8255 to the desired function. Mode 1,2 & 3

EXPECTED COMPETENCIES

Upon completing this Learning Module, you will be able to:


1. Explain the operation of the basic input and output interfaces.
2. Decode an 8-, 16-, and 32-bit I/O device so that it can be used at any I/O port
address.
3. Define handshaking and explain how to use it with I/O devices.
4. Interface and program the 82C55 programmable peripheral interface.

CONTENT/TECHNICAL INFORMATION

I/O Instructions

The instruction set contains one type of instruction that transfers information to
an I/O device (OUT) and another to read information from an I/O device (IN).
Instructions (INS and OUTS found on all versions except the 8086/8088) are also
provided to transfer strings of data between the memory and an I/O device. (Brey, 1997)

Instruction Data Function


Width
IN AL,p8 8 A byte is input from port p8 into AL
IN AX,p8 16 A word is input from port p8 into AX
IN EAX, p8 32 A doubleword is input from port p8 into EAX
IN AL,DX 8 A byte is input from the port addressed by DX into AL
IN AX,DX 16 A word is input from the port addressed by DX into AX
IN EAX,DX 32 A word is input from the port addressed by DX into EAX
INSB 8 A byte is input from the port addressed by DX into the extra
segment memory location addressed by DI, then DI= DI± 1
INSW 16 A word is input from the port addressed by DX into the extra
segment memory location addressed by DI, then DI= DI± 2
INSD 32 A doubleword is input from the port addressed by DX into the
extra segment memory location addressed by DI, then DI± 4
74

OUT p8,AL 8 A byte is output from AL to port p8


OUTp8,AX 16 A word is output from AX to port p8
OUTp8,EAX 32 A doubleword is output from EAX to port p8
OUTDX,AL 8 A byte is output from AL to the port addressed by DX
OUTDX,AX 16 A word is output from AX to the port addressed by DX
OUTDX,EAX 32 A doubleword is output from EAX to the port addressed by DX
OUTSB 8 A byte is output from the data segment memory location
addressed by SI to the port addressed by DX, then SI = SI ± 1
OUT SW 16 A word is output from the data segment memory locations
addressed by SI to the port addressed by DX, then SI = SI ± 2
OUTSD 32 A doubleword is output from the data segment memory locations
addressed by SI to the port addressed by DX, then SI = SI ± 4
(Brey, 1997)

Both the IN and OUT instructions transfer data between an I/O device and the
microprocessor's accumulator (AL, AX, or EAX). The I/O address is stored in register
DX as a 16-bit I/O address or in the byte (p8) immediately following the opcode as an
8-bit I/O address. Intel calls the 8-bit form (p8) a fixed address because it is stored with
the instruction, usually in a ROM. The 16-bit I/O address in DX is called a variable
address because it is stored in a DX then used to address the I/O device. Other
instructions that use DX to address I/Oare INS and OUTS instructions. (Brey, 1997)

Whenever data are transferred using the IN or OUT instruction, the I/O address,
often called a port number, appears on the address bus. The external I/O interface
decodes it in the same manner that it decodes a memory address. The 8-bit fixed port
number (p8) appears on address bus connections A7- A0 with bits A15-A8 equal to
000000002. The address connections above A15 are undefined for an I/O instruction.
The 16-bit variable port number (DX) appears on address connection A15-A0. (Brey,
1997)

This means that the first 256 I/O port addresses (OOH-FFH) are accessed by
both the fixed and variable I/O instructions, but any I/O address from OlOOH-FFFFH
is accessed only by the variable I/O address. In many dedicated task systems, only the
rightmost 8-bits of the address are decoded, thus reducing the amount of circuitry
required for decoding. In a PC computer, all 16 address bus bits are decoded with
locations OOXXH-03XXH, which are the 110 addresses used for 110 inside the PC.
(Brey, 1997)

The INS and OUTS instructions address the 110 device using the DX register,
but do not transfer data between the accumulator and the 110 device as IN and OUT.
Instead, these instructions transfer data between memory and the 110 device. The
memory address is located by ES:DI for the INS instruction and DS:SI for the OUTS
instruction. As with other string instructions, the contents of the pointers are
incremented or decremented as dictated by the state of the direction flag (DF). Both
75

INS and OUTS can also be prefixed with the REP prefix, allowing more than one byte
or word to be transferred between 110 and memory. (Brey, 1997)

Isolated and Memory-Mapped I/O

There are two completely different methods of interfacing I/O to the


microprocessor: isolated 1/0 and memory-mapped I/O. In isolated I/O, the IN, INS,
OUT, and OUTS instructions transfer data between the microprocessor accumulator or
memory and the I/O device. In memory-mapped I/O, any instruction that references
memory can accomplish the transfer. (Brey, 1997)

Isolated I/O
- The most common I/O transfer technique used in the Intel microprocessor-
based system is isolated I/O. The term isolated describes how the I/O
locations are isolated from the memory system in a separate I/O address
space.
– in the PC, isolated I/O ports are used to control peripheral devices.
– an 8-bit port address is used to access devices located on the system
board, such as the timer and keyboard interface.
– a 16-bit port is used to access serial and parallel ports, video and disk drive
systems.
(https://microprocessors414.files.wordpress.com, 2015)

Memory and I/O Maps for 8086 Microprocessors. (Isolated I/O) (Brey, 1997)

Memory Mapped I/O

- Memory-mapped I/O does not use the IN, INS, OUT, or OUTS
instructions.
76

- It uses any instruction that transfers data between the microprocessor and
memory and treated as a memory location in memory map
- Advantage is any memory transfer instruction can access the I/O device.
- Disadvantage is a portion of memory system is used as the I/O map thus
reduces memory available to applications
(https://microprocessors414.files.wordpress.com, 2015)

Memory and I/O Maps for 8086 Microprocessors. (Memory Mapped I/O) (Brey, 1997)

Personal Computer I/O Map

– the PC uses part of I/O map for dedicated functions, as shown below.
– I/O space between ports 0000H and 03FFH is normally reserved for the
system and ISA bus.
– ports at 0400H–FFFFH are generally available for user applications,
mainboard functions, and the PCI bus.
– 80287 coprocessor uses 00F8H–00FFH, so Intel reserves I/O ports 00F0H–
00FFH.
(https://microprocessors414.files.wordpress.com, 2015)
77

The I/O map of a personal computer illustrating many of the fixed I/O areas
(Brey, 1997)

Basic Input and Output Interfaces

The basic input device is a set of three-state buffers. The basic output device is
a set of data latches. The term IN refers to moving data from the I/O device into the
microprocessor, and the term OUT refers to moving data out of the microprocessor to
the I/O device. (Brey, 1997)

The Basic Input Interface

Three-state buffers are used to construct the 8-bit input port depicted below.
• External TTL data are connected to the inputs of the buffers.
– buffer outputs connect to the data bus
• The circuit of allows the processor to read the contents of the eight switches that
connect to any 8-bit section of the data bus when the select signal becomes a logic 0.

(https://microprocessors414.files.wordpress.com, 2015)
78

The basic input interface illustrating the connection of eight switches.


Note that the 74ALS244 is a three-state that controls the application of the switch data to the
data bus. (Brey, 1997)

o When the IN instruction executes, contents of the switches copy to the AL


register.
o This basic input circuit is not optional and must appear any time input data are
interfaced to the microprocessor.
o Sometimes it appears as a discrete part of the circuit, as shown above
– also built into programmable I/O devices
o Sixteen- or 32-bit data can also be interfaced but is not nearly as common as 8-
bit data.
(https://microprocessors414.files.wordpress.com, 2015)

The Basic Output Interface

• Receives data from the processor and usually must hold it for some external device.
– latches or flip-flops, like buffers in the input device, are often built into the
I/O device
• Figure below shows how eight light-emitting diodes (LEDs) connect to the processor
through a set of eight data latches.
• The latch stores the number output by the microprocessor from the data bus so that
the LEDs can be lit with any 8-bit binary number.

(https://microprocessors414.files.wordpress.com, 2015)
79

The basic output interface connected to a set of LED displays (Brey, 1997)

• Latches hold the data because when the processor executes an OUT, data are
only present on the data bus for less than 1.0 µs.
– the viewer would never see the LEDs illuminate
• When the OUT executes, data from AL, AX, or EAX transfer to the latch via
the data bus.
• Each time the OUT executes, the SEL signal activates, capturing data to the
latch.
– data are held until the next OUT
• When the output instruction is executed, data from the AL register appear on
the LEDs.

Handshaking

Many I/O devices accept or release information at a much slower rate than the
microprocessor. Another method of I/O control, called handshaking or polling,
synchronizes the I/O device with the microprocessor. An example device that requires
handshaking is a parallel printer that prints 100 characters per second (CPS). It is
obvious that the microprocessor can definitely send more than 100 CPS to the printer,
so a way to slow the microprocessor down to match speeds with the printer must be
developed. (Brey, 1997)
80

The DB25 connector found on computers and the Centronics 36-pin connector
found on printers for the Centronics parallel printer interface. (Brey, 1997)

• The software polls or tests the BUSY pin to decide whether the printer is busy.
– If the printer is busy, the processor waits
– if not, the next ASCII character goes to the printer
• This process of interrogating the printer, or any asynchronous device like a printer, is
called handshaking or polling.

I/O Port Address Decoding

I/O port address decoding is very similar to memory address decoding,


especially for memory mapped I/0 devices. In fact, we do not discuss memory-mapped
I/O decoding because it is treated exactly the same as memory, except that the IORC
and IOWC are not used, since there is no IN or OUT instruction. The decision to use
memory-mapped I/O is often determined by the size of the memory system and the
placement of the I/O devices in the system.

The main difference between memory decoding and isolated I/0 decoding is the
number of address pins connected to the decoder. We decode A32-A0, A23-A0, or A19-
A0 for memory and A15-A0 for isolated I/O. Sometimes, if the I/O devices use only fixed
I/O addressing, we decode only A7-A0. Another difference is that we use the IORC and
IOWC to activate I/O devices for a read or a write operation. On earlier versions of the
microprocessor, IO/M = 1 and RD or WR are used to activate I/O devices. On the
newest versions of the microprocessor, the M/IO = 0 and W/R are used to activate I/O
devices.
81

Decoding 8-Bit I/O Address

As mentioned, the fixed 1/0 instruction uses an 8-bit I/O port address that
appears on A15-A0 as 0000H-00FFH. If a system will never contain more than 256 I/O
devices, we often decode only address connections, A0 for an 8-bit I/O port address.
Thus, we ignore address connections A15-A8.
(You cannot ignore these address connections in a personal computer-all 16 bits
must be used.) Please note that the DX register can also address I/O ports OOH-FFH.
Also note that if the address is decoded as an 8-bit address, then we can never include
I/0 devices that use a 16-bit I/O address. (Brey, 1997)

• Fixed I/O instruction uses an 8-bit I/O port address that on A15–A0 as 0000H–00FFH.
– we often decode only address connections A7 – A0 for an 8-bit I/O port address
• The DX register can also address I/O ports 00H–FFH.
• If the address is decoded as an 8-bit address, we can never include I/O devices using
a 16- bit address.
– the PC never uses or decodes an 8-bit address
Figure below shows a 74ALS138 decoder that decodes 8-bit I/O ports F0H - F7H.
– identical to a memory address decoder except we only connect address bits
A7 –A0 to the inputs of the decoder.
(https://microprocessors414.files.wordpress.com, 2015)

A port decoder that decodes 8-bit I/O ports. This decoder generates
active low outputs for ports F0H-F7H. (Brey, 1997)

Figure below shows the PAL version of this decoder. Notice that this is a better
decoder circuit because the number of integrated circuits has been reduced to one
device, the PAL. The program for the PAL appears in after the figure. (Brey, 1997)
82

A PAL16L8 decoder that generates I/O port signals for port F0H – F7H and its corresponding code to function
as a decoder (Brey, 1997)

Decoding 16-Bit I/O Addresses

We also decode 16-bit I/O addresses, especially in a personal computer system.


The main difference between decoding an 8-bit I/O address and a 16-bit I/O address is
that eight additional address lines (A15-A8) must be decoded. Figure below illustrates a
circuit that contains a PAL16L8 and an 8-input NAND gate used to decode I/O ports
EFF8H-EFFFH. These are common I/O port assignments in a PC used for the serial
communications port. (Brey, 1997)

A PAL16L8 decoder that decodes 16-bit address EFFBH-EFFFH


The NAND gate decodes the first 8-bits of the 1/0 port address (A15-A8) so that
it generates a signal to enable the PAL16L8 for any 1/0 address between EFOOH and
EFFFH. The PAL16L8 further decodes the 1/0 address to produce eight active low
output strobes EFF8HEFFFH. The program for the PAL16L8 appears below. (Brey,
1997)
83

Program used for programming the PAL16L8 (Brey, 1997)

8- and 16-Bit Wide I/O Ports

• Data transferred to an 8-bit I/O device exist in one of the I/O banks in a 16-bit
processor such as 80386SX.
• The I/O system on such a microprocessor contains two 8-bit memory banks.
• Figure below shows separate I/O banks for a 16-bit system such as 80386SX.
• Because two I/O banks exist, any 8-bit I/O write requires a separate write.

(https://microprocessors414.files.wordpress.com, 2015)

The I/O banks found in 8086, 80186, 80286 and 80386SX (Brey, 1997)
• I/O reads don’t require separate strobes.
– as with memory, the processor reads only the byte it expects and ignores the other
byte
– a read can cause problems when an I/O device responds incorrectly to a read
operation
84

• Figure below shows a system with two different 8-bit output devices, located at 40H
and 41H.
• These are 8-bit devices and appear in different I/O banks.
– thus, separate I/O write signals are generated to clock a pair of latches that capture
port data
(https://microprocessors414.files.wordpress.com, 2015)

An I/O port decoder that selects ports 40H and 41H for output data. (Brey, 1997)

– all I/O ports use 8-bit addresses


– ports 40H & 41H can be addressed as separate 8-bit ports
– or as one 16-bit port
(https://microprocessors414.files.wordpress.com, 2015)
85

A 16-bit-wide port decoded at I/O addresses 64H and 65H


(https://microprocessors414.files.wordpress.com, 2015)

• Figure above shows a 16-bit device connected to function at 8-bit addresses 64H &
65H.
• The PLD decoder does not have a connection for address bits BLE (A0) and BHE
because the signals don’t apply to 16-bit-wide devices.
• The program for the PLD, illustrated in below, shows how the enable signals are
generated for the three-state buffers (74HCT244) used as input devices.
(https://microprocessors414.files.wordpress.com, 2015)

PLD Program. (Brey, 1997)


86

32 Bit Wide I/O Ports

Although 32-bit wide I/O ports are not common, they may eventually become
commonplace because of new buses found in computer systems. The once-promising
EISA system bus supports 32-bit I/O as well as the VESA local and current PCI bus,
but these are only found in some computer systems.
The circuit below illustrates a 32-bit input port for the 80386DX or 80486
microprocessors. As with prior interfaces, this circuit uses a single PAL to decode the
I/O ports and four 74LS244 buffers to connect the I/O data to the data bus. The I/O
ports decoded by this interface are 8-bit ports 70H-73H, as illustrated by the PAL
program below. Also notice again that we decode only an 8-bit I/O port address.

A 32-bit input port decoded at bytes 70H-73H (Brey, 1997)


87

PAL Program for the 32-Bit Decoder (Brey, 1997)

With the Pentium/Pentium Pro microprocessors and their 64-bit data bus, I/O
ports appear in various banks as determined by the I/O port address. For example, 8-bit
I/O port 0034H appears in Pentium I/O bank 5, while the 16-bit 1/0 port 0034H-0035H
appears in Pentium banks 5 and 6. A 32-bit I/O access in the Pentium system can appear
in any four consecutive I/O banks. For example, 32-bit 1/0 port 0100H-0103H appears
in banks 0-3. (Brey, 1997)

The 8255 Programmable Peripheral Interface

The 8255 is a programmable chip used for interfacing peripheral devices. It


connects with a processor or a controller and increases the number of I/O pins that can
be used in the system. It has three 8-bit ports used to connect I/O devices. (Rahman,
2020)
That means we have 24-pins for interfacing I/O devices that are divided into
three ports. Why? For additional control and features. We’ll find out more about that in
the subsequent sections. (Rahman, 2020)

Features of 8255 Programmable Peripheral Interface

• It is compatible with a wide range of microprocessors and microcontrollers,


making it widely popular.
• It has three 8-bit I/O: Ports A, B, and C.
• Port A and port B can function as 8-bit input or output ports.
• Bits of port C are divided into two subgroups of 4 bits each – port C upper and
port C lower.
• There are other control pins which are used to specify and control the flow of
data and operation of the 8255. We’ll check them out soon.
• The port pins have the ability to source 1 mA current at 1.5 V when programmed
to function as output pins. This provides the capability of driving Darlington
transistors for applications such as printers and high voltage displays.
• The most important feature of 8255 is that it is ‘programmable.’ This means that
the operation of 8255 can be controlled by programming the microprocessor
88

appropriately. This gives us the freedom to use 8255 in a number of ways


without having to change the wiring and connections.
• It has a few different modes of operation. This is awesome because we get the
freedom to choose from a bunch of different functionalities.

(Rahman, 2020)

Pin diagram of 8255

8255 Pin Diagram (Rahman, 2020)

Pin Name No. of Pins Description


Vcc 1 Used to supply power to the IC. Usually at +5 V dc with
respect to ground.
Ground 1 Ground pin. All the voltages (signals) are measured with
respect to this pin. It is connected to the common ground of
the circuit.
PA0-PA7 8 These 8-bit bi-directional I/O pins are used to send data to
output from a device and to receive data from an input
device.
PB0-PB-7 8 These 8-bit bi-directional I/O pins are used to send data to
an output device and to receive data from an input device.
PC0-PC3 4 These four pins are used by 8255 for communicating with
an I/O device.
PC4-PC7 4 These four pins are used by 8255 for communicating with
an I/O device.
D0-D7 8 These are the data pins used by the Master (uP/uC) to
communicate with 8255. All the data to be transmitted and
received and control instructions are transmitted to and from
8255 through these pins. These are connected to the data bus
of the microprocessor.
89

CS 1 This is an active low input pin. The microprocessor uses this


input to select the chip 8255. In other words, the
microprocessor uses this pin to say to 8255 that “Hey!! Now
I am talking to you. And I will keep talking to you until this
CS signal remains low.”
RD 1 This is also an active low input pin used by the
microprocessor to tell 8255 that it wants to read data from
one of its ports.
WR 1 This is also an active low input pin used by the
microprocessor to tell 8255 that it wants to write data to one
of its ports.
A1, A0 2 These are the port address pins. They are used to select the
port with which the microprocessor intends to communicate.
For values of A1A0:
00 = Port A is selected

01 = Port B is selected

10 = Port C is selected

11 = Control port is selected.


Reset 1 An active high input. Used to reset 8255. Immediately after
reset, all the three ports work as input ports in mode 0.
(Rahman, 2020)

Block diagram and internal structure of 8255

8255 Block Diagram (Rahman, 2020)


90

Features

• Data bus buffer: It is a bidirectional (tri-state) data bus. Notice that this connects
every single block in 8255 with each other except the read-write control logic
block. All the data flows through this bus.
• Ports A, B, and C are divided into two groups.
• Group A: Port A (PA0-PA7) + Port C upper (PC4-PC7)
• Group B: Port B (PB0-PB7) + Port C upper (PC0-PC3)
• There are separate control units for Group A and Group B that get their control
signals from the main read-write logic control logic block. The control logic
receives individual control signals as inputs and responds accordingly.
• Group A control and Group B control blocks are connected to their respective
Group blocks. They control the flow of data and modes of operation at those
ports.
• Controls of group A and group B, represented by the control blocks ‘group A
control‘ and ‘group B control‘ together make up ‘the control port.’ This port is
used to specify the modes of operations and is manipulated by the
microprocessor (by writing control word). Hence, these two blocks are
connected by the data bus in the above diagram.
• These control blocks play a crucial role as they ensure that ports can be used in
a number of ways. Hence, 8255 gives a lot of freedom and control to the user.
(Rahman, 2020)

Control port and control word in 8255

• The fourth port of the 8255 is the control port.


• As you would have noticed in the pin diagram, there are no pins for this port.
This is because the control port is not supposed to be used for input or output
purposes. This port is used by the microprocessor to give instructions to 8255
about the modes in which it wants to use a specific I/O port.
• To communicate with this port, the microprocessor needs to make the inputs at
A1 and A2 to be both high (Already stated in the section explaining pin
diagram). When both of these are high, 8255 knows that the microprocessor is
telling it about the modes of the I/O ports that need to be chosen.
• There are two major modes of operation.
I/O Mode: Ports A, B, and C are used as 8-bit I/O ports.
Bit Set-Reset (BSR) Mode: The bits of port C can also be used as
individual I/O bits and can be set and reset individually.
• Corresponding to each mode of operation, there are two different control words.
In simple words, there are two different notations of ‘what each bit of control
word means’ depending on the mode selected out of the above two modes.
91

• If we see from outside, there is only one control port. But internally, there are
two different latches to store the control word for I/O mode and BSR mode.
Control words for these modes are stored in the corresponding latches.
(Rahman, 2020)

Operating Modes

8255 Operating Mode (Rahman, 2020)

I/O mode
In I/O mode of operation, Ports A, B, and C of the 8255 can function in 3 different
modes:

• Mode 0
• Mode 1
• Mode 2
(Rahman, 2020)

I/O mode control word

These modes can be selected by setting and resetting specific bits of the I/O
mode control word. (Rahman, 2020)

You can observe from the illustration below how the microprocessor selects
the mode of operation by issuing control instructions at the control port (control
word). (Rahman, 2020)

In other words, the below diagram explains to you, “what exactly does the
microprocessor write to the control port to set a particular port in a particular mode of
operation.” (Rahman, 2020)
92

I/O Definition Control Word. (Rahman, 2020)

Mode 0

• This is the simplest mode of operation.


• In this mode, the I/O device cannot tell the microprocessor that it wants to read
data or give data to the microprocessor. Hence, the microprocessor should know
when to read and when to write data.
• The I/O device reads or writes data when it wants to.
• Similarly, the microprocessor reads or writes data according to the way we
programmed it.
• There is no communication between the I/O device and the microprocessor.
(Rahman, 2020)

We can say that this mode is used for I/O devices whose timing characteristics
are already known. For example, we can use this mode for an input device that wants
to provide us with input every 0.5 seconds. So, we program the microprocessor to read
from that device after every 0.5 seconds without the input device actually telling the
microprocessor that it wants to send data. (Rahman, 2020)

This mode can be applied when we need to read switch settings or temperature
readings from a sensor. In such cases, the readings are always present at the input port.
The microprocessor reads them when it requires the data for processing. (Rahman,
2020)
93

Mode 1 (Strobed I/O or Handshake I/O)

This mode uses handshaking to make the exchange of data more efficient.

In mode 1 and mode 2 operations of I/O ports, the I/O device and the processor
can communicate with each other apart from the exchange of input and output data. It
goes down like this:

When the input device writes data at a port, it tells the port that new data has
entered. The port then informs the microprocessor that new unread data is there at the
port. When the microprocessor is done reading the data, the port informs the input
device that the data on the port is read and new data can be entered. This
communication happens using the ‘handshaking signals’. (Rahman, 2020)

This above process is referred to as ‘handshaking.’ It is named so because it is


similar to the handshaking between two strangers before they start talking to each other.
(Rahman, 2020)

The lines of port C are used for handshaking signals. Thus, only port A and port
B can work in mode 1, while port C provides handshake signals. (Rahman, 2020)

Three handshake signals are required for a port to work in handshake mode.
Hence, port A and port B can both work in mode 1 simultaneously, with two lines of
port C still remaining. The unused lines of port C for handshaking can be used for I/O
in mode 0. (Rahman, 2020)

The below table specifies which lines of port C provide handshake signals for
ports A and B. (Rahman, 2020)

Mode of operation Port Input/Output Lines of port C used for handshake


Mode 1 Port A Input PC3, PC4 and PC5
Mode 1 Port A Output PC3, PC6 and PC7
Mode 1 Port B Both Input and PC0, PC1 and PC2
Output
(Rahman, 2020)
94

Mode 2 (Bidirectional I/O)

In mode 0 and mode 1, ports A and B can only work as either input ports or
output ports. But in mode 2, port A can play both roles simultaneously. A port
programmed in mode 2 can take inputs and give outputs. Since data transfer is
bidirectional in mode 2, handshake signals required are also more. (Rahman, 2020)

There are five handshake signals required for port A to function in mode 1.
These are provided by port C pins PC3-PC7. The remaining three pins can function as
handshake pins for port B working in mode 1 or can function as normal I/O pins if port
B is programmed to work in mode 0. (Rahman, 2020)

It can be easily deduced that only port A can work in mode 2 since only three
pins of port C remain after allocating handshake signals to port A in mode 2 and the
required number for port B also to work in mode 2 is five.

Port A : Mode 0, mode 1, mode 2


Port B : Mode 0, Mode 1
Port C : Mode 0
(Rahman, 2020)
BSR Mode (Control word)

As stated before, bits of port C can be set and reset individually in the BSR
mode. It is achieved by writing the appropriate control instruction at the control port.
These instructions are called ‘BSR mode control words.’ The illustration below
explains what you need to write on the control port to manipulate the bits of port C in
BSR mode. (Rahman, 2020)

Port C in BSR Mode (Rahman, 2020)


95

PROGRESS CHECK

1. Design a NAND Gate Address Decoder that could access the DMA
Controller in the Personal Computer
2. Design an Address Decoder using the 3-8 Line decoder that could access
the Timer in the Personal Computer
3. Design an Address Decoder using the Dual 2 to 4 Line decoder that could
access the CGA Adapter.
4. Identify the following modes, ports and inputs/outputs do the following
control words set the 8255 PPI.
a. 101010102
b. 111110112
c. 100101112
d. 001001102
e. 100100012
5. What is the distinction between addressing the memory as compared to
addressing the I/O’s? Justify your answer in not less than 3 sentences and
not more than 5 sentences.

REFERENCES

Brey, B. B. (1997). The Intel Microprocessors. Upper Saddle River, New Jersey: Prentice-
Hall, Inc.

https://microprocessors414.files.wordpress.com. (2015, January). Retrieved from


https://microprocessors414.files.wordpress.com:
https://microprocessors414.files.wordpress.com/2015/01/basic-input-output-
interface.pdf

Rahman, O. (2020, May 22). https://technobyte.org. Retrieved from https://technobyte.org:


https://technobyte.org/8255-programmable-peripheral-interface-ppi-guide/
96

LEARNING GUIDE

Week No.: __7__

TOPIC/S
1. Introduction to Assembly Language and Machine language.
2. Creation of a basic assembly language Shell program with program terminate
interrupt.

EXPECTED COMPETENCIES
Upon completing this Learning Module, you will be able to:
1. Know what is Assembly Language
2. Program basic Applications using 8086 Microprocessor

CONTENT/TECHNICAL INFORMATION

Machine Language

Machine language, or machine code, is a low-level language comprised of


binary digits (ones and zeros). High-level languages, such as Swift and C++ must be
compiled into machine language before the code is run on a computer.
(https://techterms.com, 2020)

Since computers are digital devices, they only recognize binary data. Every
program, video, image, and character of text is represented in binary. This binary data,
or machine code, is processed as input by the CPU. The resulting output is sent to the
operating system or an application, which displays the data visually. For example, the
ASCII value for the letter "A" is 01000001 in machine code, but this data is displayed
as "A" on the screen. An image may have thousands or even millions of binary values
that determine the color of each pixel. (https://techterms.com, 2020)

While machine code is comprised of 1s and 0s, different processor architectures


use different machine code. For example, a PowerPC processor, which has a RISC
architecture, requires different code than an Intel x86 processor, which has a CISC
architecture. A compiler must compile high-level source code for the correct processor
architecture in order for a program to run correctly. (https://techterms.com, 2020)

Assembly Language

Assembly language (or Assembler) is a compiled, low-level computer language.


It is processor-dependent, since it basically translates the Assembler's mnemonics
directly into the commands a particular CPU understands, on a one-to-one basis. These
Assembler mnemonics are the instruction set for that processor. In addition, an
Assembler provides commands that control the assembly process, handle
97

initializations, and allow the use of variables and labels as well as controlling output.
(Austerlitz, 2003)

On PCs, Assembler is normally used only under MS-DOS. When running a 32-
bit, protected-mode operating system (including Windows 95/NT and above), low-level
programs which directly access registers or memory locations produce protection
violations. All low-level access must be made through appropriate software drivers.
(Austerlitz, 2003)

For MS-DOS PCs, the most popular Assembly language was Microsoft Macro
Assembler, or MASM. As with most popular compilers, MASM was upgraded on a
regular basis. Most of this discussion refers to version 5.0 or later, which simplified the
use of certain directives and included support for instructions available only on 80286
and 80386 CPUs. (Austerlitz, 2003)

A directive is an Assembler command that does not translate into an executable


instruction, but directs MASM to perform a certain task facilitating the Assembly
process. An executable instruction is sometimes referred to as an op code, while an
Assembler directive may be referred to as a pseudo-op code. Directives can tell MASM
many different things, including which memory segment is being referred to, what the
value of a variable or memory location is, and where program execution begins.
(Austerlitz, 2003)

One important MASM directive is .MODEL, which determines the maximum


size for a program. Remember that for an 80×86 family CPU, memory is addressed as
segments, up to 64 Kbytes in length. If 16-bit addressing is used (for code or data) only
a single 64K segment will be accessed. The memory model of a program defines how
different parts of that program (code and data) access memory segments. Five memory
models are supported by MASM for DOS programs: Small, Medium, Compact, Large,
and Huge. In the Small model, all data fits within one 64K segment and all code
(executable instructions) fits within another single 64K segment. In the Medium model,
all data fits within one 64K segment but code can be larger than 64K (multisegment,
requiring 32-bit addressing for segment:offset). In the Compact model, all code fits
within one 64K segment but data may occupy more than 64K (but no single array can
be larger than 64K). In the Large model, both code and data may be larger than 64K
(still, no single data array can exceed 64K). Finally, in the Huge model, both code and
data can be larger than 64K and data arrays can also exceed 64K. (Austerlitz, 2003)

Since larger models require larger addresses, they produce bigger and slower
programs than a smaller model will. In selecting a model for a program, try to estimate
the maximum amount of data storage you will need. Let us say you are writing an FFT
program, using 16-bit integer math and a maximum sample size of 2048 points. Since
each point requires two integers (real and imaginary) and each integer is 2 bytes long,
you need 8096 bytes just to store the input (or output) data. Even if you had separate
arrays for input and output data, that would still be only 16,192 bytes. As a safety
98

margin, for temporary storage, we will double this number, to 32,384 bytes, which is
only half of a 64K segment. It is more difficult to estimate the size of the code. In this
example, we would start with the Small model. If the code turned out to be larger than
64K (which is not easy to do in assembly language), we would move to the Medium
model. These same memory models also apply to Microsoft's high-level DOS language
compilers. If you are writing a MASM program to work with another high-level
language, you should use the same memory model for both. (Austerlitz, 2003)

Here is an example of a simple MASM program that displays a text string (“This
is a simple MASM program”) on the screen using DOS function 09h:

MASM Program (Austerlitz, 2003)

Several directives are used here. DOSSEG tells MASM to take care of the order
of the various segments (code, data, stack), a detail we would rather ignore. The
directive .DATA indicates the start of the data segment while .CODE indicates the start
of the code segment. The message is referred to by the label text, where the DB directive
(Defines Bytes) indicates that this is byte data (the quotation marks indicate ASCII
text). The string must be terminated by ASCII character 24h (“$”) for DOS function
09h. The executable instructions are placed in the code segment. The label, go, refers
to the start of the program. The address of the text string is loaded into registers DS:DX.
Then DOS function 09h is called, to display the string. Finally, DOS function 4Ch is
called to exit the program and return to DOS. The final END directive tells MASM to
begin program execution at the label (address) go. (Austerlitz, 2003)

MASM is called a Macro Assembler, because it supports the use of macros. A


macro is a block of program statements that is given a symbolic name that can then be
used within the normal program code. A macro can also accept parameters when it is
called within a program. When the source file is assembled by MASM, any macros are
expanded (translated) to their original definition text. This is very handy if the same
section of code, such as a programmer-defined function, is used repeatedly. Often,
predefined macros may be kept in a separate file, along with other information, such as
99

variable initializations. The INCLUDE directive can read this file in during assembly.
(Austerlitz, 2003)

Assembly Language VS Machine Language

Basis of Machine Language Assembly Language


Difference
Level of Machine language ranks as the Assembly language refers to a low-
programming lowest level programming language. level programming language that needs
language In this language, instructions are an assembler for converting the
executed directly via the Central instructions to machine or object codes.
Processing Unit.
Ease of Machine language cannot be Assembly language can be understood,
comprehension deciphered by humans and can be used, and applied by humans.
comprehended only by computers.
Nature of Machine languages comprise of Assembly languages have a syntax that
syntax binary digits 0s and 1s. is similar to the English language;
therefore, they can be understood by
programmers and users alike.
Dependency Machine languages are platform- Assembly language comprises of
dependent, and their features vary standard instruction sets.
accordingly.
Areas of Machine language serves as a Assembly languages are used for real-
application machine code only. time systems and microprocessor-based
applications/ devices.
Usage of Machine language uses sequences of Assembly language does not require
mnemonics bits for giving commands. One users to remember op-codes. It uses
depicts the true or on the state; on the “mnemonics” names and symbols
other hand, zero depicts the false or rather than raw sequences of bits. The
off state. The conversion of high- codes in assembly languages are
level programming language to slightly more readable and can be
machine language is dependent on mapped to machine code by humans.
the CPU. “Mnemonics” are not
required in machine language.
Generation of Machine languages are first- Assembly languages are second-
programming generation programming languages. generation programming languages.
language
Modification Machine language does not support An assembly programming language
any change. can be modified easily.
Risk of errors The risk of errors existing in the The risk of errors existing in assembly
syntax of machine language is high. language is comparatively low.
Memorization Binary codes cannot be memorized. It is possible to memorize the
commands given in assembly
languages.
Compiler No compiler is necessary for A compiler, also known as an
executing commands. assembler, is needed for the proper
execution of assembly language
commands.
(https://www.stechies.com, 2019)
100

8086 Instruction Sets

Data Transfer Instructions

These instructions are used to transfer the data from the source operand to the
destination operand. Following are the list of instructions under this group −

Instruction to transfer a word

MOV − Used to copy the byte or word from the provided source to the provided
destination.

PPUSH − Used to put a word at the top of the stack.

POP − Used to get a word from the top of the stack to the provided location.

PUSHA − Used to put all the registers into the stack.

POPA − Used to get words from the stack to all registers.

XCHG − Used to exchange the data from two locations.

XLAT − Used to translate a byte in AL using a table in the memory.

(https://www.tutorialspoint.com, 2020)

Instructions for input and output port transfer

IN − Used to read a byte or word from the provided port to the accumulator.

OUT − Used to send out a byte or word from the accumulator to the provided port.

(https://www.tutorialspoint.com, 2020)

Instructions to transfer the address

LEA − Used to load the address of operand into the provided register.

LDS − Used to load DS register and other provided register from the memory

LES − Used to load ES register and other provided register from the memory.

(https://www.tutorialspoint.com, 2020)

Instructions to transfer flag registers

LAHF − Used to load AH with the low byte of the flag register.

SAHF − Used to store AH register to low byte of the flag register.

PUSHF − Used to copy the flag register at the top of the stack.

POPF − Used to copy a word at the top of the stack to the flag register.
101

Arithmetic Instructions

These instructions are used to perform arithmetic operations like addition,


subtraction, multiplication, division, etc.

Following is the list of instructions under this group −

Instructions to perform addition

ADD − Used to add the provided byte to byte/word to word.

ADC − Used to add with carry.

INC − Used to increment the provided byte/word by 1.

AAA − Used to adjust ASCII after addition.

DAA − Used to adjust the decimal after the addition/subtraction operation.

(https://www.tutorialspoint.com, 2020)

Instructions to perform subtraction

SUB − Used to subtract the byte from byte/word from word.

SBB − Used to perform subtraction with borrow.

DEC − Used to decrement the provided byte/word by 1.

NPG − Used to negate each bit of the provided byte/word and add 1/2’s complement.

CMP − Used to compare 2 provided byte/word.

AAS − Used to adjust ASCII codes after subtraction.

DAS − Used to adjust decimal after subtraction.

(https://www.tutorialspoint.com, 2020)

Instruction to perform multiplication

MUL − Used to multiply unsigned byte by byte/word by word.

IMUL − Used to multiply signed byte by byte/word by word.

AAM − Used to adjust ASCII codes after multiplication.

(https://www.tutorialspoint.com, 2020)
102

Instructions to perform division

DIV − Used to divide the unsigned word by byte or unsigned double word by word.

IDIV − Used to divide the signed word by byte or signed double word by word.

AAD − Used to adjust ASCII codes after division.

CBW − Used to fill the upper byte of the word with the copies of sign bit of the lower
byte.

CWD − Used to fill the upper word of the double word with the sign bit of the lower
word.

(https://www.tutorialspoint.com, 2020)

Bit Manipulation Instructions

These instructions are used to perform operations where data bits are involved,
i.e. operations like logical, shift, etc.

Following is the list of instructions under this group −

Instructions to perform logical operation

NOT − Used to invert each bit of a byte or word.

AND − Used for adding each bit in a byte/word with the corresponding bit in another
byte/word.

OR − Used to multiply each bit in a byte/word with the corresponding bit in another
byte/word.

XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with
the corresponding bit in another byte/word.

TEST − Used to add operands to update flags, without affecting operands.

(https://www.tutorialspoint.com, 2020)

Instructions to perform shift operations

SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.

SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.

SAR − Used to shift bits of a byte/word towards the right and copy the old MSB into
the new MSB.

(https://www.tutorialspoint.com, 2020)
103

Instructions to perform rotate operations

ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to
Carry Flag [CF].

ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to
Carry Flag [CF].

RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to
MSB.

RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to
LSB.

(https://www.tutorialspoint.com, 2020)

String Instructions

String is a group of bytes/words and their memory is always allocated in a


sequential order.

Following is the list of instructions under this group −

REP − Used to repeat the given instruction till CX ≠ 0.

REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.

REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF =


1.

MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to another.

COMS/COMPSB/COMPSW − Used to compare two string bytes/words.

INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the


provided memory location.

OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided


memory location to the I/O port.

SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in
AL or string word with a word in AX.

LODS/LODSB/LODSW − Used to store the string byte into AL or string word into
AX.

(https://www.tutorialspoint.com, 2020)
104

Program Execution Transfer Instructions (Branch and Loop Instructions)

These instructions are used to transfer/branch the instructions during an


execution. It includes the following instructions −

Instructions to transfer the instruction during an execution without any condition −

CALL − Used to call a procedure and save their return address to the stack.

RET − Used to return from the procedure to the main program.

JMP − Used to jump to the provided address to proceed to the next instruction.

(https://www.tutorialspoint.com, 2020)

Instructions to transfer the instruction during an execution with some conditions −

JA/JNBE − Used to jump if above/not below/equal instruction satisfies.

JAE/JNB − Used to jump if above/not below instruction satisfies.

JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.

JC − Used to jump if carry flag CF = 1

JE/JZ − Used to jump if equal/zero flag ZF = 1

JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.

JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.

JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.

JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.

JNC − Used to jump if no carry flag (CF = 0)

JNE/JNZ − Used to jump if not equal/zero flag ZF = 0

JNO − Used to jump if no overflow flag OF = 0

JNP/JPO − Used to jump if not parity/parity odd PF = 0

JNS − Used to jump if not sign SF = 0

JO − Used to jump if overflow flag OF = 1

JP/JPE − Used to jump if parity/parity even PF = 1

JS − Used to jump if sign flag SF = 1

(https://www.tutorialspoint.com, 2020)
105

Processor Control Instructions

These instructions are used to control the processor action by setting/resetting


the flag values.

Following are the instructions under this group −

STC − Used to set carry flag CF to 1

CLC − Used to clear/reset carry flag CF to 0

CMC − Used to put complement at the state of carry flag CF.

STD − Used to set the direction flag DF to 1

CLD − Used to clear/reset the direction flag DF to 0

STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.

CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.

(https://www.tutorialspoint.com, 2020)

Iteration Control Instructions

These instructions are used to execute the given instructions for number of
times. Following is the list of instructions under this group −

LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX = 0

LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX =


0

LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 &


CX = 0

JCXZ − Used to jump to the provided address if CX = 0

(https://www.tutorialspoint.com, 2020)

Interrupt Instructions

These instructions are used to call the interrupt during program execution.

INT − Used to interrupt the program during execution and calling service specified.

INTO − Used to interrupt the program during execution if OF = 1

IRET − Used to return from interrupt service to the main program

(https://www.tutorialspoint.com, 2020)
106

8086 Interrupts

Interrupts can be seen as a number of functions. These functions make the


programming much easier, instead of writing a code to print a character you can simply
call the interrupt and it will do everything for you. There are also interrupt functions
that work with disk drive and other hardware. We call such functions software
interrupts.

Interrupts are also triggered by different hardware; these are called hardware
interrupts. To make a software interrupt there is an INT instruction, it has very simple
syntax:
INT value

Where value can be a number between 0 to 255 (or 0 to 0FFh), generally we


will use hexadecimal numbers. You may think that there are only 256 functions, but
that is not correct. Each interrupt may have sub-functions.

To specify a sub-function AH register should be set before calling interrupt.


Each interrupt may have up to 256 sub-functions (so we get 256 * 256 = 65536
functions). In general AH register is used, but sometimes other registers maybe in use.
Generally other registers are used to pass parameters and data to sub-function.

The following example uses INT 10h sub-function 0Eh to type a "Hello!"
message. This function displays a character on the screen, advancing the cursor and
scrolling the screen as necessary.

Sample Program using 8086 Emulator. (http://www.emu8086.com, 2011)


107

List of 8086 interrupts.

INT 10h / AH = 0 - set video mode.


input:
AL = desired video mode.
these video modes are supported:
00h - text mode. 40x25. 16 colors. 8 pages.
03h - text mode. 80x25. 16 colors. 8 pages.
13h - graphical mode. 40x25. 256 colors. 320x200 pixels. 1 page.
example:
mov al, 13h
mov ah, 0
int 10h

INT 10h / AH = 01h - set text-mode cursor shape.


input:
CH = cursor start line (bits 0-4) and options (bits 5-7).
CL = bottom cursor line (bits 0-4).
when bit 5 of CH is set to 0, the cursor is visible. when bit 5 is 1, the cursor is not
visible.

; hide blinking text cursor:


mov ch, 32
mov ah, 1
int 10h

; show standard blinking text cursor:


mov ch, 6
mov cl, 7
mov ah, 1
int 10h

; show box-shaped blinking text cursor:


mov ch, 0
mov cl, 7
mov ah, 1
int 10h
; note: some bioses required CL to be >=7,
; otherwise wrong cursor shapes are displayed.

(http://www.emu8086.com, 2011)
108

INT 10h / AH = 2 - set cursor position.


input:
DH = row.
DL = column.
BH = page number (0..7).
example:

mov dh, 10
mov dl, 20
mov bh, 0
mov ah, 2
int 10h

INT 10h / AH = 03h - get cursor position and size.


input:
BH = page number.
return:
DH = row.
DL = column.
CH = cursor start line.
CL = cursor bottom line.

INT 10h / AH = 05h - select active video page.


input:
AL = new page number (0..7).
the activated page is displayed.

INT 10h / AH = 06h - scroll up window.


INT 10h / AH = 07h - scroll down window.
input:
AL = number of lines by which to scroll (00h = clear entire window).
BH = attribute used to write blank lines at bottom of window.
CH, CL = row, column of window's upper left corner.
DH, DL = row, column of window's lower right corner.

INT 10h / AH = 08h - read character and attribute at cursor position.


input:
BH = page number.
return:
AH = attribute.
AL = character.

(http://www.emu8086.com, 2011)
109

INT 10h / AH = 09h - write character and attribute at cursor position.


input:
AL = character to display.
BH = page number.
BL = attribute.
CX = number of times to write character.

INT 10h / AH = 0Ah - write character only at cursor position.


input:
AL = character to display.
BH = page number.
CX = number of times to write character.

INT 10h / AH = 0Ch - change color for a single pixel.


input:
AL = pixel color
CX = column.
DX = row.
example:

mov al, 13h


mov ah, 0
int 10h ; set graphics video mode.
mov al, 1100b
mov cx, 10
mov dx, 20
mov ah, 0ch
int 10h ; set pixel.

INT 10h / AH = 0Dh - get color of a single pixel.


input:
CX = column.
DX = row.
output:
AL = pixel color

(http://www.emu8086.com, 2011)
110

INT 10h / AH = 0Eh - teletype output.


input:
AL = character to write.
this function displays a character on the screen, advancing the cursor and scrolling the
screen as necessary. the printing is always done to current active page.

example:

mov al, 'a'


mov ah, 0eh
int 10h

; note: on specific systems this


; function may not be supported in graphics mode.

INT 10h / AH = 13h - write string.


input:
AL = write mode:
bit 0: update cursor after writing;
bit 1: string contains attributes.
BH = page number.
BL = attribute if string contains only characters (bit 1 of AL is zero).
CX = number of characters in string (attributes are not counted).
DL,DH = column, row at which to start writing.
ES:BP points to string to be printed.
example:

mov al, 1
mov bh, 0
mov bl, 0011_1011b
mov cx, msg1end - offset msg1 ; calculate message size.
mov dl, 10
mov dh, 7
push cs
pop es
mov bp, offset msg1
mov ah, 13h
int 10h
jmp msg1end
msg1 db " hello, world! "
msg1end:

(http://www.emu8086.com, 2011)
111

INT 10h / AX = 1003h - toggle intensity/blinking.


input:
BL = write mode:
0: enable intensive colors.
1: enable blinking (not supported by the emulator and windows command prompt).
BH = 0 (to avoid problems on some adapters).
example:

mov ax, 1003h


mov bx, 0
int 10h

bit color table:


character attribute is 8 bit value, low 4 bits set fore color, high 4 bits set background
color.

note: the emulator and windows command line prompt do not support background
blinking, however to make colors look the same in dos and in full screen mode it is
required to turn off the background blinking.

HEX BIN COLOR

0 0000 black
1 0001 blue
2 0010 green
3 0011 cyan
4 0100 red
5 0101 magenta
6 0110 brown
7 0111 light gray
8 1000 dark gray
9 1001 light blue
A 1010 light green
B 1011 light cyan
C 1100 light red
D 1101 light magenta
E 1110 yellow
F 1111 white
note:
; use this code for compatibility with dos/cmd prompt full screen mode:
mov ax, 1003h
mov bx, 0 ; disable blinking.
int 10h
(http://www.emu8086.com, 2011)
112

INT 11h - get BIOS equipment list.


return:
AX = BIOS equipment list word, actually this call returns the contents of the word at
0040h:0010h.
Currently this function can be used to determine the number of installed number of
floppy disk drives.
Bit fields for BIOS-detected installed hardware:
bit(s) Description
15-14 Number of parallel devices.
13 Reserved.
12 Game port installed.
11-9 Number of serial devices.
8 Reserved.
7-6 Number of floppy disk drives (minus 1):
00 single floppy disk;
01 two floppy disks;
10 three floppy disks;
11 four floppy disks.
5-4 Initial video mode:
00 EGA,VGA,PGA, or other with on-board video BIOS;
01 40x25 CGA color.
10 80x25 CGA color (emulator default).
11 80x25 mono text.
3 Reserved.
2 PS/2 mouse is installed.
1 Math coprocessor installed.
0 Set when booted from floppy.

INT 12h - get memory size.


return:
AX = kilobytes of contiguous memory starting at absolute address 00000h, this call
returns the contents of the word at 0040h:0013h.
Floppy drives are emulated using FLOPPY_0(..3) files.

INT 13h / AH = 00h - reset disk system.

INT 13h / AH = 02h - read disk sectors into memory.

(http://www.emu8086.com, 2011)
113

INT 13h / AH = 03h - write disk sectors.


input:
AL = number of sectors to read/write (must be nonzero)
CH = cylinder number (0..79).
CL = sector number (1..18).
DH = head number (0..1).
DL = drive number (0..3 , for the emulator it depends on quantity of FLOPPY_ files).
ES:BX points to data buffer.
return:
CF set on error.
CF clear if successful.
AH = status (0 - if successful).
AL = number of sectors transferred.
Note: each sector has 512 bytes.

INT 15h / AH = 86h - BIOS wait function.


input:
CX:DX = interval in microseconds
return:
CF clear if successful (wait interval elapsed),
CF set on error or when wait function is already in progress.

Note:
the resolution of the wait period is 977 microseconds on many systems (1 million
microseconds - 1 second).
Windows XP does not support this interrupt (always sets CF=1).

INT 16h / AH = 00h - get keystroke from keyboard (no echo).


return:
AH = BIOS scan code.
AL = ASCII character.
(if a keystroke is present, it is removed from the keyboard buffer).

INT 16h / AH = 01h - check for keystroke in the keyboard buffer.


return:
ZF = 1 if keystroke is not available.
ZF = 0 if keystroke available.
AH = BIOS scan code.
AL = ASCII character.
(if a keystroke is present, it is not removed from the keyboard buffer).

(http://www.emu8086.com, 2011)
114

INT 19h - system reboot.


Usually, the BIOS will try to read sector 1, head 0, track 0 from drive A: to
0000h:7C00h. The emulator just stops the execution, to boot from floppy drive select
from the menu: 'virtual drive' -> 'boot from floppy'

INT 1Ah / AH = 00h - get system time.


return:
CX:DX = number of clock ticks since midnight.
AL = midnight counter, advanced each time midnight passes.
notes:
there are approximately 18.20648 clock ticks per second,
and 1800B0h per 24 hours.
AL is not set by the emulator.

INT 20h - exit to operating system.

The short list of emulated MS-DOS interrupts -- INT 21h

DOS file system is emulated in C:\emu8086\vdrive\x (x is a drive letter)

If no drive letter is specified and current directory is not set, then


C:\emu8086\MyBuild\ path is used by default. FLOPPY_0,1,2,3 files are emulated
independently from DOS file system.

For the emulator physical drive A: is this file c:\emu8086\FLOPPY_0 (for BIOS
interrupts: INT 13h and boot).

For DOS interrupts (INT 21h) drive A: is emulated in this subdirectory:


C:\emu8086\vdrive\a\

Note: DOS file system limits the file and directory names to 8 characters, extension is
limited to 3 characters;
example of a valid file name: myfile.txt (file name = 6 chars, extension - 3 chars).
extension is written after the dot, no other dots are allowed. Back to Top

INT 21h / AH=1 - read character from standard input, with echo, result is stored in
AL.

if there is no character in the keyboard buffer, the function waits until any key is
pressed.

(http://www.emu8086.com, 2011)
115

example:

mov ah, 1
int 21h

INT 21h / AH=2 - write character to standard output.


entry: DL = character to write, after execution AL = DL.
example:

mov ah, 2
mov dl, 'a'
int 21h

INT 21h / AH=5 - output character to printer.


entry: DL = character to print, after execution AL = DL.
example:
mov ah, 5
mov dl, 'a'
int 21h

INT 21h / AH=6 - direct console input or output.

parameters for output: DL = 0..254 (ascii code)


parameters for input: DL = 255

for output returns: AL = DL


for input returns: ZF set if no character available and AL = 00h, ZF clear if character
available.
AL = character read; buffer is cleared.
example:
mov ah, 6
mov dl, 'a'
int 21h ; output character.

mov ah, 6
mov dl, 255
int 21h ; get character from keyboard buffer (if any) or set ZF=1.

INT 21h / AH=7 - character input without echo to AL.


if there is no character in the keyboard buffer, the function waits until any key is
pressed.

(http://www.emu8086.com, 2011)
116

example:

mov ah, 7
int 21h

INT 21h / AH=9 - output of a string at DS:DX. String must be terminated by '$'.
example:
org 100h
mov dx, offset msg
mov ah, 9
int 21h
ret
msg db "hello world $"

INT 21h / AH=0Ah - input of a string to DS:DX, fist byte is buffer size, second byte
is number of chars actually read. this function does not add '$' in the end of string. to
print using INT 21h / AH=9 you must set dollar character at the end of it and start
printing from address DS:DX + 2.

example:
org 100h
mov dx, offset buffer
mov ah, 0ah
int 21h
jmp print
buffer db 10,?, 10 dup(' ')
print:
xor bx, bx
mov bl, buffer[1]
mov buffer[bx+2], '$'
mov dx, offset buffer + 2
mov ah, 9
int 21h
ret

the function does not allow to enter more characters than the specified buffer size.
see also int21.asm in c:\emu8086\examples

INT 21h / AH=0Bh - get input status;


returns: AL = 00h if no character available, AL = 0FFh if character is available.

(http://www.emu8086.com, 2011)
117

INT 21h / AH=0Ch - flush keyboard buffer and read standard input.
entry: AL = number of input function to execute after flushing buffer (can be
01h,06h,07h,08h, or 0Ah - for other values the buffer is flushed but no input is
attempted); other registers as appropriate for the selected input function.

INT 21h / AH= 0Eh - select default drive.


Entry: DL = new default drive (0=A:, 1=B:, etc)
Return: AL = number of potentially valid drive letters

Notes: the return value is the highest drive present.

INT 21h / AH= 19h - get current default drive.


Return: AL = drive (0=A:, 1=B:, etc)

INT 21h / AH=25h - set interrupt vector;


input: AL = interrupt number. DS:DX -> new interrupt handler.

INT 21h / AH=2Ah - get system date;


return: CX = year (1980-2099). DH = month. DL = day. AL = day of week
(00h=Sunday)

INT 21h / AH=2Ch - get system time;


return: CH = hour. CL = minute. DH = second. DL = 1/100 seconds.

INT 21h / AH=35h - get interrupt vector;


entry: AL = interrupt number;
return: ES:BX -> current interrupt handler.

INT 21h / AH= 39h - make directory.


entry: DS:DX -> ASCIZ pathname; zero terminated string, for example:

org 100h
mov dx, offset filepath
mov ah, 39h
int 21h

ret

filepath DB "C:\mydir", 0 ; path to be created.


end
the above code creates c:\emu8086\vdrive\C\mydir directory if run by the emulator.
Return: CF clear if successful AX destroyed. CF set on error AX = error code.
Note: all directories in the given path must exist except the last one.
(http://www.emu8086.com, 2011)
118

INT 21h / AH= 3Ah - remove directory.


Entry: DS:DX -> ASCIZ pathname of directory to be removed.
Return:
CF is clear if successful, AX destroyed CF is set on error AX = error code.
Notes: directory must be empty (there should be no files inside of it).

INT 21h / AH= 3Bh - set current directory.


Entry: DS:DX -> ASCIZ pathname to become current directory (max 64 bytes).

Return:
Carry Flag is clear if successful, AX destroyed.
Carry Flag is set on error AX = error code.
Notes: even if new directory name includes a drive letter, the default drive is not
changed,
only the current directory on that drive.

INT 21h / AH= 3Ch - create or truncate file.


entry:
CX = file attributes:
mov cx, 0 ; normal - no attributes.
mov cx, 1 ; read-only.
mov cx, 2 ; hidden.
mov cx, 4 ; system
mov cx, 7 ; hidden, system and read-only!
mov cx, 16 ; archive

DS:DX -> ASCIZ filename.


returns:
CF clear if successful, AX = file handle.
CF set on error AX = error code.

note: if specified file exists it is deleted without a warning.

(http://www.emu8086.com, 2011)
119

example:
org 100h
mov ah, 3ch
mov cx, 0
mov dx, offset filename
mov ah, 3ch
int 21h
jc err
mov handle, ax
jmp k
filename db "myfile.txt", 0
handle dw ?
err:
; ....
k:
ret

INT 21h / AH= 3Dh - open existing file.


Entry:
AL = access and sharing modes:
mov al, 0 ; read
mov al, 1 ; write
mov al, 2 ; read/write

DS:DX -> ASCIZ filename.

Return:
CF clear if successful, AX = file handle.
CF set on error AX = error code.

note 1: file pointer is set to start of file.


note 2: file must exist.

(http://www.emu8086.com, 2011)
120

example:
org 100h
mov al, 2
mov dx, offset filename
mov ah, 3dh
int 21h
jc err
mov handle, ax
jmp k
filename db "myfile.txt", 0
handle dw ?
err:
; ....
k:
ret

INT 21h / AH= 3Eh - close file.


Entry: BX = file handle
Return:
CF clear if successful, AX destroyed.
CF set on error, AX = error code (06h).

INT 21h / AH= 3Fh - read from file.


Entry:
BX = file handle.
CX = number of bytes to read.
DS:DX -> buffer for data.
Return:
CF is clear if successful - AX = number of bytes actually read; 0 if at EOF (end of
file) before call.
CF is set on error AX = error code.

Note: data is read beginning at current file position, and the file position is updated
after a successful read the returned AX may be smaller than the request in CX if a
partial read occurred.

INT 21h / AH= 40h - write to file.


entry:
BX = file handle.
CX = number of bytes to write.
DS:DX -> data to write.

(http://www.emu8086.com, 2011)
121

return:
CF clear if successful; AX = number of bytes actually written.
CF set on error; AX = error code.

note: if CX is zero, no data is written, and the file is truncated or extended to the
current position data is written beginning at the current file position, and the file
position is updated after a successful write the usual cause for AX < CX on return is a
full disk.

INT 21h / AH= 41h - delete file (unlink).


Entry:
DS:DX -> ASCIZ filename (no wildcards, but see notes).
return:
CF clear if successful, AX destroyed. AL is the drive of deleted file (undocumented).
CF set on error AX = error code.

Note: DOS does not erase the file's data; it merely becomes inaccessible because the
FAT chain for the file is cleared deleting a file which is currently open may lead to
filesystem corruption.

INT 21h / AH= 42h - SEEK - set current file position.


Entry:
AL = origin of move: 0 - start of file. 1 - current file position. 2 - end of file.
BX = file handle.
CX:DX = offset from origin of new file position.
Return:
CF clear if successful, DX:AX = new file position in bytes from start of file.
CF set on error, AX = error code.

Notes:

for origins 1 and 2, the pointer may be positioned before the start of the file; no error
is returned in that case, but subsequent attempts to read or write the file will produce
errors. If the new position is beyond the current end of file, the file will be extended
by the next write (see AH=40h).

(http://www.emu8086.com, 2011)
122

example:

org 100h
mov ah, 3ch
mov cx, 0
mov dx, offset filename
mov ah, 3ch
int 21h ; create file...
mov handle, ax

mov bx, handle


mov dx, offset data
mov cx, data_size
mov ah, 40h
int 21h ; write to file...

mov al, 0
mov bx, handle
mov cx, 0
mov dx, 7
mov ah, 42h
int 21h ; seek...

mov bx, handle


mov dx, offset buffer
mov cx, 4
mov ah, 3fh
int 21h ; read from file...

mov bx, handle


mov ah, 3eh
int 21h ; close file...
ret

filename db "myfile.txt", 0
handle dw ?
data db " hello files! "
data_size=$-offset data
buffer db 4 dup(' ')

(http://www.emu8086.com, 2011)
123

INT 21h / AH= 47h - get current directory.


Entry:
DL = drive number (00h = default, 01h = A:, etc)
DS:SI -> 64-byte buffer for ASCIZ pathname.
Return:
Carry is clear if successful
Carry is set on error, AX = error code (0Fh)

Notes:
the returned path does not include a drive and the initial backslash.

INT 21h / AH=4Ch - return control to the operating system (stop program).

INT 21h / AH= 56h - rename file / move file.


Entry:
DS:DX -> ASCIZ filename of existing file.
ES:DI -> ASCIZ new filename.
Return:
CF clear if successful.
CF set on error, AX = error code.

Note: allows move between directories on same logical drive only; open files should
not be renamed!

Mouse driver interrupts -- INT 33h

INT 33h / AX=0000 - mouse initialization. any previous mouse pointer is hidden.

returns:
if successful: AX=0FFFFh and BX=number of mouse buttons.
if failed: AX=0
example:

mov ax, 0
int 33h
see also: mouse.asm in examples.

(http://www.emu8086.com, 2011)
124

INT 33h / AX=0001 - show mouse pointer.


example:
mov ax, 1
int 33h

INT 33h / AX=0002 - hide visible mouse pointer.


example:
mov ax, 2
int 33h

INT 33h / AX=0003 - get mouse position and status of its buttons.
returns:
if left button is down: BX=1
if right button is down: BX=2
if both buttons are down: BX=3
CX = x
DX = y
example:

mov ax, 3
int 33h

; note: in graphical 320x200 mode the value of CX is doubled.


; see mouse2.asm in examples.

Further Reading:

Click the link below for the tutorial on how to program using Assembly Language
using the EMU8086 Software:

https://drive.google.com/drive/folders/1DjvqbBqAOeib4pOTQhsxnVXnaDCmXvGF
?usp=sharing

PROGRESS CHECK

1. What is a machine language?


2. What is an assembly language?
3. What are the types of operands that are supported in the use of MOV
command?
4. What is the first requirement before accessing the interrupts?
125

REFERENCES

Austerlitz, H. (2003). https://www.sciencedirect.com. Retrieved from


https://www.sciencedirect.com: https://www.sciencedirect.com/topics/computer-
science/assembly-language

http://www.emu8086.com. (2011, December 10). Retrieved from http://www.emu8086.com:


http://www.emu8086.com:80/assembler_tutorial/8086_bios_and_dos_interrupts.html
#top1

https://techterms.com. (2020). Retrieved from https://techterms.com:


https://techterms.com/definition/machine_language

https://www.stechies.com. (2019, February 6). Retrieved from https://www.stechies.com:


https://www.stechies.com/difference-between-machine-language-assembly-language/

https://www.tutorialspoint.com. (2020). Retrieved from https://www.tutorialspoint.com:


https://www.tutorialspoint.com/microprocessor/microprocessor_8086_instruction_set
s.htm#:~:text=Instructions%20to%20transfer%20flag%20registers,stack%20to%20th
e%20flag%20register.
126

LEARNING GUIDE

Week No.: __8__

TOPIC/S

1. Introduction to Microcontrollers
2. Introduction to the Arduino Microcontroller Board and Atmel Microcontroller and
its function.

EXPECTED COMPETENCIES

Upon completing this Learning Module, you will be able to:


1. Discuss the basic features and specifications of the Arduino board

CONTENT/TECHNICAL INFORMATION

Microcontroller

A microcontroller is an integrated circuit (IC) device used for controlling other


portions of an electronic system, usually via a microprocessor unit (MPU), memory,
and some peripherals. These devices are optimized for embedded applications that
require both processing functionality and agile, responsive interaction with digital,
analog, or electromechanical components. (Keim, 2019)

The most common way to refer to this category of integrated circuits is


“microcontroller" but the abbreviation “MCU” is used interchangeably as it stands for
“microcontroller unit”. You may also occasionally see “µC” (where the Greek letter mu
replaces “micro”). (Keim, 2019)

“Microcontroller” is a well-chosen name because it emphasizes defining


characteristics of this product category. The prefix “micro” implies smallness and the
term "controller" here implies an enhanced ability to perform control functions. As
stated above, this functionality is the result of combining a digital processor and digital
memory with additional hardware that is specifically designed to help the
microcontroller interact with other components. (Keim, 2019)

Microcontrollers VS Microprocessors

People will sometimes use the term “microprocessor” or "MPU" when referring
to a microcontroller, but these two devices are not necessarily the same. Both
microprocessors and microcontrollers function as small, highly integrated computer
systems, but they may serve different purposes. (Keim, 2019)
127

The term “processor” is used to identify a system that consists of a central


processing unit and (optionally) some memory; a microprocessor is a device that
implements all of a processor’s functionality within a single integrated circuit.
Microcontrollers, by comparison, place greater emphasis on additional hardware
modules that allow the device to control a system rather than simply execute
instructions and store data. (Keim, 2019)

The diagram below illustrates this concept.

Comparison of Microcontroller and Microprocessor (Keim, 2019)

Microcontrollers VS Digital Signal Processors (DSPs)

A digital signal processor (or "DSP") is a microprocessor that is optimized for


demanding computational tasks such as digital filtering, mathematical analysis of real-
time signals, and data compression. A highly sophisticated microcontroller may be able
to function as a replacement for a digital signal processor, but it is still considered a
microcontroller if a significant portion of its internal circuitry is intended to control,
monitor, and communicate with the surrounding system. (Keim, 2019)
128

Elements of a Microcontroller

The Central Processing Unit

The CPU performs arithmetic operations, manages data flow, and generates
control signals in accordance with the sequence of instructions created by the
programmer. The extremely complex circuitry required for CPU functionality is not
visible to the designer. In fact, thanks to integrated development environments and
high-level languages such as C, writing code for microcontrollers is often a fairly
straightforward task. (Keim, 2019)

Memory

Nonvolatile memory is used to store the microcontroller’s program—i.e., the


(often very long) list of machine-language instructions that tell the CPU exactly what
to do. You will typically see the word “Flash” (which refers to a specific form of
nonvolatile data storage) instead of “nonvolatile memory.” (Keim, 2019)

Volatile memory (i.e., RAM) is used for temporary data storage. This data is
lost when the microcontroller loses power. Internal registers also provide temporary
data storage, but we don’t think of these as a separate functional block because they are
integrated into the CPU. (Keim, 2019)

Peripherals

We use the word “peripheral” to describe the hardware modules that help a
microcontroller to interact with the external system. The following bullet points identify
the various categories of peripherals and provide examples.

 Data converters: analog-to-digital converter, digital-to-analog converter,


reference-voltage generator
 Clock generation: internal oscillator, crystal-drive circuitry, phase-locked
loop
 Timing: general-purpose timer, real-time clock, external-event counter, pulse-
with modulation
 Analog signal processing: operational amplifier, analog comparator
 Input/output: general-purpose digital input and output circuitry, parallel
memory interface
 Serial communication: UART, SPI, I2C, USB
(Keim, 2019)
129

Support Circuitry

Microcontrollers incorporate a variety of functional blocks that cannot be


classified as peripherals because their primary purpose is not to control, monitor, or
communicate with external components. They are, nonetheless, very important—they
support the internal operation of the device, simplify implementation, and improve the
development process. (Keim, 2019)

• Debug circuitry allows the designer to carefully monitor the microcontroller as


it is executing instructions. This is an important, and sometimes indispensable,
method of tracking down bugs and optimizing firmware performance.
• Interrupts are an extremely valuable aspect of microcontroller functionality.
Interrupts are generated by external or internal hardware-based events, and they
cause the processor to immediately respond to these events by executing a
specific group of instructions.
• A clock-generation module can be considered a peripheral if it is intended to
produce signals that will be used outside the chip, but in many cases the primary
purpose of a microcontroller’s internal oscillator is to provide a clock signal for
the CPU and peripherals. Internal oscillators often have low precision, but in
applications that can tolerate this low precision, they are a convenient and
effective way to simplify the design and save board space.
• Microcontrollers can incorporate various types of power-supply circuitry.
Integrated voltage regulators allow for on-chip generation of required supply
voltages, power-management modules can be used to significantly reduce the
device’s current consumption during inactive states, and supervisor modules
can place the processor in a stable reset state when the supply voltage is not high
enough to ensure reliable operation.
(Keim, 2019)

ATMega328P Microcontroller

ATMega328P Microcontroller and its Pin Diagram. (https://components101.com, 2018)


130

ATMEGA328P is high performance, low power controller from Microchip.


ATMEGA328P is an 8-bit microcontroller based on AVR RISC architecture. It is the
most popular of all AVR controllers as it is used in ARDUINO boards.
(https://components101.com, 2018)

ATMega328 Pin Configuration

ATMEGA328P is a 28 pin chip as shown in pin diagram above. Many pins of the chip
here have more than one function. We will describe functions of each pin in below
table. (https://components101.com, 2018)

Pin Pin Name Description Secondary Function


No.
1 PC6(Reset) Pin 6 of Pin by default is used as RESET pin. PC6
PORTC can only be used as I/O pin when
RSTDISBL Fuse is programmed.
2 PD0(RXD) Pin 0 of RXD (Data Input Pin for USART)
PORTD
USART Serial Communication Interface

[Can be used for programming]


3 PD1 (TXD) Pin1 of TXD (Data Output Pin for USART)
PORTD
USART Serial Communication Interface

[Can be used for programming]

INT2(External Interrupt 2 Input)


4 PD2 (INT0) Pin2 of External Interrupt source 0
PORTD
5 PD3 Pin3 of External Interrupt source1
(INT1/OC2B) PORTD
OC2B (PWM - Timer/Counter2 Output
Compare Match B Output)
6 PD4 (XCK/T0) Pin4 of T0(Timer0 External Counter Input)
PORTD
XCK (USART External Clock I/O)
7 VCC Connected to positive voltage
8 GND Connected to ground
9 PB6 Pin6 of XTAL1 (Chip Clock Oscillator pin 1 or
(XTAL1/TOSC1) PORTB External clock input)

TOSC1 (Timer Oscillator pin 1)


10 PB7 Pin7 of XTAL2 (Chip Clock Oscillator pin 2)
(XTAL2/TOSC2) PORTB
TOSC2 (Timer Oscillator pin 2)

(https://components101.com, 2018)
131

Continued
Pin Pin Name Description Secondary Function
No.
11 PD5 Pin5 of T1(Timer1 External Counter Input)
(T1/OC0B) PORTD
OC0B (PWM - Timer/Counter0 Output
Compare Match B Output)
12 PD6 Pin6 of AIN0(Analog Comparator Positive I/P)
(AIN0/OC0A) PORTD
OC0A (PWM - Timer/Counter0 Output
Compare Match A Output)
13 PD7 (AIN1) Pin7 of AIN1(Analog Comparator Negative I/P)
PORTD
14 PB0 Pin0 of ICP1(Timer/Counter1 Input Capture Pin)
(ICP1/CLKO) PORTB
CLKO (Divided System Clock. The
divided system clock can be output on the
PB0 pin)
15 PB1 (OC1A) Pin1 of OC1A (Timer/Counter1 Output Compare
PORTB Match A Output)
16 PB2 (SS/OC1B) Pin2 of SS (SPI Slave Select Input). This pin is
PORTB low when controller acts as slave.

[Serial Peripheral Interface (SPI) for


programming]

OC1B (Timer/Counter1 Output Compare


Match B Output)
17 PB3 Pin3 of MOSI (Master Output Slave Input).
(MOSI/OC2A) PORTB When controller acts as slave, the data is
received by this pin. [Serial Peripheral
Interface (SPI) for programming]

OC2 (Timer/Counter2 Output Compare


Match Output)
18 PB4 (MISO) Pin4 of MISO (Master Input Slave Output).
PORTB When controller acts as slave, the data is
sent to master by this controller through
this pin.

[Serial Peripheral Interface (SPI) for


programming]
19 PB5 (SCK) Pin5 of SCK (SPI Bus Serial Clock). This is the
PORTB clock shared between this controller and
other system for accurate data transfer.

[Serial Peripheral Interface (SPI) for


programming]
(https://components101.com, 2018)
132

Continued
Pin Pin Name Description Secondary Function
No.
20 AVCC Power for Internal ADC Converter
21 AREF Analog Reference Pin for ADC
22 GND Ground
23 PC0 (ADC0) Pin0 of ADC0 (ADC Input Channel 0)
PORTC
24 PC1 (ADC1) Pin1 of ADC1 (ADC Input Channel 1)
PORTC
25 PC2 (ADC2) Pin2 of ADC2 (ADC Input Channel 2)
PORTC
26 PC3 (ADC3) Pin3 of ADC3 (ADC Input Channel 3)
PORTC
27 PC4 Pin4 of ADC4 (ADC Input Channel 4)
(ADC4/SDA) PORTC
SDA (Two-wire Serial Bus Data
Input/output Line)
28 PC5 (ADC5/SCL) Pin5 of ADC5 (ADC Input Channel 5)
PORTC
SCL (Two-wire Serial Bus Clock Line)
(https://components101.com, 2018)

Features
CPU 8-bit AVR
Number of Pins 28
Operating Voltage +1.8 V TO +5.5V
(V)
Number of 23
programmable I/O
lines
Communication Master/Slave SPI Serial Interface(17,18,19 PINS) [Can be
Interface used for programming this controller]

Programmable Serial USART(2,3 PINS) [Can be used for


programming this controller]

Two-wire Serial Interface(27,28 PINS)[Can be used to


connect peripheral devices like Servos, sensors and memory
devices]
JTAG Interface Not available
ADC Module 6channels, 10-bit resolution ADC
Timer Module Two 8-bit counters with Separate Prescaler and compare
mode, One 16-bit counter with Separate Prescaler,compare
mode and capture mode.
Analog Comparators 1(12,13 PINS)
DAC Module Nil
(https://components101.com, 2018)
133

Continued
PWM channels 6
External Oscillator 0-4MHz @ 1.8V to 5.5V

0-10MHz @ 2.7V to 5.5V

0-20MHz @ 4.5V to 5.5V


Internal Oscillator 8MHz Calibrated Internal Oscillator
Program Memory Flash
Type
Program Memory or 32Kbytes [10,000 write/erase cycles]
Flash memory
CPU Speed 1MIPS for 1MHz
RAM 2Kbytes Internal SRAM
EEPROM 1Kbytes EEPROM
Watchdog Timer Programmable Watchdog Timer with Separate On-chip
Oscillator
Program Lock Yes
Power Save Modes Six Modes[Idle, ADC Noise Reduction, Power-save, Power-
down, Standby and Extended Standby]
Operating -40°C to +105°C(+105 being absolute maximum, -40 being
Temperature absolute minimum)
(https://components101.com, 2018)

How to Use ATMega328P in Arduino

Since ATmega328P is used in Arduino Uno and Arduino nano boards, you can
directly replace the arduino board with ATmega328 chip. For that first you need to
install the Arduino bootloader into the chip (Or you can also buy a chip with bootloader
– ATMega328P-PU). This IC with bootloader can be placed on Arduino Uno board and
burn the program into it. Once Arduino program is burnt into the IC, it can be removed
and used in place of Arduino board, along with a Crystal oscillator and other
components as required for the project. Below is the pin mapping between Arduino
Uno and ATmega328P chip. (https://components101.com, 2018)

ATMega328P and Arduino UNO Pin Mapping (https://components101.com, 2018)


134

Arduino

Arduino is an open-source platform used for building electronics projects.


Arduino consists of both a physical programmable circuit board (often referred to as a
microcontroller) and a piece of software, or IDE (Integrated Development
Environment) that runs on your computer, used to write and upload computer code to
the physical board. (B_E_N, 2020)
The Arduino platform has become quite popular with people just starting out
with electronics, and for good reason. Unlike most previous programmable circuit
boards, the Arduino does not need a separate piece of hardware (called a programmer)
in order to load new code onto the board -- you can simply use a USB cable.
Additionally, the Arduino IDE uses a simplified version of C++, making it easier to
learn to program. Finally, Arduino provides a standard form factor that breaks out the
functions of the micro-controller into a more accessible package. (B_E_N, 2020)

Arduino UNO Board (B_E_N, 2020)

Arduino Uno Pin Diagram


The Arduino Uno board can be built with power pins, analog pins, ATmegs328,
ICSP header, Reset button, power LED, digital pins, test led 13, TX/RX pins, USB
interface, an external power supply. (https://www.elprocus.com, 2020)

Arduino Board Pin Configuration (https://www.elprocus.com, 2020)


135

Power Supply

The Arduino Uno power supply can be done with the help of a USB cable or an
external power supply. The external power supplies mainly include AC to DC adapter
otherwise a battery. The adapter can be connected to the Arduino Uno by plugging into
the power jack of the Arduino board. Similarly, the battery leads can be connected to
the Vin pin and the GND pin of the POWER connector. The suggested voltage range
will be 7 volts to 12 volts. (https://www.elprocus.com, 2020)

Input & Output

The 14 digital pins on the Arduino Uno can be used as input & output with the
help of the functions like pinMode(), digitalWrite(), & Digital Read().
(https://www.elprocus.com, 2020)

Pin1 (TX) & Pin0 (RX) (Serial): This pin is used to transmit & receive TTL
serial data, and these are connected to the ATmega8U2 USB to TTL Serial chip
equivalent pins. (https://www.elprocus.com, 2020)

Pin 2 & Pin 3 (External Interrupts): External pins can be connected to activate
an interrupt over a low value, change in value. (https://www.elprocus.com, 2020)

Pins 3, 5, 6, 9, 10, & 11 (PWM): This pin gives 8-bit PWM o/p by the function
of analogWrite(). (https://www.elprocus.com, 2020)

SPI Pins (Pin-10 (SS), Pin-11 (MOSI), Pin-12 (MISO), Pin-13 (SCK): These
pins maintain SPI-communication, even though offered by the fundamental hardware,
is not presently included within the Arduino language. (https://www.elprocus.com,
2020)
Pin-13(LED): The inbuilt LED can be connected to pin-13 (digital pin). As the
HIGH-value pin, the light emitting diode is activated, whenever the pin is LOW.
(https://www.elprocus.com, 2020)

Pin-4 (SDA) & Pin-5 (SCL) (I2C): It supports TWI-communication with the
help of the Wire library. (https://www.elprocus.com, 2020)

AREF (Reference Voltage): The reference voltage is for the analog i/ps with
analogReference(). (https://www.elprocus.com, 2020)

Reset Pin: This pin is used for reset (RST) the microcontroller.
(https://www.elprocus.com, 2020)
136

Memory

The memory of this Atmega328 Arduino microcontroller includes flash


memory-32 KB for storing code, SRAM-2 KB EEPROM-1 KB.
(https://www.elprocus.com, 2020)

Communication

The Arduino Uno ATmega328 offers UART TTL-serial communication, and it


is accessible on digital pins like TX (1) and RX (0). The software of an Arduino has a
serial monitor that permits easy data. There are two LEDs on the board like RX & TX
which will blink whenever data is being broadcasted through the USB.
(https://www.elprocus.com, 2020)

A SoftwareSerial library permits for serial communication on Arduino Uno


digital pins and the ATmega328P supports TWI (I2C) as well as SPI-communication.
The Arduino software contains a wired library for simplifying the utilization of the I2C
bus. (https://www.elprocus.com, 2020)

Features
Microcontroller ATmega328P – 8 bit AVR family microcontroller
Operating Voltage 5V
Recommended Input Voltage 7 – 12 V
Input Voltage Limits 6 – 20 V
Analog Input Pins 6 (A0 – A5)
Digital I/O Pins 14 (Out of which 6 provide PWM output)
DC Current on I/O Pins 40 mA
DC Current on 3.3V Pin 50 mA
Flash Memory 32 KB (0.5 KB is used for Bootloader)
SRAM 2 KB
EEPROM 1 KB
Frequency (Clock Speed) 16 MHz
(https://components101.com, 2018)

How to Use an Arduino Uno?

Arduino Uno can detect the surroundings from the input. Here the input is a
variety of sensors and these can affect its surroundings through controlling motors,
lights, other actuators, etc. The ATmega328 microcontroller on the Arduino board can
be programmed with the help of an Arduino programming language and the IDE
(Integrated Development Environment). Arduino projects can communicate by
software while running on a PC.
137

Arduino Programming

Once the Arduino IDE tool is installed in the PC, attach the Arduino board to
the computer with the help of USB cable. Open the Arduino IDE & select the right
board by choosing Tools–>Board..>Arduino Uno, and select the right Port by choosing
Tools–>Port. This board can be programmed with the help of an Arduino programming
language depends on Wiring. (https://www.elprocus.com, 2020)

To activate the Arduino board & flash the LED on the board, dump the program
code with the selection of Files–> Examples..>Basics..>Blink. When the programming
codes are dumped into the IDE, and then click the button ‘upload’ on the top bar. Once
this process is completed, check the LED flash on the board.
(https://www.elprocus.com, 2020)

Arduino IDE with Blink Program (B_E_N, 2020)


138

PROGRESS CHECK
1. What is the difference between a microcontroller and a microprocessor?
2. Can the ATMega328P Microcontroller be used without the Arduino Board?
Justify your answer in 5-10 sentences only.
3. On the features of the Arduino Board, it says that the operating voltage is 5 volts,
why is it then that the recommended input voltage is 7 – 12 Volts? Justify your
answer in 5-10 sentences only.

REFERENCES

B_E_N. (2020). https://learn.sparkfun.com. Retrieved from https://learn.sparkfun.com:


https://learn.sparkfun.com/tutorials/what-is-an-arduino/all

https://components101.com. (2018, April 4). Retrieved from https://components101.com:


https://components101.com/microcontrollers/atmega328p-pinout-features-
datasheet#:~:text=ATMEGA328P%20is%20high%20performance%2C%20low,is%2
0used%20in%20ARDUINO%20boards.

https://components101.com. (2018, February 28). Retrieved from


https://components101.com: https://components101.com/microcontrollers/arduino-
uno

https://www.elprocus.com. (2020). Retrieved from https://www.elprocus.com:


https://www.elprocus.com/atmega328-arduino-uno-board-working-and-its-
applications/

Keim, R. (2019, March 25). https://www.allaboutcircuits.com. Retrieved from


https://www.allaboutcircuits.com: https://www.allaboutcircuits.com/technical-
articles/what-is-a-microcontroller-introduction-component-characteristics-component/

You might also like