MPMC - UNIT - 2 - All Slides
MPMC - UNIT - 2 - All Slides
MICROCONTROLLERS
Architecture of 8051
Registers in 8051
Addressing Modes
Interrupts of 8051
Timer Interrupts
Serial COM
I/O Port Timer Port
Microcontroller
Microprocessors Versus Microcontroller
Microprocessors Versus Microcontroller
Microprocessors Versus Microcontroller
Microprocessor Microcontroller
CPU is stand-alone, CPU, RAM, ROM, I/O
RAM, ROM, I/O, timer and timer are all on a
are separate single chip
Designer can decide on Fix amount of on-chip
the amount of ROM, ROM, RAM, I/O ports
RAM and I/O ports. For applications in
Expansive which cost, power and
Versatility space are critical
General-purpose Single-purpose
Three criteria in Choosing a
Microcontroller
1. Meeting the computing needs of the
task efficiently and cost effectively
Speed, the amount of ROM and
RAM, the number of I/O ports and
timers, size, packaging, power
consumption
Easy to upgrade
Cost per unit
Three criteria in Choosing a
Microcontroller
CPU
P0 P1 P2 P3 TXD RXD
Architecture
Diagram of 8051
Microcontroller
Architecture of 8051
Architecture of 8051
Pin Diagram of 8051
XTAL
P3
P1
P0
P2
_
+
Pins of 8051 ( 1/4 )
C2
XTAL2
30pF
C1
XTAL1
30pF
GND
Pins of 8051 ( 2/4 )
RST ( pin 9 ): RESET
It is an input pin and is active high
( normally low).
The high pulse must be high at least
2 machine cycles.
It is a power-on reset.
Upon applying a high pulse to RST,
the microcontroller will reset and all
values in registers will be lost.
Reset values of some 8051 registers
Power-On RESET Circuit
Vcc
10 uF 31
EA/VPP
X1
30 pF 19
11.0592 MHz
8.2 K
X2
18
30 pF
RST
9
Pins of 8051 ( 3/4 )
ΕΑ (pin 31 ): External Access
The /EA pin is connected to GND to indicate
the code is stored externally. (Used in 8031
and 8032, as no internal ROM in 8031/32)
For 8051, this pin is connected to Vcc.
A D latch
2 Tri-state buffer
A transistor M1 gate
A Pin of Port 1
Hardware Structure of I/O Pin
A transistor M1 gate
Gate=0: open
Gate=1: close
Writing “1” to Output Pin P1.X
Writing “0” to Output Pin P1.X
Reading “High” at Input Pin
Reading “Low” at Input Pin
Other Pins
P1, P2, and P3 have internal pull-up
resisters.
P1, P2, and P3 are not open drain.
P0 has no internal pull-up resistors and
does not connects to Vcc inside the 8051.
P0 is open drain.
Compare the figures of P1.X and P0.X.
However, for a programmer, it is the
same to program P0, P1, P2 and P3.
All the ports upon RESET are
configured as output.
A Pin of Port 0
A Pin of Port 1
Port 0 with Pull-Up Resistors
Vcc
10 K
P0.0
P0.1
Port 0
P0.2
DS5000 P0.3
P0.4
8751 P0.5
P0.6
8951 P0.7
Port 3 Alternate Functions
RESET Value of Some 8051 Registers
Register Reset Value
PC 0000H
ACC 00H
B 00H
PSW 00H
SP 07H
DPTR 0000H
RAM are all zero
All the Ports are configured as
Output on Reset
Memory
Organization in
8051
INTERNAL RAM STRUCTURE
Direct &
Indirect
128 Byte
Addressing Internal RAM
128 BYTE RAM
7FH
General Purpose
Area
30H
2FH
BIT Addressable
Area
20H 128 BYTE
1FH INTERNAL RAM
Reg Bank 3
Reg Bank 2
Register Banks
Reg Bank 1
Reg Bank 0
00H
REGISTERS OF 8051
A
DPH DPL Data Pointer
B
R0
R1 PC Program
Counter
R2 16-bit Registers of 8051
R3
R4
R5
R6
R7
Some 8-bitt Registers of
the 8051
CY AC F0 RS1 RS0 OV - P
Bank 3 R0 R1 R2 R3 R4 R5 R6 R7
Bank 2 R0 R1 R2 R3 R4 R5 R6 R7
Bank 1 R0 R1 R2 R3 R4 R5 R6 R7
Bank 0 R0 R1 R2 R3 R4 R5 R6 R7
CY AC F0 RS1 RS0 OV - P
RAM memory space allocation in the 8051
7FH
30H
2FH
Bit-Addressable RAM
20H
1FH
10H
Register Bank 2
0FH
Register Bank 1 )Stack(
08H
07H
Register Bank 0
00H
SFR
(Special Function Registers)
F8 FF
F0 B F7
E8 EF
E0 Acc E7
D8 DF
D0 PSW D7
C8 CF
C0 C7
B8 IP BF
B0 P3 B7
A8 IE AF
A0 P2 A7
98 SCON SBUF 9F
90 P1 97
88 TCON TMOD TL0 TL1 TH0 TH1 8F
Memory mapping in 8051
ROM memory map in 8051 family
4K 8K 32K
0FFFH 1FFFH 7FFFH
0000H
DS5000-32
0000H
8751
AT89C51 8752
0000H
AT89C52
MOV DPTR,#7521h
MOV DPL,#21H
MOV DPH, #75
COUNT EGU 30
~
~
MOV R4, #COUNT
MOV DPTR,#MYDATA
~
~
0RG 200H
MYDATA:DB “INDIA”
Addressing Modes
Register Addressing –
Either source or destination is one of CPU register
MOV R0,A
MOV A,R7
ADD A,R4
ADD A,R7
MOV R5,DPL
MOV B,DPH
Addressing Modes
Direct Mode –
Specify data by its 8-bit address, Usually for 30h-7Fh
of RAM
Indexed Mode –
Source or destination address is the sum of the base
address and the accumulator(Index)
ORG 1000h
PC 1000 MOV A, #5
1002 MOVC A, @A + PC ;a M[1008]
1003 NOP
Table Lookup
MOVC only can read internal code memory
Instruction Set
Instruction Set
Arithmetic Operation Group
Logical Operation Group
Data Transfer Group
Boolean Variable Manipulation
Group
Program Branching Group
Instruction Set
ADDC A,Direct
ADDC A,Rn
ADDC A,@Ri
ADDC A,#Data
Arithmetic Operation Group
ADD A,Direct
ADD A,Rn
ADD A,@Ri
ADD A,#Data
Arithmetic Operation Group
SUBB A,Direct
SUBB A,Rn
SUBB A,@Ri
SUBB A,#Data
Arithmetic Operation Group
INC A
INC Direct
INC Rn
INC @Ri
Arithmetic Operation Group
DEC A
DEC Direct
DEC Rn
DEC @Ri
Arithmetic Operation Group
INC DPTR
MUL AB
DIV AB
DA A
Instruction Set
ANL A,Direct
ANL A,Rn
ANL A,@Ri
ANL A,#Data
ANL Direct,A
ANL Direct,#Data
Logical Operation Group
ORL A,Direct
ORL A,Rn
ORL A,@Ri
ORL A,#Data
ORL Direct,A
ORL Direct,#Data
Logical Operation Group
XRL A,Direct
XRL A,Rn
XRL A,@Ri
XRL A,#Data
XRL Direct,A
XRL Direct,#Data
Logical Operation Group
CLRA
CPL A
RLA
RLCA
RRA
RRCA
SWAP A
Instruction Set
MOV A,Direct
MOV A,Rn
MOV A,@Ri
MOV A,#Data
MOV Rn,Direct
MOV Rn,@Ri
MOV Rn,#Data
Data Transfer Group
MOV Direct,Direct
MOV Direct,Rn
MOV Direct,@Ri
MOV Direct,#Data
MOV Direct,A
MOV @Ri,A
MOV @Ri,#Data
Data Transfer Group
MOV @Ri,Direct
MOV DPTR,#DATA16
MOVC A,@A+DPTR
MOVC A,@A+PC
MOVX A,@Ri
MOVX @Ri,A
MOVX @DPTR,A
Data Transfer Group
PUSH Direct
POP Direct
XCH A,Rn
XCH A,Direct
XCH A,@Ri
Instruction Set
CLR C
CLR bit
SETB C
SETB bit
CPL C
CPL bit
Instruction Set
ACALLaddr11
LCALL addr16
RET
RETI
AJMPaddr11
LJMP addr16
SJMP rel
Program Branching Group
JZ rel
JNZ rel
CJNE A,Direct,rel
CJNE A,#Data,rel
CJNE Rn,#Data,rel
CJNE @Ri,#Data,rel
Program Branching Group
DJNZ Rn,rel
DJNZ Direct,rel
NOP
8051
Interrupts
8051 Interrupts
An interrupt is an external or internal event
that disturbs the microcontroller to inform
it that a device needs its service.
A Microcontroller can serve various devices.
There are two ways to do that:
interrupts &
polling.
The program which is associated with the
interrupt is called the interrupt service
routine (ISR) or interrupt handler.
Steps in executing an interrupt
Upon receiving the interrupt signal the
Microcontroller , finish current instruction and
saves the PC on stack.
Jumps to a fixed location in memory depending on
type of interrupt
Starts to execute the interrupt service routine until
RETI (return from interrupt)
Upon executing the RETI the microcontroller
returns to the place where it was interrupted. Get
pop PC from stack
Interrupt Sources
Original 8051 has 6 sources of interrupts
Reset
Timer 0 overflow
Timer 1 overflow
External Interrupt 0
External Interrupt 1
Serial Port events (buffer full, buffer empty, etc)
IP
- - - PS PT1 PX1 PT0 PX0
Interrupt
TCON
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Interrupt
Interrupt Priority
RI / TI TF1 IE1 TF0 IE0
LOW HIGH
Enabling and disabling an Interrupt
Interrupt Priorities
What if two interrupt sources interrupt at the
same time?
The interrupt with the highest PRIORITY gets
serviced first.
All interrupts have a power on default priority
order.
1. External interrupt 0 (INT0)
2. Timer interrupt0 (TF0)
3. External interrupt 1 (INT1)
4. Timer interrupt1 (TF1)
5. Serial communication (RI+TI)
Priority can also be set to “high” or “low” by IP reg.
Interrupt Priorities (IP) Register
- -_-- -- -- -_-- - PT2 PS PT1 PX1 PT0 PX0
IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timer 0 interrupt priority bit
IP.0: External interrupt 0 priority bit
Interrupt Priorities Example
-_-- -- -_-- - PT2 PS PT1 PX1 PT0 PX0
IE0
IN T 0
TF 0
INTERRUPT
IN T 1 IE1 SOURCES
TF1
TI
RI
Over all Interrupt Block Diagram
High Priority
IE Reg IP Reg Interrupt
0
IN T 0 IT 0 IE0
1
TF 0
0
Interrupt
IN T 1 IT 1 IE1
1 Polling
Sequence
TF1
TI
RI
Global
Individual
Disable
Enable
Low Priority
Interrupt
PROGRAM AND VERIFY INTERRUPT
HANDLING IN 8051(EXAMPLE-1)
1. ORG 0000H
1. MOV TH0,#0B6H
2. LJMP MAIN
2. MOV IE,#82H
3. ORG 000BH
3. SETB TR0
4. CPL P1.2
4. BACK:
5. RETI
5. MOV P0, #'A'
6. ORG 0030H
6. MOV P2, #'B'
7. MAIN:
7. SJMP BACK
8. MOV TMOD,#02H
8. END
PROGRAM AND VERIFY INTERRUPT
HANDLING IN 8051(EXAMPLE-2)
1. ORG 0000H 10. LED2:
2. LJMP MAIN 11. MOV P2,#00H
3. ORG 0003H 12. MOV R0,#0255
4. LED1: 13. DJNZ R0,LED2
5. MOV P0,#00H 14. RETI
6. MOV R0,#0255 15. ORG 0030H
7. DJNZ R0,LED1 16. MAIN:
8. RETI 17. MOV IE,#85H
9. ORG 0013H 18. HERE:SJMP HERE
19. END
8051 Timers and
Counters
TMOD Register:
Timer 1 Timer 0
TCON
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timers
TIMERS
Timer 0 Timer 1
Mode 0 Mode 0
Mode 1 Mode 1
Mode 2 Mode 2
Mode 3
PROGRAM AND VERIFY
TIMER/COUNTER IN 8051
1. ORG 0000H 12. SETB TR1
2. RPT: 13. BACK:
3. MOV TMOD,#15H 14. JNB TF1,BACK
4. SETB P3.4 15. CLR TF1
5. MOV TL0,#00H 16. CLR TR1
6. MOV TH0,#00H 17. DJNZ R0,AGAIN
7. SETB TR0 18. CPL P3.0
8. MOV R0,#70 19. MOV A,TL0
9. AGAIN: 20. MOV P2,A
10. MOV TL1,#00 21. SJMP RPT
11. MOV TH1,#00 22. END
Serial Communication
in 8051
SFRs Related to SERIAL PORT
SCON
SM0 SM1 SM2 REN TB8 RB8 TI RI
PCON
SMOD - - - GF1 GF0 PD IDL
SMOD
PROGRAM FOR SERIAL COMMUNICATION
- UART OPERATION IN 8051
1. MOV TMOD, #20H 12. CLR TI
2. MOV TH1, #-3 13. MOV SBUF, #'M‘
3. MOV SCON, #50H
14. HERE2:
4. SETB TR1
15. JNB TI,HERE2
5. AGAIN:
16. CLR TI
6. MOV SBUF, #'M'
17. MOV SBUF, #'C'
7. HERE:
18. HERE3:
8. JNB TI,HERE
19. JNB TI,HERE3
9. CLR TI
20. CLR TI
10. MOV SBUF, #'P'
11. HERE1:
21. X:SJMP X
*Discussion*