MC Mod 1
MC Mod 1
MC Mod 1
MODULE - 1
1.1 MICROPROCESSORS V/S MICROCONTROLLERS
Microprocessor Microcontroller
It has many instructions to move data between It has few instructions to move data between
memory and CPU memory and CPU
Less number of pins are multifunctional More number of pins are multifunctional
Single memory map for data and code Separate memory map for data and code
(program) (program)
Access time for memory and IO are more Less access time for built in memory and IO.
More flexible in the design point of view Less flexible since the additional circuits which is
residing inside the microcontroller is fixed for a
particular microcontroller
Large number of instructions with flexible Limited number of instructions with few
addressing modes addressing modes
4
8051-Microcontroller-18EC46 4 Sem ECE
RISC CISC
Only load/store instructions are used to access In additions to load and store instructions,
memory memory access is possible with other
instructions also.
5
8051-Microcontroller-18EC46 4 Sem ECE
Data Program
Data
Memory Memory
Address Bus
Address Bus
It uses single memory space for both It has separate program memory and data
instructions and data. memory
It is not possible to fetch instruction code and Instruction code and data can be fetched
data simultaneously
Execution of instruction takes more machine Execution of instruction takes less machine
cycle cycle
Also known as control flow or control driven Also known as data flow or data driven
computers computers
Simplifies the chip design because of single Chip design is complex due to separate memory
memory space space
6
8051-Microcontroller-18EC46 4 Sem ECE
The program stored in the computer memory in the form of binary numbers is called machine
instructions. The machine language program is called object code.
The assembly language program the programmer enters is called source code. The source code
(assembly language) is translated to object code (machine language) using assembler.
Programs can be written in high level languages such as C, C++ etc. High level language will be
converted to machine language using compiler or interpreter. Compiler reads the entire program
and translate into the object code and then it is executed by the processor. Interpreter takes one
statement of the high level language as input and translate it into object code and then executes.
7
8051-Microcontroller-18EC46 4 Sem ECE
General
A B Purpose I/O
RAM Port 1
I/O
A8-
Port 2 A15
ROM
DPTR
PC
DPH I/O
INT
DPL
Port 3 CNTR
SERIAL
RD/WR
E IE
ALE System General IP
Timing purpose PCON
PSEN
area SBUF
XTAL1 System SCON
interrupt
XTAL2 Bit addressible TCON
timers area TMOD
RESET Register Bank 3 TL0
Data
buffers Register Bank 2 TH0
Register Bank 1 TL1
Memory
Register Bank 0 TH1
VCC
GND control SFR and
General Purpose RAM
8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If the
program size is more than 4 K Bytes 8051 will fetch the code automatically from external
memory.
Accumulator is an 8 bit register widely used for all arithmetic and logical operations.
Accumulator is also used to transfer data between external memory. B register is used along
with Accumulator for multiplication and division. A and B registers together is also called
MATH registers.
8
8051-Microcontroller-18EC46 4 Sem ECE
PSW (Program Status Word). This is an 8 bit register which contains the arithmetic status of
ALU and the bank select bits of register banks.
CY AC F0 RS1 RS0 OV - P
CY - carry flag
AC - auxiliary carry flag
F0 - available to the user for general purpose
RS1,RS0 - register bank select bits
OV - overflow
P - parity
Stack Pointer (SP) – it contains the address of the data item on the top of the stack. Stack
may reside anywhere on the internal RAM. On reset, SP is initialized to 07 so that the default
stack will start from address 08 onwards.
Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data pointer lower byte). This
is a 16 bit register which is used to furnish address information for internal and external
program memory and for external data memory.
Program Counter (PC) – 16 bit PC contains the address of next instruction to be executed.
On reset PC will set to 0000. After fetching every instruction PC will increment by one.
Pinout Description
Pins 1-8 PORT 1. Each of these pins can be configured as an input or an output.
Pin 9 RESET. A logic one on this pin disables the microcontroller and clears the contents of
most registers. In other words, the positive voltage on this pin resets the
microcontroller. By applying logic zero to this pin, the program starts execution from
the beginning.
Pins10-17 PORT 3. Similar to port 1, each of these pins can serve as general input or output.
Besides, all of them have alternative functions
9
8051-Microcontroller-18EC46 4 Sem ECE
10
8051-Microcontroller-18EC46 4 Sem ECE
BANK 3
R4 1C 2F
7F 78 7F
R3 1B
2E
77 70 7E
R2 1A
R1 19 2D
6F 68 .
R0 18 2C
67 60 .
R7 17
2B
5F 58 .
R6 16
R5 15
57 50 .
BANK 2
2A
R4 14 29
4F 48 .
R3 13
28
47 40 .
R2 12
R1 11 27
3F 38 .
R0 10 26
37 30 .
R7 0F 2F 28
R6 0E 25 32
27 20
R5 0D 24 31
BANK 1
R4 0C 1F 18
R3 0B
23 30
17 10
R2 0A 22
R1 09 21 0F 08
R0 08
R7 07
20 07 00 General purpose memory
R6 06
R5 05
BANK 0
Working Registers
Register Banks: 00h to 1Fh. The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1,
R2, R3, R4, R5, R6, and R7). There are four such register banks. Selection of register bank can be
done through RS1,RS0 bits of PSW. On reset, the default Register Bank 0 will be selected.
Bit Addressable RAM: 20h to 2Fh . The 8051 supports a special feature which allows access to bit
variables. This is where individual memory bits in Internal RAM can be set or cleared. In all there
are 128 bits numbered 00h to 7Fh. Being bit variables any one variable can have a value 0 or 1. A bit
variable can be set with a command such as SETB and cleared with a command such as CLR.
Example instructions are:
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
Note, bit 25h is actually bit 5 of Internal RAM location 24h.
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between 20h and 2Fh.
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are available for
general-purpose data storage, user should take care while using the memory location from 00 -2Fh
11
8051-Microcontroller-18EC46 4 Sem ECE
since these locations are also the default register space, stack space, and bit addressable space. It is
a good practice to use general purpose memory from 30 – 7Fh. The general purpose RAM can be
accessed using direct or indirect addressing modes.
Number of address lines required for 16 Kbyte memory is 14 lines and that of 32Kbytes of
memory is 15 lines.
PSEN PSEN
A14 A14
A13 A13
A13
A12 A12
A12
… 32 Kbyte
..
A9 .
A8
A8 WE
. RAM
WR 16 Kbyte .
RD OE
A3
AL A7 RAM
LE A2
..
8051 A0-A7 A0-A7 A1
A1
A0
A0
LOWER BYTE
EA ADDRESS
GND [AD0 – AD7] DAT DAT
A A
O/P O/P
AD0
- DATA BUS [AD0 – AD7]
AD7
The lower order address and data bus are multiplexed. De-multiplexing is done by the latch.
Initially the address will appear in the bus and this latched at the output of latch using ALE signal.
The output of the latch is directly connected to the lower byte address lines of the memory. Later
data will be available in this bus. Still the latch output is address it self. The higher byte of address
bus is directly connected to the memory. The number of lines connected depends on the memory
size.
The RD and WR (both active low) signals are connected to RAM for reading and writing the data.
PSEN of microcontroller is connected to the output enable of the ROM to read the data from the
memory.
EA (active low) pin is always grounded if we use only external memory. Otherwise, once the
program size exceeds internal memory the microcontroller will automatically switch to external
memory.
1.8 STACK
A stack is a last in first out memory. In 8051 internal RAM space can be used as stack. The address
of the stack is contained in a register called stack pointer. Instructions PUSH and POP are used for
stack operations. When a data is to be placed on the stack, the stack pointer increments before
storing the data on the stack so that the stack grows up as data is stored (pre-increment). As the
data is retrieved from the stack the byte is read from the stack, and then SP decrements to point the
next available byte of stored data (post decrement). The stack pointer is set to 07 when the 8051
resets. So that default stack memory starts from address location 08 onwards (to avoid overwriting
the default register bank ie., bank 0).
13
ON
RO61 Archdteclae
AitheoHe
Qnd
PSW YUneton Ao-Ay
Rugiahors -Do-DY
RoM
bit Data
and Adds b P
DPTR RoM
Pe DPH
DPL
16BPE Addus Bus P
e-A
Buteit netton
EASyem Addses Rogi&kons
ALE imina Pogsher
e
LP
-Inerrup
Countus
PeEN&ustum Bank 8 PCON
28UF
&eal Sata
Dnleroupt Roaister &ON
RD-e
XFRL2 Bok
TCON
ReSET Regigi TMOD
Dalo Bueas TLO
VoL Hemut Raysla THO
GND Conhot Bamk o TLI
TA
Onema) RM Srucie
Cural precuaing oatt Cecr) o
The 8o51 Pu toss o 8-bE orifhmetfe sd logè Unft ui
asoRotad Sugistuns ike A, B, PW
8P 16-bPt program 0ouotus
ond 9ata
pöula heglzlers (p PTR)
The gor1' AL Can puses asi lhmatic
2 ogfe
Vortables.The atthmuetfe unt fan chons @n ebt
mu Htpfcalfon and divison paskms 0olditoo, &ubhra ion
Tho Joie Uait on
penfom JogYca operorltons Lueh
Ex-OR, uoell au Rolata, cleor ond
a8 g Anp 0R
ompemant
nal ReM (o) 0ala Memd
Byta Add r
R4
RB
R
R5
Bank1's R
R3
R Bit
RD
2F 1
Addseks
OE 2E
R5
Bank 8c RU
RS
2c 7
9B
A 9A 57
04
98
3F
07 Db 3
DS 25|PF
Bank bu Ru
R3
24197
2,3 F
R 90
0 R1 2 0 O8
20
suoently In se
Nomtrd te) lamy 8lag dudbanty enty, pavily tlag Ond Overloo
Ro, ond Re, ave sed Jo chamge he banlk tagists
The Dho0 unUsed btk Cald
ae ues-dkinale lags
cy AcFo Re Rso Dv-P
POw7 9é Psw Psw-4 Pow 3PSw. 2 PSP. b
Cagg Co)
Ror pesRoming aikfnebte and ogie Oponertton p-hume sa
oyeaom e MSB (D7-Be than Cy-), otumoie C4-0
Camyag ey Can. be Aet o 4 04 0 dreetly by on Inafructton
18n-IF
FO Avalabe or uer or urnl popesc
veFlbu Ca k d to daleel t tn Re Atpl
ged mumhet puartan edee-org
OV lag Stt Jb 4 i fhe othalocing Jupp Conditong
Occums PODN
PSW
hones o Coveyoom fo7 bu No Camy ou b A (oy-0)
5-hae ka Cormyou aom O CCy-) but Nlo Cprvy fon (Sg lb Sy bie
Oneton
-128 80 h O+6 E 100 0 0000
&+15-23-1
+(26
TO
vmDverPlod
OV-| betause Coyvyeutom 7 No Covog rom 1O b
OED
Posl swong be eausi Cpo Sho08 Onsus. as 12by(hezad of -BD
The&o51 9harations hat do mat uge dhe totema 128 -by RaM
Oddsse 0 to h
The opuralHons oeo5T 0Ae done by a grnup speife ntenap
9ugisters 20ch eald a8pi) undtbo Reafster"gF
The 3FR May be attesed by ther names r by using oddregex
Inerwcaions
PUSH dfreo ad drw
YOn cPtoo: Pugk Dne 8touke
DAChphtonTke k oinl thCtd by vne Te Conlenk
b he ihdRat varhabte k thon opleà ilotte
intumal
RPir docalion oddresed by -he tack
poinlz
lag alhad d: None
Bates:2
eydus 3
erelhn:(3p) (p)+)
Siect Oddoes ing mode
th's &oppos& bol
Istuctron
he Inshruetbn uch as puSH O pucH R
A a
The
slegal behoctions
:PusA OS0h
wine Foh t e
Rnr addres belonging o Sogis
3) Pusay O8h
Bank4
whne 0gh s the Rpm addres t Rg
POP 9keel addrug
POp haom he &atk (ep) 6
oncoo
4 to by Sack pointer
Ouunphion:his Cepizs -ha byl pointi
addek tndeata4 and deoremet
he ocaton whee doeet
by 4.
gack poin Lu
lags a i l d None
Bys&
Cycles
purioton, Sp-p)- mod
dlseuk oddsesing
Not Thlehdrutiem &uppos bnly
R20 ilegl Indhuate
The Inshruettong &uoh as Pop A o Pop
Pop DEDh
uhune E0h k fhe Rem addvas belbnging fo ugtete A
Pop 08h
uene 0gh &he RarL addees e Re ef Bomkd.
SIACk pp&atk uheng oan arta Intma PeM Used by the
CPU to &love and etrive data quickly
laclee huglsfos used fo atass the Stack callec h.
Clate pönter (sp) ugicbur
The Stackk pointer a g.bit agka used by tt eo5t
held inBema Rom oddrus that k Called he Top o
an
tha &lnch
when &o51ReOE, the &ack Ponl sot 0th
to
Mov RaBok
PuSH
Acume habt Jni HallyBank o 8oleTed ond Sp -DTh
Beve Feaurion. ARter neutton
OBA OBh
04A
09
08h 30 9p 08h
Sp-D
Spa-Ot+1
Spp08 h
aHov Ra 80h
Nev R3 lhoh
Mev Ry, hlh.
PuCH 2
PUgH3
PUSH
Agsume B o gledted and 2hack potnlai has nittally Oth
Ereaurho
Perove Exeour-tton AR PUSH 2 PUS 93 PuSH
OBh 0Bh OBh
0Bh OAh 0A 0Ph
0AA ogh 09hAo 09h Ao
04% 08h20 oaR20 0gA 30
ogh P-0Ah
08h p-09h
Pldhus (oe)
olch Anable 80bl Ttm ing
numbene.d
8o51 O machine ycle Cbnsisk b 6 atales
ycle6&tates
machne
(te) 1ne
Inahraktops
lhua micsocomhrofls ReE, Is'ruukutbtons axe alehed&
utod by Bniesoontrolbr automatieally begintng. oik The
Instruetionoealed ab RoM menor addreK 000h
tme huded to nwuli an hstruCtoo Caleulaled Q
bina x2d
3 h Oystel aqumy
yole, LEPRere 08 ttoo ALE Pulues per mathine Cud,
These PLe pulses axe Used es exenal mumaty atas
Yo.o
Po.1
Po2
Po.3
Poay
Po.5
Po.b
PoM
bbdenssr
.Ag
Pa A1s
Pot a 9W r9 a As A15
Rpwe
RD, We Pins 034 us un a ptn Rht has 'b be aces
skn RD-0,a daln bp Cam be
Aeod rn a
Ra Ipoattn
ufun NR0, a data be tan be wvten tnlö a Rom loot
Do
RAM
Ao
A
ALS
ADDER
RD PAGE
Bins
WR 0E
JnTana RoML
() uwtin E==0 ( Glo te &o51 atchas 0ddiuso00ok. through
fFFFh 0Re divgcted o nkenal Ror/EPRoM
)Poto %a maltplhcu addros Aata bus
ukun ALE 1 Poat 0 tentaing addaus bus Ao-AY)
ufn ALE 0, poxto ntains dala buu )
Thotu L8878 ued lo dmulliplex addas and addrus b
e t LS373 uill be onabled hen. ALE high, So oulput
YuL3373 Aas eusel ondar oddses -Ay s hoO
A RO/ EPRO
Ao)
ALET CAk Addr
A&
Pa o75auA p
Pronaum oiob
PseN DE
Embeddrd Micantenholea
uhiom a Compleli haadueaLe eqnlne4_to aun
apaalfeulag oppliattêopaovfdad on he
amfoa.cntrdlea chip, fl afezacd do as dn
Dmmlbedded ioaecenhiles embtdded mferocomhellor
my Aegaiae pouoea Suiet Cloe amd elock
2mbedded mienocmtelloas lommuntcalbTwti
oxtanal deufees om lz dtalal 2o phs
KTPL KTh
Grbedked paaalle4 o
Me
MM
Revel
Wee o i p t Cenbv
&d Piea
loinlzn Conh
Ermbedkced kyslem
fr es on elestronelefeetto- nmethenical &yseo
deslgned o pesfatm a Apeiie netêo md k a
Cenbtoetteo befh handwaie oisl fismaate (kae)
SURYA Gold
Date Page
InitndutktD o ES
Ccer tteface)
Embealdedhmpual
mpu &eRteas
Outpinf
Vaiabtex PHorcduane
Vaviables
nk to oikeu systm
udpa ntaecing
ad tasb.aniin
hg)matt amera
Aatniattb apiplk Cau ianDheuutng ocop
ttome 0ppllamtes uOashsmq mAlhina
Table 1.7.2
the
external 32 K EPROM and 32 K
RAM with
Example 1.7.1 Discuss interfacing of
microcontroller. Draw diagram and explain.
ne
address lines (Ag
-
A14).
Solution: To address 32 K EPROM and RAM needs 15 1s lowW
for selection of EPROM or RAM.
When A15
remaining address line Ag is used
EPROM is selected and when Ais is high RAM is selected.
RD
ALE
X P2.0
P2.7
32A
PO.0 K Ag-A15
Vcc PO.7 C
C (EPROM)
RST 8051 Do-D7
PSEN
PSEN -WR
EA WR
RD
ALE
Reset
CS CS
A15-
Fig. 1.7.8
Memory Map
femory As Au A3 Az A A0 Ag Ag Ay As As A4 Ag Az A An Address
Men *******************************.******ass
1000 0 0 0 0 00 0 0 0
0 0 8000H
********* * .... ********************* *********************************
*** -****** ******** ****.******.. (Start)
RAM
RAM 11 1 11 1 11
111 1 1 1 1 FEPH
*
..
***** ******* ******
******************************************* ******** ************* .......
. .......
(End)
0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 000OH
*******'**** ****"**********i . inottspe ************************************"**********'*****'********r********** (Start)
ivdrveidiii
EPROM
0 1 11 1 1 1 11 1 1 1 1 1 1 1 TFFFH
End)
wwwwwweeee ww *
1.7.2 An An 8051
51 based system requires external memory of four 4 kbytes of SRAM
Example 1.7.2
eMch and two chips of EPROM of SIze 2 kbytes. The EPROM starts at address 2000H
SRAM address map follows EPROM map. Give the completeinterface
AP2.0
P2.7
PO.0
PO.7 Ahns
B051
PSEN PSEN
WR
RD
ALE
Reset
EPROM 0 EPROM 1
RAM 1
RAM 2
A1 -REPROM
RAM3
RAM 4
RAM 1 -EPROM 1
RAM 2
RAM 3
IND
-RAM 4
Flg. 1.7.9
BO
Microcontroller 1-28
Address
wwwwwwwwwwwwMwwwwwwwww.wwwwwwwwwwwwww
A Ao
wwwwwwwwwwwwwwwwww
As Ag Az
Ao Ag Ag Az A^ As 2000H
A15 A14 A3 Az A 0 0 0
00
EPROMO
0 0 1 0 0 0 0 0 0 0 0 27FFH
1 1
11 1
1 11 1 2800H
0 0 1 0 0 1 1
. .
*********"****** 00 EPROM1
wwww ******** *
0 0
0 0 10 10 0 0 0 0 00 2FFFH
1 1
11 1 **************
0 0 1 0 1 11 1 1 1 1 3000H
0 0 0 RAMO
0 0 0 0
0 1 1 0 0 0 0 0 3FFFH
0 1 1 1 1 1 1 1 1 1 1 1
*
1 11 r4000H
********* ******
11
*
0 0 0 0 RAM1
0 1 0 0 0 0 0 0
0 0 0 0
1 4FFFH
4FFFH
1 1 1 1
1 1 1 1 wwwe
0 0 0 1 11
********'***************** ***
* *ss******************************
***"***
0 0 0 5000H
0 1 0 1 0 0 0 0 0 0 0 0 0 RAM2
1 5FFFH
1
1 0 1 1 1 1 1 1 1 1 1 1
0 0 0 0 00 0 0 6000H
0 1 1 0 0 00 0 RAM3
1 1 1 1 1 6FFFH
0 1 1 0 1 1 11 1 1 1 oONOOtooo000ooO0o000000000o0000oooc
oooociotooo
Example 1.7.4 Design a microcontroller system using 8051 microcontroller, 4 kbytes of ROM
and 8 kbytes of RAM interface the external memory such that the starting adäress o
VTU: Jan.-19,
Marks 8
ROM is 1000H and RAM is CO0OH
address
Solution: To address 4 kbytes ROM we need 12 address lines (A -A1) and
to
be used
8 kbytes RAM we need 13 address lines (Ag -A 12) remaining address lines
can
2 P2.0
P2.7
PO.0 AgA15
VcC PO.7
c
RST 8051 Dg-D7
PSEN
PSEN WR
WR
EA RD
RD ALE
ALE
Reset
CS
A15 A15
A4 A4 D
A13
A3
An2-
Fig. 1.7.11
1-31
Microcontroler
8051 licrocontroller
vwwww.dvvvev
Memory Map
****
wwe
eevveemern
0 0 0 10 0 0 00 0 0 0 0 0 0 0
ROM 1000H
0 0 0 1 1 1 1 1 1 1 11 1 1 11FFFH
******
RAM 1 1 0 00 0 0 0 0 0 0 0 0 0 00 CO00H
11 0 1 1 1 1 1 1 1 1 1 1 1 1
* *** vw.weedivii
1 D FFFH
Review Questions
1. Explain interfacing and timing diagrams for external program memory interfacing.
2. Explain interfacing and timing diagrams for external data memory interfacing.
3. Interface the external ROM and RAM to 8051. Explain how to access them.
VTU Dec.-11, June-13, July-18, Marks 6
4. Interface 8051 to 8K external RAM and 32K external ROM and explain how 8051 access them ?
VTU-: Jan.-16, Marks 8
5. With the help of diagram, explain how to
a
interface 8KB EPROM and 8KB RAM, to 8051
Microcontroller. VTU: June-16, Marks 6
6. Show how to interface external PROM and external RAM to 8051. Explain how 8051 access them.
VTU Jan.-18, Marks 10
Table 14-1: Powers of
2
2
10 1K
11 2K
12 4K
13 8K
14 16K
15 32K
16 64K
17 128K
18 256K
19 512K
20 1M
21 2M
22 4M
23 8M
24 16M
25 32M
64M
26
128M
27
Example 14-1
How many address lines are required for accessing the data in the following memory chips, while data is zed
as bytes in the first two cases and as nibbles in the last case?
Also find the memory capacity of each chip and the organization.
(a) 512 bytes RAM (b) 8 K RAM (c) 8 K ROM
Solution:
The memory in the first two cases is organized as bytes.
(a) 512 =2". So 9 address lines are required.
Memory capacity =512 x 8 = 4096bits.
The organization is 512 x 8.