Вы находитесь на странице: 1из 38

UNIT 1

Introduction

1 Concept of microcontroller
2 Microprocessor and Microcontroller
3 Comparison of all 8- bit microcontrollers
4 8051 architecture
5 Pin Diagram
6 Memory Organization
7 Special Function Registers
8 Internal Structure of I/O ports
9 Interfacing of 8051 with external memory
1. Concept of Microcontroller

An embedded computer is a computer that is implemented for a particular


purpose. But in comparison PC computer serves a number of purposes like
checking mails , surfing the internet, checking email , listening to music , word
processing etc.

So we can define embedded system as a

System built to perform its duty independent of human say partially


sometimes.

Specially designed to perform tasks in an efficient manner.

As a control system designed to perform a particular task.

Example mouse , keyboard, Hard disk etc.

A microcontroller is an integrated chip that forms a part of an embedded system. It


includes CPU, RAM,ROM , I/O ports , timers like a standard computer because they are
designed to execute a specific task to control the functions required on a single chip.
In a microcontroller just make proper connections and feed the program into it. After
which the microcontroller responds in accordance with the program that has been fed
into it.

The MCU contains a flash memory where it stores its program . The flash
memory is easily erased and a new program can burned. This makes it very
flexible. MCUs can be programmed few thousand times before they die.
The features of 8051 microcontroller are :

 Eight bit CPU

 On chip clock osillator

 4K bytes of internal program memory (code memory) [ROM]

 128 bytes of internal data memory[RAM]

 64 Kbytes of external program memory address space.

 64 Kbytes of external data memory address space.

 32 bi-directional address lines.

 Two 16-bit timer or counter: T0,T1.

 Full Duplex serial data receiver/transmitter

 Four register banks with 8 registers in each bank.

 Sixteen bit program counter and a data pointer.

 8 bit program status word.

 8 bit stack pointer.

 Five vector interrupt structure.

 8051 CPU consists of 8-bit ALU with associted registers like accumulator A ,B
register , PSW ,SP , 16-bit program counter, stack pointer.

 ALU

 8051 has 128 bytes of internal RAM divided into :

o Working registers (00 – 1F)

o Bit addressable memory area [20-2F]

o General purpose memory area (scratch pad memory ) [30-7F]


2. Comparison of Microprocessor and Microcontroller :

Microprocessor Microcontroller

Microprocessor contains ALU, Microcontroller contains the circuit


General purpose registers, stack of microprocessor , in addition it has
pointer, program counter, clock built –in ROM, RAM , I/O devices,
timing circuit, interrupt circuit. Timers or counters.

It has many instructions to move data It has few instructions to move data
between memory and CPU between memory and CPU.

Few bit handling instruction It has many bit handling instructions.

Less number of pins are More number of pins are


multifunctional multifunctional .

Single memory map for data and Separate memory map for data and
code(program) code (program)

Access time for memory and I/O are It requires less additional hardware.
more.

Large number of instructions with Limited number of instructions with


flexible addressing modes. few addressing modes.

3. Comparison of all 8-bit microcontrollers

Microcontrollers with small instruction set are called reduced instruction set
computer (RISC) machines and those with complex instruction set are called
complex instruction set computer (CISC). Intel 8051 is an example of CISC
machine whereas microchip PIC 18F87X is an example of RISC machine.

The Harvard architecture has separate program memory and data memory. The
instruction code and data can be fetched simultaneously.
 The PIC16C5X/XX microcontrollers is attributed primarily to its RISC
architecture.

 It employs Harvard architecture that is a separate program and memory and data
memory space.

 It uses two stage pipelining instruction fetch and execution . All instructions are
executed in a single cycle.

 Separation of program and data allows the instruction word to be optimized to


any size.

The comparison is made with the following microcontrollers:

 SGS-Thomson ST62 @ 8 MHz

 Motorola MC68HC05 @ 4.2 MHz

 Intel 8051 @ 20 MHz

 Zilog Z86CXX @ 12 MHz

 National COP800 @ 20 MHz

 Packing Binary coded Decimal.

In this example we take two bytes in RAM or registers, each containing a BCD
digit in the lower nibble and create a packed BCD data byte, which is stored
back in the register or RAM location holding the low BCD digit.

