You are on page 1of 49

EC8691 - MICROPROCESSORS AND

MICROCONTROLLERS

U Vinothkumar
Asst Prof
Dept of ECE
Dr.N.G.P.Institute of Technology
Coimbatore.
UNIT IV MICROCONTROLLER 9

Architecture of 8051 – Special Function


Registers(SFRs) - I/O Pins Ports and Circuits –
Instruction set - Addressing modes - Assembly
language programming.

2
3
Cont…

Features of 8051 Microcontroller:

1. It is an 8-bit microcontroller.

2. Single-supply +5 volt operation using HMOS technology.

3. 4096 bytes program memory on-chip (ROM).

4. 128 bytes data memory on-chip (RAM).

5. Four register banks namely Bank0,Bank1,Bank2,Bank3.

6. It has 32 general purpose registers each of 8-bit, 16 bytes of bit


addressable RAM locations and 80bytes of general purpose data
memory or scratch pad.

7. It supports 64 Kilobytes of separate program memory (ROM) and


data memory (RAM).

8. 8-bit bidirectional data bus and 16-bit unidirectional address bus.

4
Cont…

9. on-chip clock oscillator.

10. It has four 8 bit ports namely port 0, port1, port2 and port3.

11. Three internal and two external Interrupts.

12. It has two16bit timer/counters.

13. 14. It has full duplex UART.

5
BLOCK DIAGRAM OF 8051 Microcontroller

6
Pin diagram of 8051 Microcontroller

7
Cont…
• Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any
other functions. It is internally pulled up, bi-directional I/O port.
• Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its
initial values.
• Pins 10 to 17 − These pins are known as Port 3. This port serves some
functions like interrupts, timer input, control signals, serial communication
signals RxD and TxD, etc.
• Pins 18 & 19 − These pins are used for interfacing an external crystal to
get the system clock.
• Pin 20 − This pin provides the power supply to the circuit.
• Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port.
Higher order address bus signals are also multiplexed using this port.
• Pin 29 − This is PSEN pin which stands for Program Store Enable. It is
used to read a signal from the external program memory.
• Pin 30 − This is EA pin which stands for External Access input. It is used
to enable/disable the external memory interfacing.
• Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used
to demultiplex the address-data signal of port.
• Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port.
Lower order address and data bus signals are multiplexed using this port.
• Pin 40 − This pin is used to provide power supply to the circuit.
8
MEMORY ORGANIZATION
Cont…
INTERNAL AND EXTERNAL PROGRAM MEMORY (ROM):

9
Cont…
INTERNAL AND EXTERNAL DATA MEMORY (RAM):

10
Cont…
RAM (MEMORY ORGANIZATION):

11
Cont….
Central Processing Unit (CPU):
The CPU of 8051 consists of eight-bit Arithmetic and Logic unit
with associated registers like A, B, PSW, SP, the sixteen bit
program counter and “Data pointer” (DPTR) registers.

ALU:
 The 8051’s ALU can perform arithmetic and logic functions on
eight bit variables.
 The arithmetic unit can perform addition, subtraction,
multiplication and division.
 The logic unit can perform logical operations such as AND,
OR, and Exclusive-OR, as well as rotate, clear, and complement.
 An important and unique feature of the Intel 8051
Architecture and 8031 Architecture is that the ALU can also
manipulate one bit as well as eight-bit data types.
 Individual bits may be set, cleared, complemented, moved,
tested, and used in logic computation.

12
Cont…

Special Function Register:

 8051 uses memory mapped I/O through a set of special function


registers that are implemented in the address space immediately
above the 128 bytes of RAM.

 8051 contains 21 special function registers.

 All access to the four I/O ports, the CPU registers, interrupt-
control registers, the timer/counter, UART, and power control are
performed through registers between 80H and FFH.

13
Cont…

14
Cont…

Register A (Accumulator):
 It is an 8-bit register.
 It holds a source operand and receives the result of the arithmetic
instructions (addition, subtraction, multiplication, and division).
 The accumulator can be the source or destination for logical
operations and a number of special data movement instructions,
including look-up tables and external RAM expansion.
 Several functions apply exclusively to the accumulator : rotate,
parity computation, testing for zero, and so on.

15
Cont…

Register B:
 In addition to accumulator, an 8-bit B-register is available as a
general purpose register.
 It is used for the hardware multiply/divide operation.

16
Cont…
Program Status Word (Flag Register):
Many instructions implicitly or explicitly affect (or are affected by)
several status flags, which are grouped together to form the Program
Status Word.
It is an 8-bit word, containing the information as follows.

17
Cont…
Stack Pointer :
 The stack pointer register is 8 bit wide.
 It is incremented before data is stored during PUSH,POP,CALL and
