FMPMC Notes3-5

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

Course name: Fundamentals of MPMC

Unit-3 Notes
8086 Microprocessor Interfacing

1. What is an 8255 Microprocessor?

8255 microprocessor is a very popularly used programmable peripheral interface chip or PPI
chip. The function of the 8255 microprocessor is to transmit data in various conditions from
simple I/O to interrupt I/O. This microprocessor is also designed for interfacing the CPU with
its external world like ADC, keyboard, DAC, etc. This microprocessor is economical,
functional, and flexible although is a little complex, so it can be used with any microprocessor.
This microprocessor is used to connect peripheral devices & also for interfacing. So this
peripheral device is also called an I/O device because the I/O ports of this microprocessor are
used for connecting I/O devices. This processor includes three 8-bit bidirectional I/O ports
which can be configured based on necessity.

2. Explain the Features of 8255 Microprocessor.

The features of the 8255 microprocessors include the following.

 The 8255 microprocessor is a PPI (programmable peripheral interface) device.

 It includes three I/O ports which are programmed within different modes.

 This microprocessor simply provides several facilities to connect different devices.


Thus, it is used in different applications frequently.

 It operates in three modes like Mode 0 (Simple I/O), Mode 1 (Strobed I/O), and Mode
2 (Strobed bi-directional I/O).

 It is compatible totally with the families of Intel microprocessors.

 It is TTL compatible.

 For port-C of this microprocessor, direct bit SET/RESET capacity is available.


 It includes 24 programmable input/output pins which are placed as 2 to 8-bit ports & 2
to 4-bit ports.

 It includes three 8-bit ports; Port-A, Port-B & Port-C.

 The three I/O ports include a control register that defines each I/O port’s function & in
which mode they must operate.
3. Draw and explain the Pin configuration of 8255 Microprocessor.

The pin diagram of the 8255 microprocessor is shown below. This microprocessor includes 40-
pins like PA7-PA0, PC7-PC0, PC3-PC0, PB0-PB7, RD, WR, CS, A1 & A0, D0-D7 and
RESET. These pins are discussed below.

PA7 to PA0 (Port A Pins)


The PA7 to PA0 are Port A data lines pins (1 to 4 & 37 to 40) which are distributed equally on
two sides of the top of the microprocessor. These eight port A pins work as either buffered input
lines or latched output based on the loaded control word into the control word register.
PB0 to PB7 (Port B Pins)
The PB0 to PB7 from 18 to 25 are the data line pins that carry the port B data.

PC0 to PC7 (Port C Pins)


PC0 to PC7 pins are port C pins which include pin10 to pin17 which carry the port A data bits.
From there, pins 10 – pin13 are known as Port C upper pins & pin14 to pin17 are known as
lower pins. The pins from these two sections can be used individually to transmit 4 data bits
using two separate port C parts.

D0 to D7 (Data bus pins)


These D0 to D7 pins are data I/O lines which include 27-pin to 34-pin. These pins are used to
carry the 8-bit binary code and it is utilized to train the entire IC work. These pins are jointly
known as the control register/control word which carries the data of the control word.

A0 & A1
A0 and A1 pins at pin8 & pin9 simply make a decision about which port will be preferred for
transmitting the data.

If A0 = 0 & A1=0 then Port-A is selected.


If A0 = 0 & A1=1 then Port-B is selected.
If A0 = 1 & A1=0 then Port-C is selected.
If A0 = 1 & A1=1 then the control register is selected.

CS’
The pin6 like CS’ is a chip select input pin which is responsible for selecting a chip. A low
signal at CS’ pin simply allows the communication between the 8255 & the processor which
means at this pin, the operation of data transfer gets allowed by an active low signal.

RD’
The pin5 like RD’ is a read input pin that puts the chip within the reading mode. A low signal
at this RD’s pin provides data to the CPU by a data buffer.

WR’
The pin36 like WR’ pin is a write input pin that puts the chip within writing mode. So, a low
signal at WR’ pin simply allows the CPU to execute the write operation above the ports
otherwise microprocessor’s control register through the data bus buffer.

RESET
The pin35 like the RESET pin resets the whole data available in all the keys to their default
values when it is in set mode. It is an active high signal where the high signal at the RESET
pin clears the control registers &the ports are placed within the input mode.

GND
The pin7 is a GND pin of IC.
VCC
The pin26 like VCC is the 5V input pin of IC.

4. Draw and explain the Architecture of 8255 Microprocessor

The architecture of the 8255 microprocessor is shown below.

Fig: 8255 Architecture

8255 Architecture Description:

Data Bus Buffer:


The data bus buffer is mainly used for connecting the inside bus of the microprocessor with the
system bus so that proper interfacing can be established between these two. This buffer simply
permits the read or write operation to be executed from or to the CPU. This buffer permits the
data supplied from the control register or ports to the CPU in case of write operation & from
the CPU to the status register or ports in case of the read operation.

Read/Write Control Logic:


Read or write control logic unit controls the inside system operations. This unit holds the
capability to manage both the data transfer & status or control words internally & externally.
Once there needs data to fetch then it allows the provided address by the 8255 by the bus &
generates a command immediately to the two control groups for the specific operation.

Group A & Group B Control:


Both these groups are managed by the CPU and work based on the generated command by the
CPU. This CPU transmits control words toward these two groups and they consecutively
transmit the suitable command to their particular port. Group A controls port A with higher
order port C bits whereas group B controls port B with lower order port C bits.

Port A & Port B


Port A & Port B includes an 8-bit input latch and 8-bit buffered or latched output. The main
function of these ports is also independent of the mode of operation. Port A can be programmed
in 3 modes like modes 0, 1, and 2 whereas Port B can be programmed in modes 0 & mode 1.

Port C
Port C includes an 8-bit data input buffer and 8-bit bidirectional data o/p latch or buffer. This
port is divided mainly into two sections – port C upper PCU & port C lower PC. So these two
sections are mainly programmed & separately used as a 4-bit I/O port. This port is used for
handshake signals, Simple I/O & status signal inputs. This port is used in combination with
port A & Port B for both the status and handshaking signals. This port provides only direct but
sets or resets capacity.

5. Write about the Operating Modes of 8255 Microprocessor.

The 8255 microprocessor has two operating modes like bit set-reset mode and input/output
mode which are discussed below.

Bit Set-Reset Mode


Bit set-reset mode is mainly utilized to set/reset only the Port-C bits. In this type of operating
mode, it only affects a time one bit of Port C. Once the user sets the bit, then it stays set until
it will unset by the user. The user requires loading the bit pattern within the control register to
modify the bit. Once port C is used for status/ control operation, then by sending an OUT
instruction, every individual port C bit can be set/reset.

I/O Mode
I/O mode has three different modes like Mode 0, Mode 1 & Mode 2 where each mode is
discussed below.

Mode 0:
This is an I/O mode of 8255 which simply allows the programming of each port like either i/p
or o/p port. So, the I/O feature of this mode simply includes:

The input ports are buffered whenever outputs are latched.


It does not support interrupt capability/ handshaking.
Mode 1:
Mode 1 of 8255 is I/O with handshaking, so in this type of mode, both the ports like Port A &
Port B are used as I/O ports whereas port C is used for handshaking. So this mode supports
handshaking by the programmed ports as either i/p or o/p mode. Handshaking signals are
mainly used to synchronize the transfer of data between two devices that works at different
speeds. The inputs & outputs in this mode are latched and this mode also has the ability to
interrupt handling & signal control to match the CPU & IO device’s speed.

Mode 2:
Mode2 is a Bidirectional I/O port with handshaking. So, the ports in this type of mode can be
used for the bidirectional data flow through handshaking signals. The group A pins can be
programmed to perform like bidirectional data bus & PC7 – PC4 in port C are used through
the handshaking signal. The remaining lower port C bits are used for input/Output operations.
This mode has the capacity of interrupt handling.

6. Write about the Semiconductor memories interfacing (RAM, ROM).

Memory interfacing
Memory is an integral part of a microcomputer system. There are two main types of
memory.
(i) Read only memory (ROM): As the name indicates this memory is available
only for reading purpose. The various types available under this category are
PROM, EPROM, EEPROM which contain system software and permanent
system data.
(ii) Random Access memory (RAM): This is also known as Read Write Memory.
It is a volatile memory. RAM contains temporary data and software programs
generally for different applications.

While executing particular task it is necessary to access memory to get instruction codes
and data stored in memory. Microprocessor initiates the necessary signals when read or
write operation is to be performed. Memory device also requires some signals to
perform read and write operations using various registers. To do the above job it is
necessary to have a device and a circuit, which performs this task is known as
interfacing device and as this is involved with memory it-is known as memory
interfacing device. The basic concepts of memory interfacing involve three different
tasks. The microprocessor should be able to read from or write into the specified
register. To do this it must be able to select the required chip, identify the required
register and it must enable the appropriate buffers.
Fig: A Simple Memory Device