PIC 16C5X/XX Byte/Words Cycles

SWAP F REGHI,W 1 1

IORWF REG LO 1 1

0.4µs

ST 62

LD A,REGHI 2 4

RLC A 1 4

RLC A 1 4
RLC A 1 4

RLC A 1 4

ADD A,REGLO 1 4

LD REGLO,A 1 4

10 28

10 28

45.5µs

Z86CXX

SWAP REGHI 2 8

OR REGHI,REGLO 2 6

10 28

5.33 µs

COP 800

SWAP REGHI 1 2

OR REGHI,REGLO 1 1

OR A,[B] 1 1

X A,[B] 1 1

4 5

5 µs
MC68HC05

LDA REG HI 2 3

ROLA 1 3

ROLA 1 3

ROLA 1 3

ROLA 1 3

ADD REGLO 2 3

STA REGLO 2 4

10 22

10.5µsec

8051

MOV A,Rx 1 1

SWAP 1 1

ORL A,Ry 1 1

MOV Ry,A 1 1

4 4

2.4µs
LOOP CONTROL

This example is one of simple loop control where the register containing a loop
count is decremented, tested for zero, and if not zero, then branched back to the
beginning of the loop.

PIC 16C5X/XX COP 800

Byte/Words Cycles Byte/Words Cycles

DECFSZ COUNT 1 1/2 DRSZ COUNT 1 3

GOTO BEG_LOOP 1 2 JP BEG_LOOP 1 3

2 3/2 2 6

0.6 µs 6 µs

ST 62 MC68HC05

Byte/Words Cycles Bytes/Words Cycles

DEC X 1 4 DEC X 1 3

JRZ BEG_LOOP 1 2 BEQ BEG_LOOP 2 3

2 6 3 6

9.75 µs 2.86µs

Z86CXX 8051

DJNZ COUNT,BEG_LOOP 2 10/12 DJNZ Rx,BEG_LOOP 2 2


6

1.67 µs 1.2µs
BIT TEST & BRANCH

This example tests a single bit in a register or a RAM location and makes a
conditional branch. We assume that the MSb is tested and a branch is to be taken if
the bit is set

PIC 16C5X/XX COP 800

Byte/Words Cycles Byte/Words Cycles

BTFSC REG ,7 1 ½ IFBIT 7,[B] 1 3

GOTO NEWADD 1 2 JP NEWADD 1 3

2 3/2 2 6

0.6 µs 4 µs

ST 62 MC68HC05

Byte/Words Cycles Bytes/Words Cycles

JRR 7,NEWADD 3 16/18 BRCLR 7,NEWADD 3 5

8.125 µs 2.38µs

Z86CXX 8051

Byte/Words Cycles Bytes/Words Cycles

BTJRT NEWADD,REG,7 3 16/18

0.6 µs 4µs

MOV A,@Rx 1 1
JB A.7 , NEWADD 3 2

SOFTWARE TIMER

Microcontrollers need to implement time delays. Some of the examples are


Debouncing key input, pulse width modulation, and phase angle control
Implementing a 10 ms time delay loop subroutine will be considered in this
section.

PIC 16C5X/XX

Byte/Word Cycles

DELAY : MOVLW 41H 1 1

MOVWF COUNT 2 1 1

CLRF COUNT1 1 1

LOOP : INCFSZ COUNT1 1 2/1

GOTO LOOP 1 2

DECFSZ COUNT2 1 2/1

GOTO LOOP 1 1

RET 1 1

COP 800

Byte/Word Cycles

DELAY : LD COUNT 1,#0BH 1 1

LD B, #0EH 1 1

DRSZ B 1 1

LOOP : INCFSZ COUNT1 1 2/1

GOTO LOOP 1 2
DECFSZ COUNT2 1 2/1

GOTO LOOP 1 1

RET 1 1

ST62

Byte/Word Cycles

LDI A, #FF 2 4

LD X,A 1 4

LDI A,#04 2 4

LD Y,A 1 4

JRNZ LOOP 1 4

DEC Y 1 2

JRNZ LOOP 1 4

RET 1 2

10
4. 8051 architecture