RET instructions.
 The stack array can reside anywhere in on-chip RAM.
 The stack pointer is initialized to 07H after a reset. This causes
the stack to begin at location 08H.

18
Cont…
Data Pointer (DPTR) :
 The data pointer (DPTR) consists of a high byte (DPH) and a low
byte (DPL).
 Its function is to hold a 16 bit address.
 It may be manipulated as a 16 bit data register or as two
independent 8 bit registers.
 It serves as a base register in indirect jumps, lookup table
instructions and external data transfer.

19
Cont…
I/O Port Registers (P0, P1, P2 and P3):

• The 8051 Microcontroller has four Ports which can be used as


Input and/or Output.
• These four ports are P0, P1, P2 and P3.
• Each Port has a corresponding register with same names (the Port
Registers are also P0, P1, P2 and P3).
• The addresses of the Port Registers are as follows: P0 – 80H, P1 –
90H, P2 – A0H and P2 – B0H.
• Each bit in these SFRs corresponds to one physical Pin in the 8051
Microcontroller.
• All these Port Registers are both Bit Addressable and Byte
Addressable.
• Writing 1 or 0 on a Port Register Bit will reflect as an appropriate
voltage (5V and 0V) on the corresponding Pin.
• If a Port Bit is SET (declared as 1), the corresponding Port Pin will
be configured as Input and similarly if a Port Bit is CLEARED
(declared as 0), the corresponding Port Pin is configured as
Output.
• Upon reset, all the Port Bits are SET (1) and hence, all the Port
Pins are configured as Inputs.

20
Cont…
I/O Port Registers (P0, P1, P2 and P3):

21
Cont…
PCON (Power control Register):
• The PCON or Power Control register, as the name suggests is used
to control the 8051 Microcontroller’s Power Modes and is located
at 87H of the SFR Memory Space. Using two bits in the PCON
Register, the microcontroller can be set to Idle Mode and Power
Down Mode.
• During Idle Mode, the Microcontroller will stop the Clock Signal to
the ALU (CPU) but it is given to other peripherals like Timer,
Serial, Interrupts, etc. In order to terminate the Idle Mode, you
have to use an Interrupt or Hardware Reset.
• In the Power Down Mode, the oscillator will be stopped and the
power will be reduced to 2V. To terminate the Power Down Mode,
you have to use the Hardware Reset.
• Apart from these two, the PCON Register can also be used for few
additional purposes. The SMOD Bit in the PCON Register is used to
control the Baud Rate of the Serial Port.
• There are two general purpose Flag Bits in the PCON Register,
which can be used by the programmer during execution.

22
Cont…

TCON (Timer Control register):


• Timer Control or TCON Register is used to start or stop the Timers
of 8051 Microcontroller.
• It also contains bits to indicate if the Timers has overflowed.
• The TCON SFR also consists of Interrupt related bits.

23
Cont…

24
Cont…
TMOD (Timer Mode Register):
• The TMOD or Timer Mode register or SFR is used to set the
Operating Modes of the Timers T0 and T1.
• The lower four bits are used to configure Timer0 and the higher
four bits are used to configure Timer1.

25
Cont…
• GATE1 = 1 ==> Timer1 is operated only if INT1 is SET.

• GATE1 = 0 ==> Timer1 operates irrespective of INT1 pin.

• GATE0 = 1 ==> Timer0 is operated only if INT0 is SET.

• GATE0 = 0 ==> Timer0 operates irrespective of INT0 pin.

The C/Tx bit is used selects the source of pulses for the Timer to count.

• C/T1 = 1 ==> Timer1 counts pulses from Pin T1 (P3.5) (Counter Mode)

• C/T1 = 0 ==> Timer1 counts pulses from internal oscillator (Timer Mode)

• C/T0 = 1 ==> Timer0 counts pulses from Pin T0 (P3.4) (Counter Mode)

• C/T0 = 0 ==> Timer0 counts pulses from internal oscillator (Timer Mode)

26
Cont…
IE (Interrupt Enable Register):
• The IE or Interrupt Enable Register is used to enable or disable
individual interrupts.
• If a bit is SET, the corresponding interrupt is enabled and if the bit
is cleared, the interrupt is disabled.
• The Bit7 of the IE register i.e. EA bit is used to enable or disable
all the interrupts.

27
Cont…

IP (Interrupt Priority Register):


• The IP or Interrupt Priority Register is used to set the priority of the
interrupt as High or Low.
• If a bit is CLEARED, the corresponding interrupt is assigned low
priority and if the bit is SET, the interrupt is assigned high priority.

