8051 Introduction
8051 Introduction
❖ 8051 Microcontroller: Hardware, Software & Applications, V Udayashankara, M S Mallikarjunaswamy, TMH, 2009 DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
❖ 8051 Microcontroller Internals, Instructions, Programming and Interfacing, Subrata Ghoshal, Pearson, 2010
Introduction
(a) General-purpose Microprocessor System (b) Microcontroller
❖ Must add RAM, ROM I/O ports, timers to make system functional.
❖ Versatility on the amount of RAM,ROM and I/O ports
❖ Fixed amount of On chip ROM,RAM and I/O ports make them ideal
for many applications where cost and space are critical.
❖ The space, the power consumption and price per unit is much more
critical then computing power
DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
1
01-03-2023
Differences between 8085 and 8051 DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
❖ 8051 Microcontroller Internals, Instructions, Programming and Interfacing, Subrata Ghoshal, Pearson, 2010
2
01-03-2023
3
01-03-2023
Versions of 8051/52 Microcontroller From Dallas Semiconductor (Maxim) Versions of 8051 From Atmel (All ROM Flash)
4
01-03-2023
5
01-03-2023
Pipelining Concept
6
01-03-2023
7
01-03-2023
128/
8
01-03-2023
❖ 8 bit CPU with registers A (the accumulator) and register B, 8 bit ALU and working registers
❖ Eight bit program status word (PSW) and eight bit stack pointer (SP)
❖ Internal ROM or EPROM (8751) of 0 (8031) to 4 Kbytes (8051) of on-chip program memory
❖ 16 bit address bus multiplexed with port 0 and port 2 and 8 bit data bus multiplexed with port 0
❖ 64 Kbytes of external data memory address space (Read and Write) RD, WR Controlled
❖ 32 bidirectional Input/Output pins (as four 8 bit ports (P0-P3)) or 32 individually addressable I/O lines
❖ 6 sources/Five-vector interrupt structure with two priority levels (Two external and three internal and
Reset)
❖ Code memory selectable by EA DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
9
01-03-2023
10
01-03-2023
❖ 8051 can be reset in two ways 1) Power-on reset – which resets the 8051 when power is turned ON and 2) manual
reset – in which a reset happens only when a push button is pressed manually.
❖ For reset to happen, the reset input pin (pin 9) must be active high for at least 2 machine cycles.
❖ During a reset operation :- Program counter is cleared and it starts from 00H, register bank #0 is selected as default,
Stack pointer is initialized to 07H, all ports are written with FFH. A reset doesn’t affect contents of internal RAM.
DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
11
01-03-2023
❖ A general practice is to use an external crystal of 12 MHz frequency. For the systems using serial
communications facilities, a crystal of frequency 11.0592 MHz is used for the standard baud rates (9600,
4800, etc.) generation.
❖ Internally, multiple numbers of this system clock oscillation are used for various purposes. Each complete
oscillation of the external crystal or clock source is a pulse, two such pulses produce a state. Six states
generate one machine cycle. Instructions need one, two or four such machine cycles for fetching the opcode
and its execution
❖ ALE signal can be monitored through an oscilloscope and every machine cycle would generate two ALE
pulses. As MCS-51 depends on micro-programming, therefore so many pulses are necessary.
12
01-03-2023
Arrangement of program memory in XX51 Arrangement of internal and external data memory of XX51/52
with (a) EA = 1 and (b) EA = 0
❖ 8051 Microcontroller Internals, Instructions, Programming and Interfacing, Subrata Ghoshal, Pearson, 2010 DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
Registers
❖ Register are used to store information temporarily, while the information could be
❖ A byte of data to be processed, or
❖ An address pointing to the data to be fetched
❖ The vast majority of 8051 register are 8-bit registers
❖ There is only one data type, 8 bits
❖ The most widely used registers
❖ A (Accumulator)
❖ For all arithmetic and logic instructions
❖ B, R0, R1, R2, R3, R4, R5, R6, R7
❖ DPTR (data pointer), and PC (program counter)
13
01-03-2023
B-Register
❖ The 8 bit ‘B’ SFR is used during multiply and divide operations.
❖ In multiplication operation (MUL AB), one of the 8 bit operands is stored in ‘B’ register.
After the operation, it stores the higher byte of the result in ‘B’ register.
❖ In division operation (DIV AB), it holds 8 bit divisor and after the operation the remainder
is stored in ‘B’ register.
❖ For other instructions, it can be used as an 8 bit general purpose register.
❖ Register ‘B’ is bit addressable register and ‘B’ cannot be addressed indirectly.
14
01-03-2023
Signed arithmetic
❖ 8051 Microcontroller Internals, Instructions, Programming and Interfacing, Subrata Ghoshal, Pearson, 2010
Example
15
01-03-2023
PC-Program Counter
16
01-03-2023
17
01-03-2023
❖ DPTR is a 16 bit register, divided into two 8-bit registers DPH and DPL. DPH for Higher
order 8 bits, DPL for lower order 8 bits.
❖ DPTR, DPH, DPL are SFRs in 8051.
❖ 8051 uses DPTR to furnish memory addresses for internal and external code access and
external data access.
❖ The Data Pointer (DPTR) is the 8051’s only user-accessible 16-bit (2-byte) register.
(a) Output latch of one bit of port 1 (b) Output pin (showing 1 or 0 output) of port 1
(c) Reading from a port pin of port 1 (d) Reading output latch of port 1
DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
❖ 8051 Microcontroller Internals, Instructions, Programming and Interfacing, Subrata Ghoshal, Pearson, 2010
18
01-03-2023
(1)
(0)
19
01-03-2023
TB2
TB1
To switch off this FET, a ‘1’ must be written at the D-flip–flop, before
the port pin-reading operation. This is very important.
TB2
TB1
20
01-03-2023
TB2
TB1
(a) The 50% duty cycle means that the “on” and “off” states (or the high and low portions of
the pulse) have the same length. Therefore, we toggle P1.0 with a time delay in between
each state.
HERE: SETB P1.0 ;set to high bit 0 of port 1
LCALL DELAY ;call the delay subroutine
CLR P1.0 ;P1.0=0
LCALL DELAY
SJMP HERE ;keep doing it
21
01-03-2023
(b) The 66% duty cycle means the “on” state is twice the “off” state.
22
01-03-2023
In this program, instruction “JNB P1.2,AGAIN” (JNB means jump if no bit) stays in the loop as
long as P1.2 is low. When P1.2 becomes high, it gets out of the loop, writes the value 45H to
port 0, and creates an H-to-L pulse by the sequence of instructions SETB and CLR.
DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
23
01-03-2023
Read-Modify-Write Feature
24
01-03-2023
25
01-03-2023
26
01-03-2023
27
01-03-2023
Assembler Directives
❖ Assembler directives (Pseudo Operations or instructions) tell the assembler to do something
other than creating the machine code for an instruction. In assembly language
programming, the assembler directives instruct the assembler to
❖ Process subsequent assembly language instructions
❖ Define program constants
❖ Reserve space for variables
❖ END: The END directive signals the end of the assembly module. It indicates the end of the
program to the assembler. Any text in the assembly file that appears after the END directive
is ignored. If the END statement is missing, the assembler will generate an error message.
❖ DB (DEFINE BYTE): The DB directive is used to define an 8 bit data. DB directive initializes
memory with 8 bit values. The numbers can be in decimal, binary, hex or in ASCII formats.
For decimal, the 'D' after the decimal number is optional, but for binary and hexadecimal, 'B'
and ‘H’ are required. For ASCII, the number is written in quotation marks (‘LIKE’ This).
❖ DATA1: DB 40H ;hex
❖ DATA2: DB 01011100B ;b i n a r y
❖ DATA3: DB 48 ; decimal
❖ DATA4: DB ' HELLO W’ ; ASCII
28
01-03-2023
Assembler Directives
❖ EQU and SET: EQU and SET directives assign numerical value or register name to the
specified symbol name. EQU is used to define a constant without occupying a memory
location or storage for data items but associates a constant value with label. When the
label appears in the program, its constant value will be substituted for the label. The
symbol defined with EQU should not be redefined.
❖ SET directive allows redefinition of symbols at a later stage.
❖ By the use of EQU directive, value of constant used at multiple places in the program is
easily changed. One can change it once and the assembler will change all of its
occurrences
❖
DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
Addressing Modes-8051
29
01-03-2023
❖ We can also use immediate addressing mode to send data to 8051 ports
30
01-03-2023
❖ Direct addressing mode is provided to allow us access to internal data memory and Special
Function Register (SFR) only.
❖ In this addressing mode, data is obtained directly from the memory.
❖ In direct addressing, an 8 bit internal data RAM memory address is specified as part of the
instruction, It can specify the address only in the range of 00H to 7FH (entire 128 bytes) of RAM
but most often used to access the RAM locations 30-7FH or address of SFR (80H to FFH).
❖ The register bank locations are accessed by register name.
❖ Contrast this with immediate addressing mode, there is no # sign in the operand
31
01-03-2023
❖ The SFR (special function registers can be accessed by their names or by their addresses
32
01-03-2023
❖ Write code to send 55H to ports P1 and P2, using (a) their names, (b) their addresses.
33
01-03-2023
❖ Show the code to push R5, R6, and A onto the stack and then pop them back
them into R2, R3, and B, where register B = register A, R2 = R6, and R3 = R5.
34
01-03-2023
❖ Write a program to copy the value 55H into RAM memory locations 40H to 45H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and
(c) with a loop.
35
01-03-2023
❖ Write a program to copy the value 55H into RAM memory locations 40H to 45H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and
(c) with a loop.
❖ It makes accessing data dynamic rather than static as in direct addressing mode
❖ Looping is not possible in direct addressing mode
CLR A ;A=0
MOV R1,#60H ;load pointer. R1=60H
MOV R7,#16 ;load counter, R7=16 (10 in hex)
AGAIN: MOV @R1,A ;clear RAM location R1 points to
INC R1 ;increment R1 pointer
DJNZ R7,AGAIN ;loop until counter = zero
36
01-03-2023
❖ Write a program to copy a block of 10 bytes of data from RAM locations starting at 35H
to RAM locations starting at 60H.
❖ In indexed addressing, a separate register—either the program counter (PC), or the data
pointer (DTPR)—is used to hold the base address, and the A is used to hold the offset
address. Adding the value of the base address (pointer) to the value of the offset address in
A forms the effective address.
❖ Indexed addressing is used with JMP or MOVC instructions.
❖ This addressing mode is used in accessing data elements of look up tables located in the
program ROM only .
❖ The instruction used for this purpose is
37
01-03-2023
❖ In many application, the size of program code does not leave any room to share the 64 kB
code space with data.
❖ The 8051 has another 64kB of memory space set aside exclusively for data storage. This
data memory space is referred to as external memory and it is accesses only by the
MOVX instruction.
38
01-03-2023
❖ Here one is internal memory location and other is external memory location so first the content of
external memory location FF00H is loaded in accumulator then the content of internal memory
location FFH is saved first and then content of accumulator is transferred to FFH. now saved
content of FFH is loaded in accumulator and then it is transferred to FF00H.
❖ Write a program to toggle P1 a total of 200 times. Use RAM location 32H to hold
your counter value instead of registers R0 - R7.
39
01-03-2023
40
01-03-2023
41
01-03-2023
Example
❖ For each of the following instructions, state to which port the bit belongs.
❖ (a) SETB 86H (b) CLR 87H (c) SETB 92H (d) SETB 0A7H
42
01-03-2023
❖ JNC and JC are used to check the carry flag bit (CY), there are no instructions for the
overflow flag bit (OV). How would you write code to check OV?
❖ The OV flag is PSW.2 of the PSW register. PSW is a bit-addressable register; therefore,
we can use the following instruction to check the OV flag.
JB PSW.2,TARGET ;jump if OV=1
❖ Write a program to save the status of bit P1.7 on RAM address bit 05.
MOV C,P1.7 ;get bit from port
MOV 05,C ;save bit
DR. ASHOK KHERODIA, ECE DEPARTMENT, IIIT KOTA
43
01-03-2023
44
01-03-2023
45