Figure 1 8051 architecture.

 8051 microcontroller was designed by Intel in 1981.

 It is an 8-bit microcontroller.

 It is built with 40 pins DIP (dual inline package), 4kb of ROM storage and 128
bytes of RAM storage, 2 16-bit timers.

 It consists of are four parallel 8-bit ports, which are programmable as well as
addressable depending on the requirement.
 An on-chip crystal oscillator is integrated in the microcontroller having crystal
frequency of 12 MHz.

 8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If
the program size is more than 4 K Bytes 8051 will fetch the code automatically
from external memory.

 Accumulator is an 8 bit register widely used for all arithmetic and logical
operations. Accumulator is also used to transfer data between external memory.
B register is used along with Accumulator for multiplication and division. A and B
registers together is also called MATH registers.

 PSW (Program Status Word). This is an 8 bit register which contains the
arithmetic status of ALU and the bank select bits of register banks

CY AC F0 RS1 RS0 OV - P

CY - carry flag

AC - auxiliary carry flag

F0 - available to the user for general purpose.

RS1,RS0 – register bank select bits.

OV – overflow

P - parity

 Stack Pointer (SP)

It contains the address of the data item on the top of the stack. Stack may
reside anywhere on the internal RAM. On reset, SP is initialized to 07 so
that the default stack will start from address 08 onwards.

 Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data pointer
lower byte). This is a 16 bit register which is used to furnish address
information for internal and external program memory and for external
data memory.

 Program Counter (PC) – 16 bit PC contains the address of next instruction


to be executed. On reset PC will set to 0000. After fetching every
instruction PC will increment by one.
5. Pin Diagram

Pin out Description:

Pin 1-8 PORT1- Each of these pins can be configured as


an input or output.

Pin 9 RESET- A logic set on this pin disables the


microcontroller and clears the contents of most
registers. In other words a positive voltage on
this pin resets the microcontroller.

Pin 10-17 PORT -3: Similar to Port 1.Each pin serves as


general output or input .Besides all of them
have alternate functions.
Serial Asynchronous Communication Input or
Pin 10 RXD Serial Synchronous Communication Output.
Serial Asynchronous Communication Output or
Pin 11 TXD Serial Synchronous Communication clock
output
External Interrupt 0 input
Pin 12 INT0
External Interrupt 1 input
Pin 14 INT1
T0 Counter 0 clock input
Pin 15 T0
T1 Counter 1 clock input
Pin16 T1
Read from external RAM
Pin 17 RD
Internal oscillator input and output. A quartz
Pin 18, 19 XTAL2, XTAL1 crystal which specifies operating frequency is
usually connected to these pins.

Pin 20 GND Ground


If there is no intention to use external memory
Pin 21-28 Port 2 then these port pins are configured as general
inputs/outputs. In case external memory is
used, the higher address byte, i.e. addresses
A8-A15 will appear on this port. Even though
memory with capacity of 64Kb is not used,
which means that not all eight port bits are used
for its addressing, the rest of them are not
available as inputs/outputs.

If external ROM is used for storing program


Pin 29 PSEN then a logic zero (0) appears on it every time
the microcontroller reads a byte from memory.

Prior to reading from external memory, the


Pin 30 ALE microcontroller puts the lower address byte
(A0-A7) on P0 and then activates the ALE
output. After receiving signal from ALE pin, the
external latch latches the state of P0 and uses it
as a memory chip address. Immediately the
ALE pin is returned to its previous logic state
and P0 is now used as a Data Bus.

Pin 31 EA By applying logic zero to this pin, P2 and P3 are


used for data and address transmission whether
there is internal memory or not. It means that
even if there is a program written to the
microcontroller, it will not be executed. Instead,
the program written to external ROM and
executed. By applying logic one to the EA pin,
the microcontroller will use both memories, first
internal then external if present.
Similar to P2, if external memory is not used,
Pin 32-39 PORT 0 these pins can be used as general
inputs/outputs. Otherwise, P0 is configured as
address output (A0-A7) when the ALE pin is
driven high (1) or as data output (Data Bus)
when the ALE pin is driven low (0).

Pin 40 VCC +5V power supply

