INTRODUCTION TO MICROCONTROLLERS(8051) ARCHITECTURE,INSTRUCTION SET ,ADDRESSING MODES

Ramya388567 58 views 104 slides Jun 21, 2024
Slide 1
Slide 1 of 104
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104

About This Presentation

Meeting the computing needs of the task at hand efficiently and cost effectively
Speed
Packaging
Power consumption
The amount of RAM and ROM on chip
The number of I/O pins and the timer on chip
How easy to upgrade to higher performance or lower power-consumption versions
Cost per unitIntel introduce...


Slide Content

Introduction
to
Microcontrollers
8051 Microcontroller 1

The necessary tools for a
microprocessor/controller
1.CPU: Central Processing Unit
2.I/O: Input /Output
3.Bus: Address bus & Data bus
4.Memory: RAM & ROM
5.Timer
6.Interrupt
7.Serial Port
8.Parallel Port
8051 Microcontroller 2

Internal Block Diagram of CPU
8051 Microcontroller 3

Address and Data Bus
8051 Microcontroller 4

Microcontroller
•A smaller computer
•On-chip RAM, ROM, I/O ports...
•Example: Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC
8051 Microcontroller 5

Microprocessors
•CPU for Computers
•No RAM, ROM, I/O on CPU chip itself
•Example: Intel's x86, Motorola’s 680x0
8051 Microcontroller 6

Microprocessor vs. Microcontroller
Microprocessor
•CPU is stand-alone, RAM,
ROM, I/O, timer are separate
•Designer can decide on the
amount of ROM, RAM and I/O
ports.
•Expensive
•Versatility
•General-purpose
Microcontroller
•CPU, RAM, ROM, I/O and
timer are all on a single chip
•Fix amount of on-chip ROM,
RAM, I/O ports
•For applications in which cost,
power and space are critical
•Not Expensive
•Single-purpose
8051 Microcontroller 7

Microcontrollers for Embedded Systems
•Home
–Appliances,intercom,telephones,securitysystems,garagedooropeners,
answeringmachines,faxmachines,homecomputers,TVs,cableTVtuner,
VCR,camcorder,remotecontrols,videogames,cellularphones,musical
instruments,sewingmachines,lightingcontrol,paging,camera,pinball
machines,toys,exerciseequipmentetc.
•Office
–Telephones,computers,securitysystems,faxmachines,microwave,copier,
laserprinter,colorprinter,pagingetc.
•Auto
–Tripcomputer,enginecontrol,airbag,ABS,instrumentation,security
system,transmissioncontrol,entertainment,climatecontrol,cellular
phone,keylessentry
8051 Microcontroller 8

Choosing a Microcontroller
•8-bitmicrocontrollers
–Motorola’s6811
–Intel’s8051
–Zilog’sZ8
–Microchip’sPIC
•Therearealso16-bitand32-bitmicrocontrollers
madebyvariouschipmakers
8051 Microcontroller 9

Criteria for Choosing a Microcontroller
•Meetingthecomputingneedsofthetaskathand
efficientlyandcosteffectively
–Speed
–Packaging
–Powerconsumption
–TheamountofRAMandROMonchip
–ThenumberofI/Opinsandthetimeronchip
–Howeasytoupgradetohigherperformanceorlowerpower-
consumptionversions
–Costperunit
8051 Microcontroller 10

Criteria for Choosing a Microcontroller
•Availabilityofsoftwaredevelopmenttools,suchas
compilers,assemblers,anddebuggers
•Wideavailabilityandreliablesourcesofthe
microcontroller
–The8051familyhasthelargestnumberofdiversified(multiple
source)suppliers
•Intel(original)
•Atmel
•Philips/Signetics
•AMD
•Infineon(formerlySiemens)
•Matra
•DallasSemiconductor/Maxim
8051 Microcontroller 11

8051 Microcontroller
•Intelintroduced8051,referredasMCS-51,in
1981.
•The8051isan8-bitprocessor
–TheCPUcanworkononly8bitsofdataatatime
•The8051becamewidelypopularafterallowing
othermanufacturestomakeandmarketany
flavorofthe8051.
8051 Microcontroller 12

