0% found this document useful (0 votes)
12 views

Unit III 8051 Microcontroller

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit III 8051 Microcontroller

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 79

What is a Microcontroller ?

A microcontroller is a programmable integrated circuit (IC) that


consists of a small CPU, RAM and I/O pins. Microcontroller units
(MCUs) are widely used in many devices

8051 Microcontroller
S.
N Microprocessor Microcontroller
o

Microcontroller contains Microprocessor,


Microprocessor contains ALU, Memory (RAM and ROM), I/O interfacing circuit
1
control unit(clock and timing and peripheral devices such as A/D converter,
circuit), different register and serial I/O, timer,etc…
interrupt circuit,

It has many instructions to


It has one or two instructions to move data
2 move data between memory and
between memory and CPU
CPU

It has one or two bit handling


3 It has many bit handling instructions
instructions

Access times for memory and I/O Less access time for built in (memory and I/O
4
devices are more devices)

Microcontroller based system requires less


Microprocessor based system
5 hardware reducing PCB sizeand increasing
requires more hardware
reliability.
S.
Microprocessor Microcontroller
No

6 It is more flexible in design point of view Less flexible in design point of view.

7 Less no of pins are multifunctional More no of pins are multifunctional

It has single memory map for data and It has separate memory map for
8
code data and code

It has address bus, data bus and control Microcontroller communicates with
9 bus for interfacing with the outside outside world through P0, P1,
world. P2,P3 ports
What is Microprocessor?
A microprocessor is a central processing unit (CPU) that is designed to
execute instructions and perform arithmetic and logical operations in a
computer system. It is an integrated circuit that serves as the “brain” of a
computer or other electronic devices.
Microprocessors are responsible for carrying out the instructions of a
computer program by fetching, decoding, and executing them. They are
commonly found in personal computers, laptops, smartphones, and various
electronic devices.
Unlike microcontrollers, microprocessors are typically used in systems that
require general-purpose computing capabilities and are not dedicated to
specific control functions
Microprocessors:
General-purpose microprocessor
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example : Intel’s x86, Motorola’s 680x0

Many chips on mother’s board


Data Bus
CPU
General-
Serial
Purpose RAM ROM I/O Timer COM
Micro- Port
Port
processor
Address Bus

General-Purpose Microprocessor System


What is Microcontroller?
A microcontroller is a small integrated circuit that combines a
microprocessor core, memory, and peripheral devices such as
input/output ports, timers, and analog-to-digital converters. It is
designed to perform specific tasks and control functions in embedded
systems.
Microcontrollers are commonly used in various electronic devices,
including home appliances, automotive systems, industrial machinery,
and consumer electronics. They provide a cost-effective and efficient
solution for applications that require control, monitoring, and interaction
with the physical world
Microcontroller :
• A smaller computer
• On-chip RAM, ROM, I/O ports...
• Example : Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X

CPU RAM ROM