8051 microcontrollers has 4 I/O ports each of 8-bit, which can be configured as
input or output. Hence, total 32 input/output pins allow the microcontroller to be
connected with the peripheral devices.
 Pin configuration, i.e. the pin can be configured as 1 for input and 0 for
output as per the logic state.
o Input/Output (I/O) pin − All the circuits within the microcontroller
must be connected to one of its pins except P0 port because it does not
have pull-up resistors built-in.
 Port 0 − The P0 (zero) port is characterized by two functions −
o When the external memory is used then the lower address byte
(addresses A0A7) is applied on it, else all bits of this port are
configured as input/output.
o When P0 port is configured as an output then other ports consisting of
pins with built-in pull-up resistor connected by its end to 5V power
supply, the pins of this port have this resistor left out.

6. Memory Organization of 8051.

The 8051 Microcontroller Memory is separated into Program Memory (ROM) and Data
Memory (RAM). The Program Memory of the 8051 Microcontroller is used for storing the
program to be executed, which means instructions. The Data Memory is used for storing
temporary variable data and intermediate results.

8051 Microcontroller has both Internal ROM and Internal RAM. If the internal memory is
inadequate, you can add external memory using suitable circuits.

Program Memory (ROM) of 8051


In 8051 Microcontroller, the code or instructions that has to be executed are
stored in the Program Memory, which is also called as the ROM of the
Microcontroller. The original 8051 Microcontroller by Intel has 4KB of internal
ROM.

o In 4KB of Internal ROM, the address space is 0000H to 0FFFH. If the


address space that is the program addresses exceeds this value,
then the CPU will automatically fetch the code from the external
Program Memory.

o For External Access Pin (EA Pin) must be pulled HIGH i.e. when
the EA Pin is high, the CPU first fetches instructions from the
Internal Program Memory in the address range of 0000H to 0FFFFH
and if the memory addresses exceed the limit, then the
instructions are fetched from the external ROM in the address
range of 1000H to FFFFH.
There is also an alternative method to fetch the instructions that is ignore the Internal
ROM and fetch all the instructions only from External Program Memory(External ROM).

For this purpose EA pin must be connected to GND . In this case the memory addresses
of external ROM will be from 0000H to FFFFH.
Data Memory (RAM) of 8051 Microcontroller

 Data Memory or RAM of 8051 Microcontroller stores temporary data and


intermediate results that are generated and used during the normal operation of the
microcontroller. Original Intel’s 8051 Microcontroller had 128B of internal RAM.

 Currently 8051 Microcontroller have 256B of RAM. Of this 256B, the first 128B
memory addresses from 00H to 7FH is divided in to Working Registers organized as
Register Banks, Bit – Addressable Area and General Purpose RAM known as
Scratchpad area.

 In the first 128B of RAM (from 00H to 7FH), the first 32B that is memory from
addresses 00H to 1FH consists of 32 Working Registers that are organized as four
banks with 8 Registers in each Bank.
Lower 128B(00H to 07H) Upper 128B(80H-FFH)
(Direct and Indirect Addressing) (Direct Addressing) (Indirect Addressing)

 The 4 banks are named as Bank0, Bank1, Bank2 and Bank3. Each Bank consists of 8
registers named as R0 – R7. Each Register can be addressed in two ways: by name
or by address.

 To address the register by name, first the corresponding Bank has to be selected. In
order to select the bank, use RS0 and RS1 bits of the Program Status Word (PSW)
Register (RS0 and RS1 are 3rd and 4th bits in the PSW Register).

 When addressing the Register using its address say 12H for example, the
corresponding Bank may or may not be selected. (12H corresponds to R2 in Bank2).

 The next 16B of the RAM i.e. from 20H to 2FH are Bit – Addressable memory
locations. There are totally 128 bits that can be addressed individually using 00H to
7FH or the entire byte can be addressed as 20H to 2FH.

 The final 80B of the internal RAM addresses from 30H to 7FH, is the general purpose
RAM area which are byte addressable.

 These lower 128B of RAM can be addressed directly or indirectly.


SRFs Memory addresses are only direct addressable. Even though some of the
addresses between 80H and FFH are not assigned to any SFR, they cannot be used as
additional RAM area.