28
Cont…
SCON (Serial Control Register):
• The Serial Control or SCON SFR is used to control the 8051
Microcontroller’s Serial Port.
• It is located as an address of 98H.
• Using SCON, you can control the Operation Modes of the Serial
Port, Baud Rate of the Serial Port and Send or Receive Data using
Serial Port.
• SCON Register also consists of bits that are automatically SET
when a byte of data is transmitted or received.

29
Cont…
SBUF (Serial Data Buffer):
• The Serial Buffer or SBUF register is used to hold the serial data
while transmission or reception.

TL0/TH0 (Timer 0 Low/High):


The Timer 0 consists of two SFRs: TL0 and TH0. The TL0 is the lower
byte and the TH0 is the higher byte and together they form a 16-bit
Timer0 Register.

30
Cont…
TL1/TH1 (Timer 1 Low/High):
The Timer 0 consists of two SFRs: TL1 and TH1. The TL1 is the lower
byte and the TH1 is the higher byte and together they form a 16-bit
Timer1 Register.

31
I/O PORT PINS

32
Cont…
PORT 0:
8-bit Bidirectional I/O port in SINGLE CHIP MODE.
Serves as LOWER ORDER MULTIPLEXED ADDRESS and DATA BUS
(AD7-AD0) in EXPANDED MODE.
Transmits CODE BYTES during program verification of ROM. Uses
EXTERNAL PULLUP RESISTORS in this case.
When a pin has 1 in it , it acts INPUT PIN. When a pin has 0 in it , it
acts as OUTPUT PIN.
Port 0 is a TRUE BIDIRECTIONAL PORT ; Provisions are there for
READING the CONTENTS of LATCH and PIN.
The top and bottom transistors are NMOS transistors. The top input of
MUX [Connected to inverter] is labeled as 1 and lower input of
MUX[connected to 𝑄 of D-latch) is labeled as 0.
When CONTROL LINE is 0 , the port functions as NORMAL
BIDIRECTIONAL I/O port. Under this condition , writing “1” to the
LATCH makes the PORT PIN to function as INPUT or OUTPUT port.
When CONTROL LINE is 1 , the port functions as ADDRESS/DATA BUS.
Under this condition , writing “1” to the LATCH makes the PORT PIN to
function as INPUT PIN and writing “0” to the LATCH makes the PORT
PIN to function as OUTPUT PIN.

33
Cont…

34
Cont…
PORT 1:
8-bit Bidirectional I/O port in SINGLE CHIP MODE and EXPANDED
mode.

Receives A0-A7 during program verification of ROM.

When a pin has 1 in it , it acts INPUT PIN. When a pin has 0 in it ,


it acts as OUTPUT PIN.

It Has Internal PULL-UP RESISTORS.

Port 1 is a QUASI-BIDIRECTIONAL PORT.

Writing “1” to the latch makes the PORT PIN to function as INPUT
PORT. Writing “0” to the latch makes the PORT PIN to function as
OUTPUT PORT.

35
Cont…

36
Cont…
PORT 2:
8-bit Bidirectional I/O port in SINGLE CHIP MODE.
Serves as HIGHER ORDER ADDRESS BUS (A15-A8) in EXPANDED
MODE.
When a pin has 1 in it , it acts INPUT PIN. When a pin has 0 in it , it
acts as OUTPUT PIN
When CONTROL LINE is 0 , the port functions as NORMAL
BIDIRECTIONAL I/O port. Under this condition , writing “1” to the
LATCH makes the PORT PIN to function as INPUT or OUTPUT port.
Here an NMOS transistor functions as PULL-UP RESISTOR.
When CONTROL LINE is 1 , the port functions as ADDRESS BUS. Under
this condition , writing “1” to the LATCH makes the PORT PIN to
function as INPUT PIN and writing “0” to the LATCH makes the PORT
PIN to function as OUTPUT PIN

37
Cont…

38
Cont…
PORT 3:
8-bit Bidirectional I/O port in SINGLE CHIP MODE. Has Internal PULL-UP
RESISTORS.
Special purpose I/O pins in EXPANDED MODE.
P3.0 (RxD) – Serial Input Port [Connected to SERIAL RECEIVE BUFFER]
P3.1 (TxD) – Serial Output Port [Connected to SERIAL TRANSMIT
BUFFER]
P3.2 (𝐼 𝑁 𝑇 0 ) – External Level or Edge Triggered General Purpose
Interrupt 0.
P3.3 (𝐼 𝑁 𝑇 1 ) - External Level or Edge Triggered General Purpose
Interrupt 1.
P3.4 (T0) – Clock pulse input to TIMER 0 when acting as COUNTER.
P3.5 (T1) - Clock pulse input to TIMER 1 when acting as COUNTER.
P3.6 ( 𝑊 𝑅 ) – Acts as MEMORY WRITE SIGNAL for EXTENAL RAM.
P3.7 ( 𝑅 𝐷 ) - Acts as MEMORY READ SIGNAL for EXTERNAL RAM.
Writing “1” to the LATCH makes the PORT pin to function as INPUT PIN.
Writing “0” to the LATCH makes the PORT pin to function as OUTPUT pin.

