MPMC 4th Unit
MPMC 4th Unit
Timer Counter
4 KB 128 býtes
Interrupt inputs
conttol ROM RAM
Internal
interrupts
CPU
4 Input/output Serial
Bus
OSC port
Control ports
Po P, P. P, TxD RxD
Address/Data
Figure: Block Diagram of Microcomputer
All the components of åmicrocomputer are built on one chip.
Central Processing Unit (CPU)
It is the heart of microcomputer unit.
It (CPU) performs and controls all the operations of the microcomputer.
It consist of the following elements.
Arithmetic Logic Unit (ALU): It performs arithmetic operations like subtraction, addition,
multiplication, division and logical operations like AND, OR, NOT, XOR, and NAND etc.
Program Counter (PC) :It stores 16-bit address of the next instruction to be executed in the program
thus, it keeps track of the program sequence.
Stack Pointer (SP): It holds 8-bit address of the top of the stack (i.e., the address of the location in stack,
where the last data was stored).
Accumulator: It is a special 8-bit register in which the result of any mathematical operation gets accumulated
(stored) automatically. It can also be used to store the 8-bit data.
General purpose registers to store various data.
Timing and control unit : It is used to synchronize the internal operations of microcomputer with the
frequency of the clock signal and to control the flow of data within the microcomputer and also between the
microcomputer and external devíces.
Oscillator Circuit
It generates a series of clock pulses required to synchronize the operations of a
It is also known as clock generator.
microcomputer.
It consists of a resonator and necessary electrical components.
The frequency of the clock pulses and the fanctioning speed of the microcomputer
of resonator. depends upon the frequeny
On-chip Memories
There are two types of on-chip memories namely internal ROM (4kB) and
internal RAM (128 bytes).
Internal ROM stores the program (code) instructions, It is also
known as on-chip program (code)
memory.
Internal RAM stores the data. It is also known as on-chip
data memory.
Timers/Counters : These are two-byte registers which are used in a program to
PVent or to calculate the number of events. For example, pulse-width determine the time period o
measurement, frequency measurement,
rate generation, and counting of pulses, ete., can be done using on-chip timer/counters.
WARNING: Xerox{Photocopying of this book is a CRIMINAL act. Anyone found guitty is LIABLE to
fece LEGAL procesdlns
Introduction to Micro
NIT4:
Controllers and Interrupts
rupt Logic Circuitry Communication 149
tis the
logiccircuit which senses the
interrupts
the active interrupts and the order of thearising in the microcomputer and makes the microcomputer
aware
of
des interrupt enable register, interrupts which should be served by the microcomputer.
P'arallel I/O Ports
interrupt priority register and otther necessary
afand
uscd to interface external
clements.
These are
devices with the
The seria! VO port is meant for
transferring microcomputer.
the data between
of bits (i.e., one bit at a time) microcomputer and
by tes (i.e., &-bits of data at atime). whereas, the parallel /O port is for transferring external devices in the
the data in the form oB
Explain the
importance of 8051
microcontroller over microprocessor. May-19(R16), Q1(c)
niscuss the advantages of microcontrollerOR
based system over microprocessor based
nSwer: system.
The advantages of microcontroller based system
Microcontroller based system contains additionalover microprocessor based system are,
circuits
circuit and peripheral devices, such as RAM, ROM, input/output
interfacing
Iess hardware is required for the
system using
less access time is required for built-in microcontroller due to its in built additional features.
mictocontroller is more than the speedof a memory of microcontroller. Hence, speed of operation of
The PCB size required for installing a microprocessor.
microcontroller based system is less due to less hardware
Microcontroller based systems are more reliable. requirement.
Most of the pins of microcontroller are
multifunctioned i.e., one pin can perform multiple functions.
The cost of microçontroller based system is
less due to less hardware requirement.
The environment of common
software/hardwarè design is available from manufactures.
Microcontrollers can easily synchronize the communication between
Microcontroller have many bit handling instructions and hence peripherals of different speeds.
controlling is more affective.
List out the difference between microprocessor
and microcontroiler. May-16(R13), Q1(h)
OR
What is the difference between microprocessor
and microcontroller? April-18(R13), Q1(h)
(Refer any Five Points)
SWer:
The differences between a microprocessor and a microcontroller
are as follows,
Microprocessor Microcontroiler
Ihe architecture of a microprocessor consists of 1. The
architecture of a microcontroller consists of
|ALU, registers and control unit.
microprocessor along with memory (ROMand RAM),
VO interfacing unit and various peripheral
here are only few instructions in microprocessor2.There are units.
many instructions in microcontroller that
ihat support data handling in bits, support data handling in bits.
|n amicroprocessor, only few pins have multiple 3.|lna microcontroller, most
of the pins havè muitiple
functions. functions.
Data and programs are addressed using the same 4. Data and programs are
nemory address range. addressed using different
memory address range.
There are instYuctions in microprocessor that| 5. There are only few instructions in microcontroller
/erform datamanytransfer among memory and CPU. to perform data transfer among-memory
|It takes more time to and I/O 6. lt and CPU.
acess memory takes less time to access memory and /O devices
devices. as they are built in with a
microcontroller.
Hardware
hat requirements are
microprocessor. more for the systems 7. Less hardware is required for the systenms that
use
microcontroller.
The
designing
|very flexible. of microprocessor based systems is | 8. The designing of microcontroller is not as flexible
as that of microprocessor based
3.R has Single memory to store data and code.
systems.
9.It has separate menmory to store data andcode.
Pert 2
drivs
RAMAGOR
RAM Fort Pot 2 fEPROM
ROM
Program
AODR
Stack register
TNP pointe
Angiste: TMP 2 Buffer
It perforrns arithmeticoperations like addition, subtraction, multiplication, division and logical operations
like AND, OR, NOT,XOR, etc over the operands held by temporary registers.
SW (Program Status Word)
I isan &-bit register which provides the ALr status information.
Sack Poiter
It is an 8-bit register used by the CPU to store information temporarily.
it increments/decrements the SP value by pushing/popping the data on to/from the stack.
gram Address Register
Ii is used to access internal and external program memory.
It is also interfaced to program counter and increment circuit for incrementing PC.
ierial Data Buffer
is an &bit register which consists of two registers namely, Transmit buffer (parallel in serial out register)
and Receive buffer (serial in parallel out register).
is used for data transmission and reception by writing and reading data into SBUF register respectively.
CAnerenenter
It is al6-bit register which keeps incrementing the address in accordance with the program
tow.
is also used to facilitate PC to be used for addressing modes.
SPECTRUH ALLIN-ONEJOURNAL FOR ENGINEERING STUuDENTS
152
MICROPROCESsORS AND
MICROCONTROLLERS
[JNTU-HYDERABADE
Program Counter (PC)
of eurrently fetching instruction.
Is a 16-bit register that hotds nrernes eery address
It iscapable of addressing 64 K bytes of code.
DPTR (Data Pointer)
It isa l6 bit register used to hold external dáta RAM address.
registet.
IS can be accessed as two 8-bit reeisters (DPH, DPL) Or al6-bít
RAM and RAM Address Register
7FH) and their addresses.
t stores 128 bytes of internal kAM ifrom 00H to
ROM
It is a read only program memory,
(EPROM)or flash EEPROM.
The ROM can be a masked ROM. Erasable PROM
Port Latches and drivers (P0, P1, P2, P3)
output port or the value read from an input port.
These registers specify the values to be generated on an
8-bit register in the SFR.
They are also bit addressable. Each port is connected to an
Interrup, Serial port and Timer blocks
This block includes timer and control registers.
Timer registers-Timer0 and Timer-1
timers/counters and
Control registers- These regísters have control and status information for interrupts,
serial port. These include IP, IE, TMOD, TCON, SCON and PCON registers.
Instruction Register (IR)
It holds the opcode of current instruction.
It provides information to the timing and controi unit to generate signals for executing the instructions.
Timing and control unit
It is used to generate timing and control signals for internal operation of circuit.
The control unit generates control signals like ALE, PSEN, RD, and WR.
ALE: It is an active high output signal used to latch the low address byte while accessing external memory.
PSEN: It is an active low program strobe enable used for reading the contents of external program memory.
RD: It is an active low read sighal used for reading the contents of external data memory.
WR: It isan active low write signal used for writing the contents to external data memory.
Oscillator
It generates a serial of clock pulses required to synchronize the operations of the circuit.
Q7. Explainprogram status word/flag register of 8051 microcontroller.
Answer:
Program status word (PSW) is an 8 bit register which provides the ALU status information. PsW contains
four mathematical or math flags, one user program flag and two register select bits as shown in figure.
7 4 3 1
CH AC FO RSI RSO OV P
y(P)
set ifregister A contains odd number of ones and cleared if register Ahas even number of ones.
I is
F l a g(FO)
This flag is
set/cleared by software as a user defined status flag. User flag is also called general purpose flag.
P1.0 40 V Po
Pl.1 39 PO.0 (ADO)
P1.2 38 PO.1 (ADI)
PÌ.3 37 PO.2 (AD2) Port-0
Port-1
P1.4 36 PO.3 (AD3) or
Address/
Pl.5 6 35 P0.4 (AD4) Data
P1.6 34 PO.5 (ADS) lines
P1.7 33 PO.6 (AD6)
RST 32 PO.7 (AD7)
(RXD)P3.0 8051 31 BA
Microcontroller 30 ALE
(TXD)P3.1 11
(INTO)P3.2 12 PSEN
Port-3
or (INTI )P3.3 13 28 P2.7 (Al5)
Alternate (TO) P3.4 14 27 P2.6 (A14)
Function Port-2
lines (T1) P3.5 15 26 P2.5 (Ä13)
or
(WR)P3.6 16 P2.4 (A12) Address
(RD) P3.7 17 24 P2.3 (A11) lines
1JNTU-HYDERABAN
internal ciock generatrr, the
Port-0 (Pins 39-32) in case of
pin is connected as input to the isverting
A groupof &pins (i.e., 39-32) is confisgured as ofoscillator circuit and XTAL 2 pin is
port0.
to the output of inverting amplifier.
This port s dn 8-bie bidircctional bit sddress Control Pins
able 1/0.port: It is also used to access external 4
memory. * Pin-29 ( PSEN -Program Store Fnatie
The pins PO.0-P0.7 represent ADO-AD7 respec PSEN is. an active low, output control
tively. When microcontroller readsthe external
The lower order address byte (ADO-AD7) of memory, the PSEN signal gets activated z
the two byto address (AD0-AD15) of external every six oscillator periods.
memory is timé multiplexcd with the data being
written or rdad,Hence, the pins of port-0 can be The PSEN signal serves as the read strobe t
used to transfer data bytes between 8051 and the external program menory.
external.devices and also to address an external
This signalcauses the external program memsry
memory. toenable its contents to the 805 !.
Port-1 (Pins 1-8)
Pin-30 (ALE-Address Latch Enable)
The pins 1-8 are configured as port I.
The pins of this port are configured to perform ALE is an active high output signal.
only l/O function. While accessing the external memory, this sigrai
It acts as an 8-bit bidirectional bit addressable is used to latch the lower byte of the address
port. which is time multiplexed with port 2.
Port-2 (Pins 21-28) It can be used for exterr! timing or clocking
The pins 21-28 form a parallel I/O port (port 2). purpose.
The pins of port 2 can be configured as 1/O pins Pin-31 ( EA-External Access)
or they can be configured as higher order address
lines (AD8-ADI5) ofatwobyte external memory EA is an active low signal.
address. When the EA pin is high, i.e., EA =1the 805!
It acts as an 8-bit bidirectional bit addressable fetches the progran instructions fron the irntemal
port. program memory.
Port-3 (Pins 10-17) When the EA pin is low,i.e., EA =0, the 8051
The pins 10-17 are configured as Port 3, is also microcontroller fetches th¹ program instructioS
a bidirectional I/O port. from external program memory.
The pins of port3 can be configured to function as Pin-9 (RST-Reset)
/O pins and also to perform alternate functions. The RST pin is used to reset the 8051 microco
2. Power Supply and Ground Pins troller. To reset, 805 1, a high signa! should be
Pin-40 (V:): This is a power supply pin. An: applied to this pin for two machine cycles, wtie
external. D.C power supply of +5V iscomnected the oscillator is unning.
to this pin.
Q9. List the various registers present "
i Pin-20 (GND): This pin is the ground pin of
8051 and it is connected to the ground in order 8051 microcontroller and explain with an
to ground the circuit of 8051 microcontroller. example. Feb./March-22(R18), Q3b:
3. Oscillator Pins OR
XTALI,and XTAL2 are the input and output, of Describe briefly the register set of 80:!
amplifier. microcontroller. Dec.-17(RIJ),Q2
May-16/R13), Q1
In case of externalclock generator, the output of
external psuillator (clock generator) connected to Answer:
nins XTAL2 and XTALIpins must be connected
to the ground.
The register set of 8051rmicrocontroller h
following registers,
WARNING: XeroxPhatacopying of this book isaCRIMIMatt Anyone found guilty is LJABLE t# fa LEGAL proceedings
NIT-4: Introduction to Micro Controllers and
What is the purpose of Interrupts Communication 157
o11. using VO ports of 8051?
Classify and explain them in detail.
Juty-23(R18), Q4(b)
Exolain the /O pins ports and OR
circuit details of 8051 witth its diagram. April-18(R13), Q94a)
what is the function of Port OR
3 of 8051
(Refer only Port 3) microcontroller? Dec.-17(R13), Q1(g)
ANSWer:
The 805I
P2and P3.
microcontroller has 32 /O pins which
are configured as 8-bit
bidirectionai parallel ports PO, Pi,
Cach pin can be used as
input or output or both.
Each port consists of a latch, an
output driver and an input driver.
t0: Port Opin circuit consists of a
Control Sighals latch, control logic unit, buffers and FETs T., T, as shown in figure
Address/Data
(1).
Read Latch Bit
Internal
Internal Bus FET
D
Pullup
Pin
Write to Latch l.x
JNTU-HYDERABAD)
Pins 1-9 of this port cam be configured only as Pins 10-17 of this port
can be configured only as
port I.
I/0 pins. I/Opins similar to
The i/O pins of port 3are multifunctional. Apart
aport has to function as an input port then, the
also serve the
latch of port lis made high (1) by turning offthe from being port pins, they functions
output transistor. The FET load pulls up the pin of various special functional registers such as,
andits bufter and makes the pin to float in a high p3.0-RxlD :Used for receiving serial ato
impedance state. This problem is eliminated by
adding an external circuit. The external circuit P3.1-TxD : Used for transmitting serial data
drives the input low to logic 0 or input high to
logic 1. P3.2- INTO : Used for external interrupt 0
If a port has to function as an output port, the P3.3- INTI :Used for external interrupt j
latch with high input drives the external circuit P3.4-TO :Used for external tiner 0 iDut
inputto high using the internal FET pull up and :Used for external timer 1 inDut
P3.5-TI
the latch with low input drives the external circuit
input to low turning on/off the FET and internal P3,6- WR :Used for externaB data memory wpite
FET pull respectively. signal
Port 2
P3.7- RD .Jsed for external data memory read
Port 2 pin circuit consists of a latch, control logic
signal.
unit, buffers, FET and internal FET pull-up as shown in
figre (3). Q12. Explain the logical separation of program
Control Signals and data memory.
Address
Read Latch Bit
Vec Answer: Dec.-19(R16), Q4a)
Internal
In 8051 micro controller, the program and data
Internal Bus D o FET
Pullup
memory is logically separated i.e., they have
Pin
Wite to Latch
Control
2.x
different address spaces.
Logic
The logical separation enables the 8-bit addreses
PORT SFR Latch
to access the data memory which can be stored
Read Pin Data
and manipulated more easily. Data memory of
Figure (3): Port 2 Pin Circuit Configuration 8051 has 256 bytes of intermal data memory and
Pins 21-28 of this port can be configured only as 64 kB of external data memory.
VO pins similar to port 1. It is possible to generate 16-bit data memory
Port 2 pins produce higher order address byte address by using DPTR register.
outpüt with the lower order address byte of port
0 to access the external memory. The prOgram memory is a read oniy memory.
When a higher order address byte output 8051 has 4096 (4K) bytes on internal programn
produces, the address control signals temporarily memory of 8081 can address 4 kB of on-chip
alter its port pins. program memory and 64 kB of external program
Port 3 memory. These two memories will overlap
Port 3pin circuit consists ofa latch, NAND gate, hence, they can be differentiated by using PSEN
buffers, FET and internal FET pull-up as shown (Program store enable) signal.
in figure (4).
Alternate Output The PSEN signal serves as the read strobe to the
Read L.atch Bit
Vec external program memory and enable its contents
to the 8051.
Internal
nernal Bus
FET
Pullup While accessing the external data memory, sUi
Write to Latch | Pin
|3.x
produces read (RD) and write (WR) signals.
PORTSFR Latch With RD and PSEN signals as inputs and rea
strobe to the extemai program/data memory e
Read Pin Dala Output to on AND gate, both external progra
com-
Alernale Input be
memory and external data memory can
Figure (4: Port 3 Pin Cireuit Configuration bined.
WARAING: Xerox|Photocopying or tiis bo0* IS aCRIMINAL act. Anyone found quilty is LIABLE to face LEGAL proceeu
NIT-4: Introduction to Micro Controllers. and Interrupts Communication 159
Discuss about memory organization of 8051 July-23(R18),Q44a)
microcontroller.
OR
Cxolain the concept of memory organization of 8051 microcontroller. Dec.-18(R13), Q8(a)
In 8051 mierocontroller, the available memory space is segregated as program memory and data memory.
Program memory contains the programs which have to he executed. It is imolemented using EPROJM.
Data mennory contains the intermediate results, variables and constants which are
necessary tor exeGuting
theprogran. t is implemented using RAM.
Program memory and data memory are again segregated as internal memory and external memory.
rogTAm memory
s051 microcontroller can address 4KB of on-chip program memory (0000H-OFFFH) and 64KB Of
external
program memory (0O0H-FFFH). These two memories will overlap hence: they can be differentiated by using
the PSEN signal.
000011
Example
MOV IPTR#2550H can be written as,
MOV DPL, #50H
MOV DPH, #25H
2,
Register Addressing Mode: In this mode, the registers are used as address oftheoperand. The least signihicans
three bits of the instruction opcode indicates one register. Thus, a function code and operand address can
combined to fornm a one byte instruction.
Register
Opcode RO R7 in current bank
Exampies
1. ADD A, RO ;Contents of R0and A are added
2. MOV R7, DPL ;Content in DPL is moved to R7
3. MOV R6, A ;Save contents of ACC in R6
The source and destination registers must have same size.
This mode cannot be used between two Rn registers.
Example: MOV R4, R7 is invalid.
3 Direct Addressing Mode: All 128 bytes of internal RAM and the SFRs may be addressed directly
the single byte address assigned to each RAM lócaion and each special function register. In this using
mode, the data is in RAM memory location whose address is known and this address is addtessing
the instruction. given as a part of
Opcode RAM
Address
Examples
1 MOV RO, 40H ;Loading data in address 40H to RO
2. MOV 0A8H, 77H ;Loading data in 77H to 0A8H address
3 MOV 56H, A ;Load content of A in
;RAM location 56H
4. Register Indirect Addressing Mode: In register indirect addressing
address of an operand located in RAM. In other words, mode, a register is used to hold the
a register is used as pointer to the data.
If the data is used inside the CPU, The registers R0
and Rl only are used whereas R2 -R7 cannot be used.
The RO and Rl are preceded by @' sign.
Opcode
Register RO or RI in current bank
Example
1 MOV A, (@RO; Move contents of RAM location whose
address is held in RO into A
2 MOV (aRI, B;Move contents of Binto RAM location whose
address is held in RI
5 Indexed Addressing Mode
It is used to access data ¢lements of look-up table entries located
in the program ROM space of the 30
The instruction used for it is
"MOVC A,@A+DPTR".
It copies the contents of ROM address formed by (A +
DPTR)into the accumulator.
The data elements the saved in the program (code) space of
means codel instead of MOV.
8051, so, the instruction MOVC IS Us
WARNING: Xerox/Photocopying of this book is aCRIMINAL act. Anyone found guilty is LIABLEto face LEGAL proceeuu
lotroduction to Micro Controllers and Interrupts 163
INIT-4:
Communication
0t uses either program counter or data pointer as the base register and an offset (the accumulator) to form
etive address for aJMP or MOVC
Base register
inistruction.
offset ËA
PC or DPTR + ACC
Dalative addressing is only used for specific jump
instructions.
Arelative address (or offset) is a signed 8-bit value that is added to the
le address of the next instruction to be program counter (PC) to determie
executed.
Opcode Relative offset
Relative addressing (e.g., SJMP AHEAD)
The signed 8-bit offset gives the address range of -128 to+127 1ocations to
offset is added as an extra byte to the instruction. IUMP instruction. The relative
Before the addition, the PC is incremented to the address afer the iump
address is related to the following instruction and not the instruction; therefore, the new
address of the jump instruction.
Typically, jump destinations are defined as labels, and the assembler cálculates the relative offset
accordingly.
Example
010A
0109
0108
0107
0106 4
Relative offset
0105 3 from address
0102H is «g»
0104 2
0103 1
0102
0101 05
HsJMP 0107H
0100 80
0OFF
Code
memory
If the label. THERE represents an instruction at location 0107H, and the instruction
SJMP THERE is in
memory at locations 0100H and 0101H, the assembler will assign a relative offset of SH as byte 2 of the
instruction (0102H+ SH = 0107H).
¢n advantage of relative addressing is that the program code is easy to relocate in
memory in that the
addressing is relative to the position in memory.
Absolute Addressing Mode
Absolute addressing is used only with AJMP (Absolute Jump) and ACALL (Absolute Call) instructions
These 2-hvte instnuctions alow branching within the current 2K page of code memory by
providine the 1l
LSB of the destination address in the opcode (ATCA8) and byte 2 of the instruction (A7-A0)
A15 -A8 A7- A0
Opcode
Long addressing (e.g., LJMP FAR AHEAD)
The upper five bits of the destination address are the current upper five bits in the PC, so the instruction
following the brançh instruction and the destination for the branch instruction must be within the same 2K
Page, since A15-A11 do not change.
F800
I000 32 × 2K 64 K
17FF
Within any 2K
page, only the lower 2K page 2
1lbits change
1000
OFFF
2K page l
0800
07FF
0000 2K page 0
(a)
A15 A10 A0
WARNING: Xersx/Photocopying of this boek is a CRIMINÂL act. Anyone found quilty is LJABLE to face LEGAL proceedings
pomtei
Bytes: 1
Cycles: 2
(v) NOP( No Operation)
Operation: It does nothing. It is used to generate a delay in execution. The execution continues with the
next instruction.
Bytes: 1
Cycies: 1.
41.4 Simple Programs using StackPointer
Q23. Expiain how stack is implemented in 8051 C?
OR
How stack are accessed in the 80517
Answer:
Stack Operation in 8051
Siack is asection of RAM used by the CPU to store the information temporarity. The register used to accese
the stack is called SP (stack pointer) register and it is 8 bits wide.
The address held in stack pointer is the location in the internal RAM, where last byte of data was stored by
stack operation.
The 8 bit Stack Pointer (SP) register is used to store the address of internal RAM, hence it is called as "on
of the Stack".
When 8051 is turned on the SP register has value 07. So, 08H in the RAM location is the first location used
for the stack by the 8051.
SP points to the last used location of the stack.
By using two operation, the data'can be inserted into the stack as well as, the data
stack i.e., can be deleted from the
() PUSH
(ii) POP.
By using PUSH operation, the data can be inserted into the
last row
by incrementing the stack pointer i.e., illustrated in the figure as of the stack and next data can be inserted
shown below.
By using POP operation, the data can be retrieved from
from the stack, decrementing the stack pointer after the first row of stack and next data can be retrieved
getting the data from the stack as illustrated in the figure.
CPUAction CPUAction Retrievl
OA Store Address 0A Get SP
09
SP StoreAddress 09 Get 09 SP
SP 08
StoreAddress 08 Get SP
SP 07 Address 07 07 SF
Ihsertion
Increment of SP)
(Decrement of SP)
Figura: Stack Operation
Upper Limit of Stack
TheRAM location 08H to 1FH (24 bytes) is assigned
TEmte than 24 bytes is to be used by to stack.
stack, then the SP points to RAM
locations 30H -7FH.
DAINe- Xerox/Photocopying ot ths DOOK IS a CRIMJNAL act.
Anyone found guilty is LJARIE te
face LEGAL proceedings.
NIT4:Introduction tó Micro Controllers"andlnterrupts Communication 179
rOgrams
The stack pointer (SP) is an 8-bit register used by the 8051 microcontroller to manage the stack. By default,
initialized
to 07H after areset.
is
itdProgram Push and Pop Operations
This demonstrates the use of push and pop instructions to store
program and retrieve data from the stack.
(ORG00H ;Origin address
MOVSP, #30H ; Initialize stack pointer to 30H
MOV A, #55H ; Load A with 55H
PUSH ACC ; Push A onto the stack:
MOVA, #0 ; Clear A
POP ACC ;Pop the top of the stack into A
uERE: SJMP HERE;Infinite loop to end the program
END
Program 2: Subroutine Call and Return
This program demonstrates callinga subroutine and returning from it, utilizing the stack to store the return
ddress.
sIT4:
P r o g r
Recursive Subroutine
a m
8:
ew demonstrates a simple recursive subroutine: Note that the 805 1 is not
program optimized for recursion,
This
Cxampleis purely illustrative.
ORG 0 0 H :Origin address
MOV SP. #30H ; Initializestack pointer to 30H
MOV R0, #3 ; Set the rccursion depth
LCALL RECURSE
:Call the recursive subroutine
HERE:SJMP HERE ; Infinite loop to end the program
RECURSE:
DEC RO ; Decrement RO
LCALL RECURSE
; Recursively callthe subroutine
INCRO ;Incremcnt RO (back to original value before returning)
RET ;Return from subroutine
END
Program 9: Interrupt Service Routine (ISR) Using Stack
Thisprogram demonstrates handling an interrupt and using the stack to save the context.
ORG 00H ;Origin address
MOVSP, #30H ; Initialize stack pointer to 30H
SETB EA ; Enable global interrupts
SETB EX0 Enable external interrupt 0
HERE: SJMP HERE ; Infinite loop to wait for interrupt
; Interrupt Service Routine for external interrupt 0
ORG 0003H
ISR EXO:
PUSH ACC ;Save accumulator
PUSH PSW ;Save program status word
MOV A, #0AAH ; Perform some action in ISR
POP PSW ;Restore programn status word
POP ACC ; Restore accumulator
RETI ;Retürn from interrupt
END
#Program 10: Using the Stack for Parameter Passing
This program demonstrates passing parameters to a subroutine using the stack.
ORG 00H ;Origin address
MOV SP, #30H ;Initialize stack pointer to 30H
MOV A, #0FH ; Load A with the first parameter
PUSH ACC : Push the first parameter onto the stack
MOV A, #1FH : Load Awith the second parameter
PUSH ACC :Push the second parameter onto the stack
LCALL SUB ;Call subroutine
HERE: SJMP HERE :Infinite loop to end the program
SUB:
POP ACC ;Pop the second parameter from the stack
MOV B, A ;Store the second parameter in B
POP ACC ;Pop the first parameter from the stack
MOV R0, A :Store the first paraneter in RO
; Perform some operation with parameters in RO and B
RET :Return from subroutine
END
SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTS
182 MiCROPROCESsORS AND MICROCONTROLLERS [JNTU-HYDERABAD
addresses and errors that are detected by the assembler.
4.1.5 Assembly language
80513 programming o This file is optional, because it is not used; much and this
is accessible only with the help of editors. One can view
Q24. Define the terms machine the syntax errors in the code and only after correcting
language,
assembly language and mnemonics. these errors, the object file is ready to link with other
Answer: required files.
Machine Language After this object code is given to a linker progran
that generates a file with extension .abs', which is
A language that allows the programmers to read
and write the instructions only in binary number format referred as absolute object file. This file is then fed to
is called machine language. Machine language is a low OH (Object to Hex converter) program that generates
level programming language and it is casily understood an equivalent program called 'hexfile' with extension
by CPU. However, machine language is highly impos *hex'. The hex file obtaíned is wTitten into ROM of
sible for
humans to use since, it comprises of binary microcontroller which is ready to perform the operation
numbers i.e., 0's and 1's. The following flowchart illustrates the total
Example: Machine language instruction is, process of assembling and running an 8051 program
1000001000000110 Editor program
Assembly Language myfile.asm
A language that allows the programmers. to
read and write the instructions in a naming or keyword Assembler
format is called assembly language. Assembly language program
possesses same structure and set of commands as that
myfile.lst
of machine language but allows the programmer to use
named instructions instead of binarynumbers. It is easily myfile.obj -other obË files
understood by humans but hard for CPU. However,
Linker program
the programs written in assembly language need to be
translated into machine language. Thus, this can be done
myfile.abs
by using a program called assembler.
Example: ADD A, A7 OH program
Mnemonics
The predefined names allotted for machine myfile.hex
instructions in an assembly language is called mnemonics.
Figure: Assembling and Running 8051 Program
Example: MOV RS, #25H
Q26. Explain the basic structure of assembly
language.
Mnemonic Operands Answer:
Q25. Write the process of assembling and
running'an 8051 program. An assembly language instruction contains four
fields label, mnemonics, operands and comments. The
Answer: general structure of instruction is shown below.
The assembly language program is first written
using an editor. There exists different types of editors like [label:] mnemonic [operands] [; comment)
Notepad in Windows or MS-DOS EDIT program. The bracket specifies that they are optional.
most widely used is the MS-DOS EDIT program, as it Label: Label is a symbolic name given to a branch of
is available in all Microsoft operating systems. The only instructions for which the program is referred. The
condition on an editor is that it must be able to produce length of the label fields limited to certain
an ASCII file. and l¡bel is not mandatory to exist in an characterS
the label field is optional. instruction. So,
Assembling and Running: The code written in an editor
is known as source code. Depending on the type of Mnemonics: Mnemonics are predefined words allocate
assembler used, the extension of the source code varies to machine instruction.
Iike 'asm' or 'src'.
Operand: Operand represents the data available in
This source file is fed to an 8051 assembler, The registers and addresses to be operated. u
assembler converts the total program (instructions) into Comment: Comment field describes each line in the
machine code. Thus, the object file is created as outout program so that it becomes reasier for the
along with a list file with extensions ".obj' and lst' to read and understand thecode. programnc
respectively. The list file consists of all the opcodes. and starts with Comment is optioa
semicolon (;)
WARNINR Xerox/Photocopying of this book 1S a titMiNAL act.
Anyone found guilty is LIABLE 1e face LEGAL proceedings
Inntroduction to Micro
T4:
Controllers ahd interrupts 187
The four register banks are labelled
as Bank Communicatio
1, Bank 2 and Bank 3 and the eight Overflow Flag (0OV)
sank
working
stersin cach register bank are named as RO to R7. It is set whenever the result of a sigmed umber
is addressable either by its
hregister
name or by its operation is too large causing the higher order bit to fiow
M address.
into the sign bit.
Onlyone register bank is used for an Parity (P)
instant.
knows the register bank that is currently in CPU
PSW..3 (RSO) and use Itis set if register A
contains oddnumber of ones
bythe bits PSW4 (RS1) in
status word as shown in table the and elearcd if register A has even number of ones.
program
TimerI is a l6-bit register separated into two registers TLI and TLI. TH1represents lower byte register
and THl represents higher byte register. Timerl register is accessed similar to timerO.
The timers 0 and I use TMOD register for setting different modes.
Q37. Describe the operation of timers present in 8051 microcontroller. FebJMarch-22(R18), Q3{a)
OR
Describe TMOD register.
Answer :
Timer Mode Register (TMOD): Timers of 805Imicrocontrollers has four types of modes namely mode 0, model,
mode 2 and mode3. Seletion of aparticular mode is done by setting mode bits M1 and MO in a register
known as
Timer Mode register (TMOD) as shown in figure.
7 4 3 2
Gate CT MI MOGate C/T| MI MO
Timer 1 Timer 0
Figure: Timer Mode Register (TMOD)
Gate
The gate bit decides the hardware or software control over
timer/ counter.
When TRx bit in TCON register =1,
If Gate = | then, Timer/Counter x is enabled when
INTx is high i.e., hardware control.
(i) If Gate =0 then, Timer/Counter x is enabled only
when TRx is high i.e.. software control.
Where, x indicates I/0.
cr: The timer/counter bit decides the type of operation
(timer or counter) to be performed.
JfC/T= 1, counter operation is performed.
IfCT= 0, timer operation is performed.
WARNING: Xerox/PhotocopYny or ts Do0k 3 8 GRIMINAL act. Anyone found guity is LUARLE to faee LEGAL proceediis
rate.
For XTAL= ll.0592 MHz 57.600
and 921.6 kHz / 16 = Hz since
Machine cycle frequency |L.0592 MHz / 12 = 921.6 kHz
SMOD |.
rate.
Ihis is the frequency used by Timer lto set the baud
for SMOD 0and SMOD=lare as follows,
For XTAL =|1.0592 MHz, the baud rates
THI Decimal Hex SMOD=0 SMOD =1
-3 FD 9600 19,200
-6 FA 4800 9600
Gate
INTÖpin
It is a l6-bit timer register which allows loading of values between 0000H toFFFFH in TL and TH registers.
Once the registers are loaded, the timer starts by setting the run bit, TR using the instructions SETB TRO
for timer 0 and SETB TRIfor timer 1.
Once the timer starts, it counts till the maximum value (FFFFH) isreached and then rolls to 00001 setting
the flag bit, TF high.
When TF=1,the timer stops and clears the flag bit using the instructions CLR TROfor Timer 0 and CLR
TRI for Timer 1.
Operation of mode 1 is illustrated in figure (2).
XTAL TH TL TF
+12
oscillator
overfiow
TF goes high
when FFFF>0 fiag
C/T=0. TR
XTAL TL TF flag
+12
oscillator
reload TF goes high
TR when FF ’0
C/T=0
TH
Figure (3): Mode 2 0peration
Mode 3
Timero and i can be programmed in mode 0,1 or 2independently. This does not hold good for mode 3.
Placing timer inmode 3 causes it to stop counting. The control bit TRI and the timerl flag TFl are then
ised by timero, TimerO in mode 3becomes two completely separate 8-bit counters, TLOiscontrolled by the
gate arrangement andsets timer flag TFO whenever lt overtoWs from FFH to 0OH. THO receives the timer
Clock 1 under the control of TRÍ only and sets he 1F lag when it overfrws
SPECTRUM ALL-IN-ONE JOUKhL POR ENGINEERING STUPENTS
(JNTU-HYDERABADI
204 MICROPROCESSORS AND MCROCONTROLLERS
To pin lControl
TRO
Gate
Gate CT MI MO Gate CT MI MO
1 0 0 0
6 3 2
Reload
THI
Figure
WA BAHNG: Xerox/Photocopyy o s ou sa bHIMINAL act. Anyone found guilty is LiABEE to face LFGAL
proceedings