Any memory device must contain address lines and Input, output lines, selection input,
control input to perform read or write operation. All memory devices have address
inputs that select memory location within the memory device. These lines are labelled
as AO ...... AN. The number of address lines indicates the total memory capacity of the
memory device. A 1K memory requires 10 address lines A0-A9. Similarly, a 1MB
requires 20 lines A0-A19 (in the case of 8086). The memory devices may have separate
I/O lines or a common set of bidirectional I/O lines. Using these lines data can be
transferred in either direction. Whenever output buffer is activated, the operation is read
whenever input buffers are activated the operation is written. These lines are labelled
as I/O, ......... I/On or DO .............Dn. The size of a memory location is dependent upon
the number of data bits. If the number of data lines are eight D0 - D7 then 8 bits or 1
byte of data can be stored in each location. Similarly, if numbers of data bits are 16 (D0
- D15) then the memory size is 2 bytes. For example, 2K x 8 indicates there are 2048
memory locations and each memory location can store 8 bits of data.

Memory devices may contain one or more inputs which are used to select the memory
device or to enable the memory device. This pin is denoted by CS (Chip select) or CE
(Chip enable). When this pin is at logic '0' then only the memory device performs a read
or a write operation. If this pin is at logic ‘1’ the memory chip is disabled. If there are
more than one CS input then all these pins must be activated to perform read or write
operation.

All memory devices will have one or more control inputs. When ROM is used, we find
OE output enable pin which allows data to flow out of the output data pins. To perform
this task both CS and OE must be active. A RAM contains one or two control inputs.
They are R / W or RD and WR. If there is only one input R/ W then it performs read
operation when R/ W pin is at logic 1. If it is at logic 0 it performs write operation. Note
that this is possible only when CS is also active.
7. Briefly explain about the Seven segment Display Interfacing to 8086
microprocessors.
8. Briefly explain about the Keypad Interfacing to 8086 microprocessors.

Keyboard Interfacing
In most keyboards, the key switches are connected in a matrix of Rows and Columns.
Getting meaningful data from a keyboard requires three major tasks:

1. Detect a Key Press


2. Debounce the Key Press
3. Encode the keypress (produce a standard code for the pressed key).

Logic „0‟ is read by the microprocessor when the key is pressed.

Key Debounce:
Whenever a mechanical push-bottom is pressed or released once, the mechanical
components of the key do not change the position smoothly; rather it generates a
transient response. These may be interpreted as the multiple pressures and responded
accordingly.
 The rows of the matrix are connected to four output Port lines, &columns are
connected to four input Port lines.

 When no keys are pressed, the column lines are held high by the pull-up resistors
connected to +5v.
 Pressing a key connects a row & a column.
 To detect if any key is pressed is to output 0‟s to all rows & then check columns
to see it a pressed key has connected a low (zero) to a column.
 Once the columns are found to be all high, the program enters another loop,
which waits until a low appears on one of the columns i.e indicating a key press.
 A simple 20/10 msec delay is executed to debounce task.
 After the debounce time, another check is made to see if the key is still pressed.
If the columns are now all high, then no key is pressed & the initial detection
was caused by a noise pulse.
 To avoid this problem, two schemes are suggested:
1. Use of Bistable multivibrator at the output of the key to debounce it.
2. The microprocessor has to wait for the transient period (at least for 10 ms),
so that the transient response settles down and reaches a steady state.
 If any of the columns are low now, then the assumption is made that it was a
valid key press.
 The final task is to determine the row & column of the pressed key &convert
this information to Hex-code for the pressed key.
 The 4-bit code from I/P port & the 4-bit code from O/P port (row &column) are
converted to Hex-code.

9. Briefly explain about the Stepper motor Interfacing to 8086 microprocessors.

Stepper Motor Interfacing:


 A stepper motor is a device used to obtain an accurate position control of
rotating shafts. It employs rotation of its shaft in terms of steps, rather than
continuous rotation as in case of AC or DC motors. To rotate the shaft of the
stepper motor, a sequence of pulses is needed to be applied to the windings of
the stepper motor, in a proper sequence.
 The number of pulses required for one complete rotation of the shaft of the
stepper motor is equal to its number of internal teeth on its rotor. The stator teeth
and the rotor teeth lock with each other to fix a position of the shaft.
 With a pulse applied to the winding input, the rotor rotates by one teeth position
or an angle x. The angle x may be calculated as:
X=3600/no. of rotor teeth
 After the rotation of the shaft through angel x, the rotor locks itself with the next
tooth in the sequence on the internal surface of stator.
 The internal schematic of a typical stepper motor with four windings is shown
in fig.1.
 The stepper motors have been designed to work with digital circuits. Binary
level pulses of 0-5V are required at its winding inputs to obtain the rotation of
shafts. The sequence of the pulses can be decided, depending upon the required
motion of the shaft.
Fig.2 shows a typical winding arrangement of the stepper motor.
Fig.3 shows conceptual positioning of the rotor teeth on the surface of rotor, for
a six teeth rotor.

 The circuit for interfacing a winding Wn with an I/O port is given in fig.4.
Each of the windings of a stepper motor needs this circuit for its interfacing
with the output port. A typical stepper motor may have parameters like torque
3 Kg-cm, operating voltage 12V, current rating 0.2 A and a step angle 1.80 i.e.
200 steps/revolution (number of rotor teeth).
 A simple schematic for rotating the shaft of a stepper motor is called a wave
scheme. In this scheme, the windings Wa, Wb, Wc and Wd are applied with
the required voltages pulses, in a cyclic fashion. By reversing the sequence of
excitation, the direction of rotation of the stepper motor shaft may be reversed.
 Table.1 shows the excitation sequences for clockwise and anticlockwise
rotations.

10. Briefly explain about the Direct memory access with DMA controller 8237.

Direct memory access with DMA controller 8237

 Suppose any device which is connected to input-output port wants to transfer data to
memory, first of all it will send input-output port address and control signal, input-
output read to input-output port, then it will send memory address and memory write
signal to memory where data has to be transferred. In normal input-output technique
the processor becomes busy in checking whether any input-output operation is
completed or not for next input output operation, therefore this technique is slow.

 This problem of slow data transfer between input output port and memory or between
two memory is avoided by implementing Direct Memory Access (DMA) technique.
This is faster as the microprocessor/computer is bypassed and the control of address
bus and data bus is given to the DMA controller.
 HOLD – hold signal

 HLDA – hold acknowledgment

 DREQ – DMA request

 DACK – DMA acknowledgment

Suppose a floppy drive that is connected at input-output port wants to transfer data to
memory, the following steps are performed:

 Step-1: First of all the floppy drive will send a DMA request (DREQ) to the
DMAC, it means the floppy drive wants its DMA service.

 Step-2: Now the DMAC will send a HOLD signal to the CPU.

 Step-3: After accepting the DMA service request from the DMAC, the CPU will
send hold acknowledgment (HLDA) to the DMAC, it means the microprocessor
has released control of the address bus the data bus to DMAC and the
microprocessor/computer is bypassed during DMA service.

 Step-4: Now the DMAC will send one acknowledgement (DACL) to the floppy
drive which is connected at the input-output port. It means the DMAC tells the
floppy drive be ready for its DMA service.

 Step-5: Now with the help of input-output read and memory write signal the data
is transferred from the floppy drive to the memory.
Modes of DMAC:
1. Single Mode – In this only one channel is used, means only a single DMAC is
connected to the bus system.

2. Cascade Mode – In this multiple channels are used, we can further cascade more
number of DMACs.

Advantages:
Improved performance: DMA improves system performance by freeing up the CPU to
perform other tasks while data is being transferred between memory and I/O devices. This
allows for faster and more efficient data transfer.
Reduced CPU overhead: With DMA, the CPU is not required to be involved in data
transfer, which reduces the CPU overhead and allows it to focus on other tasks. This is
particularly useful in real-time systems where low latency and fast response times are
important.
Support for high-bandwidth devices: DMA can support high-bandwidth devices such as
graphics cards and network interfaces that require fast data transfer rates.
Efficient use of system resources: DMA allows multiple devices to access memory
simultaneously, which makes more efficient use of system resources.