8051 Family
•The 8051 is a subset of the 8052
•The 8031 is a ROM-less 8051
–Add external ROM to it
–You lose two ports, and leave only 2 ports for I/O operations
8051 Microcontroller 13

8051 Features
•64KB Program Memory address space
•64KB Data Memory address space
•4K bytes of on-chip Program Memory
•128 bytes of on-chip Data RAM
•32 bidirectional and individually addressable I/0 lines
•Two 16-bit timer/counters
•Full duplex UART
•6-source/5-vector interrupt structure with two priority
levels
•On-chip clock oscillator
8051 Microcontroller 14

Pin Diagram of the 8051
8051 Microcontroller 15

Interrupt
Control
CPU
4K
ROM
128 B
RAM
OSC
Bus
Control
4 I/O Ports
Serial
Port
Timer 1
Timer 0
General Block Diagram of 8051
TXDRXD
P0P1P2P3
8051 Microcontroller 16

Detailed Block Diagram
8051 Microcontroller 17

8051
Memory Space
8051 Microcontroller 18

8051 Memory Structure
External
INT
128
SFR
External
Program Memory Data Memory
64K
EA = 0
EA = 1
4K
64K
8051 Microcontroller 19

128 Byte RAM
•Thereare128bytesofRAMinthe8051.
–Assignedaddresses00to7FH
•The128bytesaredividedinto3different
groupsasfollows:
1.Atotalof32bytesfromlocations00to1F
hexaresetasideforregisterbanksandthe
stack.
2.Atotalof16bytesfromlocations20Hto2FH
aresetasideforbit-addressableread/write
memory.
3.Atotalof80bytesfromlocations30Hto7FH
areusedforreadandwritestorage,called
scratchpad.
8051 Microcontroller 20
128 BYTE
INTERNAL RAM
Register Banks
RegBank 0
RegBank 1
RegBank 2
RegBank 3
BIT Addressable
Area
General Purpose
Area

8051 Programming Model
8051 Microcontroller 21

8051 RAM with addresses
8051 Microcontroller 22

8051 Register Banks with address
8051 Microcontroller 23

Bit Addressable & Byte Addressable
8051 Microcontroller 24

Bit Addressable Programming
•Example: Find out to which by each of the following bits
belongs. Give the address of the RAM byte in hex
(a) SETB 42H, (b) CLR 67H, (c) CLR 0FH (d) SETB 28H, (e) CLR 12, (f) SETB 05
8051 Microcontroller 25

Special Function Registers [SFR]
8051 Microcontroller 26

Internal RAM Structure
Direct &
Indirect
Addressing
Direct
Addressing
Only
SFR [ Special Function
Registers]
128 Byte Internal RAM
8051 Microcontroller 27

Program Status Word [PSW]
8051 Microcontroller 28
C AC F0 RS1 RS0 OV --P
Register Bank Select
Carry
Auxiliary Carry
User Flag 0
Parity
Reserved for future use
Overflow

Flags in PSW
8051 Microcontroller 29

Flags in PSW
8051 Microcontroller 30

Flags in PSW
8051 Microcontroller 31

Example for Overflow flag
8051 Microcontroller 32

8051 instructions that affects flag
8051 Microcontroller 33

8051 Stack
•ThestackisasectionofRAMusedbytheCPUtostore
informationtemporarily.
–Thisinformationcouldbedataoranaddress
•TheregisterusedtoaccessthestackiscalledtheSP
(stackpointer)register
–Thestackpointerinthe8051isonly8bitwide,whichmeans
thatitcantakevalueof00toFFH
–Whenthe8051ispoweredup,theSPregistercontainsvalue
07
–RAMlocation08isthefirstlocationbeginusedforthestackby
the8051
8051 Microcontroller 34

8051 Stack
•ThestoringofaCPUregisterinthestackiscalledaPUSH
–SPispointingtothelastusedlocationofthestack
–Aswepushdataontothestack,theSPisincrementedbyone
–Thisisdifferentfrommanymicroprocessors
•LoadingthecontentsofthestackbackintoaCPU
registeriscalledaPOP
–Witheverypop,thetopbyteofthestackiscopiedtothe
registerspecifiedbytheinstructionandthestackpointeris
decrementedonce
8051 Microcontroller 35

