0% found this document useful (0 votes)
20 views26 pages

Unit IV

This document discusses the architecture and features of the 8051 microcontroller. It describes the differences between microprocessors and microcontrollers, and provides details on the memory organization, registers, I/O ports, timers/counters and serial communication capabilities of the 8051.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views26 pages

Unit IV

This document discusses the architecture and features of the 8051 microcontroller. It describes the differences between microprocessors and microcontrollers, and provides details on the memory organization, registers, I/O ports, timers/counters and serial communication capabilities of the 8051.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT IV and V

Microcontroller 8051 and Interfacing

Difference between Microprocessor and Microcontroller

S.No Microprocessor Microcontroller


1 It consist of ALU, Registers and It consists of CPU, Memory (RAM and ROM),
Timing and Control Unit Peripherals( Parallel and serial I/O ports,
Interrupts, Timer, ADC, DAC, PWM, etc.,)
2 General Purpose Application specific
3 Requires more number of hardware Requires minimum number of hardware.
4 Design point of view is more flexible Less flexible
5. Less number pins are multifunctional More number of pins are multifunctional

Features

• The 8051 is an 8-bit microcontroller.

• 128 bytes of RAM (Data Memory)

• 4K bytes of on-chip ROM (Program Memory)

• Two 16 bit timers/counters (T0 and T1)

• One serial port

• Four parallel I/O ports( P0, P1,P2, P3), each 8 bits wide

• Interrupts - 2 external and 3 internal interrupt sources

• 1 to 16 MHz clock

Pin Diagram
Pins 1 – 8 (PORT 1): Pins 1 to 8 are the PORT 1 Pins of 8051. PORT 1 Pins consists of 8 – bit
bidirectional Input / Output Port with internal pull – up resistors. In older 8051
Microcontrollers, PORT 1 doesn’t serve any additional purpose but just 8 – bit I/O PORT.

In some of the newer 8051 Microcontrollers, few PORT 1 Pins have dual functions. P1.0 and
P1.1 act as Timer 2 and Timer 2 Trigger Input respectively.

P1.5, P1.6 and P1.7 act as In-System Programming Pins i.e. MOSI, MISO and SCK respectively.

Pin 9 (RST): Pin 9 is the Reset Input Pin. It is an active HIGH Pin i.e. if the RST Pin is HIGH for a
minimum of two machine cycles, the microcontroller will be reset. During this time, the
oscillator must be running.
Pins 10 – 17 (PORT 3): Pins 10 to 17 form the PORT 3 pins of the 8051 Microcontroller. PORT 3
also acts as a bidirectional Input / Output PORT with internal pull-ups. Additionally, all the PORT
3 Pins have special functions. The following table gives the details of the additional functions of
PORT 3 Pins.

PORT 3 Pin Function Description


P3.0 RXD Serial Input
P3.1 TXD Serial Output
P3.2 INT0 External Interrupt 0
P3.3 INT1 External Interrupt 1
P3.4 T0 Timer 0
P3.5 T1 Timer 1
P3.6 WR External Memory Write
P3.7 RD External Memory Read

Pins 18 & 19: Pins 18 and 19 i.e. XTAL 2 and XTAL 1 are the pins for connecting external
oscillator. Generally, a Quartz Crystal Oscillator is connected here.

Pin 20 (GND): Pin 20 is the Ground Pin of the 8051 Microcontroller. It represents 0V and
is connected to the negative terminal (0V) of the Power Supply.

Pins 21 – 28 (PORT 2): These are the PORT 2 Pins of the 8051 Microcontroller. PORT 2 is
also a Bidirectional Port i.e. all the PORT 2 pins act as Input or Output. Additionally,
when external memory is interfaced, PORT 2 pins act as the higher order address byte.
PORT 2 Pins have internal pull-ups.

Pin 29 (PSEN): Pin 29 is the Program Store Enable Pin (PSEN). Using this pins, external
Program Memory can be read.

Pin 30 (ALE/PROG): Pin 30 is the Address Latch Enable Pin. Using this Pins, external
address can be separated from data (as they are multiplexed by 8051).

During Flash Programming, this pin acts as program pulse input (PROG).
Pin 31 (EA/VPP): Pin 31 is the External Access Enable Pin i.e. allows external Program
Memory. Code from external program memory can be fetched only if this pin is LOW.
For normal operations, this pins is pulled HIGH.

During Flash Programming, this Pin receives 12V Programming Enable Voltage (VPP).

Pins 32 – 39 (PORT 0): Pins 32 to 39 are PORT 0 Pins. They are also bidirectional Input /
Output Pins but without any internal pull-ups. Hence, we need external pull-ups in order
to use PORT 0 pins as I/O PORT.

In addition to acting as I/O PORT, PORT 0 also acts as lower order address/data bus
when external memory is accessed.

Pin 40 (VCC): Pin 40 is the power supply pin to which the supply voltage is given (+5V).