In some microcontrollers, there is an additional 128B of RAM, which share the


memory address with SFRs that is 80H to FFH. But, this additional RAM block is only
accessed by indirect addressing.

7.Special Function Registers

o The 8051 Microcontroller Special Function Registers act as a control table that
monitor and control the operation of the 8051 Microcontroller.

o In Internal RAM Structure, the Address Space from 80H to FFH is allocated to
SFRs.

o Out of these 128 Memory Locations (80H to FFH), there are only 21 locations
that are actually assigned to SFRs.

o Each SFR has one Byte Address and also a unique name which specifies its
purpose.

o The 21 Special Function Registers of 8051 Microcontroller are categorized in


to seven groups.

They are:

 Math or CPU Registers: A and B

 Status Register: PSW (Program Status Word)

 Pointer Registers: DPTR (Data Pointer – DPL, DPH) and SP (Stack Pointer)

 I/O Port Latches: P0 (Port 0), P1 (Port 1), P2 (Port 2) and P3 (Port 3)

 Peripheral Control Registers: PCON, SCON, TCON, TMOD, IE and IP

 Peripheral Data Registers: TL0, TH0, TL1, TH1 and SBUF


Name of the Register Function Internal RAM Address(Hex)
ACC Accumulator E0H
B B Register(Arithmetic) F0H
DPH Addressing External 83H
Memory
DPL Addressing External 82H
Memory
IE Interrupt Enable Control A8H
IP Interrupt Priority B8H
P0 Port 0 Latch 80H
P1 Port 1 Latch 90H
P2 Port 2 Latch A0H
P3 Port 3 Latch B0H
PCON Power Control 87H
PSW Program Status Word D0H
SCON Serial Port Control 98H
SBUF Serial Port Data Buffer 99H
SP Stack Pointer 81H
TMOD Timer/Counter Mode 89H
control
TCON Timer/Counter Control 88H
TL0 Timer 0 Low Byte 8AH
TH0 Timer 0 High Byte 8CH
TL1 Timer 1 High Byte 8BH
TH1 Timer 1 Low Byte 8DH

CPU or Math Registers

A or Accumulator (ACC)

The Accumulator or Register A is the most important and most used 8051 Microcontroller
SFRs. The Register A is located at the address E0H in the SFR memory space. The
Accumulator is used to hold data for almost all ALU Operations.

Some of the operations where the Accumulator is used are:


 Arithmetic Operations like Addition, Subtraction, Multiplication etc.
 Logical Operations like AND, OR, NOT etc.
 Data Transfer Operations (between 8051 and External Memory)

0 0 0 0 0 0 0 0

Bit7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

MSB LSB

B (Register B)

The B Register is used along with the accumulator in Multiplication and Division operations.
These two operations are performed on data that are stored only in Registers A and B.
During Multiplication Operation, one of the operand either the multiplier or multiplicand is
stores in B Register and also the higher byte of the result.

In case of Division Operation, the B Register holds the divisor and also the remainder of the
result. It can be used as a General Purpose Register for normal operations and is often used
as an Auxiliary Register by Programmers to store temporary results.

Register B is located at the address F0H of the SFR Address Space.

0 0 0 0 0 0 0 0

Bit7 Bit6 Bi5 Bit4 Bit3 Bit2 Bit1 Bit0

Program Status Word (PSW)

The PSW or Program Status Word Register is called as Flag Register is one of the important
SFRs. The PSW Register consists of Flag Bits, which help the programmer in checking the
condition of the result and make decisions.

Flags are 1-bit storage elements that store and indicate the nature of the result that is
generated by execution of certain instructions. The following image shows the contents of
the PSW Register.
0 0 0 0 0 0 0 0

PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0

CY AC F0 RS1 RS0 OV ----- P

The following table describes the function of each flag.

BIT SYMBOL FLAG NAME DESCRIPTION

7 C or CY Carry Used in Arithmetic, Logic and


Boolean Operations

6 AC Auxiliary Carry Used in BCD operations

5 F0 Flag0 General Purpose User Flag

4 RS1 Register Bank Selection Bit 1

3 RS0 Register Bank Selection Bit 0