Pushing onto Stack
8051 Microcontroller 36

Popping from Stack
8051 Microcontroller 37

Single bit Instructions
8051 Microcontroller 38

8051 Addressing Modes
•The CPU can access data in various ways, which are
called addressing modes
1.Immediate
2.Register
3.Direct
4.Register indirect
5.External Direct
8051 Microcontroller 39

Immediate Addressing Mode
•Thesourceoperandisaconstant.
•Theimmediatedatamustbeprecededbythepoundsign,“#”
•Canloadinformationintoanyregisters,including16-bitDPTR
register
–DPTRcanalsobeaccessedastwo8-bitregisters,thehighbyteDPHand
lowbyteDPL
8051 Microcontroller 40

Register Addressing Mode
•Use registers to hold the data to be manipulated.
8051 Microcontroller 41
•The source and destination registers must match in size.
MOV DPTR,A will give an error
•The movement of data between Rn registers is not allowed
MOV R4,R7 is invalid

Direct Addressing Mode
•It is most often used the direct addressing mode to
access RAM locations 30 –7FH.
•The entire 128 bytes of RAM can be accessed.
•Contrast this with immediate addressing mode, there is
no “#” sign in the operand.
8051 Microcontroller 42

SFR Registers & their Addresses
MOV0E0H,#55H;is the same as
MOVA,#55H ;which means load 55H into A (A=55H)
MOV0F0H,#25H;is the same as
MOV B,#25H ;which means load 25H into B (B=25H)
MOV0E0H,R2 ;is the same as
MOVA,R2 ;which means copy R2 into A
MOV0F0H,R0 ;is the same as
MOVB,R0 ;which means copy R0 into B
8051 Microcontroller 43

SFR Addresses ( 1 of 2 )
8051 Microcontroller 44

SFR Addresses ( 2 of 2 )
8051 Microcontroller 45

Example
8051 Microcontroller 46

Stack and Direct Addressing Mode
•Only direct addressing mode is allowed for pushing or
popping the stack.
•PUSH Ais invalid.
•Pushing the accumulator onto the stack must be coded
as PUSH 0E0H.
8051 Microcontroller 47

Register Indirect Addressing Mode
•Aregisterisusedasapointertothedata.
•OnlyregisterR0andR1areusedforthispurpose.
•R2–R7cannotbeusedtoholdtheaddressofan
operandlocatedinRAM.
•WhenR0andR1holdtheaddressesofRAMlocations,
theymustbeprecededbythe“@”sign.
8051 Microcontroller 48

Register Indirect Addressing Mode
•Theadvantageisthatitmakesaccessingdatadynamic
ratherthanstaticasindirectaddressingmode.
•Loopingisnotpossibleindirectaddressingmode.
•Write a program to clear 16 RAM locations starting at
RAM address 60H.
8051 Microcontroller 49

Index Addressing Mode
For example
1.MOVC A, @ A + DPTR ( This instruction moves the
data from the memory to accumulator; whose
address is computed by adding the contents of
accumulator and DPTR)
NMAMIT 8051 Microcontroller 50

External Direct
•External Memory is accessed.
•There are only two commands that use External Direct
addressing mode:
–MOVX A, @DPTR
MOVX @DPTR, A
•DPTR must first be loaded with the address of external
memory.
8051 Microcontroller 51

Register Indirect Addressing Mode
•Write a program to copy the value 55H into RAM memory locations 40H
to 41H using (a) direct addressing mode, (b) register indirect addressing
mode without a loop, and (c) with a loop.
8051 Microcontroller 52

8051 Instruction Set
•DATA TRANSFER GROUP
•ARITHMETIC GROUP
•LOGICAL GROUP
•CONTROL TRANSFER GROUP
8051 Microcontroller 53

Structure of Assembly Language
ORG 0H ;start (origin) at location 0
MOV R5,#25H;load 25H into R5
MOV R7,#34H;load 34H into R7
MOV A,#0 ;load 0 into A
ADD A,R5 ;add contents of R5 to A
;now A = A + R5
ADD A,R7 ;add contents of R7 to A
;now A = A + R7
ADD A,#12H ;add to A value 12H
;now A = A + 12H
HERE: SJMP HERE ;stay in this loop
END ;end of asm source file
8051 Microcontroller 54

