Unit 4 Introduction To Microcontroller 8051a
Unit 4 Introduction To Microcontroller 8051a
Introduction to Microcontroller
8051
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 learn 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.
Aspects of a microprocessor/controller
ADDRESS BUS
16/32/ 64-bit wide
CPU contains
8085 8 3.07 64 K b
80286 16 6-12.5 16 Mb
80386 32 16-33 4 Gb
Pentiu 64 60-100 4 Gb
CPU for Computers, No RAM, ROM, I/O on CPU chip itself m
Example:Intel’s x86, Motorola’s 680x0
Pen- 64 150-200 4 Gb
Pro
Features 8031 8051 8052
ROM -- 4K 8K
Timers 2 2 3
• A smaller computer, On-chip RAM, ROM, I/O ports... Serial 1 1 1
• Example:Motorola’s 6811, Intel’s 8051, and PIC ports
16X Interrupts 6 6 8
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 single-cycle/two-stage
Typically deep pipeline (5-20
stages) pipeline
• Clock speed range from 6-20 MHZ
Clock speed range from 3.07 -
100 MHZ
Has less no. of multifunctional • Ports used are multifunctional
pins
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.
Three criteria in Choosing a Microcontroller
Address Data
Von Neumann Architecture
Memory
(Program and Data)
Address
Address
Input Control CPU ALU Output
Program Data
Memory Memory
Comparison of Von Neumann and Harvard
Von Neumann architecture Harvard architecture
RISC CISC
2. Only LOADs, STOREs access memory 2. Any Instruction. may access memory
INT0
INT1
T0
TF0
Interrupt Timer 0
TF1 4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
P0 P1 P2 P3
8051 Internal Block Diagram
8051
Foot Print
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0)
P1.2 3 38 P0.1(AD1)
P1.3 4 37 P0.2(AD2)
EA- External P1.4 5 36 P0.3(AD3)
access P1.5 6 35 P0.4(AD4)
PSEN-Program P1.6 7 34 P0.5(AD5)
Store Enable. P1.7 8 33 P0.6(AD6)
used to read RST 9
8051 32 P0.7(AD7)
external (RXD)P3.0 10 (8031) 31 EA/VPP
memory. (TXD)P3.1 11 30 ALE/PROG
VPP-During (INT0)P3.2 12
(8751) 29 PSEN
Flash (INT1)P3.3 13 (8951) 28 P2.7(A15)
Programming, (T0)P3.4 14 27 P2.6(A14)
this Pin (T1)P3.5 15 26 P2.5(A13)
receives 12V (WR)P3.6 16 25 P2.4(A12)
Programming (RD)P3.7 17 24 P2.3(A11)
Enable XTAL2 18 23 P2.2(A10)
Voltage (VPP). XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)
BLOCK DIAGRAM
Accumulator A & B with 32 GPR, 21 SFRs
Parallel Input/Output Ports
Memory RAM/ROM
System Clock Generator
Serial Port
Timers/counters
Interrupt Control
Registers
A PSW (8)
R0 SP (8)
R1
R2
DPH DPL DPTR (16)
R3
R4
PC PC (16)
R5
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
Pointers SP, DPTR, PC
P1
P0
RESET
+
_
P3
P2
XTAL
Important points
Power On reset -- Restore initial setting – 2M/C(Machine cycle-Fetch,
decode and execute cycles)
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
Important pins
P1
P0
RESET
+
_
P3
P2
XTAL
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
7. Relative
Short : SJMP with in 256 byes (-128 to +127)
Absolute : ACAll, AJMP – with in 2Kb
Long : LCALL, LJMP -- with in 64Kb
Instruction Set
5 Groups
Data Transfer Group
Arithmetic Operation Group
Logical Operation Group
Boolean Variable Manipulation Group
Program Branching Group
Data Transfer Group [ MOV, PUSH, POP,
XCHG]
MOV A,Direct
MOV @Ri, A PUSH Direct
MOV A,Rn POP Direct
MOV @Ri,#Data
MOV A,@Ri XCH A,Rn
MOV @Ri, Direct
MOV A,#Data XCH A,Direct
MOV Rn,Direct
MOV DPTR, #DATA16 XCH A,@Ri
MOV Rn,@Ri MOVC A, @A+DPTR XCHD A,@Ri
MOV Rn,#Data MOVC A, @A+PC
MOV Direct, Direct MOVX A, @Ri
MOV Direct, Rn MOVX @Ri, A
MOV Direct, @Ri MOVX @DPTR, A
MOV Direct, #Data
MOV Direct, A
Data Transfer Group [ MOV, PUSH, POP,
XCHG]
Arithmetic Instructions
[Add, SUB, INC, DEC, Multi, Divide, Decimal
ANL C, bit; ORL C, bit; CLR C; CLR bit; CPL C; CPL bit; SETB C; SETB bit.
Logical Operation Group - Rotate
RRC A RR a [D0-D7]
RRC a C
[D0-CY-D7]
Logic Instructions
Logic operations, Clear, Rotate, Swap, Complement
Boolean Variable Manipulation Group
SETB C JC rel
SETB bit JNC rel
CLR C JB
CLR bit bit,re
CPL C l
CPL bit JNB
bit,re
l
MOV C,
bit JBC
bit,re
MOV bit,
l
C
ANL C, bit
ANL
C,/bit
ORL C, bit
ORL
C,/bit
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.
Program control group of instructions
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
JNZ: Jump if Acc. Not Zero RLC: Rotate Acc. Left Through
Equal
Carry
JZ: Jump if Accumulator Zero
CLR: Clear Register
RR: Rotate Accumulator Right
LCALL: Long Call
CPL: Complement Register
RRC: Rotate Acc. Right Through
LJMP: Long Jump Carry
DA: Decimal Adjust
MOV: Move Memory SETB: Set Bit
DEC: Decrement Register
MOVC: Move Code Memory SJMP: Short Jump
DIV: Divide Accumulator by B
MOVX: Move Extended SUBB: Sub. From Acc. With Borrow
DJNZ: Dec. Reg. & Jump if Not
Memory
Zero
SWAP: Swap Accumulator Nibbles
MUL: Multiply Accumulator by B
INC: Increment Register
XCH: Exchange Bytes
NOP: No Operation
JB: Jump if Bit Set
XCHD: Exchange Digits
ORL: Bitwise OR
JBC: Jump if Bit Set and Clear Bit
XRL: Bitwise Exclusive OR
POP: Pop Value From Stack
Undefined: Undefined Instruction
TIMERS
SERIAL PORT
INTERRUPTS
BLOCK DIAGRAM
INT0
INT1
T0
TF0
Interrupt Timer 0
TF1 4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
P0 P1 P2 P3
Timer/ Counters
TL0 Counters
Timers
Timer/Counters
P1
P0
RESET
+
_
P3
Timer/Counter
P2
XTAL
TIMER / COUNTER-- Summary
Parameter Internal External
Application Timer Counter
Purpose Delay Event Counting
Dependant On Oscillator External Pulses
Control Software Hardware
SFRS TMOD, Upper TMOD, Lower
Nibble of TCON Nibble of TCON
INTERRUPTS
SKB's
BLOCK DIAGRAM
INT0
INT1
T0
TF0
Interrupt Timer 0
TF1 4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
P0 P1 P2 P3
PIN DETAILS
P1
P0
RESET
+
_
Hardwar
P3
eSoftware
P2
XTAL
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] )
INT0
INT1
T0
TF0
Interrupt Timer 0
TF1 4K 128
Control TI/RI
ROM RAM Timer 1
T1
CPU
TXD RXD
P0 P1 P2 P3
Basics of serial communication
P2
XTAL
Programming Examples
Programming Examples– Addition and Subtraction Programming – Mul and Div
.org 0000h --- Memory initialization
.org 0000h ---- Memory initialization mov r0, # 05h
mov a, # 05h ---- Store 1st no. in Acc mov b, #02
mov r0, # 04h ---- Store 2st no. in register mov a, r0
add a, r0 ---- Add the nos mul ab
mov b, a ---- store in b reg. mov r2, a
subb a, r0 ---- subtract here: sjmp here
mov r4, a ---- store in r4 reg.
here: sjmp here ---- Terminate the program
Thank you