Disadvantages:
Complexity: DMA requires specialized hardware and software to function, which can add to
the complexity of a system. This can make it difficult to implement and troubleshoot.
Security risks: DMA can be a security risk if not properly configured or secured. Hackers
can exploit vulnerabilities in DMA to gain unauthorized access to a computer system or steal
data.
Limited control: Since the CPU is not involved in data transfer with DMA, it has limited
control over the transfer process. This can lead to data corruption or errors if the transfer
process is not properly managed.
Resource conflicts: DMA can lead to resource conflicts if multiple devices attempt to access
memory simultaneously. This can cause system instability and performance issues if not
properly managed.
8257 DMA Controller:
Features of 8257:
1. It is a 4-channel Direct Memory Access (DMAC) interface IC which allows data transfer
between memory and up to 4 I/O devices, bypassing CPU.
2. A maximum of 16 KB of data (= 214) can be transferred by this IC sequentially at a time.
When a DMA request comes from a peripheral, the DMAC 8257, via its HRQ (Hold Request)
pin, requests the CPU on its HOLD pin. CPU then acknowledges this request via its HLDA pin
which goes to HLDA pin of 8257. After this, DMAC generates the required MEMR, MEMW,
I/OR, I/OW signals.
3. Initialization of the DMAC is done under program control for each channel. The parameters
which need to be initialized for each channel are starting address, number of bytes of data to
be transferred, mode of operation, etc.
4. DMAC can be operated in three modes: (a) DMA Read (reading from memory, writing into
peripheral), (b) DMA Write (writing into memory, reading from peripheral), (c) DMA verify.
5. Priority for each of the 4 channels can be set in (a) fixed priority, (b) rotating priority.
6. A Terminal Count Register exists for each of 4 channels. The number of bytes of data to be
transferred is stored in the D13–D0 positions of the 16-bit Terminal Count Register. On
completion of data transfer, the Terminal Count (TC) pin goes high.
7. When the CPU is in control of its buses (address bus, data bus and control bus), it acts as
master and DMA controller acts as the slave. When DMA controller takes control of the buses,
it becomes the master and CPU becomes the slave.
Description of pins:
D0-D7:It is a bidirectional ,tri state ,Buffered ,Multiplexed data (D0-D7)and (A8-A15). In the
slave mode it is a bidirectional (Data is moving). In the Master mode it is a unidirectional
(Address is moving).
IOR: It is active low, tristate, buffered, Bidirectional lines. In the slave mode it functions as
an input line. IOR signal is generated by microprocessor to read the contents of 8257 registers.
In the master mode it functions as an output line. IOR signal is generated by 8257 during write
cycle.
IOW: It is active low, tristate, buffered, Bidirectional lines. In the slave mode it functions as
an input line. IOW signal is generated by microprocessor to write the contents of 8257 registers.
In the master mode it functions as an output line. IOR signal is generated by 8257 during read
cycle.
CLK: It is the input line, connected with TTL clock generator. This signal is ignored in slave
mode.
RESET: It is used to clear mode set registers and status registers.
A0-A3: These are the tristate, buffer, bidirectional address lines. In slave mode, these lines are
used as address inputs lines and internally decoded to access the internal registers. In master
mode, these lines are used as address outputs lines, A0-A3 bits of memory address on the lines.
CS: It is active low, Chip select input line. In the slave mode, it is used to select the chip. In
the master mode, it is ignored.
A4-A7: These are the tristate, buffer, output address lines. In slave mode, these lines are used
as address outputs lines. In master mode, these lines are used as address outputs lines, A0-A3
bits of memory address on the lines.
READY: It is a asynchronous input line. In master mode, When ready is high it is received the
signal. When ready is low, it adds wait state between S1 and S3. In slave mode,this signal is
ignored.
HRQ: It is used to receiving the hold request signal from the output device. HLDA: It is
acknowledgment signal from microprocessor.
MEMR: It is active low, tristate, Buffered control output line. In slave mode, it is tristated. In
master mode, it activated during DMA read cycle.
MEMW: It is active low, tristate, Buffered control input line. In slave mode, it is tristated. In
master mode, it activated during DMA write cycle.
AEN (Address enable): It is a control output line. In master mode, it is high. In slave mode, it
is low. Used it isolate the system address, data and control lines.
ADSTB: (Address Strobe) It is a control output line. Used to split data and address line. It is
working in master mode only. In slave mode it is ignore.
TC (Terminal Count): It is a status of output line. It is activated in master mode only. It is
high, it selected the peripheral. It is low, it free and looking for a new peripheral.
MARK: It is a modulo 128 MARK output line. It is activated in master mode only. It goes
high, after transferring every 128 bytes of data block.
DRQ0-DRQ3(DMA Request): These are the asynchronous peripheral request input signal.
The request signals are generated by external peripheral device.
DACK0-DACK3: These are the active low DMA acknowledge output lines. Low level
indicates s that, peripheral is selected for giving the information (DMA cycle). In master mode
it is used for chip select.

BLOCK DIAGRAM OF 8257

It contains five main Blocks:


1. Data bus buffer 2. Read/Control logic 3. Control logic block 4. Priority resolver 5. DMA
channels.
DATA BUS BUFFER: It contains tristate, 8 bit bi-directional buffer. Slave mode, it
transfer data between microprocessor and internal data bus. Master mode, the outputs A8-
A15 bits of memory address on data lines (Unidirectional).
READ/CONTROL LOGIC: It controls all internal Read/Write operation. Slave mode, it
accepts address bits and control signal from microprocessor. Master mode, it generate
address bits and control signal. Control logic block: It contains, Control logic, Mode set
register and Status Register.
CONTROL LOGIC: Master mode, It control the sequence of DMA operation during all
DMA cycles. It generates address and control signals. It increments 16 bit address and
decrement 14 bit counter registers. It activates a HRQ signal on DMA channel Request.
Slave, mode it is disabled.
Register organization of 8257: The 8257 performs the DMA operation over four
independent DMA channels. Each DMA channel has a pair of two 16-bit registers i.e.
address register and terminal count register. Also there are two common registers for all
the channels are Mode set register and Status register. The CPU selects one of these
registers using address lines A0 to A3.
DMA address register: The function of this register is to store the address of the starting
memory location, which will be accessed by the DMA channel.
Terminal Count register: The function of this register is to indicate the end of the data
transfer after required number of DMA cycles. The low order 14-bits of the terminal count
register are initialized with the number of DMA cycles minus one. After each DMA cycle
this register content can be decremented by 1and becomes zero after all DMA cycles are
over. The bits 14 and 15 indicate the type of the DMA operation.
The bits B0 – B3 enable one of the four DMA channels of 8257. The extended write bit
extends the duration of MEMW and IOW signals. B4 provides rotating and normal
priority. TC stop bit is set, the selected channel is disabled, so that it can be prevented for
further DMA cycle on the channel. If auto load bit is set enables the same channel for the
repeat block chain operations without software intervention between two successive
blocks.
Status Register: The lower order 4-bits are used to indicate the terminal count condition
for the four individual channels. These bits remain set till either the status is read by CPU
or reset by 8257. The update flag can only be cleared by resetting 8257 or by resetting the
auto load bit of the mode set register. If update flag register is set, the contents of channel
3 registers are reloaded to the corresponding registers of channel 2.

11. Briefly explain about the Intel 8251 USART Architecture and Interfacing.

Microprocessor | 8251 USART

8251 universal synchronous asynchronous receiver transmitter (USART) acts as a mediator


between microprocessor and peripheral to transmit serial data into parallel form and vice
versa.
1. It takes data serially from peripheral (outside devices) and converts into parallel
data.
2. After converting the data into parallel form, it transmits it to the CPU.
3. Similarly, it receives parallel data from microprocessor and converts it into serial
form.
4. After converting data into serial form, it transmits it to outside device (peripheral).
Block Diagram of 8251 USART –

It contains the following blocks:


1. Data bus buffer – This block helps in interfacing the internal data bus of 8251 to
the system data bus. The data transmission is possible between 8251 and CPU by
the data bus buffer block.
2. Read/Write control logic – It is a control block for overall device. It controls the
overall working by selecting the operation to be done. The operation selection
depends upon input signals as:
In this way, this unit selects one of the three registers- data buffer register, control
register, status register.
3. Modem control (modulator/demodulator) – A device converts analog signals to
digital signals and vice-versa and helps the computers to communicate over
telephone lines or cable wires. The following are active-low pins of Modem.
 DSR: Data Set Ready signal is an input signal.
 DTR: Data terminal Ready is an output signal.
 CTS: It is an input signal which controls the data transmit
circuit. RTS: It is an output signal which is used to set the status RTS.
4. Transmit buffer – This block is used for parallel to serial converter that receives
a parallel byte for conversion into serial signal and further transmission onto the
common channel.
 TXD: It is an output signal, if its value is one, means transmitter will
transmit the data.
5. Transmit control – This block is used to control the data transmission with the
help of following pins:
 TXRDY: It means transmitter is ready to transmit data character.
 TXEMPTY: An output signal which indicates that TXEMPTY pin has
transmitted all the data characters and transmitter is empty now.
 TXC: An active-low input pin which controls the data transmission