RS1 RS0 Bank

0 0 Bank 0

0 1 Bank1

1 0 Bank2

1 1 Bank3

2 OV Overflow Used in Arithmetic Operations

1 _ Reserved May be used as General


Purpose Flag
0 P Parity Set to 1 If A has odd number
of 1’s ; Otherwise Reset.

Pointer Registers

Data Pointer (DPTR – DPL and DPH)

The Data Pointer is a 16-bit Register and is physically the combination of DPL (Data Pointer
Low) and DPH (Data Pointer High) SFRs. The Data Pointer can be used as a single 16-bit
register (as DPTR) or two 8-bit registers (as DPL and DPH).

DPTR doesn’t have a physical Memory Address but the DPL (Lower Byte of DPTR) and DPH
(Higher Byte of DPTR) have separate addresses in the SFR Memory Space. DPL = 82H and
DPH = 83H.

The DPTR Register is used by the programmer addressing external memory (Program –
ROM or Data – RAM).

Bit 0 bit 7 bit0 bit 7

DPH DPL

DPTR

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

DPL

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

DPH

Stack Pointer (SP)

SP or Stack Pointer points to the top of the Stack and it indicates the next data to be
accessed. Stack Pointer can be accessed using PUSH, POP, CALL and RET Instructions. The
Stack Pointer is an 8-bit register and upon reset, the Stack Pointer is initialized with 07H.

When writing a new data byte into the stack, the SP (Stack Pointer) is automatically
incremented by 1 and the new data is written at an address SP+1. When reading data from
stack, the data is retrieved from the Address in SP and after that the SP is decremented by 1
(SP-1).

0 0 0 0 0 0 0 0

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit1 Bit 0

I/O Port Registers (P0, P1, P2 and P3)

The 8051 Microcontroller 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.
1 1 1 1 1 1 1 1

P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

P0

1 1 1 1 1 1 1 1

P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

P1

1 1 1 1 1 1 1 1

P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

P2

1 1 1 1 1 1 1 1

P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0


P3

Peripheral Control Registers

PCON (Power Control)

The PCON or Power Control register,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, 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, 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.

0 0 0 0 0 0 0 0

SMOD --- ---- ----- GF1 GF0 PD IDL

SCON (Serial Control)

The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s Serial Port. It is
located at an address of 98H. Using SCON, helps in the 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.
0 0 0 0 0 0 0 0

SM0 SM1 SM2 REN TB8 RB8 TI RI

MSB LSB

SCON

Serial Port Mode Control Bits

SM0 SM1 Mode Description Baud Rate


0 0 0 8-bit Synchronous Shift Fixed Baud Rate Frequency
Register Mode of Oscillator/12
0 1 1 8-bit Standard UART Variable Baud Rate (can be
Mode set by Timer 1)
1 0 2 9-bit multi processor Fixed Baud Rate Frequency
communication mode of oscillator/32 0r 64
1 1 3 9-bit multi processor Variable Baud Rate (can be
communication mode set by Timer 1)

TCON (Timer Control)

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.

0 0 0 0 0 0 0 0

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

MSB LSB

TMOD (Timer Mode)

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.
0 0 0 0 0 0 0 0

GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0

The Gatex bit is used to operate the Timerx with respect to the INTx pin or regardless of the
INTx pin.

GATE1 = 1 ==> Timer1 is operated only if INT1 is SET.

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

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

GATE0 = 0 ==> Timer0 is 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)

TM0 TM1 Mode Description


0 0 0 13-bit Timer Mode
0 1 1 16-bit Timer Mode
1 0 2 8-bit Auto Reload Timer
Mode
1 1 3 Two 8-bit Timer mode
IE (Interrupt Enable)

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 that is EA bit is used to enable or disable all the
interrupts.

0 0 0 0 0 0 0 0

EA ---- ET2 ES ET1 EX1 ET0 EX0

MSB LSB

IP (Interrupt Priority)

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.

0 0 0 0 0 0 0 0

---- ---- PT2 PS PT1 PX1 PT0 PX0

MSB LSB

Peripheral Data Registers

SBUF (Serial Data Buffer)

The Serial Buffer or SBUF register is used to hold the serial data while transmission or
reception.

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

