Professional Documents
Culture Documents
MICROCONTROLLERS
U Vinothkumar
Asst Prof
Dept of ECE
Dr.N.G.P.Institute of Technology
Coimbatore.
UNIT IV MICROCONTROLLER 9
2
3
Cont…
1. It is an 8-bit microcontroller.
4
Cont…
10. It has four 8 bit ports namely port 0, port1, port2 and port3.
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…
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):
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…
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.
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…
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.
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.
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.
40
Cont…
1) Immediate addressing mode:
Examples:
MOV A,#0AFH;
MOV R3,#45H;
MOV DPTR,#1200H;
ADD A,#80H
41
Cont…
2) Register addressing mode:
Examples:
MOV A,R5;
MOV R0,A;
MOV R5,DPH;
ADD A,R0;
42
Cont…
3) Direct addressing mode:
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.
• The base address is stored in the DPTR or any other register and
the offset value is stored in the accumulator.
MOVC A,@A+DPTR;
MOVC A,@A+PC;
45
ASSEMBLY LANGUAGE PROGRAMMING
8 BIT ADDITION / SUBTRACTION:
46
ASSEMBLY LANGUAGE PROGRAMMING
8 BIT MULTIPLICATION:
47
ASSEMBLY LANGUAGE PROGRAMMING
8 BIT DIVISION:
48
ASSEMBLY LANGUAGE PROGRAMMING
Finding 2’s Complement of a given number:
49