rate of transmitted data.
6. Receive buffer – This block acts as a buffer for the received data.
 RXD: An input signal which receives the data.
7. Receive control – This block controls the receiving data.
 RXRDY: An input signal indicates that it is ready to receive the data.
 RXC: An active-low input signal which controls the data transmission
rate of received data.
 SYNDET/BD: An input or output terminal. External synchronous
mode-input terminal and asynchronous mode-output terminal.
Advantages:

Versatility: The 8251 USART can be used for both synchronous and asynchronous
communication, making it a versatile peripheral.
Error detection: The USART includes built-in error detection features, such as parity
checking, which help to ensure the accuracy of transmitted data.
Flow control: The USART includes flow control features, which allow for the regulation of
data transmission and reception, preventing data loss and overloading.
Compatibility: The 8251 USART is compatible with a wide range of microprocessors,
making it a popular choice for serial communication in many different systems.
Ease of use: The USART includes simple interface pins and registers, making it relatively
easy to use and program.

Disadvantages:

Limited speed: The 8251 USART has a relatively low maximum data transfer rate of 115.2
kbps, which may be insufficient for some applications.
Limited buffer size: The USART has a small internal buffer size, which may result in data
loss if data is not read from the buffer in a timely manner.
Complex programming: Although the interface pins and registers of the USART are
relatively simple, programming the USART can be complex, requiring careful attention to
timing and other parameters.
Cost: While the 8251 USART is relatively affordable, it does add cost to a system,
particularly if multiple USARTs are required.
Limited functionality: While the 8251 USART is a useful peripheral for serial
communication, it does not include more advanced features, such as DMA (direct memory
access) or advanced error correction.
Course name: Fundamentals of MPMC
Unit-4 Notes
8051 Microcontroller

1. What are the Key features of the 8051 Microcontroller?


The Key features of the 8051 Microcontroller
 4 KB on-chip ROM (Program memory).
 128 bytes on-chip RAM (Data memory).
 The 8-bit data bus (bidirectional).
 16-bit address bus (unidirectional).
 Two 16-bit timers.
 Instruction cycle of 1 microsecond with 12 MHz crystal.
 Four 8-bit input/output ports.
 128 user-defined flags.
 Four register banks of 8 bit each.
 16-byte bit-addressable RAM.
 The general-purpose registers are 32 each is 8-bit.
 8051 has two external and three internal interrupts.
 8051 microcontroller specifies some special function features like UARTs, ADC, Op-
amp, etc.
 It has a 16-bit program counter and data pointer.

2. Distinguish Microprocessors with Microcontrollers.

Difference between Microprocessor and Microcontroller

Microprocessor Microcontroller

Since memory and I/O are connected Since memory and I/O are present
externally, the circuit becomes large in together, the internal circuit is small in
size. size.

It cannot be used in compact systems It can be used in compact systems.

Cost is high Cost is low


It is not suitable for devices that run on
It can be used on devices that use stored
stored power since total power
power since total power consumption is
consumption is high due to external
low due to less external components.
components.

RAM, ROM, I/O units, and other


RAM, ROM, CPU and other peripherals
peripherals are not embedded on a single
are embedded on a single chip.
chip.

Do not have power saving mode. Have power-saving mode.

Used in personal computers. Used in embedded systems.

Less number of registers. More number of registers.

Uses an external bus. Uses an internal controlling bus.

Based on the Von Neumann model Based on the Harvard architecture

It is a byproduct of the development of


It is a central processing unit on a single
microprocessors with a CPU along with
silicon-based integrated chip.
other peripherals.

Complex and expensive due to large Simple and inexpensive due to less
number of instructions to process. number of instructions to process.

Can run at a very high speed. Can run up to 200MHz or more.

3. Draw and explain the Pin Configuration of 8051 Microcontroller.

The pin diagram of 8051 microcontroller looks as follows −


 Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other
functions. It is internally pulled up, bi-directional I/O port.
 Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial
values.
 Pins 10 to 17 − These pins are known as Port 3. This port serves some functions like
interrupts, timer input, control signals, serial communication signals RxD and TxD,
etc.
 Pins 18 & 19 − These pins are used for interfacing an external crystal to get the
system clock.
 Pin 20 − This pin provides the power supply to the circuit.
 Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher order
address bus signals are also multiplexed using this port.
 Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to read a
signal from the external program memory.
 Pin 30 − This is EA pin which stands for External Access input. It is used to
enable/disable the external memory interfacing.
 Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to
demultiplex the address-data signal of port.
 Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower order
address and data bus signals are multiplexed using this port.
 Pin 40 − This pin is used to provide power supply to the circuit.
4. Draw and explain the Hardware Architecture of 8051 Microcontroller.

Fig: INTEL 8051 Microcontroller Architecture

Central Processor Unit (CPU)

As we know that the CPU is the brain of any processing device of the microcontroller. It
monitors and controls all operations that are performed on the Microcontroller units. The User
has no control over the work of the CPU directly. It reads program written in ROM memory
and executes them and do the expected task of that application.

Interrupts

As its name suggests, Interrupt is a subroutine call that interrupts of the microcontrollers main
operations or work and causes it to execute any other program, which is more important at the
time of operation. The feature of Interrupt is very useful as it helps in case of emergency
operations. An Interrupts gives us a mechanism to put on hold the ongoing operations, execute
a subroutine and then again resumes to another type of operations.

The Microcontroller 8051 can be configured in such a way that it temporarily terminates or
pause the main program at the occurrence of interrupts. When a subroutine is completed, Then
the execution of main program starts. Generally, five interrupt sources are there in 8051
Microcontroller. There are 5 vectored interrupts are shown in below

 INTO
 TFO
 INT1
 TF1
 R1/T1

Out of these, (INT0) ̅ and (INT1) ̅ are external interrupts that could be negative edge triggered
or low level triggered. When All these interrupts are activated, set the corresponding flogs
except for serial interrupt,. The interrupt flags are cleared when the processor branches to the
interrupt service routine (ISR). The external interrupt flags are cleared when the processor
branches to the interrupt service routine, provides the interrupt is a negative edge triggered
whereas the timers and serial port interrupts two of them are external interrupts, two of them
are timer interrupts and one serial port interrupt terminal in general.

Memory

Microcontroller requires a program which is a collection of instructions. This program tells


microcontroller to do specific tasks. These programs require a memory on which these can be
saved and read by Microcontroller to perform specific operations of a particular task. The
memory which is used to store the program of the microcontroller is known as code memory
or Program memory of applications. It is known as ROM memory of microcontroller also
requires a memory to store data or operands temporarily of the micro controller. The data
memory of the 8051 is used to store data temporarily for operation is known RAM memory.
8051 microcontroller has 4K of code memory or program memory,that has 4KB ROM and
also 128 bytes of data memory of RAM.

BUS

Basically Bus is a collection of wires which work as a communication channel or medium for
transfer of Data. These buses consists of 8, 16 or more wires of the microcontroller. Thus, these
can carry 8 bits,16 bits simultaneously. Hire two types of buses that are shown in below

 Address Bus
 Data Bus

Address Bus: Microcontroller 8051 has a 16 bit address bus for transferring the data. It is used
to address memory locations and to transfer the address from CPU to Memory of the
microcontroller. It has four addressing modes that are

 Immediate addressing modes.


 Bank address (or) Register addressing mode.
 Direct Addressing mode.
 Register indirect addressing mode.

Data Bus: Microcontroller 8051 has 8 bits of the data bus, which is used to carry data of
particular applications.
Oscillator

Generally, we know that the microcontroller is a device, therefore it requires clock pulses for
its operation of microcontroller applications. For this purpose, microcontroller 8051 has an on-
chip oscillator which works as a clock source for Central Processing Unit of the
microcontroller. The output pulses of oscillator are stable. Therefore, it enables synchronized
work of all parts of the 8051 Microcontroller.

Input/Output Port

Normally microcontroller is used in embedded systems to control the operation of machines in


the microcontroller. Therefore, to connect it to other machines, devices or peripherals we
require I/O interfacing ports in the microcontroller interface. For this purpose, microcontroller
8051 has 4 input, output ports to connect it to the other peripherals

Timers/Counters

8051 microcontrollers have two 16-bit timers and counters. These counters are again divided
into a 8 bit register. The timers are used for measurement of intervals to determine the pulse
width of pulses.

5. Briefly explain the Instruction SET of 8051 Microcontroller.

Based on the operation they perform, all the instructions in the 8051 Microcontroller

Instruction Set are divided into five groups. They are:

 Data Transfer Instructions

 Arithmetic Instructions

 Logical Instructions

 Boolean or Bit Manipulation Instructions

 Program Branching Instructions

We will now see about these instructions briefly.