Data Types & Directives
ORG 500H
DATA1: DB 28 ;DECIMAL (1C in Hex)
DATA2: DB 00110101B ;BINARY (35 in Hex)
DATA3: DB 39H ;HEX
ORG 510H
DATA4: DB “2591” ; ASCII NUMBERS
ORG 518H
DATA6: DB “My name is Joe” ;ASCII CHARACTERS
8051 Microcontroller 55

Data Transfer Instructions
MNEMONIC DESCRIPTION BYTES
•MOV A, Rn (A) (Rn) 1
•MOVA,Rx (A)(Rx) 2
•MOVA,@Ri (A)(Ri) 1
•MOVA,#X (A)Data 2
•MOVRn,A (Rn)(A) 1
•MOVRn,#X (Rn)Data 2
•MOVRx,A (Rx)(A) 2
8051 Microcontroller 56

Data Transfer Instructions
MNEMONIC DESCRIPTION BYTES
•MOV Rx, @ Ri (Rx) (Ri) 2
•MOV Rx, # X (Rx) Data 3
•MOV @ Ri, A (Ri) (A) 1
•MOV @ Ri, Rx (Ri) (Rx) 2
•MOV @ Ri, #X (Ri) Data 2
•MOV DPTR, #X (DPTR) Data 3
•MOVC A @ A+DPTR (A) (A+DPTR) 1
•MOVC A@A+PC (A) (A+PC) 1
8051 Microcontroller 57

Data Transfer Instructions
MNEMONIC DESCRIPTION BYTES
•MOVX A, @ DPTR (A) (DPTR) 1
•MOVX @Ri, A (Ri) (A) 1
•MOVX A,@ Ri A (Ri) 1
•XCH A, Rn (A) (Rn) 1
•XCH A, Rx (A) (Rx) 2
•XCH A, @Ri (A) (Ri) 1
•MOVX @DPTR, A (DPTR) (A) 1
•PUSH Rx Push directly addressed 2
Rx register on stack
•POP Rx (A) (Rx) 2
8051 Microcontroller 58

Data Transfer Instructions
•XCHD Exchange 4 lower bits in 1
accumulator with indirectly
addressed register
8051 Microcontroller 59

MOV Instruction
•MOV destination, source ; copy source to destination.
•MOV A,#55H;load value 55H into reg. A
MOV R0,A;copy contents of A into R0
;(now A=R0=55H)
MOV R1,A;copy contents of A into R1
;(now A=R0=R1=55H)
MOV R2,A;copy contents of A into R2
;(now A=R0=R1=R2=55H)
MOV R3,#95H;load value 95H into R3
;(now R3=95H)
MOV A,R3;copy contents of R3 into A
;now A=R3=95H
8051 Microcontroller 60

Arithmetic Instructions
•These instructions perform several basic operations.
After execution, the result is stored in the first operand.
•8 bit addition, subtraction, multiplication, increment-
decrement instructions can be performed.
MNEMONICS DESCRIPTION BYTE
•ADD A, Rn A = A + Rn 1
•ADD A, @ Ri A = A+ Ri 1
•ADD A, # X A = A + Byte 2
•ADDC A, Rn A = A + Rn + C 1
8051 Microcontroller 61

Arithmetic Instruction
•ADDC A, @ Ri A = A + Ri + C 1
•ADDC A, # X A = A + Byte + C 2
•SUBB A, Rn A = A –Rn –1 1
•SUBB A, @ Ri A = A –Ri –1 1
•SUBB A, # X A = A –Byte –1 2
•INC A A = A + 1 1
•INC Rn Rn = Rn + 1 1
•INC @ Ri Ri = Ri + 1 1
8051 Microcontroller 62

Arithmetic Instruction
•DEC A A= A –1 1
•DEC Rn Rn= Rn –1 1
•DEC @ Ri Ri= Ri –1 1
•INC DPTR DPTR= DPTR + 1 1
•MUL AB B:A = A * B 1
•DIV AB A = [A/B] 1
•DA A Decimal adjustment of 1
accumulator according
to BCD code
8051 Microcontroller 63