A single chip
Serial
I/O Timer COM
Port
Port
Microcontroller
Which is better, Microprocessor or a Microcontroller?
Ultimately, the choice between a microprocessor and a
microcontroller depends on the specific application requirements,
including processing power, system complexity, power consumption,
cost, flexibility, and development considerations. It is essential to
carefully evaluate these factors to determine which option best suits
the project’s needs.
Features of 8051
• Fabricated with CMOS technology with 2.7V to 5.5 V.
• Boolean Processor
• Operates on 12 MHz frequency
• 8 bit CPU with registers A and B
• 16 bit PC and DPTR (data pointer).
• 8 bit program status word (PSW)
• 8 bit Stack Pointer
• 4K Internal ROM ( On chip program memory)
• 128bytes Internal RAM (on chip data memory)
- 4 register banks each having 8 registers
16 bytes,which may be addressed at the bit level.
80 bytes of general purpose data memory
• Memory Address Capacity
• 64 K ROM * 64 K RAM
• 32 I/O pins arranged as four 8 bit ports:P0 to P3
• 6 Interrupt sources
• Two 16 bit timer/counters:T0 and T1
• Full duplex serial data receiver/transmitter
• Control registers: TCON,TMOD,SCON,PCON,IP and IE
• Two external and Three internal interrupt sources.
• Oscillator and Clock Circuits.
8051 microcontroller is 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 as per the requirement.
An on-chip crystal oscillator is integrated in the microcontroller having
crystal frequency of 12 MHz
1. Central processing unit (CPU): 8051 uses the 8-bit processor. This unit
carries out the operation on 8-bit data. A processor is the heart of
microcontroller. As the execution of the program stored in the memory is
performed by the processor.
The unit performs arithmetic and logical operations on 8-bit data as it has ALU,
with internal registers and program counters.
Several logical operations are performed by the ALU according to the program
stored in the memory.
The processor of 8051 microcontrollers possesses a special feature by which it
can process single bit or 8-bit data. This simply means that it has the ability to
access each single bit data either to clear, set or move etc. for any logical
computation.
2. Memory: Basically 8051 microcontroller consists of on-chip program memory
i.e., ROM and on-chip data memory i.e., RAM.
ROM –
8051 microcontroller has 4 KB ROM with 0000H to 0FFFH as the addressable
space. It is completely a program or code memory that means used by the
programmer to store the programs that are to be executed by the
microcontroller.
The operations that are executed by the device in which the microcontroller is
present are stored in the ROM of the memory at the time of fabrication. Hence
cannot be changed or modified.
RAM –
8051 holds a 128 bytes RAM. Basically, RAM is used to store data or operands for only
a small time duration. It can be altered anytime according to the need of the user. It is
also known as the data memory as it stores the data temporarily.
Out of the 128-byte RAM, first, 32 bytes is held by the working registers. Basically,
these are 4 banks which separately has 8 registers. These registers are accessed
either by its name or address. It is to be noted here that at a particular time only a
single register bank can be used.
As in 8051, the data and program memory i.e., RAM and ROM hold a definite memory
space. However, for some applications there exist the need for external memory to
enhance the memory space, thus external RAM, ROM/EPROM is used by the 8051
microcontrollers.
3. Input/ Output port: 8051 consists of 4 parallel ports of 8 bit each thereby
providing 32 input-output pins. All the 4 ports function bidirectionally i.e., either
input or output according to the software control.
4. Timer and Control Unit: Timers are used to create a time gap or delay
between 2 events. 8051 microcontroller consists of 2 timers of 16 bit each by
which the system can produce two delays simultaneously in order to generate the
appropriate delay.
.

8051 Microcontroller
8051 also known as MCS-51 is one of the most popular microcontroller.
Initially, it was created using NMOS technology but since NMOS technology needs more
power to function therefore later Intel introduced a new version of 8051
microcontroller based on CMOS technology and this new edition is called 80C51.
These most modern Microcontrollers need a fewer amount of power to function in
comparison to their forerunners.
8051 is an 8-bit microcontroller that includes 40-pins with dual inline package or DIP,
128 bytes RAM, 4-kb ROM and two 16-bit timers. It also includes four parallel 8-bit
ports that are programmable and addressable based on the requirement. It also has an
on-chip crystal oscillator of frequency 12 MHz integrated in it.
8051 Architecture
CPU (Central Processing Unit)
The Central Processor Unit or CPU is the mind of any processing machine.
It scrutinizes and manages all processes that are carried out in the
Microcontroller.
It interprets the program printed in storage space (ROM) and carries out all of
them and does the projected duty.
It also manages different types of registers in the microcontroller.

Interrupts
Interrupt is a subroutine (Subroutines are programs that are used by other
routines to accomplish a particular task. A set of instructions that are used
repeatedly in a program can be referred to as a Subroutine) call that
temporarily suspends the Microcontroller’s main program and helps it to
perform some other program which is more important than that at that
particular time and resume the main program where it had left it off.
Interrupts provide us a method to postpone the current process, carry out a
sub-routine task and then all over again restart standard program
implementation.
8051 has 5 interrupt sources, out of which two are peripheral Interrupts, two
are timer interrupts and one is a serial port interrupt.
Memory
A program is a set of commands, which is needed by the Microcontroller to
perform a particular task.
These programs need a storage space where they can be stored and later the
microcontroller interpret them and perform the specific task.
8051 microcontroller architecture include two types of memory, such as :
program memory and data memory.

The instructions of the CPU are stored in the Program Memory.