Data Transfer Instructions


The Data Transfer Instructions are associated with transfer of data between registers or external

program memory or external data memory. The Mnemonics associated with Data Transfer are

given below.

Mnemonic Description
MOV Move Data

MOVC Move Code

MOVX Move External Data

PUSH Move Data to Stack

POP Copy Data from Stack

XCH Exchange Data between two Registers

XCHD Exchange Lower Order Data between two Registers

The following table lists out all the possible data transfer instructions along with other details

like addressing mode, size occupied and number machine cycles it takes.

Arithmetic Instructions
Using Arithmetic Instructions, you can perform addition, subtraction, multiplication and

division. The arithmetic instructions also include increment by one, decrement by one and a

special instruction called Decimal Adjust Accumulator.

The Mnemonics associated with the Arithmetic Instructions of the 8051 Microcontroller

Instruction Set are:

Mnemonic Description

ADD Addition without Carry

ADDC Addition with Carry

SUBB Subtract with Carry


INC Increment by 1

DEC Decrement by 1

MUL Multiply

DIV Divide

DA A Decimal Adjust the Accumulator (A Register)

The arithmetic instructions have no knowledge about the data format i.e., signed, unsigned,

ASCII, BCD, etc. Also, the operations performed by the arithmetic instructions affect flags like
carry, overflow, zero, etc. in the PSW Register.

All the possible Mnemonics associated with Arithmetic Instructions are mentioned in the

following table.

Logical Instructions
The next group of instructions are the Logical Instructions, which perform logical operations

like AND, OR, XOR, NOT, Rotate, Clear and Swap. Logical Instruction are performed on

Bytes of data on a bit-by-bit basis.

Mnemonics associated with Logical Instructions are as follows:

Mnemonic Description

ANL Logical AND

ORL Logical OR

XRL Ex-OR

CLR Clear Register


CPL Complement the Register

RL Rotate a Byte to Left

RLC Rotate a Byte and Carry Bit to Left

RR Rotate a Byte to Right

RRC Rotate a Byte and Carry Bit to Right

SWAP Exchange lower and higher nibbles in a Byte

The following table shows all the possible Mnemonics of the Logical Instructions.

Boolean or Bit Manipulation Instructions


As the name suggests, Boolean or Bit Manipulation Instructions deal with bit variables. We

know that there is a special bit-addressable area in the RAM and some of the Special Function

Registers (SFRs) are also bit addressable.

The Mnemonics corresponding to the Boolean or Bit Manipulation instructions are:

Mnemonic Description

CLR Clear a Bit (Reset to 0)

SETB Set a Bit (Set to 1)

MOV Move a Bit

JC Jump if Carry Flag is Set


JNC Jump if Carry Flag is Not Set

JB Jump if specified Bit is Set

JNB Jump if specified Bit is Not Set

JBC Jump if specified Bit is Set and also clear the Bit

ANL Bitwise AND

ORL Bitwise OR

CPL Complement the Bit

These instructions can perform set, clear, and, or, complement etc. at bit level.

Program Branching Instructions

The last group of instructions in the 8051 Microcontroller Instruction Set are the Program

Branching Instructions. These instructions control the flow of program logic. The mnemonics

of the Program Branching Instructions are as follows.

Mnemonic Description

LJMP Long Jump (Unconditional)

AJMP Absolute Jump (Unconditional)

SJMP Short Jump (Unconditional)


JZ Jump if A is equal to 0

JNZ Jump if A is not equal to 0

CJNE Compare and Jump if Not Equal

DJNZ Decrement and Jump if Not Zero

NOP No Operation

LCALL Long Call to Subroutine

ACALL Absolute Call to Subroutine (Unconditional)

RET Return from Subroutine

RETI Return from Interrupt

JMP Jump to an Address (Unconditional)

All these instructions, except the NOP (No Operation) affect the Program Counter (PC) in one

way or other. Some of these instructions has decision making capability before transferring

control to other part of the program.

6. Briefly explain the Addressing Modes of 8051 Microcontroller.


An Addressing Mode is a way to locate a target Data, which is also called as Operand. The

8051 Family of Microcontrollers allows five types of Addressing Modes for addressing the

Operands. They are:


 Immediate Addressing

 Register Addressing

 Direct Addressing

 Register – Indirect Addressing

 Indexed Addressing

Immediate Addressing
In Immediate Addressing mode, the operand, which follows the Opcode, is a constant data of

either 8 or 16 bits. The name Immediate Addressing came from the fact that the constant data

to be stored in the memory immediately follows the Opcode.

The constant value to be stored is specified in the instruction itself rather than taking from a

register. The destination register to which the constant data must be copied should be the same

size as the operand mentioned in the instruction.

Example: MOV A, #030H

Here, the Accumulator is loaded with 30 (hexadecimal). The # in the operand indicates that it

is a data and not the address of a Register.

Immediate Addressing is very fast as the data to be loaded is given in the instruction itself.

Register Addressing
In the 8051 Microcontroller Memory Organization Tutorial, we have seen the organization of

RAM and four banks of Working Registers with eight Registers in each bank.

In Register Addressing mode, one of the eight registers (R0 – R7) is specified as Operand in

the Instruction.

It is important to select the appropriate Bank with the help of PSW Register. Let us see a

example of Register Addressing assuming that Bank0 is selected.

Example: MOV A, R5

Here, the 8-bit content of the Register R5 of Bank0 is moved to the Accumulator.
Direct Addressing

In Direct Addressing Mode, the address of the data is specified as the Operand in the

instruction. Using Direct Addressing Mode, we can access any register or on-chip variable.

This includes general purpose RAM, SFRs, I/O Ports, Control registers.

Example: MOV A, 47H

Here, the data in the RAM location 47H is moved to the Accumulator.

Register Indirect Addressing


In the Indirect Addressing Mode or Register Indirect Addressing Mode, the address of the

Operand is specified as the content of a Register. This will be clearer with an example.

Example: MOV A, @R1

The @ symbol indicates that the addressing mode is indirect. If the contents of R1 is 56H, for

example, then the operand is in the internal RAM location 56H. If the contents of the RAM

location 56H is 24H, then 24H is moved into accumulator.

Only R0 and R1 are allowed in Indirect Addressing Mode. These register in the indirect

addressing mode are called as Pointer registers.

Indexed Addressing Mode


With Indexed Addressing Mode, the effective address of the Operand is the sum of a base

register and an offset register. The Base Register can be either Data Pointer (DPTR) or Program

Counter (PC) while the Offset register is the Accumulator (A).

In Indexed Addressing Mode, only MOVC and JMP instructions can be used. Indexed
Addressing Mode is useful when retrieving data from look-up tables.

Example: MOVC A, @A+DPTR

Here, the address for the operand is the sum of contents of DPTR and Accumulator.

MNEMONIC DESTINATION OPERAND, SOURCE OPERAND


7. Write about the Memory Organization of 8051 Microcontroller.

8051 Memory Organization

The 8051 microcontroller's memory is divided into Program Memory and Data Memory.
Program Memory (ROM) is used for permanent saving program being executed, while Data
Memory (RAM) is used for temporarily storing and keeping intermediate results and variables.

Program Memory (ROM)


Program Memory (ROM) is used for permanent saving program (CODE) being executed. The
memory is read only. Depending on the settings made in compiler, program memory may also
used to store a constant variables. The 8051 executes programs stored in program memory
only. code memory type specifier is used to refer to program memory.
8051 memory organization alows external program memory to be added.
How does the microcontroller handle external memory depends on the pin EA logical state.

Internal Data Memory


Up to 256 bytes of internal data memory are available depending on the 8051 derivative.
Locations available to the user occupy addressing space from 0 to 7Fh, i.e. first 128 registers
and this part of RAM is divided in several blocks. The first 128 bytes of internal data memory
are both directly and indirectly addressable. The upper 128 bytes of data memory (from 0x80
to 0xFF) can be addressed only indirectly.
Since internal data memory is used for CALL stack also and there is only 256 bytes splinted
over few different memories areas fine utilizing of this memory is crucial for fast and
compact code. See types efficiency also.

Memory block in the range of 20h to 2Fh is bit-addressable, which means that each bit being
there has its own address from 0 to 7Fh. Since there are 16 such registers, this block contains
in total of 128 bits with separate addresses (Bit 0 of byte 20h has the bit address 0, and bit 7
of byte 2Fh has the bit address 7Fh).

Three memory type specifiers can be used to refer to the internal data memory: data, idata,
and bdata.

External Data Memory


Access to external memory is slower than access to internal data memory. There may be up to
64K Bytes of external data memory. Several 8051 devices provide on-chip XRAM space that
is accessed with the same instructions as the traditional external data space. This XRAM
space is typically enabled via proper setting of SFR register and overlaps the external
memory space. Setting of that register must be manually done in code, before any access to
external memory or XRAM space is made.
The mikroC PRO for 8051 has two memory type specifiers that refers to external memory
space: xdata and pdata.