ADD Instruction
•ADD A, source;ADD the source operand to the
accumulator
•MOV A, #25H ;load 25H into A
MOV R2,#34H ;load 34H into R2
ADD A,R2 ;add R2 to accumulator
;(A = A + R2)
8051 Microcontroller 64

8051 Microcontroller 65

Subtraction
8051 Microcontroller 66

8051 Microcontroller 67

8051 Microcontroller 68

DECIMAL ADJUST for Addition
8051 Microcontroller 69

ASCII TO BCD and BCD TO ASCII
8051 Microcontroller 70

Multiplication of Unsigned Numbers
MULAB; A B, place 16-bit result in B and A
MOV A,#25H;load 25H to reg. A
MOV B,#65H;load 65H in reg. B
MUL AB ;25H * 65H = E99 where B = 0EH and A = 99H
Table 6-1:Unsigned Multiplication Summary (MUL AB)
MultiplicationOperand 1 Operand 2 Result
byte byte A B A=low byte,
B=high byte
8051 Microcontroller 71

8051 Microcontroller 72

Division of Unsigned Numbers
DIV AB; divide A by B
•MOV A,#95H ;load 95 into A
•MOV B,#10H ;load 10 into B
•DIVAB ;now A = 09 (quotient) and B = 05 (remainder)
8051 Microcontroller 73
Table 6-2:Unsigned Division Summary (DIV AB)
Division Numerator DenominatorQuotientRemainder
byte / byteA B A B

Logical Instructions
8051 Microcontroller 74

Logical Instructions
8051 Microcontroller 75

Logical Instructions
8051 Microcontroller 76

Logical Instructions
8051 Microcontroller 77

Logical Instructions
8051 Microcontroller 78

Logical Instructions
8051 Microcontroller 79

Logical Instructions
8051 Microcontroller 80

Logical Instructions
8051 Microcontroller 81

Logical Instructions
8051 Microcontroller 82

Logical Instructions
8051 Microcontroller 83

Program Flow Control Instructions
8051 Microcontroller 84

Program Flow Control Instructions
8051 Microcontroller 85

PROGRAM USING LCALL INSTRUCTION
8051 Microcontroller 86

Program Flow Control Instructions
8051 Microcontroller 87

Program Flow Control Instructions
8051 Microcontroller 88

Conditional Jump Example
8051 Microcontroller 89

Unconditional Jump Instructions
•All conditional jumps are short jumps
–Target address within -128 to +127 of PC
•LJMP(long jump): 3-byte instruction
–2-byte target address: 0000 to FFFFH
–Original 8051 has only 4KB on-chip ROM
•SJMP(short jump): 2-byte instruction
–1-byte relative address: -128 to +127

Call Instructions
•LCALL(long call): 3-byte instruction
–2-byte address
–Target address within 64K-byte range
•ACALL(absolute call): 2-byte instruction
–11-bit address
–Target address within 2K-byte range

8051 Software Overview
1.Addressing
Modes
2.Instruction Set
3.Programming
8051 Microcontroller 92

Checking an input bit
8051 Microcontroller 93
JNB (jump if no bit) ; JB (jump if bit = 1)

Switch Register Banks
8051 Microcontroller 94

Pushing onto Stack
8051 Microcontroller 95

Popping from Stack
8051 Microcontroller 96

Looping
8051 Microcontroller 97

Loop inside a Loop (Nested Loop)
8051 Microcontroller 98

8051 Conditional Jump Instructions
8051 Microcontroller 99

Conditional Jump Example
8051 Microcontroller 100

Unconditional Jump Instructions
•All conditional jumps are short jumps
–Target address within -128 to +127 of PC
•LJMP(long jump): 3-byte instruction
–2-byte target address: 0000 to FFFFH
–Original 8051 has only 4KB on-chip ROM
•SJMP(short jump): 2-byte instruction
–1-byte relative address: -128 to +127
8051 Microcontroller 101

Conditional Jump Example
8051 Microcontroller 102

Call Instructions
•LCALL(long call): 3-byte instruction
–2-byte address
–Target address within 64K-byte range
•ACALL(absolute call): 2-byte instruction
–11-bit address
–Target address within 2K-byte range
8051 Microcontroller 103

8051 Microcontroller 104
Tags