Architecture of 8051
Architecture consists of CPU, Memory, and Peripherals.

CPU: ALU, Registers, Timing and control unit

Registers: A, B, PSW, DPTR, PC, SFR, (DPTR-16bit reg. DPL-low byte, DPH- High Byte)

PSW (Program Status Word)

00-Bank 0; 01-bank1, 10-bank2; 11-bank 3


Peripherals: 0001 1000 –byte; RS1 -1, RS0-1, PSW.3-1, PSW.4-1

Parallel port (PO, P1, P2, P3), Serial port, Timers (T0 and T1) and Interrupts.
Memory Organization of 8051

Program Memory

Onchip – 4KB; Internal Memory (EA=1), External Memory (EA=0)-(64KB)

Data Memory

Onchip -128Bytes; External RAM – 64KB


Internal RAM are divided into 3 sections,

1) Register Bank – 32 (00H-1FH)

2) Bit addressable RAM- 16 (20H-2FH)

3) Scratch pad RAM -80 (30H-7FH)

Each Register Bank has 8 general purpose registers (R0, R1, R2, R3, R4, R5, R6, R7)

R0 and R1 – general purpose registers and pointer register.


BANK 0

R4-, psw- RS1-0, RS0-1, R2…R0 and R1- pointer registers.

BIT addressable -20- 07,06, 05, 01,00

21 ,09,08
• 128 bytes of internal RAM (00H to 7FH)is general R/W storage.

• Part of this RAM is used as general purpose registers.

• 21 Special-Function Registers (SFR) which are not part of 128 bytes of RAM at
80H to F8H locations of the RAM space.
• 64 KB External RAM can be used fully in addition to 128 internal RAM.

• Although 8051 normally operates with separate program and data memory
space, there are applications where it can be used as one 64 KB of memory.
When this is done, 8051 can input a block of data through its serial
communication port, load it into memory, and then execute that data as a
program.

Addressing Modes

1) Immediate

MOV A, #5FH

SUB A, #27H

2) Register

MOV A, R1; MOV A, B; MOV A, PSW

ADD A, R4

3) Direct

MOV A, 3FH 3F – (55)

ADD A, 45H

4) Register Indirect [R0, R1, DPTR, DPL, DPH]

MOV A, @R0

ADD A, @R1

5) Indexed

MOVC A, @ A+DPTR

MOVC A, @ A+PC
Instruction sets of 8051

Based on the operation, instructions are divided into following groups

 Data Transfer Instructions


 Arithmetic Instructions
 Logical Instructions
 Boolean or Bit Manipulation Instructions
 Program Branching Instructions

Data Transfer

 MOV, MOVC,MOVX,PUSH,POP,XCH,XCHD

[Source-Immediate, register, memory location] [Destination - register, memory


location]
PUSH B ; SP-07 ; 08-(77), MOV 27H, 29H

POP A; SP-1F (25-A) ; SP-1E

Rn= R0 – R7

Arithmetic Instructions

ADD, ADDC, SUBB, INC, DEC, MUL, DIV, DA A


Logical Instructions

ANL, ORL, XRL, CLR, CPL, RL, RLC, RR, RRC, SWAP
Boolean or Bit manipulations

CLR, SETB, MOV, JC, JNC, JB, JNB, JBC, ANL, ORL, CPL

A-1100 0000 (C 0)- A- BYTE ADDRESSABLE-8, BIT addressable – 1bit.. ACC.7, ACC.3,
ACC.0

CLR P0.0, CLR PSW.7


SETB C, SETB P2.5, CLR ACC.0, CLR P0.7

CPL P3.7; MOV C, ACC.0

JNC 45H

ADD A, 25H

JB (Jump if bit set), JNB P1.1, loop1 SETB P1.1 (SOC)

JB P1.2, LOOP1
SUBB

Branching Instructions

LJMP, AJMP, SJMP, JZ, JNZ, CJNE, DJNZ, NOP, LCALL, ACALL, RET, RETI, JMP
DJNZ R5, 7FH; DJNZ 44H, 54H
TIMERS/COUNTERS OF 8051

Two 16 bit timers/counters – T0 and T1

Timer: Generates time delay, clock input – oscillator frequency

Counter: Counts the clock pulses arrives at Timer Input pins.

SFR registers:

TMOD, TCON, TH0, TL0, TH1, TL1

TMOD Register

M1, M0 (16 BIT TIMER/COUNTER – TH0 AND TL0 -3FFF- FFFF) (00-FF)

M1 M0 MODE DESCRIPTION
0 0 0 13-bit timer-8192
0 1 1 16-bit timer-65536
1 0 2 8-bit auto-reload-256
1 1 3 Split mode-256
C/T

COUNTER = 1, TIMER = 0

GATE

GATE=0, START TIMER/COUNTER OPERATION BY TRX (TIMER RUN CONTROL BIT)- S/W

GATE=1, START TIMER/COUNTER OPERATION BY TRX AND INTX- (H/W)