It is usually implemented as Read-Only Memory or ROM, where the Program
written into it will be retained even when the power is down or the system is
reset.
Data Memory in a Microcontroller is responsible for storing values of
variables, temporary data, intermediate results, and other data for the proper
operation of the program.
Bus
Bus is a group of wires which function as a communication canal or means to
transfer the Data
. These buses consists of 8, 16, or more cables. As a result, a bus can bear 8 bits,
16 bits altogether. There are two types of buses:
Address Bus: Microcontroller 8051 consists of a 16-bit address bus. It
is used to address memory positions. It is also utilized to transmit the
address from the CPU to Memory.
Data Bus: Microcontroller 8051 consists of 8 bits data bus. he data bus
is a signal line for exchanging the data between the CPU and the
memory, and between the CPU and I/O, and handles the data stored in
the specified location
Oscillator
Microcontroller 8051 consists of an on-chip oscillator that functions as a
time source for the CPU .
It allows the operations inside the microcontroller and other parts to be
synchronous.
Input/Output Port
8051 consists of 4 parallel ports of 8 bit each thereby providing 32 input-
output pins. All the 4 ports function bidirectionally i.e., either input or
output according to the software control
Timers/Counters
Timers are used to create a time gap or delay between two events.
The timer produces the delay according to the demand of the processor
and sends the signal to the processor once the respective delay gets
produced.
Microcontroller 8051 is incorporated with two 16 bit counters & timers.
The counters are separated into 8-bit registers.
The timers are utilized for measuring the intervals, to find out pulse width,
etc.
The microcontroller also includes a program counter, data pointer, stack &
stack pointer, instruction registers including latches, temporary registers &
buffers for the I/O ports.
Features of 8051 Microcontroller
Let’s discuss the common feature of 8051 microcontroller:
An 8-bit CPU with two registers A (ACC or accumulator) and B
Internal Flash ROM of 8KB – It is a flash memory that supports in – system
programming.
Internal RAM of 256 bytes – The first 128 Bytes of the RAM i.e. 00H to 7FH
is again divided in to 4 banks with 8 registers (R0 – R7) in each bank, 16 bit
addressable registers and 80 general purpose registers.
The higher 128 Bytes of the RAM i.e. 80H to FFH consists of Special
Function Registers (SFR) which are used to control different peripherals such
as Timers, Serial Port, all I/O Ports, etc.
32 I/O Pins – Arranged as 4 Ports: P0, P1, P2 and P3
8-bit Stack Pointer (SP) and Processor Status Word (PSW)
16-bit Program Counter (PC) and Data Pointer (DPTR)
Two 16-bit timers – T0 and T1
Interrupts – Two external and three internal
Oscillator and clock circuit
Serial Data Transmitter and Receiver for Full-Duplex operation – SBUF
Control Registers – SCON, PCON, TCON, TMOD, IP and IE
Applications of 8051 Microcontroller
1.Even with the development of many advanced and superior Microcontrollers, 8051
Microcontroller is still being used in many embedded system and applications.
2.Some of the applications of 8051 Microcontroller are mentioned below:
3.Consumer Appliances (TV Tuners, Remote controls, Computers, Sewing
Machines, etc.)
4.Home Applications (TVs, VCR, Video Games, Camcorder, Music Instruments,
Home Security Systems, Garage Door Openers, etc.)
5.Communication Systems (Mobile Phones, Intercoms, Answering Machines,
Paging Devices, etc.)
6.Office (Fax Machines, Printers, Copiers, Laser Printers, etc.)
7.Automobiles (Air Bags, ABS, Engine Control, Transmission Control, Temperature
Control, Keyless Entry, etc)
8.Aeronautical and Space
9.Medical Equipment
10.Defence Systems
11.Robotics
12.Industrial Process and Flow Control
13.Radio and Networking Equipment
14.Remote Sensing
Signal Diagram
8051 is an 8-bit microcontroller that contains 40 pins of dual-in-
line packaging. Out of the 40 pins, 32 pins are allotted to I/O ports.
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0
P1.2 3 38 ) 0.1(AD1)
P
P1.3 4 37 P0.2(AD2
P1.4 5 36 )P0.3(AD3)
P1.5 6 35 P0.4(AD4)
P1.6 7 34 P0.5(AD5)
P1.7 8 33 P0.6(AD6)
RST 9 8051 32 P0.7(AD7)
(RXD)P3.0 10 31 EA/VPP
(TXD)P3.1 11 30 ALE/PROG
(INT0)P3.2 12 29 PSEN
(INT1)P3.3 13 28 P2.7(A15)
(T0)P3.4 14 27 P2.6(A14
(T1)P3.5 15 26 )P2.5(A13
(WR)P3.6 16 25 )P2.4(A12
(RD)P3.7 17 24 )P2.3(A11
XTAL2 18 23 )P2.2(A10)
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)
8051 Schematic Pin Diagram
Port 3 Alternate Functions
XTAL Connection to 8051