39
Cont…
In 8051 There are Five types of addressing modes.

• Immediate Addressing Mode

• Register Addressing Mode

• Direct Addressing Mode

• Register Indirect Addressing Mode

• Indexed Addressing Mode

40
Cont…
1) Immediate addressing mode:

• In this Immediate Addressing Mode, the data to be operated is


directly specified in the instruction itself.

• The data is provided immediately after the opcode.


• The data is preceded by the # symbol.

Examples:

MOV A,#0AFH;

MOV R3,#45H;

MOV DPTR,#1200H;

ADD A,#80H

41
Cont…
2) Register addressing mode:

• In this addressing mode, the register that contains the data to be


operated is specified in the instruction.

• The registers such as A,DPTR and (R0-R7) are used in this


register addressing.

Examples:

MOV A,R5;

MOV R0,A;

MOV R5,DPH;

ADD A,R0;

42
Cont…
3) Direct addressing mode:

• In this addressing mode, the memory address that contains the


data to be operated is specified in the instruction.

• All the internal RAM addresses, including that of the special


function registers can be used in this direct addressing instruction.

Examples:

MOV A,80H;

MOV R2,45H;

MOV 63H,R0;

ADD A,74H;

43
Cont…
4) Register Indirect addressing mode:
• In this addressing mode, the register that contains the memory
address of the data is specified in the instruction.
• The register specified is preceded by the ‘@’ symbol in the
assembly language format.
• R0-R7 is used to access the data in the Internal memory (RAM
location) with 8-Bit address.
•DPTR is used to access the data in the External memory with 16-
Bit address.
Examples:
MOV A,@R0;
MOV @R1,A;
MOV @R2,30H;
MOVX @DPTR,A;
MOVX A,@DPTR;
ADD A,@R0;

44
Cont…
5) Indexed addressing mode:
• In this addressing mode, the instruction consists of two parts – a
base address and an offset value.

• This type of addressing is useful in relative memory accessing


and relative jumping.

• The base address is stored in the DPTR or any other register and
the offset value is stored in the accumulator.

• Only program memory can be accessed using indexed addressing


mode.
Examples:

MOVC A,@A+DPTR;

MOVC A,@A+PC;

45
ASSEMBLY LANGUAGE PROGRAMMING
8 BIT ADDITION / SUBTRACTION:

CLR C ;Clear the PSW

MOV A,#data1 ;Load 1st number in the accumulator

SUBB/ADDC A,# data2 ;Add/Sub the two numbers

MOV DPTR,#4500 ;Load destination address in the DPTR

MOVX @ DPTR, A ;Store sum in destination address

LOOP1 SJMP LOOP1 ; Terminate the program

46
ASSEMBLY LANGUAGE PROGRAMMING
8 BIT MULTIPLICATION:

MOV A, #data1 ;Load A register with data1


MOV B, #data2 ;Load B register with data2
MUL AB ;Multiply A &B
MOV DPTR, # 4500H ;Initialize destination address
MOVX @ DPTR, A ;Store lower order product
INC DPTR ;Increment DPTR
MOV A,B ;Move higher order product to A
MOVX @ DPTR, A ;Store higher order product
STOP SJMP STOP ;Terminate the program

47
ASSEMBLY LANGUAGE PROGRAMMING
8 BIT DIVISION:

MOV A, #data1 ;Load A register with data1


MOV B, #data2 ;Load B register with data2
DIV AB ;Divide A &B
MOV DPTR, # 4500H ;Initialize destination address
MOVX @ DPTR, A ;Store Quotient
INC DPTR ;Increment DPTR
MOV A,B ;Move reminder to reg A
MOVX @ DPTR, A ;Store reminder
STOP SJMP STOP ;Terminate the program

48
ASSEMBLY LANGUAGE PROGRAMMING
Finding 2’s Complement of a given number:

MOV DPTR,# 4500H ;Initialize input data address


MOVX A,@ DPTR ;Load accumulator with data
CPL A ;Complement the contents of A
ADD A,#01H ;Add 01H to the contents of A
INC DPTR ;Increment DPTR
MOVX @ DPTR,A ;Store the result
HERE: SJMP HERE ;Terminate the program

49

You might also like