TCON REGISTER (BIT addressable/byte)

0100-0000-40; MOV TCON, #40H - BYTE

SETB TR1/ SETB TCON.6

SETB TR0/ SETB TCON.4

CLR TF0; CLR TR1

FFFF-0000

TIMER/COUNTER OPERATING MODES

MODE 0 – 13 BIT TIMER/COUNTER 0000-8191-0000, Timer INt


MODE 1- 16BIT TIMER/COUNTER 0000-65535-0000 (4500-65535-0000)

MODE 2- 8BIT AUTO RELOAD ( 25d -255d) –(25-255-25)


MODE 3- TWO 8 BIT TIMER/COUNTER (SPLIT UP TIMER)

Programming the timer

1) program the TMOD register.

2) load the count values in TLX and THX

Count values = required time delay / timer clock period

Timer clock frequency = oscillator frequency / 12

Timer clock period = 1 / Timer clock frequency

8 bit timer

256 – count values

16 bit timer FFEE - FFFF

65536 – count values – FF(THX) EE(TLX)

13 bit timer

8192 - count values

3) START TIMER, MAKING SETB TRX


4) CHECK TFX, JB/JNB, JNB TF1

5) CLR TRX

SERIAL PORT – 8051

One serial port – TXD, RXD

UART

SFR registers

SBUF (2), SCON

SCON REGISTER

SM0, SM1 – Serial mode control bits

Baud rate
SM0 SM1 MODE Description

0 0 0 shift register (Fosc./12)

0 1 1 8 bit UART Variable (set by Timer 1)

1 0 2 9 bit UART (Fosc./64) or (Fosc/32)

1 1 3 9 bit UART Variable (set by Timer 1)

SM2: Enable multiprocessor


Enable multiprocessor communication in modes 2 and 3 (for 9 bit UART).
REN: Receiver Enable
Set/clear by software to enable/disable receive operation
TB8: Transmit bit 8
Set or clear by software. The 9 bits will be transmitted in mode 2 and 3.
TB8 = 1, a value is written to the serial port, 9th bit of data = 1.
TB8 = 0, 9th bit of data = 0, RI will not set.
RB8: Receive bit 8
Set or clear by software. The 9 bits will be received in mode 2 and 3. First eight bits are
the data received and 9th bit received will be placed in RB8.
TI: Transmit Interrupt flag
Set by hardware when a byte is transmitted completely. Now the port is free and ready
to send the next byte. This bit must be cleared by software.
RI: Receive Interrupt flag
Set by hardware when a byte has been completely received. This lets the program to
know that it needs to read the value quickly before another byte is read. This bit must
be cleared by software.

Variable baud rate programming (mode 1 and mode 3)

Clock Frequency
Baud Rate= -----------------------
12 x 32 x (256-TH1)

TH1 = 256 – ((Crystal frequency / 384) / Baud rate)

Serial port Programming

1) Initialize SCON register

2) Place the byte in SBUF register (Transmission)

3) Initialize TMOD register (Timer1 in mode 2 (auto reload); load values in TH1

4) start timer1

5) check for occurrence of TI flag.

6) After the occurrence RI flag, Copy SBUF to Accumulator.


Interrupts of 8051

Sources

External interrupt 0 – INT0 (0x03)

Timer 0 – TF0 (0x0B)

External interrupt 1 – INT1 (0x13)

Timer 1- TF1 (0x1B)

Serial PORT – TI & RI (0x23)

Registers

IE, IP, TCON

IE (Interrupt Enable)

EA - global interrupt enable/disable:


0 - disables all interrupt requests. 1 - enables all individual interrupt requests.
ES - enables or disables serial interrupt:
0 - UART system cannot generate an interrupt. 1 - UART system enables an interrupt.
ET1 - bit enables or disables Timer 1 interrupt:
0 - Timer 1 cannot generate an interrupt. 1 - Timer 1 enables an interrupt.
EX1 - bit enables or disables external 1 interrupt:
0 - change of the pin INT0 logic state cannot generate an interrupt.
1 - enables an external interrupt on the pin INT0 state change.
ET0 - bit enables or disables timer 0 interrupt:
0 - Timer 0 cannot generate an interrupt. 1 - enables timer 0 interrupt.
EX0 - bit enables or disables external 0 interrupt:
0 - change of the INT1 pin logic state cannot generate an interrupt.
1 - enables an external interrupt on the pin INT1 state change.
*bit6 is not implemented.

IP (Interrupt Priority )

PS - Serial Port Interrupt priority bit


Priority 0 or Priority 1
PT1 - Timer 1 interrupt priority
Priority 0 or Priority 1
PX1 - External Interrupt INT1 priority
Priority 0 or Priority 1
PT0 - Timer 0 Interrupt Priority
Priority 0 or Priority 1
PX0 - External Interrupt INT0 Priority
Priority 0 or Priority 1

You might also like