• These two pins are allotted for internal clock


signaling.
• The external oscillator forms a connection to
these two pins so as to have an internal clock
signal to the system..
C2
XTAL2
30pF
C1
XTAL1
30pF

GND
Power Supply Signal
Vcc provides supply voltage to the chip.
The voltage source is +5V.
GND ( pin 20 ): ground
RST ( pin 9 ): reset (Input)
•input pin and active high.
•The high pulse must be high at least 2 machine cycles.
•There are two ways to reset the controller
Power on Reset
Manual Reset
Power-on reset.
Upon applying a high pulse to RST, the microcontroller will reset and all
values in registers will be lost.(PC,ACC,B,DPTR are 0000H and Content of SP
0007H)

ALE ( pin 30 ): address latch enable


It is an output pin and is active high.
The ALE pin is used for de-multiplexing the address and data bus (A0 to A7 & D0
to D7).
ALE =0 port 0 provides Data Bus D0-D7
ALE=1 Port 0 provides Address Bus A0-A7
EA : External Access
•This pin is connected to either Vcc or GND
•EA is high or Vcc can be execute program from internal
program memory till address 0FFFH.
•EA is Low can be execute program from external memory
only.

PSEN : ( Program Store Enable)


This pin is active low output signal

PSEN is High during execution of program from internal


Program Memory.

PSEN is Low it is used as reading data from the external


program memory. ( It is activated after every six clock cycles
during fetch data from external ROM)
Power-On RESET
RESET is an active High input When RESET is set to High, 8051 goes back to
the power on state.
The 8051 is reset by holding the RST high for at least two machine cycles and
then returning it low.

Vcc

31
EA/VPP
X1
10 uF 30 pF

X2
RST
9
8.2 K
* Power On Reset.

Initially charging of capacitor makes RST High


When capacitor charges fully it blocks DC.

•Manual Reset

closing the switch momentarily will make RST High.


Register Set of 8051

Register A (Accumulator)
• It is an 8 bit register
• It holds a source operand( it defines the data being processed by the
instructions JNZoffset) and receives the result of the arithmetic
instruction( addition, subtraction, multiplication and division)
• The accumulator can be the source or destination for logical operations
and no of special data movement instructions….
Register B
• It is an 8 bit B register
• It is an general purpose register.
• It is used for the Multiply/Divide operation to store the second operand for
MUL & DIV instruction.
• It hold Upper 8 bit of result and remainder in case of division are
stored in B register
Register Set of 8051
Program Status Word (PSW)
* It is an 8 bit register

CY AC F0 RS1 RS0 OV -- P

Carry flag PSW.7CY


Auxiliary carry flag PSW.6AC
User defined flag PSW.5 --
Register Bank selector bit 1 PSW.4RS1
Register Bank selector bit 0 PSW.3RS0
Overflow flag PSW.2OV
User define bit PSW.1 --
Parity flag Set/Reset odd/even parity PSW.0 P

RS1 RS0 Register Bank Address


0 0 Bank 0 00H-07H
0 1 Bank 1 08H-0FH
1 0 Bank 2 10H-17H
1 1 Bank 3 18H-1FH
The 8051 microcontroller utilizes a stack to temporarily store
data and manage subroutine calls. This stack is a Last-In-
First-Out (LIFO) data structure, meaning that the last item
pushed onto the stack is the first one to be retrieved

Stack Pointer (SP)