SFR Memory
The 8051 provides 128 bytes of memory for Special Function Registers (SFRs). SFRs are bit,
byte, or word-sized registers that are used to control timers, counters, serial I/O, port I/O, and
peripherals.
Course name: Fundamentals of MPMC
Unit-5 Notes
Microcontroller Programming and Applications
1. What are the Key features of the 8051 Microcontroller?
The Key features of the 8051 Microcontroller
 4 KB on-chip ROM (Program memory).
 128 bytes on-chip RAM (Data memory).
 The 8-bit data bus (bidirectional).
 16-bit address bus (unidirectional).
 Two 16-bit timers.
 Instruction cycle of 1 microsecond with 12 MHz crystal.
 Four 8-bit input/output ports.
 128 user-defined flags.
 Four register banks of 8 bit each.
 16-byte bit-addressable RAM.
 The general-purpose registers are 32 each is 8-bit.
 8051 has two external and three internal interrupts.
 8051 microcontroller specifies some special function features like UARTs, ADC, Op-
amp, etc.
 It has a 16-bit program counter and data pointer.

2. What are the major Applications of the 8051 Microcontroller?

Applications of 8051 Microcontroller

The 8051 microcontroller is widely used in various applications due to its versatility,
flexibility, and easy-to-use architecture. Here are some of the applications of the 8051
microcontrollers:

Home automation:
The 8051 microcontrollers can be used in home automation systems to control lighting,
temperature, and security systems.

Industrial control:
The 8051 microcontroller is used in industrial control applications to control processes,
machinery, and equipment. It can also be used to monitor and control temperature, pressure,
and other environmental factors.

Robotics:
The 8051 microcontroller is used in robotics applications to control the movement of robots,
sensors, and actuators.
Automotive:
The 8051 microcontroller is used in the automotive industry for engine control, anti-lock
braking systems, airbag control, and other applications.
Medical devices:
The 8051 microcontroller is used in medical devices for patient monitoring, drug delivery
systems, and other applications.

Communication systems:
The 8051 microcontroller is used in communication systems for data transmission, signal
processing, and other applications.

Consumer electronics:
The 8051 microcontroller is used in consumer electronics such as remote controls, digital
cameras, and smart home devices.

In summary, the 8051 microcontroller is a versatile and widely used microcontroller that can
be applied in various fields, including home automation, industrial control, robotics,
automotive, medical devices, communication systems, and consumer electronics. Its ease of
use and flexibility make it a popular choice for embedded systems designers and engineers.

3. Write the steps that 8051 goes through in transmitting a character via TxD.
1. The byte character to be transmitted is written into the SBUF register
2. The start bit is transferred
3. The 8-bit character is transferred on bit at a time
4. The stop bit is transferred
 It is during the transfer of the stop bit that 8051 raises the TI flag, indicating that the
last character was transmitted
5. By monitoring the TI flag, we make sure that we are not overloading the SBUF
 If we write another byte into the SBUF before TI is raised, the un transmitted portion
of the previous byte will be lost
6. After SBUF is loaded with a new byte, the TI flag bit must be forced to 0 by CLR TI in
order for this new byte to be transferred
 By checking the TI flag bit, we know whether or not the 8051 is ready to transfer
another byte
 It must be noted that TI flag bit is raised by 8051 itself when it finishes data transfer
 It must be cleared by the programmer with instruction CLR TI
 If we write a byte into SBUF before the TI flag bit is raised, we risk the loss of a
portion of the byte being transferred
 The TI bit can be checked by
 The instruction JNB TI, xx Using an interrupt
4. write about the steps in programming the 8051 to receive character bytes serially.

1. TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode
2. (8-bit auto-reload) to set baud rate
3. TH1 is loaded to set baud rate
4. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8-
bit data is framed with start and stop bits
5. TR1 is set to 1 to start timer 1
6. RI is cleared by CLR RI instruction
7. The RI flag bit is monitored with the use of instruction JNB RI, xx to see if an entire
character has been received yet
8. When RI is raised, SBUF has the byte, its contents are moved into a safe place.
9. To receive the next character, go to step 5.

5. Write a Program for KEYPAD INTERFACING with 8051 Microcontroller

KEYPAD INTERFACING WITH 8051 Microcontroller

4X4 Matrix Keypad


Before we interface the keypad with microcontroller, first we need to understand how it works.
Matrix keypad consists of set of Push buttons, which are interconnected. Like in our case we
are using 4X4 matrix keypad, in which there are 4 push buttons in each of four rows. And the
terminals of the push buttons are connected according to diagram. In first row, one terminal of
all the 4 push buttons is connected together and another terminal of 4 push buttons are
representing each of 4 columns, same goes for each row. So, we are getting 8 terminals to
connect with a microcontroller.
As shown in above circuit diagram, to interface Keypad, we need to connect 8 terminals of the
keypad to any port (8 pins) of the microcontroller. Like we have connected keypad terminals
to Port 1 of 8051. Whenever any button is pressed, we need to get the location of the button,
means the corresponding ROW and COLUMN no. Once we get the location of the button, we
can print the character accordingly.

Now the question is how to get the location of the pressed button?

1. First we have made all the Rows to Logic level 0 and all the columns to Logic level 1.

2. Whenever we press a button, column and row corresponding to that button gets shorted and
makes the corresponding column to logic level 0. Because that column becomes connected
(shorted) to the row, which is at Logic level 0. So, we get the column no. See main () function.
3. Now we need to find the Row no., so we have created four functions corresponding to each
column. Like if any button of column one is pressed, we call function row_finder1(), to find
the row no.

4. In row_finder1() function, we reversed the logic levels, means now all the Rows are 1 and
columns are 0. Now Row of the pressed button should be 0 because it has become connected
(shorted) to the column whose button is pressed, and all the columns are at 0 logic. So we have
scanned all rows for 0.

5. So whenever we find the Row at logic 0, means that is the row of pressed button. So now
we have column no (got in step 2) and row no., and we can print no. of that button using LCD
data function.

Same procedure follows for every button press, and we are using while (1), to continuously
check, whether button is pressed or not.

Program Code for KEYPAD INTERFACING with 8051 Microcontroller:

#include<reg51.h>

#define display_port P2 //Data pins connected to port 2 on microcontroller

sbit rs = P3^2; //RS pin connected to pin 2 of port 3

sbit rw = P3^3; // RW pin connected to pin 3 of port 3

sbit e = P3^4; //E pin connected to pin 4 of port 3

sbit C4 = P1^0; // Connecting keypad to Port 1

sbit C3 = P1^1;

sbit C2 = P1^2;

sbit C1 = P1^3;

sbit R4 = P1^4;

sbit R3 = P1^5;
sbit R2 = P1^6;

sbit R1 = P1^7;

void msdelay(unsigned int time) // Function for creating delay in milliseconds.

unsigned i,j ;

for(i=0;i<time;i++)

for(j=0;j<1275;j++);

void lcd_cmd(unsigned char command) //Function to send command instruction to LCD

display_port = command;

rs= 0;

rw=0;

e=1;

msdelay(1);

e=0;

void lcd_data(unsigned char disp_data) //Function to send display data to LCD

display_port = disp_data;

rs= 1;

rw=0;

e=1;

msdelay(1);

e=0;

void lcd_init() //Function to prepare the LCD and get it ready

lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD


msdelay(10);

lcd_cmd(0x0F); // turn display ON, cursor blinking

msdelay(10);

lcd_cmd(0x01); //clear screen

msdelay(10);

lcd_cmd(0x81); // bring cursor to position 1 of line 1

msdelay(10);

void row_finder1() //Function for finding the row for column 1

R1=R2=R3=R4=1;

C1=C2=C3=C4=0;

if(R1==0)

lcd_data('1');

if(R2==0)

lcd_data('4');

if(R3==0)

lcd_data('7');

if(R4==0)

lcd_data('*');

void row_finder2() //Function for finding the row for column 2

R1=R2=R3=R4=1;

C1=C2=C3=C4=0;

if(R1==0)

lcd_data('2');

if(R2==0)

lcd_data('5');
if(R3==0)

lcd_data('8');

if(R4==0)

lcd_data('0');

void row_finder3() //Function for finding the row for column 3

R1=R2=R3=R4=1;

C1=C2=C3=C4=0;

if(R1==0)

lcd_data('3');

if(R2==0)

lcd_data('6');

if(R3==0)

lcd_data('9');

if(R4==0)

lcd_data('#');

void row_finder4() //Function for finding the row for column 4

R1=R2=R3=R4=1;

