8051architectures
8051architectures
By
P RAMANJANEYA PRASAD
Head – Department of Electronics
Avanthi Degree & PG College
Email: [email protected]
What Are Embedded Systems?
Integratedsystems in
aircrafts and missiles ?
Cellular telephones
Engine controllers in
automobiles
Calculators
Medical equipment’s
Videogames
T H E N E C E S S A R Y TOOLS FOR A M ICROPRO C E S S O R /M ICROCONTROLLER
Timer
Interrupt
Introduction to Microprocessors
Microcontroll Microprocessor
er RAM, ROM, I/O
• CPU,
• CPU is stand-alone, RAM, ROM,
and timer are all on a
I/O, timer are separate.
single chip•
• Designer can decide the amount
• Fix amount of on-chip
• of ROM, RAM and I/O ports
ROM, RAM, I/O ports
• Expensive
• For applications in
• General-purpose
which cost, power and
space are critical
• Expensive
• Single-purpose
8051 Microcontroller Block Diagram
•
Architecture of
8051
The 8051 Architecture consist of
these specific features
The 8 bit CPU with Registers A and B
Internal ROM
16-bit program counter(PC) and data
pointer(DPTR)
Internal RAM of 128 bytes
8-bit Program Status word(PSW)
4 eight-bit ports
The first 6 crystal pulses (clock cycle) is used to fetch the Opcode
and the second 6 pulses are used to perform the operation on the
operands in the ALU.
As the CPU fetches the opcode from the program ROM,
the program counter is increasing to point to the next
instruction.
pop
push
stack pointer
stack
Memory Organization
INTERNAL MEMORY
128 bytes of RAM.
1.Working register:
Thirty-two bytes from address 00h to 1Fh that make up 32 working register
organized as Four bank of eight bit each.
Bits RS0 and RS1 in the PSW determine which bank of register is currently
Is use.
Bank 0 is selected upon reset
2.Bit addressable:
A bit addressable area of 16 bytes occupies RAM bytes addresses 20h to
2Fh,forming A total of 128 addressable bits.
An addressable bit may be specified by its bit address of 00h to 7Fh.
3.General purpose:
A general-purpose RAM area above the bit area,form 30h to 7Fh,addresable
as bytes.
Internal RAM Organization
F8
B
F0
ACC
E8
PSW
E0
D8 IP
P3
D0 IE
P2
C8 SCON SBUF
P1
C0 TCON TMOD TL0 TL1 TH0 TH1
P0 SP DPL DPH PCON
B8
Special Function Register
(SFR)cont..
Special Function Register
(SFR)cont..
128 byte address space,
directly addressable as 80 to FF
hex.
16 addresses are bit
addressable: (those ending in 0
or 8).
Serial I/O:
- SCON : Serial port control.
- SBUF : Serial data registers.
Other:
- PCON : Power control
I/O Ports
- Quasi-bidirectional:
Port 0
P2.0 (A8)
Timer/Counters
Two 16-bit up counters, named T0 and T1, are provided for the general use
of the programmer.
Each counter may be programmed to count internal clock pulses, acting as a
timer, or programmed to count external pulses as a counter.
The counters are divided into two 8-bit registers called the timer low
(TL0,TL1) and high (TH0, TH1) bytes.
All counter action is controlled by bit states in the timer mode control register
(TMOD), the timer/counter control register (TCON) and certain program
instructions.
TMOD is dedicated to the two timers and can be consider two duplicate 4-bit
registers, each of which controls the action of the timers.
TCON has control bits and flags for the timers in the upper control bits and
flags for the external interrupts in the lower nibble.
Interrupt System
5 Interrupt Sources (in order of priority):
1 External Interrupt 0 (IE0)
2 Timer 0 (TF0)
3 External Interrupt 1 (IE1)
4 Timer 1 (TF1)
5 Serial Port (RI/TI)
Each interrupt type has a separate vector
address.
Each interrupt type can be programmed
to one of two priority levels.
External interrupts can be
programmed for edge or level
sensitivity.
Interrupt vector Addresses
Source Address
IE0 03H
TF0 0BH
IE1 13H
TF1 1BH
RI&TI 23H
- 0 = Disabled.
- 1 = Enabled.
IP: Interrupt Priority Register
----- ----- ----- PS PT1 PX1 PT0 PX0
- PS : Serial interface.
- PT1 : Timer 1.
- PX1 : External interrupt
1.
- PT0 : Timer 0.
- PX0 : External interrupt
0.
- 0 = Low priority.
- 1= High
priority.
8051 Microcontroller & its Applications
UNIT – 2
INSTRUCTION SETS OF 8051 MICROCONTROLLER
Instruction sets of 8051 microcontroller
Instruction Sets
Data Transfer
Arithmetic
Logical
Control or Jump
General Purpose Data Transfer
address in DPTR.
MOVC – Move byte from program memory to A. (Base
Reg + Index reg. indirect addressing)
XCH A, <byte>
XCH A, Rn – Register Addressing XCH A,
XCHD A, @Ri (Ri = R0 or R1)
MOVX < dest. Byte> <src. Byte>
MOVX A, @ Ri
MOVX A , @ DPTR
MOVX @ Ri, A
MOVX @ DPTR , A
MOVC A, @A + DPTR (Move code byte)
(A) ((DPTR)+(A))
MOVC A, @A + PC
(A) (( PC) +(A))
16-bit Data Transfer
ADDITION:-
ADD A, (src. Byte) - Add (A)← (A) + (Src.)
ADDC A, (Src byte) Add with carry (A) ← (A) +(Src) + (C)
Register Addressing-
ADD A, Rn (A) ← (A)+(Rn)
ADDC A, Rn (A) ← (A)+(Rn)+(C)
Direct Addressing-
ADD A, Address (A) ← (A)+(Direct Address)
ADDC A, Address (A) ← (A)+(C)+(Direct Address)
Arithmetic Instructions
Indirect Addressing
ADD A, @Ri (A) ← (A)+Address[Ri]
ADDC A,@Ri (A) ← (A)+(C)+ Address[Ri]
**Ri = R0 or R1
Immediate Addressing
ADD A, #data (A) ← (A)+(Data)
ADDC A, #data (A) ← (A)+(C)+(Data)
Increment
INC Rn Rn ← Rn + 1
INC @Ri, Ri ← [Ri] + 1
INC DPTR DPTR ← DPTR +
1
Arithmetic Instructions
SUBTRACTION
SUBB – Subtract with Borrow
After Operation
(A) - lower order byte of the result.
(B) - Higher order byte of the result
DIVIDE
DIV AB (A) – Operand 1 or
Dividend,
(B) – Operand 2 or
Divisor
After Operation
Arithmetic Instructions
DA A (Decimal Adjust Accumulator for addition)
If two BCD numbers (in packed BCD format) are added using ADD or
ADDC instructions then to represent the result in BCD.
(1) If value of lower Nibble in ACC > 9 or if AC Flag is set then 6 is added
to ACC.
(2) Now after (1), if value of higher Nibble is greater than 9 or if CY Flag is
set then 6 is added to higher Nibble of ACC.
Logical Instructions
Logical Instructions
B7 B6 B5 B4 B3 B2 B1 B0
B7 B6 B5 B4 B3 B2 B1 B0
AND Operation
ANL A, Rn (A) ← (A) AND Rn
ANL A, direct address (A) ← (A) AND (direct address)
ANL A, @Ri (A) ← (A) AND Address[Ri]
ANL A,#data (A) ← (A) AND (data)
OR Operation
ORL A, Rn (A) ← (A) OR Rn
ORL A, direct address (A) ← (A) OR (direct address)
ORL A, @Ri (A) ← (A) OR Address[Ri]
ORL A,#data (A) ← (A) OR (data)
**Ri = R0 or R1
Logical Instructions
XOR Operation
XRL A, Rn (A) ← (A) XOR Rn
XRL A, direct address (A) ← (A) XOR (direct address)
XRL A, @Ri (A) ← (A) XOR Address[Ri]
XRL A,#data (A) ← (A) XOR (data)
**Ri = R0 or R1
Control Transfer Instructions
LCALL FACT
--------
--------
---------
FACT: --------
--------
--------
RET
Unconditional calls, Returns and
jumps
JUMP
NEXT:
JNB bit, rel (Jump if bit not set)
JC rel (Jump if carry set)
JNC rel (Jump if carry not set)
JBC bit, rel (Jump if bit is set and clear bit)
JZ rel , Jump if result in ACC is zero
JNZ rel , Jump if result in ACC is not zero.
CJNE <dest>, <Src>,address.
If (dest) ≠ (Src) then jump to specified address.
If <dest> less than <Src> CY=1
If <dest> greater than <src> CY =0
Register Addressing
Direct Addressing
Examples
MOV A,R1 //Copy the contents of register R1 into A
Examples
MOV A, 10H //Copy data from address 10H into A
MOV 10H, //Copy data from A into address 10H
A
MOV R5, 80H //Copy data from address 80H into register
R5
MOV 50H, R3 // Copy data from register R3 into address 50H
MOV 20H, #32H // load immediate data 32H into address 20H
UNIT – 3
PROGRAMMING OF 8051 MICROCONTROLLER
TIMER / COUNTER PROGRAMMING IN 8051
8051 Microcontroller & its Applications
Label
It is used to assign name to the memory location or
program
Basics of a program statement in
assembly language
Comments
Comments are used to describe the operation performed by
an instruction.
They help in understanding the program easily.
Mostly comments start with “//”
Example
MOV R0, #10H
Opcode – MOV //Move 10H into register R0
Operands – R0, 10H
Basics of a program statement in
assembly language
Instructions
Every instruction has two parts
Algorithm
1. Initialise the memory location.
2. Get the first number in register A.
3. Add the second number to the contents of Reg A
4. Store the result in the next consecutive memory
location.
5. Halt the program
ADDITION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
ADD A,@R0 //Add the contents in next location
to the contents of Reg A//
INC @R0 //increment R0//
MOV @R0,A //Store the result in next location//
LCALL 0003H //Halt the program//
Data Result
70H – 07H 72H – 0CH
71H – 05H
ADDITION PROGRAM
Start
A ← First Number
A ← A + 2nd Number
Stop
ADDITION PROGRAM
Algorithm
1. Initialise the memory location.
2. Get the first number in register A.
3. Subtract the second number to the contents of Reg A
4. Store the result in the next consecutive memory
location.
5. Halt the program
SUBTRACTION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
SUBB A,@R0 //Subtract the contents in next location
to the contents of Reg A//
INC R0 //increment R0//
MOV @R0,A //Store the result in next location//
LCALL 0003H //Halt the program//
Data Result
70H – 07H 72H – 02H
71H – 05H
SUBTRACTION PROGRAM
Start
A ← First Number
A ← A - 2nd Number
Stop
ADDITION PROGRAM
Data Result
70H – 11H 72H – 32H
71h – 12H 73H – 01H
DIVISION PROGRAM
Data Result
70H – 09H 72H – 01H
71h – 02H 73H – 04H
LARGEST NUMBER PROGRAM
Data Result
50H – 86H 53H – 89H
51H – 89H
52H – 88H
SMALLEST NUMBER PROGRAM
Data Result
50H – 86H 53H – 89H
51H – 89H
52H – 88H
DESCENDING ORDER PROGRAM
Data Result
50H – 04H 50H – 04H
51H – 02H 51H – 03H
52H – 01H 52H – 02H
53H – 03H 53H – 01H
ASCENDING ORDER PROGRAM
ALP TO ARRANGE A SET OF GIVEN NUMBERS IN
ASCENDING ORDER
Algorithm
1. Initialize the memory location
2. Get the count in one of the register.
3. Get the first number in register A .
4. Get the second number in Memory.
5. Compare and check for carry. If no carry, move the smaller number in
accumulator
6. If carry decrement the count and compare for remaining numbers.
7. Check for the remaining comparison other than the smallest number.
8. Store the result.
9. Halt the program
ASCENDING ORDER PROGRAM
MOV R7,#04H //Get count in Register R7 for number of entries//
GO: MOV R0,#50H //Initialize the memory location//
MOV R6,#04H //Get count in Register R6 for number of comparisons//
MOV A,@R0 //Get first number in A//
INC R0 //increment R0//
NEXT: MOV F0H,@R0 //Get next number in register B//
CJNE A,F0H, LOOP //Compare A and B registers, if not equal jump to LOOP//
SJMP UP //Short jump to the specified location//
LOOP: JC UP //Is Carry present ? – Yes, jump to UP//
MOV @R0,A //Move smallest number in Memory location //
DEC R0 //Decrement the memory location//
MOV @R0,F0H //Move the largest number in next location//
UP: DJNZ R6, NEXT //R6=R6-1 and check R6. if not ‘0’ jump to NEXT//
DJNZ R7, GO //R6=R6-1 and check R6. if not ‘0’ jump to GO//
LCALL 0003H //Halt the program//
Data Result
50H – 04H 50H – 01H
51H – 02H 51H – 02H
52H – 01H 52H – 03H
53H – 03H 53H – 04H
SUBROUTINES
CLR P0.2
ACALL DELAY X X 1 X X 0 X X
SETB P0.5 B7 B6 B5 B4 B3 B2 B1 B0
ACALL DELAY
SJMP BACK
GENERATION OF A WAVEFORM BY
ACCESSING A PORT
Create a square wave of 50% duty cycle on bit 0 of port 1.
Solution:
The 50% duty cycle means that the “on” and “off” state (or the high & low
portion of the pulse) have the same length. Therefore, we toggle P1.0 with a time
delay in between each state. Science
P1.0
8051 Microcontroller & its Applications
Two 16-bit up counters, named T0 and T1, are provided for the general use
of the programmer.
Each counter may be programmed to count internal clock pulses, acting as a
timer, or programmed to count external pulses as a counter.
The counters are divided into two 8-bit registers called the timer low
(TL0,TL1) and high (TH0, TH1) bytes.
All counter action is controlled by bit states in the timer mode control register
(TMOD), the timer/counter control register (TCON) and certain program
instructions.
TMOD is dedicated to the two timers and can be consider two duplicate 4-bit
registers, each of which controls the action of the timers.
TCON has control bits and flags for the timers in the upper control bits and
flags for the external interrupts in the lower nibble.
Timer/Counters(cont..)
These timers exist in the SFR area as pairs of 8- bit registers.
– TL0 (8AH) and TH0 (8CH) for Timer0.
– TL1 (8BH) and TH1 (8DH) for Timer1. (LSB is bit 0 ; MSB is bit 7)
The pins must be held high for one complete machine cycle and then
low for one complete machine cycle.
Timer/Counters: Block Schematic
Timer/Counters: Application
2. Event counting
Determine the number of occurrences of an event.
An event is any external stimulus that provides a 1-
to-0 transition on a pin of the µC.
TCON (Counter/Timer Control Register)
M1 M0 Mode
0 0 0
0 1 1
1 0 2
1 1 3
TIME DELAY CALCULATIONS
The clock frequency of 8051 is 11.0592 MHz.
CPU executing an instruction takes a certain number of
clock cycles.
These are referred as to as machine cycles.
The length of machine cycle depends on the frequency
of the crystal oscillator connected to 8051.
In original 8051, one machine cycle lasts 12 oscillator
periods
TIME DELAY CALCULATIONS
Find the delay caused in the following program
UNIT – 4
SERIAL COMMUNICATION
APPLICATIONS OF MICROCONTROLLER
8051 Microcontroller & its Applications
SERIAL COMMUNICATIONS
Basics of serial communication
Basics of serial communication
• To transfer to a device located many meters away,
the serial method is used.
Modes of operation
APPLICATION OF MICROCONTROLLER
8051 Microcontroller & its Applications
APPLICATIONS
Temperature Measurement
LCD Interfacing
Stepper Motor
Keyboard Interfacing
8051 Microcontroller & its Applications
8051 Interfacing LCD (Liquid Crystal Display)
VEE 10K
P7 D7 LCD
8051 VSS POT
RS R/W E
P3.2
P3.3
LCD Program
• org 0000h
• mov a,#38h ; INITIALIZE 2 line 5x7
• acall comm ;CALL COMMAND SUBROUTINE
• acall delay ;GIVE LCD SOME TIME
• mov a,#'R'
• acall data1
• acall delay
• mov a,#„E'
• acall data1
• acall delay
• mov a,#'S'
• acall data1
• acall delay
LCD Program
• comm: ;SEND COMMAND TO LCD
• mov p0,a ;COPY REG A TO PORT0
• clr p2.2 ;RS=0 FOR COMMAND
• clr p2.1 ;R/W=0 FOR WRITE
• setb p2.0 ;E=1
• clr p2.0 ;E=0
ret
• data1: ;WRITE DATA TO LCD
• mov p0,a ;COPY REG A TO PORT0
• setb p2.2 ;RS=1 FOR DATA
• clr p2.1 ;R/W=0 FOR WRITE
• setb p2.0 ;E=1
• clr p2.0 ;E=0
ret
• delay: mov r1,#255
• here2: mov r2,#255
• here: djnz r2,here
• djnz r1,here2
• Ret
• end
8051 Interfacing ADC
holds the data in the internal registers. A HIGH signal on this pin
will bring the data on the output lines.
• D0-D7 are the digital Data output lines.
8051 Interfacing ADC
TEMPERATURE MEASUREMENT
internally calibrated.
• LM35 are precision integrated circuit temperature sensors whose
output.
DAC 0808
• The total current provided by the Iout pin is a function of the binary
• Where D0 is the LSB, D7 is the MSB for the inputs, and Iref is the
input current that must be applied to pin 14. The Iref current is
generally set to 2.0 mA.
DAC 0808
Interfacing Diagram
8051 Interfacing Stepper Motor
Stepper motor is a widely
used device that
translates electrical pulses
into
mechanical movement
Stepper motor is used in
applications for position
control such as
disk drives
dot matrix printer
robotics etc.
Stepper motors commonly
have a permanent
magnet rotor (shaft)
surrounded by a stator
8051 Interfacing Stepper Motor
Commonly used stepper motors There are 8 main poles on
have four stator windings that the stator, each having 5
are paired with a center–tapped tooth in the pole face
commonly.
• If the data read from columns is D3-D0 = 1111 , no key is pressed and
the process continues till key press is detected.
• If one of the column bits has a zero, means key press has occurred.
• If D3-D0 = 1101 this means that key in the column D1 has been
pressed.