The Stack Pointer (SP) is an 8-bit register that keeps track of
the top of the stack. When the 8051 is reset, the SP is
initialized to 07H, indicating that the stack begins at memory
location 08H. As we push data onto the stack, the SP is
automatically incremented, and when we pop data off the
stack, it’s decremented.
Stack Operations
Two primary operations are performed on the stack:
PUSH: This operation adds data to the top of the stack.
POP: This operation retrieves data from the top of the stack.
Mastering the PUSH Operation
The PUSH operation is fundamental to stack management in
the 8051. When we push data onto the stack, several steps
occur:
The Stack Pointer (SP) is incremented.
The data is stored at the memory location pointed to by the SP.
MOV A, #42H ; Load the accumulator with the value 42H
PUSH ACC ; Push the accumulator contents onto the stack
In this code snippet, we first load the accumulator with the
hexadecimal value 42H. The PUSH instruction then increments
the SP and stores the accumulator’s contents at the new stack
top.
Perfecting the POP Operation
The POP operation is the counterpart to PUSH, allowing us to
retrieve data from the stack. When we pop data, the following
steps take place:
The data at the current stack top is read.
The Stack Pointer (SP) is decremented.
POP ACC ; Pop the top stack value into the accumulator
This instruction retrieves the value from the top of the stack,
stores it in the accumulator, and then decrements the SP.
Stack in the 8051

• The register used to


