Escolar Documentos
Profissional Documentos
Cultura Documentos
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Unit VI- Introduction to Microcontroller 8051
Microprocessors and Microcontrollers comparison, 8051 architecture, Pin description,
addressing modes, instruction set of 8051, concepts of Counters and Timers with the help
of status registers, Serial communication, Port Structure and Interrupts. Simple
programming examples for addition, subtraction, multiplication.
Unit Objectives:
To understand the basics of Microprocessors & Microcontrollers.
To lean in depth concepts of hardware in Microcontroller.
To understand architecture and features of typical Microcontroller
To understand the programming details of Microcontroller.
Unit outcomes:
Able to select the microcontroller according to application
Able to learn use of hardware and software tools
Able to do programming using assembly
Reference Books:
1. Muhammad Mazidi, Janice Mazidi and Rolin McKinley, The 8051 Microcontroller
and Embedded Systems using Assembly and C, Pearson Education, 2nd edition.
2. Kenneth J. Ayala, The 8051 Microcontroller, Cengage Learning.
3. Myke Predko, Programming and customizing the 8051 microcontroller, TMH.
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Aspects of a microprocessor/controller
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Microprocessor Basic concept
ADDRESS BUS
16/32/ 64-bit wide
CPU contains
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
The Necessary Tools for MP/ MC
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Microprocessors and Microcontrollers
CPU Bus- Clock- Mem
bit MHz
4004 4
8085 8 3.07 64 K b
80286 16 6-12.5 16 Mb
80386 32 16-33 4 Gb
Pentium 64 60-100 4 Gb
CPU for Computers, No RAM, ROM, I/O on CPU chip itself
Pen-Pro 64 150-200 4 Gb
ExampleIntels x86, Motorolas 680x0
Features 8031 8051 8052
ROM -- 4K 8K
I/O Pins 32 32 32
Timers 2 2 3
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Microprocessor vs. Microcontroller
Microprocessor Microcontroller
CPU is stand-alone, RAM, CPU, RAM, ROM, I/O and timer
ROM, I/O, timer are external are all on a single chip
Expensive and versatile for Fixed amount of on-chip ROM,
design RAM, I/O ports
General-purpose Application Specific
High power consumption Low power consumption
Instruction sets focus on Instruction sets focus on control
processing-intensive operations and bit-level operations
Typically 32/64 bit Typically 8/16 bit
Typically deep pipeline (5-20 Typically single-cycle/two-stage
stages) pipeline
Clock speed range from 3.07 - Clock speed range from 6-64 MHZ
100 MHZ
Uses Von Neumann May be with Harward
Architecture- CISC Architecture Towards RISC
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Applications of Microcontroller
Home
Appliances, intercom, telephones, security systems, garage door
openers, answering machines, fax machines, home computers, TVs,
cable TV tuner, VCR, camcorder, remote controls, video games,
cellular phones, musical instruments, sewing machines, lighting
control, paging, camera, pinball machines, toys, exercise equipment
Office
Telephones, computers, security systems, fax machines, Microwave,
copier, laser printer, colour printer, paging
Auto
Trip computer, engine control, air bag, ABS, instrumentation, security
system, transmission control, entertainment, climate control, cellular
phone, keyless entry.
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Three criteria in Choosing a Microcontroller
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Architecture of MP and MC
Address
Address
Memory
(Program and Data)
Address
Address
Input Control CPU ALU Output
Program Data
Memory Memory
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Comparison of Von Neumann and Harvard
Von Neumann architecture Harvard architecture
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
RISC vs CISC: Characteristics
RISC CISC
2. Only LOADs, STOREs access memory 2. Any Instruction. may access memory
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Limitation of 8 bit controller
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
FEATURES of 8051
8-bit CPU optimized for control applications
On-chip clock oscillator 6-12 MHz
4K bytes of on-chip Program Memory
128 bytes of on-chip Data RAM
64K Program Memory address space
64K Data Memory address space
32 bidirectional and individually addressable 1/0 lines
Can operate on Bits, Bytes, Words
34- general purpose, 3- pointers , 21 SFRs
Extensive boolean processing (Single-bit logic) capabilities
Two 16-bit timer/counters
Full duplex UART
6-source/5-vector interrupt structure with two priority levels
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
BLOCK DIAGRAM
INT0
INT1
T0
TF0
Interrupt TF1
Timer 0
4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
SPPU PUNE : Digital Electronics P0Mantri (SIT,
Expert: D. S. P1 Lonavala)
P2 and A.N.
P3Paithane (RSCOE,Pune)
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
BLOCK DIAGRAM
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Registers
A PSW (8)
R0 SP (8)
R1
R2
DPH DPL DPTR (16)
R3
R4
PC PC (16)
R5
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Registers A, B , R0-7
Accumulator (A ,8 bit 0E0 )
It is a general-purpose register used for storing intermediate results obtained during
operation.
Prior to executing an instruction upon any number or operand, it is necessary to store it in
the accumulator first.
All results obtained from arithmetical operations performed by the ALU are stored in the
accumulator.
Data to be moved from one register to another must go through the accumulator.
In other words, the A register is the most commonly used register in performing
operations on Data.
Accumulator (B ,8 bit - 0F0)
It is a general-purpose register used for performing Multiplication and Division operation
with A
Multiplies 8 bit unsigned values in A and B and leaves the 16 bit result in A (low byte) and B
(high byte)
Divided A by B, leaving the integer result in A and remainder in B
It is bit-addressable
Registers (r0-r7 , 8 bit - 00-1F)
These are general-purpose register available in Banks -- Total 32
Out of that r0 & r1 act as pointers for internal memory related operations
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Program status word (PSW ,8 bit , 0D0)
Flags are 1-bit registers used to store the result of program instructions
Has 4 math flags (CY, AC, OV, P), 3 - General purpose flags set by User
It is Bit addressable
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Pointers SP, DPTR, PC
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Registers --- SFRS
Special Function Registers (SFRs) are a sort of control table used for
running and monitoring the operation of the microcontroller.
SFR has only 21 such registers.
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Map of SFRS (80-FF)
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
80 P0 SP DPL DPH PCON 87
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Mapping of Internal RAM
Internal : RAM - 128 Bytes : 00- 7F SFRS : 80- FFH , ROM: 4 KBytes
External : Ram and Rom : 64 KB
7F
General Purpose
Area
30
2F
BIT Addressable
Area
128 BYTE 20
1F
Reg Bank 3
17
Reg Bank 2
Register Banks 0F
Reg Bank 1
07
Reg Bank 0
00
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Programming Model of 8051
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
PIN DETAILS
P1
P0
RESET
+
_
P3
P2
XTAL
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Important points
Power On reset -- Restore initial setting 2M/C
Clock circuit --- uses 11.0592 MHz clock- need to maintain std baud rate
T= [No. of M/c Cycles * 12]/Crystal frequency
Normally clock frequency for 8051 is chosen as 11.0592 MHz yield
921.6KHz which can be evenly divided by standard communication
baud rates 300,600 ,1200, 2400,4800,9600 and so on
By default all I/O lines go high on Reset
Ioh= 60 A, -Current sinking capacity
Iol =1.6 ma - current sourcing capacity
EA & PSEN used to access internal and External memory
ALE - Differentiate Address and data on AD0- AD7 port0
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Important pins
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
PORT OPERATIONS
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
PIN DETAILS
P1
P0
RESET
+
_
P3
P2
XTAL
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Port Operation
One of the most useful features of the 8051 is that it contains four I/O
ports (P0 - P3) By default all lines are HIGH
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Hardware Structure of I/O Pin
Each pin of I/O ports
Internally connected to CPU bus
A D latch store the value of this pin
Write to latch1write data into the D latch
2 Tri-state buffer
TB1: controlled by Read pin
Read pin1really read the data present at the pin
TB2: controlled by Read latch
Read latch1read value from internal latch
A transistor M1 gate
Gate=0: open
Gate=1: close
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Ports of 8051
Port0- Pins 32-39, SFR 80]-needs external pull ups Port1- [ Pins 1-8, SFR 90]- does not needs external pull ups
RD
WR
Port 2- [ Pins 21-28, SFR A0]- used for high byte Address Port 3- [pin 10-17, SFR-B0]- Multi functional port
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
A Pin of Port 1
TB1
Read pin
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Writing 1 to Output Pin P1.X
TB1
Read pin
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Writing 0 to Output Pin P1.X
TB1
Read pin
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Reading High at Input Pin
1 1 P1.X pin
Internal CPU bus D Q
P1.X
0 M1
Write to latch Clk Q
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Reading Low at Input Pin
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Addressing Modes
The way of specifying operand of an instruction is called addressing modes
7. Relative
Short : SJMP with in 256 byes (-128 to +127)
Absolute : ACAll, AJMP with in 2Kb
Long : LCALL, LJMP -- with in 64Kb
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Instruction Set
5 Groups
Data Transfer Group
Arithmetic Operation Group
Logical Operation Group
Boolean Variable Manipulation Group
Program Branching Group
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Data Transfer Group [ MOV, PUSH, POP, XCHG]
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Arithmetic Instructions
[Add, SUB, INC, DEC, Multi, Divide, Decimal
ADD A,Direct
SUBB A, Direct INC A DEC A MUL AB
ADD A,Rn Direct DEC Direct DIV AB
SUBB A, Rn INC
ADD A,@Ri DEC Rn DA A
SUBB A,@Ri INC Rn
ADD A,#Data DEC @Ri
SUBB A,#Data INC @Ri
ADDC A,Direct INC DPTR
ADDC A,Rn
ADDC A,@Ri
ADDC A,#Data
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Logic Instructions
Logic operations, Clear, Rotate, Swap, Complement
ANL A, Direct ORL A, Direct
ANL A, Rn ORL A, Rn 00001111
00001111
ORL A, @Ri ORL 10101100
ANL A, @Ri ANL 10101100
ORL A, #Data 10101111
ANL A, #Data 00001100
ANL Direct, A ORL Direct, A
ANL Direct, #Data ORL Direct, #Data
CLR ( Set all bits to 0)
XRL A, Direct 00001111 CLR A 10101100
XRL CLR
XRL A, Rn 10101100 CLR byte (direct mode) 00000000
XRL A, @Ri 10100011 CLR Ri (register mode)
XRL A, #Data CLR @Ri(register
XRL Direct, A indirect) 10101100
CPL
XRL Direct, #Data CPL A 01010011
ANL C, bit; ORL C, bit; CLR C; CLR bit; CPL C; CPL bit; SETB C; SETB bit.
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Logical Operation Group - Rotate
RL A Mov a,#0xF0
Rl a
;a 11110000
a 11100001
RLC A C
RLC a
RR A [D7-CY-D0]
RRC A RR a [D0-D7]
RRC a C
[D0-CY-D7]
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Logic Instructions
Logic operations, Clear, Rotate, Swap, Complement
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Boolean Variable Manipulation Group
SETB C JC rel
SETB bit JNC rel
CLR C JB bit,rel
CLR bit JNB bit,rel
CPL C JBC bit,rel
CPL bit
MOV C, bit
MOV bit, C
ANL C, bit
ANL C,/bit
ORL C, bit
ORL C,/bit
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Program Branching Group
ACALL addr11 JMP @A+DPTR DJNZ Rn, rel
LCALL addr16 JZ rel DJNZ Direct, rel
RET JNZ rel NOP
RETI CJNE A, Direct,rel
AJMP addr11 CJNE A, #Data, rel
LJMP addr16 CJNE Rn, #Data, rel
SJMP rel CJNE @Ri, #Data, rel
LJMP(long jump)
LJMP is an unconditional jump. It is a 3-byte instruction. It allows a jump to any memory
location from 0000 to FFFFH.
AJMP(absolute jump)
In this 2-byte instruction, It allows a jump to any memory location within the 2k block of
program memory.
SJMP(short jump)
In this 2-byte instruction. The relative address range of 00-FFH is divided into forward and
backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the
current PC.
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Program control group of instructions
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
8051 Instruction Set
ACALL: Absolute Call JC: Jump if Carry Set PUSH: Push Value Onto Stack
ADD, ADDC: Add Acc. (With Carry) JMP: Jump to Address RET: Return From Subroutine
AJMP: Absolute Jump JNB: Jump if Bit Not Set RETI: Return From Interrupt
ANL: Bitwise AND JNC: Jump if Carry Not Set RL: Rotate Accumulator Left
CJNE: Compare & Jump if Not Equal JNZ: Jump if Acc. Not Zero RLC: Rotate Acc. Left Through Carry
CLR: Clear Register JZ: Jump if Accumulator Zero RR: Rotate Accumulator Right
CPL: Complement Register LCALL: Long Call RRC: Rotate Acc. Right Through Carry
DIV: Divide Accumulator by B MOVC: Move Code Memory SUBB: Sub. From Acc. With Borrow
DJNZ: Dec. Reg. & Jump if Not Zero MOVX: Move Extended Memory SWAP: Swap Accumulator Nibbles
JBC: Jump if Bit Set and Clear Bit ORL: Bitwise OR XRL: Bitwise Exclusive OR
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TIMERS
SERIAL PORT
INTERRUPTS
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
BLOCK DIAGRAM
INT0
INT1
T0
TF0
Interrupt TF1
Timer 0
4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
SPPU PUNE : Digital Electronics P0Mantri (SIT,
Expert: D. S. P1 Lonavala)
P2 and A.N.
P3Paithane (RSCOE,Pune)
Timer/ Counters
Set the initial value of registers Count the number of events
Start the timer and then the Show the number of events on
8051 counts up. registers
Input from internal system External input from Tx input pin.
clock (machine cycle) We use Tx to denote T0 or T1.
When the registers equal to 0 for counter0 or 1
and the 8051 sets a bit to
denote time out
8051 TH0
P1
P2 P1 TL0
Set
Timer TH0 P3.4
a switch T0
8051
TL0
Counters
Timers
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Timer/Counters
8051 has two 16-bit Timer/Counter registers
Timer/Counter 0
Timer/Counter 1
These registers can be used as timers or as event
counters
When a register is in Timer state, it is incremented
every machine cycle
In Counter state, the register is incremented when
there is a 1-to-0 transition at its external input pin, pin
T0 (P 3.4) or T1 (P 3.5)
Both registers have additional four operating modes
Has Two SFRs TMOD & TCON
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
PIN DETAILS
P1
P0
RESET
+
_
P3
Timer/Counter
P2
XTAL
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TIMER / COUNTER-- schematic
OSC 12
C T
C // T 0
0 TL TH TF
(8 Bit) (8 Bit) (1 Bit)
C /T 1
T PIN
INTERRUPT
TR
TR
Gate
Gate GATE =0 , C/T=0. TR0/1 -Effective -- Internal operation -Timer
GATE =0 , C/T=1. TR0/1 -Effective -- Internal operation Counter
INT PIN
INT PIN
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
SFRs Related to TIMER
Gate C/ M1 M0 Gate C/ M1 M0
Timer 1 Timer 0
Gate C/ M1 M0 Gate C/ M1 M0
Timer 1 Timer 0
Gate : When gate=1, and TRx in TCON =1, Timer/counter will only runs while INT(0,1) is high.
(hardware Control)
When gate=0, Timer/counter will only runs while TRx in TCON =1, No use of INT(0,1)
(Software control)
C/T : Counter/Timer select bit = 0, Timer Input from Internal system clock Delay
= 1, Counter- Input from Tx pin Event count
M1 : Mode bit 1. M0 : Mode bit 0.
M1 M0 Mode Explanation Over flow
0 0 0 3 bit Timer [TL= 5, TH=8 bit] 0000-1FFF
0 1 1 16 bit Timer/counter [TL=TH=8 bit] 0000- FFFF
1 0 2 8 bit auto reload Timer/Counter[TH TL] 00-FF
1 1 3 Split Separate TL0 - T0 control and TH0- T1 control 00- FF
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TCON Register
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
Timer control register: TMOD
Upper nibble for timer/counter, lower nibble for interrupts
TR (run control bit)
TR0 for Timer/counter 0; TR1 for Timer/counter 1.
TR is set by programmer to turn timer/counter on/off.
TR=0: off (stop) TR=1: on (start)
TF (timer flag, control flag)
TF0 for timer/counter 0; TF1 for timer/counter 1.
TF is like a carry. Originally, TF=0. When TH-TL roll over to 0000 from
FFFFH, the TF is set to 1.
TF=0 : not reach TF=1: reach
If we enable interrupt, TF=1 will trigger ISR.
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TIMER / COUNTER-- Summary
Parameter Internal External
Application Timer Counter
Purpose Delay Event Counting
Dependant On Oscillator External Pulses
Control Software Hardware
SFRS TMOD, Upper Nibble TMOD, Lower Nibble
of TCON of TCON
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Timer/Counter Modes
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TIMER / COUNTER --Modes
TIMERS
Timer 0 Timer 1
Mode 0 Mode 0
Mode 1 Mode 1
Mode 2
Mode 2
Mode 3
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TIMER 0/1 Mode 0 (Prescaler)
OSC 12
TL0 TH0
TF0 INTERRUPT
C /T 0 (5 Bit) (8 Bit)
TR 0
C /T 0 (8 Bit) (8 Bit)
OSC TH0
12 TF1 INTERRUPT
(8 Bit)
TR1
It uses the Timer 1 flag for the control operation even though
timer 0s , TH0 is used
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TIMER 0 Mode 3 ( Split mode)
Mode 3 is different for Timer 1 and Timer 0
Timer 1 just holds its count. It operates the same as when TR1
is set to 0
For Timer 0 is different, TL0 and TH0 of Timer 0 are
established as two separate counters
TL0 uses Timer 0 control bits for its work: C/T, GATE, TR0,
~INT0, and TF0
TH0 is locked into a timer function (counting machine cycles)
and takes over the use of TR1 and TF1 from Timer 1. TH0 is
now actually in control of Timer 1 interrupt
Mode 3 is provided for applications that require an extra 8-bit
timer or counter
With Timer 0, 8051 looks like it has three Timer/Counters
When Timer 0 in Mode 3, Timer 1 can be turned on and off by
switching it out of and into its own Mode 3, or can still be used in any
application not requiring an interrupt
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
TIMER Mode 0-3
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
INTERRUPTS
INT0
INT1
T0
TF0
Interrupt TF1
Timer 0
4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
SPPU PUNE : Digital Electronics P0Mantri (SIT,
Expert: D. S. P1 Lonavala)
P2 and A.N.
P3Paithane (RSCOE,Pune)
PIN DETAILS
P1
P0
RESET
+
_
P3
Hardware
Software
P2
XTAL
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
SFRs Related to interrupts
IE
LOW HIGH
TCON
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
INTERRUPTS
The Interrupt structure has the following features:
6 sources / 5 vectored interrupts
Each interrupts can be individually programmable
Each interrupts can have two priority levels
Priority levels can be programmed
All interrupts can be masked by a single bit - EA
External interrupt type can be programmed
Edge triggered
Level Triggered
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Interrupt
Original 8051 provides 6 interrupt sources
1 Reset
2 external interrupts [ External Interrupt 0 -- INT0
External Interrupt 1 -- INT1]
2 timer interrupts [ Timer 0 overflow- TF0;
Timer 1 overflow - TF1]
1 Serial Port events (buffer full, buffer empty, etc [TI/RI] )
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Interrupts structure
0 IE0
IT
IT 0
INT
INT00 0
TCON.1
1
TF
TF00
INTERRUPT
0 IE1 SOURCES
IT11
INT
INT11 IT TCON.3
1
TF
TF11
TI
TI
RI
RI
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Interrupt structure
IE Reg IP Reg High Priority
Interrupt
0
INT 0 IT 0 IE0
1
TF 0
0 Interrupt
INT 1 IT1 IE1 Polling
1 Sequence
TF1
TI
RI
Global
Individual Enable Disable EA=0
Low Priority Interrupt
DSM
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Interrupt ISR (Vector)Address
Each interrupt has a specific place in code memory where program execution
(interrupt service routine) begins.
This internal priority structure determined by the polling sequence and
the vector addresses is shown in the following table
Interrupt ISR address Priority/pin Flag Clearing
System Reset 0000 /9 Auto
External INT0 0003 Highest/P3.2 (12) Auto
Timer0 TF0 000B Auto
External INT1 0013 /P3.3 (13) Auto
Timer1 TF1 001B Auto
Serial port RI/TI 0023 Lowest Manual
Programmer
Timer 2 overflow(8052+) 002bh
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
IE: Interrupt Enable Register (bit addressable)
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
External interrupts- Edge Triggered
1. Upon power on reset INT0 and INT1 becomes the low level triggered
[ TCON.0 and TCON.2 are zero(0) ] [IE0 ,1 Not used at all]
2. To make them edge triggered ,we must program the bits of TCON register
TCON
Timers Interrupt
3. IF TCON.0 and TCON.2 are set one (1) INT0,1 becomes edge triggered
4. High to low signal will interrupt the processor and jumps to ISR.
5. IE0 and IE1 is used to keep track of edge triggered Interrupts
6. While executing ISR no transition of H-L is detected on INT0,1 , only RETI will
clear IEx
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Internal Interrupts
Timer interrupts are generated by TF0 and TF1 flags in their respective Timer/Counter registers
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
SERIAL PORT
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
BLOCK DIAGRAM
INT0
INT1
T0
TF0
Interrupt TF1
Timer 0
4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
SPPU PUNE : Digital Electronics P0Mantri (SIT,
Expert: D. S. P1 Lonavala)
P2 and A.N.
P3Paithane (RSCOE,Pune)
Basics of serial communication
Parallel: expensive - short distance fast no modulation
Serial :cheaper long (two different cities by modem)-slow
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Packaging Data- Asynchronous
In asynchronous transmission : each character is placed between
start and stop bits ( Framing Bits) , When there is no transfer
the signal is high
Transmission begins with a start (low) bit It is always one bit
Stop bit can be one or Two , Stop bit is high
LSB is send out First
Data transfer rate (baud rate) is stated in bps
Asynchronous serial transmission is widely used for the character oriented Transmission
Computer Baud Rates : 150, 300,600, 1200,2400, 4800, 9600, 19200 etc
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Pin out diagram
+
+
+
+
P1 + P0
+
RESET + +
Serial + _
P3
+
P2
XTAL
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
SFRs Related to Serial Port
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Serial control (SCON) Register
7 6 5 4 3 2 1 0
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Importance
Importance of the TI flag
of the TI and RI flag
check the TI flag bit, we know whether can transfer another byte
TI flag bit is raised by the 8051
TI flag cleared by the programmer
writing a byte into SBUF before the TI flag bit is raised, may lead to
loss of a portion of the byte being transferred
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Mode 0 - 8 bit shift-Fixed Baud rate
Application
Port expansion
8051
TXD clk
RXD Shift register
data
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Mode 1- Variable Baud Rate
Ten bits are transmitted (through TxD) or received (through RxD)
(A start bit (0), 8 data bits (LSB first), and a stop bit (1) )
On receive, the stop bit goes into RB8 in SCON
the baud rate is determined by the Timer 1 overflow rate.
Timer1 clock is 1/32 machine cycle (MC=1/12 XTAL)
16 57,600 Hz
11.0592 MHz by UART
XTAL 921.6 kHz (SMOD=1)
12 Timer 1
OSC
32 28,800 Hz
by UART
(SMOD=0)
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Baud rate in the 8051
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Mode2- 9 bit shift
Eleven bits are transmitted (through TxD), received (through RxD)
A start bit (0)
8 data bits (LSB first)
A programmable 9th data bit
and a stop bit (1)
On transmit, the 9th bit (TB8) can be assigned 0 or 1.
On receive, the 9the data bit goes into RB8 in SCON.
the 9th can be parity bit
The baud rate is programmable to 1/32 or 1/64 the oscillator frequency in Mode
2 by SMOD bit in PCON register
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Mode 3- 9 bit Variable Baud rate
The Serial Port in Mode-3 has the following features:
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Mode of operation
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Programming Examples
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)
Thank you
SPPU PUNE : Digital Electronics Expert: D. S. Mantri (SIT, Lonavala) and A.N. Paithane (RSCOE,Pune)