C1=C2=C3=C4=0;

if(R1==0)

lcd_data('A');

if(R2==0)

lcd_data('B');

if(R3==0)

lcd_data('C');

if(R4==0)
lcd_data('D');

void main()

lcd_init();

while(1)

msdelay(30);

C1=C2=C3=C4=1;

R1=R2=R3=R4=0;

if(C1==0)

row_finder1();

else if(C2==0)

row_finder2();

else if(C3==0)

row_finder3();

else if(C4==0)

row_finder4();

6. Write a Program for DISPLAY INTERFACING with 8051 Microcontroller.

Interfacing 16×2 LCD with 8051

In this project, we will have brief discussion on how to interface 16×2 LCD module to

AT89C51, which is an 8051-family microcontroller. We use LCD display for the displaying

messages in a more interactive way to operate the system or displaying error messages etc.
Interfacing 16×2 LCD with 8051 microcontroller is very easy if you understanding the working
of LCD.
A Brief Note on 16×2 LCD

16×2 Liquid Crystal Display which will display the 32 characters at a time in two rows (16

characters in one row). Each character in the display is of size 5×7-pixel matrix. This matrix

differs for different 16×2 LCD modules, if you take JHD162A, this matrix goes to 5×8. There

are 16 pins in the LCD module, the pin configuration us given below

So, by reading the above table you can get a brief idea how to display a character. For

displaying a character, you should enable the enable pin (pin 6) by giving a pulse of 450ns,

after enabling the pin6 you should select the register select pin (pin4) in write mode. To select

the register, select pin in write mode you have to make this pin high (RS=1), after selecting the

register select you have to configure the R/W to write mode that is R/W should be low

(R/W=0).

Follow these simple steps for displaying a character or data

 E=1; enable pin should be high

 RS=1; Register select should be high

 R/W=0; Read/Write pin should be low.

To send a command to the LCD just follows these steps:E=1; enable pin should be high

 RS=0; Register select should be low

 R/W=0; Read/Write pin should be low.


Interfacing 16×2 LCD with 8051 Circuit Diagram

Components Required

 AT89C51 (8051 Microcontroller)

 16X2 LCD Display

 11.0592MHz Crystal

 2 X 33pF Capacitors

 2 X 10 KΩ Resistors

 1 KΩ X 8 Resistor Pack

 10 KΩ Potentiometer

 330Ω Resistor

 Push Button
 10μF/16V Capacitor

 8051 Programmer
 5V Power Supply

 Connecting Wires

Programming LCD to 8051

Coming to the programming you should follow these steps:

 STEP1: Initialization of LCD.

 STEP2: Sending commands to LCD.

 STEP3: Writing the data to LCD.

Initializing LCD To initialize LCD to the 8051 the following instruction and commands are

to be embed in to the functions0x38 is used for 8-bit data initialization.

 0xoC for making LCD display on and cursor off.

 0X01 for clearing the display of the LCD.

 0x80 for positioning the cursor at first line.

Sending Commands to the LCD

 E=1; enable pin should be high


 RS=0; Register select should be low for sending commands
 Placing the data on the data registers
 R/W=0; Read/Write pin should be low for writing the data.

Writing the Data to the LCD

 E=1; enable pin should be high


 RS=1; Register select should be high for writing data
 Placing the data on the data registers
 R/W=0; Read/Write pin should be low for writing the data.

PROGRAM CODE for Interfacing 16×2 LCD with 8051 Circuit Diagram
#include<reg51.h>
#define lcd P0

sbit rs=P2^0;
sbit e=P2^1;

void delay (int);


void cmd (char);
void display (char);
void custom (void);
void string (char *);
void init (void);

unsigned char custom_char[]= {0x00,0x04,0x0E,0x04,0x15,0x0E,0x04,0x00,


0x00,0x04,0x0E,0x0E,0x0E,0x1F,0x04,0x00,
0x00,0x0A,0x15,0x11,0x11,0x0A,0x04,0x00,
0x0E,0x11,0x11,0x1F,0x1B,0x1B,0x1B,0x1F,
0x0E,0x11,0x10,0x1F,0x1B,0x1B,0x1B,0x1F,
0x00,0x0E,0x15,0x1B,0x0E,0x0E,0x00,0x00,
};

void delay (int d)


{
unsigned char i;
for(;d>0;d--)
{
for(i=250;i>0;i--);
for(i=248;i>0;i--);
}
}
void cmd (char c)
{
lcd=c;
rs=0;
e=1;
delay(5);
e=0;
}
void display (char c)
{
lcd=c;
rs=1;
e=1;
delay(5);
e=0;
}
void custom (void)
{
int k;
cmd(0x40);
for(k=0;k<48;k++)
display(custom_char[k]);
cmd(0x80);
}

void string (char *p)


{
while(*p)
{
display(*p++);
}
}
void init (void)
{
cmd(0x38);
cmd(0x0c);
cmd(0x01);
cmd(0x80);
}
void main()
{
init();
custom();
string("Electronics Hub ");
cmd(0xc0);
string(" Presents ");
delay(2000);
cmd(0x01);
cmd(0x80);
string(" Custom Chars ");
cmd(0xc0);
string(" on 16x2 LCD ");
delay(2000);
cmd(0x01);
cmd(0x80);
string("ABCDEFGHIJKLMNOP");
cmd(0xc0);
string("1234567890");
cmd(0xca);
display(0);
display(1);
display(2);
display(3);
display(4);
display(5);
while(1);
}

7. Write a Program for STEPPER MOTOR INTERFACING with 8051 Microcontroller.

Stepper Motor Driver connection with 8051


 Here we are going to interface 6 wires Unipolar Stepper Motor with an 8051
controller.
 Only four wires are required to control the stepper motor.
 Two common wires of the stepper motor are connected to the 5V supply.
 ULN2003 driver is used to driving the stepper motor.
 Note that to know the winding coil and their centre tap leads measure resistance in
between leads. From centre leads, we will get half the resistance value of that
winding.
Example
Let’s program At89S52 to rotate the stepper motor 360° clockwise by half-step sequence and
360° anticlockwise by full-step sequence.

8051 STEPPER MOTOR PROGRAM CODE


#include <reg52.h>
#define Stepper_Port P2 /* Define Stepper Motor Port */
/* Function to provide delay of 1ms at 11.0592 MHz */
void delay(unsigned int count)
{
int i,j;
for(i=0; i<count; i++)
for(j=0; j<112; j++);
}
int main(void)
{
int i,period;
period = 100; /* Set period in between two steps of Stepper Motor
*/
while (1)
{
/* Rotate Stepper Motor clockwise with Half step sequence */
for(i=0; i<12; i++)
{
Stepper_Port = 0x09;
delay(period);
Stepper_Port = 0x08;
delay(period);
Stepper_Port = 0x0C;
delay(period);
Stepper_Port = 0x04;
delay(period);
Stepper_Port = 0x06;
delay(period);
Stepper_Port = 0x02;
delay(period);
Stepper_Port = 0x03;
delay(period);
Stepper_Port = 0x01;
delay(period);
}
/* last one step to acquire initial position */
Stepper_Port = 0x09;
delay(period);
delay(1000);
/* Rotate Stepper Motor Anticlockwise with Full step sequence */
for(i=0; i<12; i++)
{
Stepper_Port = 0x09;
delay(period);
Stepper_Port = 0x03;
delay(period);
Stepper_Port = 0x06;
delay(period);
Stepper_Port = 0x0C;
delay(period);
}
Stepper_Port = 0x09;
delay(period);
delay(1000);
}
}
8. Write a program for SERVO MOTOR INTERFACING with 8051 Microcontroller.

Servo motors are very useful in electronics and embedded systems. You can find the use of
Servo motor everywhere around you, they are used in toys, robots, CD tray of computer, cars,
aeroplane etc. The reason of this wide scope is that, servo motor is very reliable and precise.
We can rotate it to any particular angle. They are available in wide range, form high torque
motor to low torque motors. In this tutorial we are going to interface a servo motor to 8051
microcontroller (AT89S52).

First we need to understand the working principle of servo motors. Servo motor
works on PWM (Pulse width modulation) principal, means its angle of rotation is controlled
by the duration of applied pulse to its Control PIN. Basically servo motor is made up of DC
motor which is controlled by a variable resistor (potentiometer) and some gears. High
speed force of DC motor is converted into torque by Gears. We know that WORK= FORCE X
DISTANCE, in DC motor Force is less and distance (speed) is high and in Servo, force is High
and distance is less. Potentiometer is connected to the output shaft of the Servo, to calculate
the angle and stop the DC motor on required angle.