access the stack is called 7FH
SP (stack pointer) Scratch pad RAM
register.
30H
• The stack pointer in the 2FH
8051 is only 8 bits Bit-Addressable RAM
wide, which means that 20H
it can take value 00 to 1FH Register Bank 3
FFH. When 8051 18H
powered up, the SP 17H
Register Bank 2
register contains value 10H
0FH
07. 08H
Register )Stack(
Bank 1
• During PUSH operation 07H
Register Bank 0
first increment SP and 00H
then copy data.
• POP operation copy data
and then decrement SP.
Register Set of 8051

DTPR ( Data Pointer)


• It is an 16 bit register
• It consist of Higher byte (DPH) and lower byte (DPL) of 16 bit external data RAM
address.
• It is very useful for string operation.
• With 16 bit DPTR maximum of 64K off chip data memory and 64K of off chip
program memory can be addressed.
• It can be read data from memory location and write data from memory location.
• If Increment the content of DPTR used INC DPTR is executed.

Serial Port Data Buffer

• It is an two independent register such as transmit buffer and receive buffer.


• Transmit buffer is a parallel in serial out register (PISO).
• Receive buffer is a serial in parallel out register (SIPO).
• It identified as SBUF. If data is moved to SBUF it goes to the transmit buffer and sets
serial transmission.
• When data is moved from SBUF it receives serial data from receive buffer.
Register Set of 8051

Timing Register
• It is an two 16 bit register
• It will access lower and upper bytes.
• The TH0 and TL0 represent lower and upper byte of timer 0
• The TH1 and TL1 represent lower and upper byte of timer 1.
• The address of register have been stored in SFR from 80H to FFH.
• There is a Timer control (TCON) and Timer Mode(TMOD) register to
configure all timer/counter in various modes.

Control Register

• It is an control register consist of


– Interrupt Priority (IP)
– Interrupt Enable (IE)
– Timer Mode(TMOD)
– Timer control (TCON)
– Serial port control (SCON)
– Power control (PCON)
• All of these register have allotted address in Special function register.
Register Set of 8051

Timing and Control Unit


• The common control signals are
– ALE
– PSEN
– RD
– WR

Instruction Register

• It is used to decode the opcode of any instruction to be executed.


• After decoding this register sends the decoded information to the timing and
control unit to generate necessary signals for execution of specified instruction.

Program address Register


It is an on chip EPROM

RAM
This block provide internal on chip RAM 128 bytes.
ALU
SFR ( Special Function Register)
This register bank is a set of registers
Name Function Name Function

A Accumulator SBUF Serial Port data buffer


B Arithmetic SP Stack Pointer
DPH Addressing Ext TMOD Timer/Counter mode
Memory cntrl
DPL Addressing Ext TCON Timer/Counter cntrl
Memory
IE Interrupt enable TL0 Timer0 lower byte
IP Interrupt Priority TH0 Timer0 higher byte
P0 I/O Port Latch TL1 Timer1 lower byte
P1 I/O Port Latch TH1 Timer1 higher byte
P2 I/O Port Latch
P3 I/O Port Latch
PCON Power Control
PSW Pgm Status Word
SCON Serial PortCntrl
SFR Memory Map
Internal RAM
It has 128 byte internal RAM. It is accessed using RAM address register.
Internal RAM is organised as three areas
* Working registers * Bit addressable * General Purpose
Internal ROM
It has 4K byte internal ROM. It is accessed address space from 0000H to
0FFFH. By connecting external access (EA) pin to ground.

Program
PC
Address
DPTR
Register

0FFFH 4K
EPROM/
0000H ROM
I/O Ports
It has 32 I/O pins configured as four eight bit parallel ports (P0,P1, P2, P3)
all are bidirectional each pin will be configured as input and output (or
Both) under software control. Each port consist of Latch output driver and
an input buffer.
PORT 0
• When connecting an 8051 to an external memory, the 8051 uses ports to send addresses and read instructions.
– 16-bit address : P0 provides both address A0-A7, P2 provides address A8-A15.
– Also, P0 provides data lines D0-D7.
• When P0 is used for address/data multiplexing, it is connected to the 74LS373 to latch the address

PORT 1 : Used only as I/O Pins


PORT 2 : used to access external memory hold higher order address
PORT 3:
Port 3 is configured as an output port upon reset, this is not the
way it is most commonly used.
• Port 3 has the additional function of providing signals.
– Serial communications signal : RxD, TxD
– External interrupt : INT0, INT1
– Timer/counter : T0, T1
– External memory accesses : WR, RD
Port 0 with Pull-Up Resistors

Vcc
10 K

P0.0
DS5000 P0.1

Port 0
P0.2
8751 P0.3
8951 P0.4
P0.5
P0.6
P0.7
• Here if there is no pull up, when switch is open
the input is in floating state that means it is
neither high nor low. You cannot know the
state of the pin.
• Now if you use a pull up resistor (usually around
10k)
– when switch is open the input is connected to vcc
through resistor and the input reads high.
– If the switch is closed the input reads low
because no current flows into input pin.
• Not only 8051 we can use pull up resistor for
any microcontrollers, but new microcontrollers
nowadays have inbuilt pull up resistors which
you can configure.
IMPORTANT PINS (IO Ports)

One of the most useful features of the 8051 is that it


contains four I/O ports (P0 - P3)
• Port 0 ( pins 32-39 ): P0 ( P0.0 ~ P0.7 )
– 8-bit R/W - General Purpose I/O
– Or acts as a multiplexed low byte address and data bus for external
memory design

• Port 1 ( pins 1-8 ) : P1 ( P1.0 ~ P1.7 )


– Only 8-bit R/W - General Purpose I/O

• Port 2 ( pins 21-28 ): P2 ( P2.0 ~ P2.7 )


– 8-bit R/W - General Purpose I/O
– Or high byte of the address bus for external memory design

• Port 3 ( pins 10-17 ): P3 ( P3.0 ~ P3.7 )


– General Purpose I/O
• Each port can be used as input or output (bi-direction)
8051 Port 3 Bit Latches and
I/O Buffers
Hardware Structure of I/O Pin

Read latch Vcc


TB2
Load(L1)

Internal CPU D Q P1.X


bus P1.X pin

Write to latch Clk Q M1

TB1
Read pin
Writing “1” to Output Pin P1.X

Read latch Vcc


TB2
Load(L1) 2. output pin is
1. write a 1 to the pin Vcc
Internal CPU D Q 1 P1.X
bus P1.X pin
0 M1
output 1
Write to latch Clk Q

TB1
Read pin
Writing “0” to Output Pin P1.X

Read latch Vcc


TB2
Load(L1) 2. output pin is
1. write a 0 to the pin ground
Internal CPU D Q 0 P1.X
bus P1.X pin
1 M1
output 0
Write to latch Clk Q

TB1
Read pin
Reading “High” at Input Pin

Read latch Vcc


TB2
1. write a 1 to the pin MOV Load(L1)
P1,#0FFH

1 1 P1.X pin
Internal CPU bus D Q
P1.X
0 M1
Write to latch Clk Q

TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
Timer /Counters
Timer is used to count machine cycle and provide a specified time
delay.

Actually a machine cycle consist of 12 oscillator periods.


Oscillator frequency/12

• It is a two 16 bit timer/counter such as Timer 0(T0) and


Timer 1 (T1).
• Each timer can be programmed to count internal clock pulses.
• Maximum value is 65536
• Timers/Counters can be operated by user with special function
registers.

• T0 and T1 share two SFRs: TMOD and TCON


• Each timer has also two registers dedicated to : TH0/TL0 and
TH1/TL1.
• These timers are used for following functions

– Calculating time delay between two events.


– Counting the number of events
– Generate baud rate for serial ports
– Frequency measurements.
– Pulse width measurement.

* When oscillator frequency is 12 MHz then time period


of one cycle is 1 s.
TMOD (Timer Mode Register) is a non-bit-addressable, 8-bit register

• Lower 4 bits are for Timer0


• Upper 4 bits are for Timer1
• GATE bit is used for choice of internal or external control
• GATE=0 is for internal control, start and stop are controlled by
software
• GATE=1 is for external control, start and stop are controlled by
software and external source
• C/T bit decides about timer type: interval timer or counter
• M0 and M1 bits are used to set timer mode ( Timer0 and
Timer1)
• 8051 delivers 4 timer modes

Mode 0: THx as 8bit timer/counter and TLx as 5 bit timer/counter


(prescalar)
Mode 1: THx and TLx are cascaded and there is no prescalar
Mode 2: THx hold count value when is to be reloaded into TLx after each
overflows.
Mode 3: Timer 0 used as two 8 bit timer, Timer 1 used as baud rate
generator.
GATE
If TRx is set and GATE =1.
Timer/Counter x will operate only when INTx pin is high
for hardware control.
When GATE =0 timer/counter x will run only if TRx=1 for
software control.

C/T(Clock/Timer)
The C/T bit in TMOD register is used to take decision
whether the timer is used as a timer or an counter.
If
C/T=0 : Timer/counter selector bit is cleared to generate
time delay.
C/T=1 : Timer/Counter selector bit is counting pulses from
external input pin Tx (T1 and T0)
TCON (Timer Control Register)

It is a bit-addressable, 8-bit register where 4 upper bits are


responsible for timers/counters

TF1 : Timer 1 Overflow Flag.


TF1=1 : It is set by hardware when timer/counter 1 overflows.
TF1=0 : It is cleared by hardware processor vectors to the Interrupt service
routine.`

TF0 : Timer 0 Overflow Flag


TF0=1 : Program software to enable timer 0 to count.
TF0=0 : It is cleared 0 by software to halt timer.

TR1 : Timer 1 Run control bit.


TR1=1 software program to enable timer 1 to count.
TR1=0 it is cleared to 0 by program to halt timer.
IE1 External Interrupt 1 edge flag
IE1 =1 if set when high to low (the falling) edge signal is received on
port (INT1)
IE1=0 its cleared when processor vector to ISR.

IE0 External Interrupt 0 edge flag


IE0 =1 if set program to enable interrupt 0 to be triggered by a high to low
(falling) edge signal is received on port (INT1).
IE0=0 Program to enable a low level signal on external interrupt 0 to
generate an interrupt.

IT1 Interrupt 1 Type control bit


IT1 =1 if set when program to enable external interrupt 1 to be triggered
by falling edge signal.
IT1=0 if set to 0 by program to enable low level signal on external
interrupt 1 to generate an interrupt.
Serial Communication
Basics of serial
communication
Parallel : expensive - short distance – fast
Serial : cheaper– long (two different cities by modem)-slow
Basics of serial communication
There are two methods of serial communication,
• Synchronous : Transfer block of data at a time.
• Asynchronous : Transfer single byte at a time.
We will controlled by software synchronous & Asynchronous.

We use hardware such as : UART & USART.


8051 build with UART.
It support full duplex serial port. TxD and RxD pin for transmission
and receive data respectively.

The function of serial port is to perform


* parallel to serial conversion for data output
* serial to parallel conversion for data input.
Start and stop bits
 When there is no transfer the signal is high
Transmission begins with a start (low) bit LSB first
Finally 1 stop bit (high)
Data transfer rate (baud rate) is stated in bps
bps: bit per second
Registers for Serial Data
Communication
• The following registers are used for Serial data communication.
– SBUF ( Serial Port Data Buffer)
– SCON ( Serial port control register)
– PCON( Power control register)

SBUF ( Serial Port data Buffer)

Two registers
• Used to hold data that to be transmitted through TxD it is
write only type.
• Used to hold data from external sources through RxD it
is read only type.
Serial control (SCON) Register

SM0 SM1 SM2 REN TB8 RB8 TI RI

SM0 (SCON.7) : mode specifier


SM1 (SCON.6) : mode specifier
SM2 (SCON.5) : used for multi processor communication
REN (SCON.4) : Receive Enable (by software enable/disable)
TB8 (SCON.3) : Transmit Bit8
RB8 (SCON.2) : Receive Bit 8
TI (SCON.1) : Transmit Interrupt flag set by HW clear by SW
RI (SCON.0) : Receive Interrupt flag set by HW clear by SW
Mode of operation

SM0 SM1 MODE operation transmit


rate
0 0 0 8 Shift register fixed
(xtal/12)
0 1 1 8 bit UART variable (timer1)
1 0 2 9 bit UART fixed (xtal/32 or
xtal/64)
1 1 3 9 bit UART variable (timer1)
PCON : Power control register
Power control
• A standard for applications where power consumption is critical
• Two power reducing modes
– Idle
– Power down

• RST signal clears the IDL bit directly


• Last instruction executed before going into the power down mode
• The on-chip oscillator is stopped.

All functions are stopped, the contents of the on-chip RAM and
Special Function Registers are maintained.
* The ALE and PSEN output are held low
* The reset that terminates Power Down
Interrupts
An interrupt is an external or internal event that interrupts the microcontroller to
inform it that a device needs its service.

• A single microcontroller can serve several devices.


• The program which is associated with the interrupt is called the
interrupt service routine (ISR) or interrupt handler.
• Finish current instruction and saves the PC on stack.
• Jumps to a fixed location in memory depend on type of interrupt
• Starts to execute the interrupt service routine until RETI (return
from interrupt)
• Upon executing the RETI the microcontroller returns to the place
where it was interrupted. Get pop PC from stack

Interrupt Control Register


•Interrupt Enable Register (IE)
•Interrupt Priority Register (IP)
Interrupt Sources
• Original 8051 has 6 sources of
interrupts
– Reset
– Timer 0 overflow
– Timer 1 overflow
– External Interrupt 0
– External Interrupt 1
– Serial Port events (buffer full, buffer empty, etc)

• Enhanced version has 22 sources


– More timers, programmable counter array, ADC, more
external interrupts, another serial port (UART)
Interrupt Enable (IE) register

• All interrupt are disabled after reset


• We can enable and disable them by IE
• EA : Global Interrupt (enable/disable)
– 1 Enable all individual interrupt request
– 0 disable all interrupt request

• ES : Serial Interrupt (enable/disable)


• 1 UART system enable interrupt
• 0 UART system cannot generate interrupt
• ET1 : Timer 1 Interrupt ( enable/ disable)
• 1 Timer 1 enable an interrupt
• 0 Timer 1 cannot generate interrupt

• EX1 : External 1 Interrupt ( enable/disable)


• 1 enable
• 0 disable
• ET0 : Timer 0 Interrupt
1 enable 0 disable
• EX0 : External 0 Interrupt
0 change of the INT1 pin logic state cannot generate interrupt.
Interrupt Priorities
• What if two interrupt sources interrupt at the same
time?
• The interrupt with the highest PRIORITY gets serviced
first.
• All interrupts have a power on default priority order.
1. External interrupt 0 (IE0)
2. Timer Flag interrupt 0 (TF0)
3. External interrupt 1 (INT1)
4. Timer Flag interrupt1 (TF1)
5. Serial communication (RI+TI)
• Priority can also be set to “high” or “low” by IP reg.
Interrupt Priorities (IP) Register

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

IP.7: reserved
IP.6: reserved
IP.5: timer 2 interrupt priority bit(8052 only)
IP.4: serial port interrupt priority bit
IP.3: timer 1 interrupt priority bit
IP.2: external interrupt 1 priority bit
IP.1: timer 0 interrupt priority bit
IP.0: external interrupt 0 priority bit
Interrupt Priorities Example
--- --- PT2 PS PT1 PX1 PT0 PX0

• MOV IP , #00000100B or SETB IP.2 gives priority order


1. Int1
2. Int0
3. Timer0
4. Timer1
5. Serial
• MOV IP , #00001100B gives priority order
1. Int1
2. Timer1
3. Int0
4. Timer0
5. Serial
Interrupt inside an interrupt
--- --- - PS PT1 PX1 PT0 PX0

 A high-priority interrupt can interrupt a low-priority interrupy


 All interrupt are latched internally
 Low-priority interrupt wait until 8051 has finished servicing the high-priority
interrupt
PX0 : External Interrupt 0
PT0 : Timer 0 Interrupt
PX1 : External Interrupt 1
PT1 : Timer 1 Interrupt
PS : Serial Port Interrupt

You might also like