IV Unit MPMC
IV Unit MPMC
1
Boolean Processor:
Boolean processor has its own instructions set, accumulator and bit addressable RAM.
Carry flag serves as the accumulator.
The instructions that allow bit manipulations perform operations like compliment bit, set bit and clear
bit, jump if bit set, AND, OR operations. The result is stored into the carry bit.
2
Registers:
The general purpose or working registers, stack pointer, program counter and in addition to these CPU
registers, there are 21 special function registers (SFRs).
The working registers are accumulator, B register and four register banks. Each register bank has 8
registers R0 through R7.
Accumulator:
It is an 8 bit register. It is referred as register ‘A’ It is used by the all the arithmetic and logical
instructions. Accumulator has a special importance, that is one of the operands is stored in it before the
execution of an instructions and it also stores the result after the execution of an instruction.
B Register:
It is an 8 bit register. B register is used as a general purpose register when it is not being used by the
multiplication and division instructions. While multiplying, it holds one of the 8 bit operands and after the
execution of the multiplication, it stores the higher byte of the result. While dividing, it holds an 8 bit divisor
and after the execution, the remainder is stored in B register.
Registers R0 through R7:
These are used as scratch pad registers. There are four register banks each containing R0 through R7.
At a time only one bank can be selected by RS0 and RS1 bits in the PSW.
Stack Pointer:
Stack pointer is an 8 bit register. it is incremented during push or call operations and is decremented
during pop or return instructions.
Program Counter:
It is a 16 bit register. PC always points to the instruction to be fetched and it is automatically
incremented after fetching the instruction.
Special function registers:
The RAM locations from 80H to FFH are reserved for the special functions and therefore these are
called as Special function registers (SFRs). The SFRs are used for the special functions and cannot be used
for any other purpose. These SFRs are used for control or show the status of various functions done by
microcontroller. All SFRs are directly addressable and can be read or written to as well.
Data Pointer:
It is a 16 bit register, contains two bytes. The higher byte is called DPH and lower byte is called DPL.
It is used for addressing the off-chip data and program with the MOVX and MOVC commands respectively.
Timer registers:
Register pairs (TH0, TL0), (TH1, TL1) from two 16 bit timers in 8051. The operation may be timing
or counting. There are various modes in which timers can be configured. For this TCON and TMOD
registers are used.
3
Ports 0 to 3:
P0, P1, P2 and P3 are the SFRs corresponding to four I/O ports respectively. Each of these ports is bit
as well as byte addressable.
Control Registers:
TCON, TMOD, IE, IP, SCON, PCON contain the control and status for interrupts, serial I/O and
timers/counters.
Oscillator:
8051 operates at 12MHz frequency.. The quartz crystal has to be connected externally and all the other
oscillator circuit is on chip.
4
MEMORY ORGANISATION;
In the 8051, the memory is organized logically into program memory and data memory separately.
The program memory is read-only type; the data memory is organized as read–write memory.
Again, both program and data memories can be within the chip or outside.
The Intel 8051 has 128 bytes of RAM and 4 KB of ROM within the chip.
The address bus of the 8051 is 16 bits wide. So it can access 64 KB of memory.
• As the memory is organized separately as program memory and data memory, the 8051
microcontroller can access 64 KB of program memory and 64 KB of data memory.
• The user can configure the entire program memory outside the chip or use 4 KB inside and 60 KB
outside the chip.
• The internal data memory is accessed with 8-bit addresses and the external data memory with 16-bit
addresses.
• So the maximum data memory that can be connected to the 8051 system is 64 KB.
SPECIAL
EXTERNAL
FUNCTION
PROGRAM
REGISTERS
MEMORY
(21) EXTERNAL
(EA=0) DATA
MEMORY
(EA=0)
INTERNAL EXTERNAL
PROGRAM PROGRAM INTERNAL
MEMORY MEMORY DATA
MEMORY
(EA=1) (EA=0) (EA=1)
5
INTERNAL RAM STRUCTURE:
• The 8051 has 128 bytes of internal data RAM, which is accessible as bytes or sometimes as bits.
• The address of the internal RAM starts at 00H and occupies space up to 7FH. The RAM space is
divided into three blocks—the register banks, the bit-addressable memory, and the scratch pad
memory.
• The 8051 has four register banks of eight registers each, with addresses from 00H to 1FH. In assembly
language, they are addressed by the names R0–R7.
• The register banks are identified with 2 bits in the processor status word.
• The PSW has two bits for identifying the register bank, i.e., 00 represents bank 0, 01 represents bank
1, 10 represents bank 2, and 11 represents bank 3.
• In the 8051, bitwise operations are also possible with special instructions using the bit addresses. The
bit-addressable memory is both bit-addressable (from 00H to 7FH) and byte-addressable (from 20H to
2FH). Bit operations are helpful in many control algorithms.
Using general-purpose scratch pad memory, programmers can read and write data at any time for any purpose.
This memory ranges from the byte address 30H to the address 7FH.
6
1F R7 7F
1E R6
1D R5
1C R4
BANK 3 1B R3
1A R2
19 R1
18 R0
17 R7
16 R6
15 R5
14 R4
BANK2
13 R3
12 R2
11 R1
10 R0
0F R7 2F 7F 78
0E R6 2E 77 70
0D R5 2D 6F 68
0C R4 2C 67 60
BANK1
0B R3 2B 5F 58
0A R2 2A 57 50
09 R1 29 4F 48
08 R0 28 47 40
07 R7 27 3F 38
06 R6 26 37 30
05 R5 25 2F 28
04 R4 24 27 20
BANK 0
03 R3 23 1F 18
02 R2 22 17 10
01 R1 21 0F 08
00 R0 20 07 00 30
Register Banks Bit Addressable Memory Scratch Pad Memory
7
SPECIAL FUNCTION REGISTERS
• SFR, which occupies upper 128 bytes of internal memory are the registers, that control the entire
processor
• They can e accessed by DIRECT addressing.
• The registers available in the 8051 are as follows :
• Accumulators - A and B
• Process Status Word - PSW
• I/O port registers - P0, P1, P2, P3
• Data pointers - DPH and DPL
• Serial data buffer register - SBUF
• Stack pointer - SP
• Timer registers - TH0, TH1 and TL0, TL1
• Timer Control Registers - TCON, TMOD
• Power and Port control - PCON, SCON
• Interrupt Control Registers - IP, IE.
• Programmers should not use the addresses in the range 80H to FFH (other than SFR) as it is used by
INTEL CORPORATION for expansion functions of 8051.
• The 8051 has two accumulators -A register and B register.
• The register B forms the accumulator for multiplication and division instructions and for other
instructions it can be accessed as a general purpose register.
• The stack in the 8051 is organized within the internal RAM area.
• The stack pointer is eight bits wide and has to be initialized with an address in the RAM area.
• When the 8051 is reset, the stack pointer is by default set to 07H.
• The stack pointer is incremented before storing a data in the stack.
• Similarly, while reading data from the stack, the data is read first and then the stack pointer is
decremented.
Special Function Registers of 8051:
80 P0
81 SP
82 DPL
83 DPH
87 TCON
88 TMOD
8
89 TL0
8A TL1
8B TH0
8C TH1
90 P1
98 SCON
99 SBUF
A0 P2
A8 IE
B0 P3
B8 IP
D0 PSW
E0 ACC
F0 B
Bit Address D7 D6 D5 D4 D3 D2 D1 D0
•
• Parity bit (P)
– It is set to 1 if the accumulator contains an odd number of 1s, after an arithmetic or logical
operation.
9
RS1 RS0 Selected Bank Address Range
ACCUMULATOR:
B REGISTER:
B register bits B.7 B.6 B.5 B.4 B.3 B.2 B.1 B.0
Bit Address F7 F6 F5 F4 F3 F2 F1 F0
• Some of these modes let the microcontroller go into a ‘sleep’ mode, which makes it consume lesser
power than during normal operation.
• The power control modes are selected through the Special Function Register PCON.
10
Bit Name Explanation of Function
6 - Reserved
5 - Reserved
4 - Reserved
IDLE MODE:
• The micro-controller enters the idle mode whenever the PCON.0 bit is set to 1.
• In the idle mode, the clock pulses applied to the CPU are masked while all other units like interrupt
controller etc., will be kept active.
• The contents of the CPU are not affected in this idle mode.
• The processor can be revoked -idle mode -hardware interrupt or by giving a hardware reset signal.
• These two actions - reset PCON.0 and the processor execution -resumed to the instruction following
the instruction that set idle mode.
• In general, the stack is initialized in the internal RAM area. Any 8-bit data can be stored and retrieved
from the stack using PUSH and POP instructions, with the help of the stack pointer.
• The stack pointer (SP) is an 8-bit register within the SFR area, with the address 81H.
• This register can hold one 8-bit address at a time, which is actually the memory location at top of the
stack.
• A push operation in the 8051 is used to store an 8-bit data in the stack.
• The PUSH instruction first increments the value of SP and then stores the data mentioned in the
instruction in the memory location pointed to by SP.
• Similarly, the POP instruction stores the value from the top of the stack in the register mentioned in
the instruction and then decrements the value of SP.
11
• The stack pointer is initialized to the value 07H when the 8051 microcontroller is reset.
• The other instructions of the 8051 that affect the stack and the stack pointer are ACALL, LCALL,
RET, and RETI.
• The stack pointer can be initialized to any internal RAM address by the programmer, by writing the
required address in the SP SFR address 81H.
12
INSTRUCTION SET OF 8051
• Instruction supported by 8051 can be classified into different types depending upon their operational
functions.
• The instruction set classification is as followed.
Addressing Modes
Mnemonic Operation
Direct Indirect Register Immediate
DPTR = 16-bit
MOV DPTR, # data 16 √
immediate data
INC SP:
PUSH <src> √
MOV “@SP”, <scr>
MOV <dest>,
POP <dest> “@SP”: √
DEC SP
ACC and @ Ri
XCHD A, @Ri exchange low √
nibbles
13
Copy 8 bit data from
the external RAM
MOVX A, @Ri Only Indirect Addressing mode
location pointed to
by Ri to register A
Read Program
MOVC A, @A + DPTR Memory at (A + Only Indirect Addressing mode
DPTR)
Read Program
MOVC A, @A + PC Only Indirect Addressing mode
Memory at (A + PC)
• The instructions with the mnemonic MOVX is used to access data from external memory locations
using indirect addressing only. –
• MOVX instruction must use Accumulator (A) register as -destination or source and the other is
indirectly accessed external memory.
• MOVX can be used -8 bit external memory address and 16 bit external memory address. It can be
noted that the external memory -interfaced with 8051 with either 8 bit address or 16 bit address.
• If the 8 bit address is used-internal register (any location in Internal RAM) -hold the address of the
memory. If 16 bit address is used-Data Pointer (DPTR) is used to hold the address.
• The instructions MOVC A,@A+DPTR and MOVC A,@A+PC are the two instructions meaning
MOVE CODE MEMORY and are used to transfer data from program memory using indexed
addressing
14
• The program memory addressing using MOVC instruction needs 16 bit address. So, the Data Pointer
register (DPTR) and Program Counter (PC) -base registers -instructions.
• Data can only be read from the program memory and not written into because the program memory is
generally ROM.
• PUSH instruction is used to copy data in any internal RAM location to the stack
• The POP instruction is used to copy data from the top of the stack to the RAM location specified in
the instruction.
• XCHD is used to transfer only the lower-order nibble between the accumulator and the indirectly
addressed internal RAM.
• XCH is used to exchange the contents of the accumulator and a register or the internal memory of the
8051.
Arithmetic Instructions:
• These instructions are used to do arithmetic operations.
• The common arithmetic operations like addition, subtraction, multiplication and division are possible
with 8051.
• All the data used in arithmetic instructions must be available inside the controller i.e. in the internal
RAM area only.
• ADD instruction is used to add any 8 bit data with Accumulator and the result is stored in Accumulator
(A) register. The carry generated if any is stored in Carry flag of the processor status word.
• The ADDC instruction is also used to add any 8 bit data with Accumulator along with Carry bit.
• The SUBB instruction -subtract contents of a register from the Accumulator content and during this
subtraction, the Carry bit is also subtracted from the accumulator.
• For ADD and SUBB instructions, one of the data must be in Accumulator and the other data - in any
direct addressed or indirect addressed internal memory location or can be an immediate data.
• In addition to - ADD, ADDC and SUBB instructions in 8085, -have instructions MUL and DIV.
• The register B is exclusively used for these two instructions. The operands should be stored in the
registers A and B for the MUL and DIV instructions.
• The MUL instruction multiplies the contents of A and B registers and stores the 16 bit result in the
combined A and B registers.
• The lower order byte -result is stored in A register and the higher order byte - stored in B register.
• The DIV instruction upon execution will divide the contents of A register by the contents of B register.
• The quotient of the result - stored in A register and the remainder is stored in B register.
• A division by 0 i.e. 0 in the B register before executing DIV AB will result in the overflow flag (OV)
set to 1.
• DA A instruction -to convert binary sum obtained after adding two BCD numbers into BCD number.
15
Addressing Modes
Mnemonic Operation
Direct Indirect Register Immediate
ADD A,
A = A + <byte> √ √ √ √
<byte>
ADDC A = A +
√ √ √ √
A,<byte> <byte>+C
SUBB A, A = A –
√ √ √ √
<byte> <byte>–C
A = Int [A/B]
DIV AB Accumulator Only
B = Mod [A/B]
Decimal
DA A Adjust Accumulator Only
Accumulator
Logical Instructions:
• In addition to logical AND, OR and XRL operation, 8051 has additional instructions - CLR, CPL. All
the data for the logical instructions -available in the internal RAM only.
• The instruction CLR A -to clear the contents of A register, CPL is used to complement or logically
invert the contents of the A register and SWAP - to swap the nibbles of A register.
• 8051 supports four rotate operations with the options –rotating left or right and rotating through carry
or not.
Addressing Modes
Mnemonic Operation
Direct Indirect Register Immediate
16
XRL A, <byte> A = A XOR <byte> √ √ √ √
Branching Instructions:
• 8051 supports unconditional jumping and subroutine calling in three different ways.
• They are Absolute jump AJMP, ACALL, long jump LJMP, LCALL, and short jump SJMP.
Addressing Modes
Mnemonic Operation
Direct Indirect Register Immediate
CJNE Jump if A ≠=
√ √
A,<byte>,rel <byte>
17
Bit Manipulation Instructions:
• The special feature of the 8051 micro controller is that it can handle bit data also like that of byte data.
• The internal data memory map of 8051 has a bit- addressable area also.
• The special function registers that have the address with 0 or 8 as last digit in their hex address, are
also bit addressable.
• The bit manipulation instructions include logical instructions and conditional branching.
Mnemonic Operation
CLR C C=0
SETB C C=1
CPL C C = NOT C
JC rel Jump if C = 1
• The logical instructions - ANL and ORL. Conditional branching - JC, JNC, JB, JNB, JBC.
• The other instructions available -CLR, SETB, CPL, and MOV.
• There are no instructions for halting the machine execution.
• Figure 10.6 shows the flag bits affected by the various instructions.
• Increment and decrement instructions do not affect the flag register.
18
Assembler Directives:
The assembler directives are special instruction to the assembler program and are used to define specific
operations. These directives are not part of the executable program.
ORG - OriGinate, defines the starting address for the program in program (code) memory
EQU - EQUate, assigns a numeric value to a symbol identifier so as to make the program more readable.
DB - Define a Byte, puts a byte (8-bit number) number constant at the memory location specified.
DW - Define a Word, puts a word (16-bit number) number constant at the memory location specified.
DBIT - Define a Bit, defines a bit constant, which is stored in the bit addressable section of the Internal RAM.
END - This is the last statement in the source file to advise the assembler to stop the assembly process.
19
Internal Structure of Port 1
20
Internal Structure of Port 2
Structure of Port 3:
• Port 3 is different from the other ports in the aspect that individual port pins can be programmed for
input and output operation.
• Each pin of port 3 can be programmed - for input, output operation or alternate functions.
• All the Port3 pins -serve an alternate function according to the hardware signals and interfacing.
• The alternate functions can be activated only if the port3 bits are written with 1s in their position.
The instructions, which have the operations – Read, Modify and write to the port – read the Port latches and
not the pins.
21
8051 Timers
• The 8051 comes -with two 16 bit timers, both of which may be controlled, set, read, and configured
individually. The 8051 timers have three general functions:
o Programming predefined length of time, and then issuing an interrupt request.
o Counting the transitions on an external pin,
o Generating baud rates for the serial port.
• If the timer registers are incremented by the internal clock pulses from the microcontroller, then the
operation is termed as ‘Timing’ operation.
• Meanwhile if the timer registers get their clock pulses from an external device through the port 3 pins
of 8051, then the operation is termed as ‘Counting’.
• Timer 0 external input pin P3.4 (T0) is used give clock input to timer 0 to act as counter.
• Timer 1 external input pin P3.5 (T1) is used give clock input to timer 1.
• One timer is TIMER0 and the other is TIMER1. Each timer also has two 8 bit SFRs namely TH0 and
TL0 forming the higher and lower order bytes of Timer0 and TH1 and TL1 forming the higher and
lower bytes of Timer1.
TMOD:
The TMOD SFR -used to control the mode of operation of both timers.
The higher order four bits (bits 4 through 7) relate to Timer 1 whereas the low four bits (bits 0 through 3)
perform the same functions for timer 0.
22
TCON:
Bit
D7 D6 D5 D4 D3 D2 D1 D0
position
Timer 0
Time 1 Interrupt
Interrupt. 0.
Timer 1 Timer 0
Set to 1 by
run run Set to 1 by
External software External
Timer 1 control Timer 0 control software
Interrupt for edge Interrupt
Explanation Overflow bit. Set Overflow bit. Set to for edge
1 edge triggering 0 edge
flag to 1 by flag 1 by triggering
detect bit and detect bit
software software and
cleared for
to run. to run. cleared for
level
triggering level
triggering
• When TL0/1 is incremented from 31, it will "reset" to 0 and increment TH0/1. So, the timer can only
contain 8192 values from 0 to 8192.
• The timer can be operated as timer with internal clock pulses or as a counter with external clock pulses.
• This selection is done by D2 bits of TMOD for Timer 0 and D6 bit of TMOD for Timer1
• The clock pulses selected by D2 and D6 bits of TMOD is then controlled by programmer setting and
connected to the Timer registers. The control is by three different means.
• First is the Timer Run control bits D4 and D6 of TCON register. The timer will run only when Timer
run control bits are set to 1.
• The other controls for the timers are through the GATE control bits D4 and D7 of TMOD and the
External inputs for timer. Setting GATE to 1 allows the timer to count only if the external control input
INT0 or INT1 is set to 1. Setting Gate to 0 will disable the corresponding external timer control inputs
INT0 and INT1.
• Setting Timer to mode 0 will overflow back to zero after 8192 counts. This will set the TF1 and TF0
bits for timer 1 and timer 0 respectively.
23
Mode 0 Operation of Timer 1 of 8051
24
Mode 3 – Split Timer Mode
Mode "3" of 8051 timer is a split-timer mode and is applicable only for Timer 0. When Timer 0 is placed
in mode 3, it essentially becomes two separate 8-bit timers. That is, Timer 0 is TL0 and Timer 1 is TH0.
Both timers count from 0 to 255 and overflow back to 0.
In mode 3, all the bits that are related to Real Timer 1 will simply hold its count and will not run and the
situation is similar to keeping TR1=0.
In Split Timer mode of Timer 0, the real Timer 1 (i.e. TH1 and TL1) can not be started or stopped since
the bits that do that are now linked to TH0. The real timer 1, in this case, will be incremented every
machine cycle no matter what.
When two separate timers in addition to a baud rate generator is required in an application, then real
Timer 1 can be used as a baud rate generator and TH0/TL0 can be used as two separate timers in mode
3.
25
8051 Interrupts
• 8051 basically has following five interrupt sources so that any of the following events will make 8051
to execute an interrupt service routine.
• Timer 0 Overflow.
• Timer 1 Overflow.
• Reception/Transmission of Serial Character.
• External hardware interrupt 0.
• External hardware interrupt 1.
Bit position D7 D6 D5 D4 D3 D2 D1 D0
Bit Address AF AC AB AA A9 A8
Priority of Interrupts:
External 0 Interrupt
Timer 0 Interrupt
External 1 Interrupt
Timer 1 Interrupt
Serial Interrupt
• So, whenever the External 0 interrupt and Timer 1 interrupt occurs at the same instant, then 8051
microcontroller executes the interrupt service routine corresponding to External 0 interrupt first.
• Then 8051 microcontroller will return to the main program, execute one instruction and then execute
the interrupt service routine corresponding to Timer 1 Interrupt.
• Interrupt priorities are controlled by the IP SFR (B8h). For example, if the Serial Interrupt is much
more important than the Timer 0 interrupt, then the Interrupt Priority register IP SFR at the address
B8h can be properly programmed to set the priority.
26
• This is done by assigning a high priority to the Serial Interrupt and a low priority to the Timer 0
Interrupt. By setting the D4 bit to 1, the serial interrupt will be set to higher priority and making D1
bit to 0, the Timer 0 interrupt will be set to lower priority.
• For example, the timer 0 interrupt priority can be made high by setting the D1 bit of IP SFR. So, the
following instructions can be used for the same.
• SETB PT0 (or) SETB B9H (or) MOV IP, #82H
IP register:
Bit position D7 D6 D5 D4 D3 D2 D1 D0
Bit Address BC BB BA B9 B8
Explanation Enable
Interrupts External External
- Serial Timer 1 Timer 0
1 0
Undefined Undefined Interrupt Interrupt Interrupt
Made 0 to Interrupt Interrupt
Priority Priority Priority
disable all Priority Priority
interrupts
30
8051 Serial Ports:
• One of the 8051’s many powerful features is its integrated Universal Asynchronous Receiver
Transmitter (UART), otherwise known as a serial port. With integrated serial port of 8051, data can
be transmitted and received easily by reading and writing the data to the serial port registers.
• In addition to the above two registers, the MSB of PCON register named as SMOD bit is used to
double the baud rate of serial transmission and reception.
31
Definition of Bits SM0 and SM1 in SCON SFR
OPERATING MODES
• Bits SM0 and SM1 are used to set the serial mode to a value between 0 and 3.
• Selecting the Serial Mode selects the mode of operation (8-bit/9-bit, UART or Shift Register) and also
determines how the baud rate will be calculated.
• In modes 0 and 2 the baud rate is fixed based on the oscillator’s frequency. In modes 1 and 3 the baud
rate is variable based on Timer 1 overflows.
MSB LSB
32
Mode 2 (9 bit UART):
In this mode, 11 bits are transmitted through TxD or received through RxD.
The 11 bits are made up of one start bit (always 0), 8 data bits (LSB first), a programmable 9th data
bit, and a stop bit (always 1).
• The 9th data bit transmitted is same as TB8 bit in SCON special function register. It can be assigned
the value of 0 or 1 by the programmer.
• On reception, the 9th data bit goes into RB8 in Special Function Register SCON, while the stop bit is
ignored.
• The baud rate is programmable to either 1/32 or 1/64 of the oscillator clock frequency.
In this mode, 11 bits are transmitted through TxD and simultaneously 11 bits are received through
RxD.
The 11bits are made up of a start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a
stop bit (1). In fact, Mode 3 is the same as Mode 2 in all respects except baud rate.
The baud rate in Mode 3 is variable. The baud rate for mode 3 is fixed at the following rate similar to
mode1.
Baud rate= 2SMOD/64 X (Timer 1 overflow rate)
SBUF
• SBUF is physically two registers with the same address.
• When data to be transmitted is written to the SBUF register, then it will be shifted bit by bit into the
TXD line of 8051. The port 3 pin 3.1 acts as the TXD line. The shifting is done by the transmit clock
which determines the baud rate.
• Similarly, when the data bits are received on the RXD line (Pin 3.0 of port 3), the bits are shifted
serially into the shift register according to the Receive clock.
• After the reception is complete, the data received will be placed on SBUF from where it can be read
by the programmer through the internal bus.
33
34