MA Chap 1
MA Chap 1
Basiesof Microcontroller
and
intel 8051 Arehitecture
invented in
The first microcontroller was
1970-71, when
Texas Instruments. During
the world's
Intel was working on inventing
Boone of Texas
first microprocessor, Gray concept
Instruments was working on similar
invented microcontroller. Boone
and
chip
designed asingle integrated circuit (1C)
circuits to form acalculator, only
that could hold nearly all the essential was
were not incorporated. The first IC
the display and the keypad 3000
has 5000 transistors providing
named as TMS1302NC. This device memory. It can perform a
memory and 128 bit of access
bits of program
range of functions.
ntroduction
systems, which can be
Microcontroller is one of the powerful control
control applications in industry as
used for the measurement and sometimes
as in a specific application. Microcontroller is
well because it contains all the
described as a "Computer on Chip" Unit (CPU),
Central Processing
features of full computer including
clock circuitry ROM, RAM, input and output ports with special
inbuilt analog to digital conversion
features such as serial communication,
and more recently signal processing.
purpose device, which is meant to read data, perform
It is a general control its environment based
limited calculations on the data and microcontroller is to control
use of
on those calculations. The prime
operation of machine using a fixed program that is stored in
the of system.
ROM and that does not change over the lifetime
(1.1)
S.Y,B.Sc. (Comp. Sci.) :Elect. - P-] S-II 1-2 Basics of Microcontroller ....
Aspecial application of microcontroller is well suited for data logging
as well as monitoring and recording the environmental, physical and
chemical parameters. In addition to the above home monitoring
systems, embedded processorsare used in home appliances.
Microcontrollers have many advantages as compared with the
microprocessors especially in the areas where high integration and
saving of space is required.
The main difference between microprocessor and microcontroller is
given below:
Microprocessor Microcontroller
1. Microprocessor is used in 1. Microcontroller is used in
computer system. It is the embedded systems.
heart of computer.
2. It consists of Memory and I/O 2. Microcontroller consists of
components which are inbuilt internal memory and
externally connected to it. I/O ports.
3. Since memory and I/O are 3 Since memory and 1/0 are
externally connected, the inbuilt, the circuit is small.
circuit becomes large.
4. Cost of the entire system is 4. Cost of the entire system is
more. less.
5. Total power consumption is 5. Total power consumption is
high as compared with less and can be used in
microcontrollers, so cannot be battery operated devices.
used in battery operated
devices.
6. Most of the micro processors 6. Most of the microcontrollers
do not have power saving have power saving modes
features. like idle mode and power
saving mode. This helps to
reduce power consumption
even further.
... (Contd.,;
S.Y.B.Sc. (Comp. Sci.) : Elect, - P-I - S-I1 1-3 Basics of Microcontroller ....
Microprocessor Microcontroller
7. Since memory and I/0 7. Since components are in
Components are externally built, most of the operations
Connected, each instruction are done by internal
will need external
operation, instructions, hence speed is
hence it is relatively slower. fast.
8. Microprocessors are based on 8. Microcontrollers are based
Von-Neur ann architecture on Harvard architecture
where program and data are
where program memory and
stored in same memory Data memory are separate.
module.
9. Mainly used in personal 9. Mainly used in washing
computers. machine, MP3 players, stand
alone systems.
More than ten times as many
are manufactured and sold in the microcontrollers than microprocessors
world inspite of the
recent time, eight bit microcontrollers have become high profile. In
the backbone
for the solution o* application of
intermediate
low power consumpticn and flexibility makecomplexity. Small size,
these devices ideal.
Being software driven, microcontrollers greatly
simplify the design of
sophisticated instrumentation and control circuitry.
Now-a-days everyone is familiar with a term embedded system like
cell phon es, remote controllers, washing
fax machines etc. The meaning of machines, microwave ovens,
which performs embedded system is the system
some specifictask.
In other words, an
embedded products uses microprocessor (or
microcontroller) do one and only one task. These systems are not
tc
generalized. They are assigned some specific sequence of
and requires sort of operations
operations.
automation to control the sequence of
Internal
Internal
ROM.
RAM Anterropt
circuits
Program counter
Fig. 1.1 :Ainternal block diagram of microcontroller
Fig. 1,1 shows the block diagram of typical microcontroller. It is the
on chip computer. It contains allblocks such as RAM, ROM, parallel
VO and serial /O ports on a single chip. Along with this, it also has
ALU, SP and other registers to perfcrm the features supported by
microprocessor.
Microcontroller can perform simple ar thmetic and logical operations.
The prime use of microcontroller is to control operations with proper
sequence.
This chapter provides a broad overview of the architecture of the
8051. The 8051 introduced by Intel Corporations is also referred as
MCS-51.
Salient Features of 8051 Microcontroller :
The salient features of 8051 microcontroller are :
1. It is an 8-bit microcontroller, designed using NMOS technology.
2. 8-bit data bus, 16-bit address bus.
S.Y.B.Se. (Comp. Scl.): Eloct. P-I - 5-11 1-5 Basics of Microcontroller .
DPTR
KDEH
TITT
DP ROM
Begister
bank 0
The programming model of 8051 is shown in Fig. 1.3. From the block
diagram and programming model registers, Special Function
Registers (SFR) and its memory location can be studied.
S.Y.B.Sc. (Comp. Sci.): Elect. - P-I- S-ilI 1-7 Basics of Microcontroller
8 B8 8A8
8898B8
TMODA TCON FFF
Register Registor Rogister Registe:
B Interrupt registers Timerlcounter registers
Ragiste Regster
Math registers 88 88A. 88D 8 B
ETHO TLOH1TL1
eounterConter counter Counter
Timer/counter registers
8 98
B849987e
SCON SBU PGON PSW.
Do
7F RegistarRegster Register Register
Serial data registers Flags
Gareral
area SP
30
2F
Regisler
Bit 7E
addrasS Stack pointer register
Bit Addresses for This RAM
20 arep Area Only
00
1FRegister
Sbank
183
17Register 883 B82 16 No AddresS
10
bankag Data pointer
OF
DPH DPE Program counter
Regieter Data and code addressing
08 bank
1 registers
07bARRT
Register 06ERAR6
05R6 880SA90%
Port 0
Porta aAD8BO
Port2
Bank Lafch atch Port3
0 04R4
03P3
Latoh Latch
02 R2:E
Number of
01R1Sk bits
Direct byte address
00ROR
Byte Internal 000
Addresses RAM Internal
ROM
Note: Indicates bit addressable
Fig. 1.3 :8051
1 2Registers
programming model
Fig. 1.3 shows the
programming model having collection of 8-bit and
16-bit registers. These
to cperate using the registers and memory locations can be made
part of the design. Thesoftware instructions that are incorporated as a
program instructions are related with the
1-8 Basics of Microcontroller ....
S.Y.B.SC. (Comp. Sci.) : Elect. - P-I - S-lI
control of registers and digital data paths that are physically
contained inside the 8051.
In CPU, registers are used to store information temporarily. That
information could be a byte of data to be processed, or ar address
pointing to the data to be fetched.
D, D6 Ds D D3 D, D1 Do
8-bit Register
The 8-bit register is shown from the MSB (Most Significant Bit) D, to
the LSB (Least Significant Bit) D, with an 8-bit data type. 8051
contains large number of registers. Some of the widely used registers
are Accumulator Aand Bregister, register Ro, R, Re Rg, Ra, Rs, Ro R,
as well as Data Pointer (DPTR), Stack Pointer (SP) and Program
Counter (PC). All the above registers are 8-bit except DPTR and PC.
There are two math registers name y Accumulator A and B. These
registers (A and 8) are mostly used in arithmetic and logical
operations as well as used in data transfer between 8051 and
external memory. B register is used with A for multiplication and
division operations. Some Special Function Registers (SFR) are also
there in 8051.
Each register with an exception of Program Counter (PC) has an
internal 1 byte address assigned to it. Some registers marked with *
(asterisk sign) are both bit and byte acdressable. i.e. the entire byte
of data at each register address may be read or altered, or individual
bits may be read or altered. Software instructions are generally able
to specify a register by its address, its symbolic name or both.
1.3 Flags and Program Status Word (PSW)
The 8051 has a flag registers to indicate arithmetic condition. These
are 1-bit registers provided to store the results of certain program
instructions. Other instructions can test the condition of flags and
make the decisions based on the flag status.
The flags may be conveniently addressed and they are grouped
inside the Program Status Word (PSW) and Power Control register
(PCON).
1.3.1 PSW (Program Status Word) Register
The Program Status Word (PSW) Register is an 8-bit register. It is
referred as flag register.
Basics of Microcontroller
s.Y.B.Sc. (Comp. Sci.): Elect. - P-1- S-II 1-9
While the data is retrieved from the stack, the data is taken out first
and then stack pointer is decremented.
" Fig. 1.4 shows the stack operations.
CPU CPU
action action
SP07
PAddress
07 SPP07
Storlng data
on the stack Internal RAM
Getting data
(increment from the
then store) stack (get then
decrement)
Fig. 1.4: Stack operations
On reset, stack pointer contains the value 07H.
The stack size is limited to the size o
internal RAM. The stack is
overlapped on bank 1 because they have same starting address, even
though bank 1 can be used separately by
the SP has to be initialized programmer. In this case,
somewhere else by the programmer, so
that bank 1 and stack would rot be
overlapped.
1.6 Special
FunctionRegister (SFR)
These 128 bytes are reserved for SFRs. From
SFRS are addressed. Table 1.2 address 80H to FFH all
shows the list of SFRs which may be
addressed like internal RAM.
Some of the SFRs marked with asterisk ()
are bit addressable.
Not all
addresses from 80H to FFH are used for SFRs. Some of the addresses
are not defined. In the
instruction, user can either specify the SFR
name or the address of the SFR.
Microcontroller ..
1-12 Basics of
Elect. P-I -S-III
S.Y.B.Sc. (Comp. Sci.) :
opcodes by their functional names, such as
SFRS are named in certain opcode by thei address, such as
referen ced by other
A orTH0, are
OEOH or 8CH.
a
the program must start with
Note that, any address used in accumulator A begins with 0
number, thus address EOH for the SFR
assembler error.
i.e. OEOH, otherwise there will be
internal RAM address
Table 1.2 : SFR names and equivalent
Internal RAM Address
Name Function
(HEX)
Accumulator OEO
A
Arithmetic OFO
B
Addressing external memory 83
DPH
Addressing external memory 82
DPL
Interrupt enable control OA8
IE
OB8
IP Interrupt priority
Input/Output port latch 80
PO
P1 Input/Output port latch 90
SP Stack pointer 81
P1.2
Je (ADO)PO.0
(AD1)PO.1
P1.3
(AD2)P0.2
P1.4
(AD3)PO.2
P1.5
(AD4)PO.4
P1.6
(AD5)PO.5
P1.7 8
(AD6)PO.6
Reset ’ RST (AD7)PO.7
Recevo P3.0(RXD) 10 (Vpp)/EAEaly
8051.
TronsmP3.1(TXD)
initupt P3.2(0NTO)
(PROG) ALEEble
Pr
29 PSEN -
inte oP3.3((NTT) 13 (A15)P2.7
Timer P3.4(TO)14 (A14)P2.6
Time P3.5(T1)15 (A13)P2.5
Fwrile/Read P3.6(WR) (A12)P2.4
RecddataP3.7(RD) (A11)P2.3
ECuSa ceil a XTAL2 (A10)P2.2
oeilosr XTAL1 9 (A9)P2.1
Ground (A8)P2.0
crak 12Mhrg Fig. 1.5 : Pin diagram of 8051
P2 Gives hisher bit
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I-
S-I 1-14
Fig. 1.5 shows pin Basics of Microcontroller
IC, 32diagram
It is a 40 pin of 8051.
each port takes 8 pins are set aside for the 4
pins. The rest of the ports (P0
XTAL2, RST, EA, PSEN and pins are Vcc P3), where
Out of these 8 ALE. GND, XTAL1,
pins, six of them
EA) are used by all (VCC, GND, XTAL1,
in ordermembers of 8051 and XTAL2, RST and
connected to
is from work the system,
8031 families. They must be
microcontroiier
The other two pins
8051 or 8031 family. regardless of
whether the
Below details of these PSEN and ALE are
six mainly used in 8031
Vcc Pin 40
: pins are given: systems.
source is +5 V.provides the supply voltage to the chip.
GND: Pin 20 is ground. The voltage
XTAL1 and XTAL2 : The 8051
external clock to run it. A has on-chip oscillator but
these pins (pin no. 19 crystal oscillator is requires an
and 18). connected
capacitor between
them, of value 30 It also needs the
as shown in Fig. pF. One side of capacitor is between
1.6. connected to ground
30 pF
XTAL1
30 pF XTAL2
Gnd
Fig. 1.6
Speed of 8051 refers to the
connected to XTAL1 and XTAL2. For maximum frequency of oscillator
of oscillator then speed at example, if 12 MHz is frequency
8051 is also 12 MHz.
RST : Pin 9 is the RESET pin. It is
pu'se to this pin, the active high input pin. Upon applying
activities. This is mìcrocontroller will reset and terminates all
also referred as
cause all values in registers to be power-on-reset.
lost.
Activating reset will
S-III 1-15 Basics of Microcontroller ....
S.Y.B.Sc. (Comp. Scl.):Elect. - P-I-
Table L.3: On RESET values of some registers
Value
Reglster
PC 0000.
ACC 0000
B 0000
PSW 0000
SP 0007
Value of PC is reset to 0000H fTrcing microcontroller to fetch the first
instruction rom ROM memory location 0000H. Therefore, we must
place the code at location 0000H.
Fig. 1.7 shows the reset circuit of 8051.
PVoc
10 uf
30 pF EAVPR
11.052 MHz
w
T830 pF
RST
PO.0 Port 0
VPO.1
P0.2
8051 PO
PO4
PO5
Po.6
PO7
Fig. 1.9 : Port 0
This is due to the fact that PO is an open drain configuration. In order
to make it as input port, it must be programmed by writing 1 to all
the pins.
S.Y.B.SC. (Comp. Sci.) : Elect. P-I- S-IM 1-18Baslcs of
Dual role of port 0 : Mlcrocontroller .5
be used as Port Ois also
address bus or data bus.designed as ADo - AD¬,
O
provides both address and data. case of external allows it to
In
order address bus (Ao - A) 8051 has a memor, port
for demultiplexing it. and data bus (Do- DT).multiplexed
ALE lowered
1.8.2 Port 1 signal is used
Read latch bit
Vcc
Internal
FET
Internal bus Pull-up
Write to latch
Pin
Byte
Byte
Address
Address
7F
1F
3Bank 1E Ro
1D SR5
1C
1B
1A
19
18
17
2Bank 16R6:
15 R
14
13 R3
12 R{
11 R1 Byle Bit
10 RO Address Address
OF R7 2F ZR
Bank
1 OE R6 2E 7 7 7 0
OD R5 2D G8
0C R4 2C 6 60
OB R3 2B
OA R2 2A
50
09 29 4F 48
08 RO 28
27 40
07 RT 3E
0Bank 06 R6 26 37 30
05 RE 25 2F28.
04 24
03 23
18
02
10:
01 21 08
00 RO 20 07 00 30
7
Working Registers Bit Addressable
General Purpose
Fig. 1.13 : Internal RAM organization
Thirty-two bytes from address 00H to 1FH that make up 32
registers are organized as four banks of eight registers each. working
1-21 Basics of Microcontroller....
trollerT.Y.B.Sc. (Comp. Sci) : Elect. - P-I- S-II
0 to 3 and are made up
The four register banks are numbered from
of eight registers named RO to R7.
(when its bank is selected)
Each register can be addressed by narme is RO (if bank 3 is currently
of bank 3
or by its RAM address. Thus, RO 3 is selected or not).
selected) oraddress 18H (whether bank
determine which bank of registers is
Bits RSO and RS1 in the PSW
program is running.
currently in use at any time when the
used as general purpose RAM.
Register banks not selected can be
Bank 0 is selected on reset.
area of 16 bytes occupies RAM byte addresses 20H
A bit-addressable addressable bits.
to 2FH, forming a total of 128
addressable bit may be specified by its bit address of 00H to 7FH,
An from 20H to 2FH. Thus, for
may form a byte address
or 8-bits address 29H.
example, bit address 4FH is also bit 7 of byte
only to
Addressable bits are useful when the program need
remember a binary event (switch ON, light OFF etc.).
area, from 30H to 7FH is
A general purpose RAM and above bit
addressable as bytes.
1.9.2 Internal ROM
Eig. 1.14 of
internal data may also be
shows the
connections between storage is not sufficient
FA pin is 8051 and external
connected VCC to indicate that the
to memory.
stored in the on-chip ROM. If it is program code is
is connected to GND. in off-chip
As PC of 8051 is
(external ROM) then EA
16-bit, it is capable of
memory.
PO and P2
addressing 64 k bytes
external
provide the 16-bit address to
PO provides lower
8-bit access external memory.
address A8 - A15. address A0 - A7 and P2
provides higher 8-bit
PO is also used to
PO port is
provide the 8-bit data bus DO - D7.
multiplexed
to demultiplex the port and ALE signal of
As a result to
port when ALE = 1. It is used microcontroller
is used
for the address path.
extract the address from PO pins, PO
74LS373 latch and ALE to enable the latch.
is connected to
PSEN : Another iimportant signal for
enable) PSEN which is connected to 8051 is PSEN
(programme store
program code. It is important to the OE onpin of ROM containing
of 8051 emphasize role of PSEN and EA
when EA is connected to
from external ROM using PSEN GND, the 8051 fetches
signal. (Fig. 1.14 shows opcode
ROM with 8051.) interfacing 8 k
EA P3.7 (RD)
CP3a (WR)
PSEN
P2:7 A12
8051F2.0 2864
ALE A8 A8 (2764)
AD7
PO:7 A7 8kx8
P0:0 74LS373
ADO OGAO A0
D7DO
D7
DO
Fig. 1.14 : Interfacing of 8051 and external
memory
S.Y.B.Sc. (Comp. Sc.) :Elect. - P-I - S-II 1-23 Basics of Microcontroller ...
DO
Fig. 1.15: Interfacing with data ROM
In this case, RD is
connected to OE of memory chip. While chip select
(CE) is connected to simple address
decoding logic. PSEN is not used
as it is a data memory. Again
EA is GND to show that it is
memory. external
S.Y.B.Sc. (Comp. Scl.) : Elect. - P-[- S-II 1-24 Basics of Microcontroller ....
For 8 k bytes memory, 13 address lines are
lower 8 lines are taken from PO by required, out of which
are taken from port 2. demultiplexirg
it' and higher 5-bits
1.10.3 Interfacing External RAM to 8051
MOVX A, @ DPTR is the instruction used to
read external RAM, This
has many applications where
collecting large rumber of bytes data is
required.
" Fig. 1.16 shows the interfacing
external data RAM with 8051. Note
that RD and WR is connected to OE and WE of
RAM. While chip
select (CE) is connected to simple address decoding logic.
8051
P3.7 (RD)
P36 (WR) A15
PSEN
P2.7 A14
EA12A13 A13
P2.0
ALE A8 8k8
AD7 Data
PO:7 RAM
PO.0 74LS373
ADO OcAO A0 D7 DO
D7
DO
" If the memory acçess is for a byte of program code in the ROM. tha
PSEN (Program Store Enable) pin will go low to enable the ROM to
is for a
place a byte of program code on the data bus. If the access
RAM byte, the WR (write) or RD (read) pins will go' loW, enabling data
to flow between the RAM and data bus.
A0-A7 DO-D7
Port 0
Port 2 A8-A15