MSB LSB
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.

TH0 TL0

TIMER 0

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit 0

TL0

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit 0

TH0

TL1/TH1 (Timer 1 Low/High)

The TL1 and TH1 are the lower and higher bytes of the Timer 0.

TH1 TL1
TIMER 1

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit 0

TL1

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit 0

TH1

8.Internal Structure and Operation of I/O ports

All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be configured
as inputs or outputs. Accordingly, in total of 32 input/output pins enabling the
microcontroller to be connected to peripheral devices are available for use.

Pin configuration, to know whether it is to be configured as an input (1) or an output (0),


depends on its logic state. In order to configure a microcontroller pin as an input, it is
necessary to apply a logic zero (0) to appropriate I/O port bit. In this case, voltage level on
appropriate pin will be 0.

Similarly, in order to configure a microcontroller pin as an input, it is necessary to apply a


logic one (1) to appropriate port. In this case, voltage level on appropriate pin will be 5V (as
is the case with any TTL input).

The figure above illustrates a simplified schematic of all circuits within the
microcontroller connected to one of its pins. It refers to all the pins except those of
the P0 port which do not have pull-up resistors built-in.
Output pin A logic zero (0) is applied to a bit of the P register. The output FE transistor is
turned on, thus connecting the appropriate pin to ground.

Input pin A logic one (1) is applied to a bit of the P register. The output FE transistor is
turned off and the appropriate pin remains connected to the power supply voltage over a
pull-up resistor of high resistance.

Logic state (voltage) of any pin can be changed or read at any moment. A logic zero (0) and
logic one (1) are not equal. A logic one (0) represents a short circuit to ground. Such a pin
acts as an output. A logic one (1) is “loosely” connected to the power supply voltage over a
resistor of high resistance. Since this voltage can be easily “reduced” by an external signal,
such a pin acts as an input.
Port 0

The P0 port is characterized by two functions. If external memory is used then the lower
address byte (addresses A0-A7) is applied on it. Otherwise, all bits of this port are
configured as inputs/outputs.

If any pin of this port is configured as an input then it acts as if it “floats”. Such an input has
unlimited input resistance and in determined potential.
When the pin is configured as an output, it acts as an “open drain”. By applying logic 0 to a
port bit, the appropriate pin will be connected to ground (0V). By applying logic 1, the
external output will keep on “floating”. In order to apply logic 1 (5V) on this output pin, it is
necessary to built in an external pull-up resistor.

Port 1

P1 is a true I/O port, because it doesn't have any alternative functions as is the case with P0,
but can be configured as general I/O only. It has a pull-up resistor built-in and is completely
compatible with TTL circuits.

Port 2

P2 acts similar to P0 when external memory is used. Pins of this port occupy addresses
intended for external memory chip. This time it is about the higher address byte with
addresses A8-A15. When no memory is added, this port can be used as a general
input/output port showing features similar to P1.

Port 3

All port pins can be used as general I/O, but they also have an alternative function. In order
to use these alternative functions, a logic one (1) must be applied to appropriate bit of the P3
register. In terms of hardware, this port is similar to P0, with the difference that its pins have
a pull-up resistor built-in.

9.Interfacing External Memory with 8051 Microcontroller

The design for 8051 Microcontroller based system is not limited to the internal RAM and
ROM present in the 8051 Microcontroller. There is also a provision of connecting both
external RAM and ROM i.e. Data Memory and Program.

The reason for interfacing external Program Memory or ROM is that complex programs
written in high – level languages often tend to be larger and occupy more memory.
Another important reason is that chips like 8031 or 8032, do not have any internal ROM but
have to be interfaced with external ROM.

A maximum of 64B of Program Memory (ROM) and Data Memory (RAM) each can be
interfaced with the 8051 Microcontroller.

Consider the following block diagram of interfacing 64KB of External RAM and 64KB of
External ROM with the 8051 Microcontroller.
References:

1. The 8051 Microcontroller and Embedded Systems using


Assembly and C by Muhammad Ali Mazidi.

2. The 8051 Microcontroller by I. Scott Mackenzie, Raphael C.W


Phan

Вам также может понравиться