Servo motor can be rotated from 0 to 180 degree, but it can go up to 210 degree, depending on
the manufacturering. This degree of rotation can be controlled by applying a LOGIC level 1
pulse for a duration between 1ms to 2ms. A 1 ms can rotate servo to 0 degree, 1.5ms can rotate
to 90 degree and 2 ms pulse can rotate it to 180 degree. Duration between 1 to 2 ms can rotate
Servo motor to any angle between 0 to 180 degree.
Circuit Diagram and Working Explanation

Servo motor has three wires Red for Vcc (power supply), Brown for Ground, and Orange is
control wire. Control wire can be connected to 8051, we have connected it to Pin 2.1 of 8051.
Now we have to keep this pin to Logic 1 for 1ms to rotate it 0 degree, 1.5ms for 90 degree, 2
ms for 180 degree. We have used on chip Timers of 8051 to create delay. We have created
delay of 50us through the function “servo_delay”, and used “for” loop to create delay in
multiple of 50us.

We are using Timer 0 and in Mode 1, so we have put 01H in TMOD register. Mode 1 is 16-bit
timer mode and TH0 contain High byte and TL0 contain Low byte of 16-bit timer. We have
put FFD2 in 16-bit timer register, FF in TH0 and D2 in TL0. Putting FFD2 will create the delay
of approx. 50 us with the crystal of 11.0592MHz. TR0 and TF0 are the bits of TCON register,
TR pin used to start timer when set and stop when reset (0). TF is overflowing flag, set by
hardware when overflow and need to reset it by software. Basically, TF tells the completion of
Timer, and set by hardware when 16 timer transit from FFFFH to 0000H. You can read about
the “8051 Timers” to understand the calculation of value in timers registers, to create the 50 us
delay.

Now when measured from CRO, 13 loops of servo_delay function will give the delay of 1ms,
so we have started from 1ms (13 loops) and gone to 2 ms (26 loops) to rotate the servo from 0
to 180 degree. But we have slowly incremented the delay from 1ms, we have divided 1ms to 2
ms window into 7 parts like 1.14ms, 1.28 ms, 1.42ms and so on, so the servo will rotate in
multiple of approx. 26 degrees (180/7). After 180 it will automatically return to 0 degree.

PROGRAM CODE for SERVO MOTOR INTERFACING with 8051 Microcontroller


// C program for interfacing servo motor with 8051 microcontroller

#include<reg51.h>

sbit output=P2^0;

void msdelay(unsigned int time) // Function for creating delay in milliseconds.

unsigned i,j ;

for(i=0;i<time;i++)

for(j=0;j<1275;j++);

void servo_delay(int times) // Creating Delay in multilple of 50us using 8051


Timers

int m;

for(m=0;m<times;m++)

TH0=0xFF;

TL0=0xD2;

TR0=1;

while(TF0==0);

TF0=0;

TR0=0;

void main()

int n;

TMOD=0x01; // Selecting Timer 0, Mode 1

output=0;

while(1)
{

for(n=13;n<28;n=n+2)

output=1;

servo_delay(n);

output=0;

servo_delay(260);

msdelay(200);

9. Write a program to interface DAC AD7523 with to generate Triangle wave of period
1 ms with Vmax 5v.

FILE NAME DAC_TO_TRIANG.ASM

ORG C100H
X DW 00FFH ; the fall of rise and time I proportional
directly to the value.

ORG C000H
PA EQU D8H
PB EQU D9H
PC EQU DAH
CTRL EQU DBH

MVI A, 88H
OUT CTRL ; Purpose to configure 8255 ports
; The next 7 instructions will generate rising portion
of the triangular waveform.
; And it is done by sending to DAC through Port A
values from 00H to FFH,
; in steps of 01. Also the increment will be done after
a small time delay here.

LOOP: MVI A, 00H


ASCEND: OUT PA
PUSH PSW
CALL DELAY

POP PSW
INR A
JNZ ASCEND

DCR A ; Now A contents will be FFH

DESCEND: OUT PA
PUSH PSW

CALL DELAY

POP PSW
DCR A
CPI FFH
JNZ DESCEND

JMP LOOP
; These Subroutines are used only for the generation of
delay
; which is proportional to all the contents of word
located at X.

DELAY: LHLD X
AGAIN: DCX H
MOV A, H
ORA L
JNZ AGAIN
RET

10. Write a program for 8051 Microcontroller to arrange the given 10 numbers in
ascending order.
Algorithm
 To arrange the numbers in ascending order, first compare two numbers.
 If first number > second number, then interchange these numbers.
 Then compare second number and third number, interchange if needed.
 Continue same process for remaining numbers in the array.
 We have to repeat process until we get arranged data in ascending order.
 For this process, two counters are required.
 Byte counter, for accessing numbers from array.
 Pass counter for repeating the comparison process.
Step 1: Initialize a counter for comparison (Pass counter).
Step2: Initialize memory pointer to read number from the array.
Step3: Initialize byte counter.
Step 4: Read numbers from array.
Step 5: Compare two numbers.
Step 6: If number less than or equal to next number, then go to step 8.
Step 7: Replace number with next number which is largest.
Step 8: Increment memory pointer to read next number in the array.
Step 9: Decrement byte counter by 1.
Step 10: If byte counter is not equal to zero then go to step 4.
Step 11: Decrement pass counter by 1.
Step 12: If pass counter is not equal to zero then go to step 2.
Step 13: Stop.

Assembly language Program

MOV R0, #0AH ; Initialize pass counter.

REP1: MOV DPTR, #4000H ; Initialize memory pointer

MOV R1, #0AH ; Initialize byte counter

REPEAT: MOV R2, DPL ; Save the lower byte address

MOVX A, @DPTR ; Read the number from array

MOV 0F0H, A ; Store the number in register B

INC DPTR ; Increment memory pointer

MOVX A, @DPTR ; Take the next number from array

CJNE A, 0F0H, NEXT ; Compare number with next number

AJMP SKIP ; Jump to SKIP unconditionally

NEXT: JNC SKIP ; If number>next number then go to SKIP

MOV DPL, R2 ; Else exchange the number with next number

MOVX @DPTR, A ; Copy greater number to memory location


INC DPTR ; Increment memory pointer

MOV A, 0F0H

MOVX @DPTR, A

SKIP: DJNZ R1, REPEAT ; Decrement byte counter by 1, if byte counter≠ 0

then go to REPEAT.

DJNZ R0, REP1 ; Decrement pass counter if not zero then go to REP1

STOP: AJMP STOP ; Stop

SAMPLE PROGRAMS:

1. Add two 8-bit numbers


MOV A, #30H ; (A) 30
ADD A, #50H ; (A) (A) + 50H
2. Add two 16- bit numbers
MOV DPTR, #2040H ; (DPTR) 2040H (16 bit number)
MOV A, #2BH ; (A) 2BH (lower byte of second 16 bit number) MOV A,
#20H ; (B) 20H (Higher byte of second 16 bit number) ADD A, DPL
; Add lower bytes
MOV DPL, A ; Save result of lower byte addition MOV
A, B ; Get higher byte of second number in A
ADD A, DPH ; Add higher bytes with any carry from lower byte
addition MOV DPH, A ; Save result of higher byte addition
3. Division two 8-bit numbers
MOV A, #90 ; Get the first number in A
MOV B, #20 ; Get the second number in B
DIV A, B ; A+B, Remainder in B and Quotient in A
4. Multiply two 8-bit numbers
MOV A, #8F ; Get the first number in A
MOV B, #79 ; Get the second number in B
MUL A, B ; A x B, Higher byte of result in B and lower byte of result in A
5. To add two 16 bit BCD numbers
MOV DPTR, #1234H ; Load first number
MOV R0, #20H ; Load lower byte of second number
MOV R1, #30H ; Load higher byte of second number
MOV A, R0 ; Get the lower byte of second number
ADD A, DPL ; add two lower bytes
DA A ; Adjust result to valid BCD
MOV DPL, A ; Store the sum of lower bytes
MOV A, R1 ; Get the higher byte of second number
ADDC A, DPH ; Add two higher bytes considering carry of lower byte
addition DA A ; Adjust result to valid BCD
MOV DPH, A ; Store the sum of higher bytes
6. To find the sum of 10 numbers stored in the array
MOV DPTR, #2200H ; Initialize memory pointer
MOVX A, @DPTR ; Get the count
MOV R0, A ; Initialize the iteration counter
INC DPTR ; Initialize pointer to array of numbers
MOV R1, #00 ; Result = 0
BACK: MOVX A, @DPTR ; get the number
ADD A, R1 ; A Result + A
MOV R1, A ; Result A
INC DPTR ; Increment the array pointer
DJNZ R0, BACK ; Decrement iteration count if not zero
repeat MOV DPRT, #2300H ; Initialize memory pointer
MOV A, R1 ; Get the result
MOVX @DPTR, A ; Store the result

You might also like