Ntroduction O Icrocontrollers: Fig. 2.1: Functional Block Diagram of A Microcontroller
Ntroduction O Icrocontrollers: Fig. 2.1: Functional Block Diagram of A Microcontroller
INTRODUCTION T O MICROCONTROLLERS
2.1 MICROPROCESSOR S AND M ICROCONTROLLERS
Microcontrollers are similar to microprocessors, but they are designed to work as a true
single-chip system by integrating all the devices needed for a system on a single-chip. The basic
functional units of a microprocessor will be ALU, a set of registers, timing and control unit. The
microcontroller will have these functional blocks and in addition may have IO ports, a programmable
timer, RAM memory and EPROM/EEPROM memory. Some microcontrollers may even have internal
ADC and/or DAC.
Internal Bus
Instruction Program
Register ALU Timing & Decoding
Control Counter
Array Unit
Unit
Flag Register
decoding Unit, Timing and control unit, RAM memory, EPROM/EEPROM memory, Parallel IO
port, Serial IO port, Programmable timer, ADC and DAC. All microcontrollers may not have all the
blocks shown in Fig. 2.1. Some of the functional blocks shown in Fig. 2.1, may not be available in
certain microcontrollers.
The ALU is the computational unit of the microcontroller which performs arithmetic and
logical operations. The various conditions of the result are stored as status bits called flags in the
flag register. The register array and internal RAM memory are used as a temporary storage device
for storing temporary data during execution of a program.
The program codes and permanent data are stored in EPROM/EEPROM. In microcontroller-
based systems, an external memory is provided only when the internal memory is not sufficient
and so in most of the microcontroller-based systems, the program and data are stored in the
internal memory of the microcontroller itself.
The program counter generates the address of the instructions to be fetched from the memory
and sends it through the internal bus to the memory. (If the instruction to be fetched is stored in the
external memory then the address is send through IO ports to the external memory. Because the
microcontrollers communicate with the external world only through IO ports.) The memory will
send the instruction codes, which are decoded by the instruction decoding unit and send the
information to the timing and control unit. The timing and control unit will generate the necessary
control signals for internal and external operation of the microcontroller.
The parallel and serial IO ports are used for interfacing IO devices like switches, keyboard,
LCD/LED, ADC, DAC, etc., and also for any other input/output operations.
Microcontrollers do not have a dedicated external address and data bus. Therefore for
interfacing any additional peripheral devices, the external address and data buses are formed only
by using port lines.
Microcontrollers with internal ADC can directly accept analog signals for processing. Likewise,
microcontrollers with internal DAC can directly generate analog signals for controlling analog devices.
A programmable timer can be used for time-based operations and it can also be used as a counter.
TABLE - 2.1 : COMPARISON OF MICROPROCESSOR AND MICROCONTROLLER
Microprocessor Microcontroller
1. The functional blocks of a microprocessor are 1. The microcontroller includes the functional blocks
the ALU, registers, and timing and control unit. of a microprocessor and in addition has a timer,
a parallel IO port, a serial IO port, internal RAM
and EPROM/EEPROM memory. Some
controllers even have ADC and/or DAC.
2. A microprocessor is concerned with rapid 2. A microcontroller is concerned with rapid
movement of code and data between the movement of code and data within the
external memory and the microprocessor. microcontroller. Hence it has few instructions for
Hence it has a large number of instructions for data transfer between the external memory and
moving data between the external memory and the microcontroller.
the microprocessor.
3. Microprocessors mostly operate on byte/word 3. Microcontrollers often manipulate with bits and
data and so have very few bit manipulating so have a large number of bit manipulating
instructions. instructions.
C HAPTER 2 I NTRODUCTION T O M ICROCONTROLLERS 2. 3
The INTEL 8051 is an 8-bit microcontroller with 128 byte internal RAM and 4 kb internal
ROM. The INTEL 8031 is same as 8051 except that it does not have an internal ROM (i.e., the
8031 has all features of 8051 except the internal ROM). The 8051 is a 40-pin IC available in Dual
In-line Package (DIP) and it requires a single power supply of +5-V. Its maximum internal clock
frequency rating is 12 MHz. The 8031 is pin to pin compatible with 8051. The 8X5X family
members listed in Table-2.2 are also pin-to-pin compatible with 8051. The pin configuration of
8031/8051 microcontroller is shown in Fig. 2.3 and the signals of the controller are listed in
Table-2.3. Some of the port pins of a 8031/8051 microcontroller have alternate functions and they
are listed in Table-2.4.
TABLE- 2.2 : SOME MEMBERS OF THE 8X5X FAMILY OF MICROCONTROLLERS
8 8
P1.0 ¬
®1 40 ¬ VCC P1.7 - P1.0 P0.7 - P0.0
P1.1 ¬
®2 39 ¬
® P0.0(AD0) (AD7 - AD0)
®3
P1.2 ¬ ® P0.1(AD1)
38 ¬
8
P1.3 ¬
®4 37 ¬
® P0.2(AD2) P2.7 - P2.0
P1.4 ¬
®5 36 ¬
® P0.3(AD3) ® (A15 - A8)
(RD) P3.7 ¬
P1.5 ¬
®6 35 ¬
® P0.4(AD4) (WR) P3.6 ¬ ®
®7
P1.6 ¬ 34 ¬
® P0.5(AD5) (T1) P3.5 ¬
®
P1.7 ¬
®8 33 ¬
® P0.6(AD6) ®
(T0) P3.4 ¬
8031/8051
8031/8051
RST ® 9 ® P0.7(AD7)
32 ¬ (INT1) P3.3 ¬ ®
(RXD) P3.0 ¬ ® 10 31 ¬ EA/VPP (INT0) P3.2 ¬ ® ¬ EA/V PP
(RD) P3.7 ¬® 17 24 ¬
® P2.3(A11) XTAL2 ¬
®
XTAL2 ¬ ® 18 ® P2.2(A10)
23 ¬
XTAL1 ® 19 XTAL1 ®
® P2.1(A9)
22 ¬
VSS ¬ 20 ® P2.0(A8)
21 ¬
Note : The signals shown within brackets are alternate functions of port pins.
Pins/Signal Description
PSEN Program store enable. Used as read control or enable for external
program memory.
ALE/PROG Address Latch Enable or program pulse input during EPROM/
ROM programming.
The 8031/8051 microcontroller has 32 IO pins and they are organized as four numbers of an
8-bit parallel port. The ports are denoted as port-0, port-1, port-2 and port-3. Each port can be
used either as an 8-bit parallel port or 8 numbers of 1-bit port (i.e., individual pins of each port can
be used as 1-bit IO line independently). When used as 1-bit port, the port pins are denoted as PX.Y,
where X can take values 0 to 3 and Y can take values 0 to 7. For example, the bit-0 of
port-1 is denoted as P1.0. The ports behave as latches during output operation and as buffers
during input operation. Except port-0 all other ports are provided with internal pull up. Hence, while
using port-0 for IO operation, external pull up should be provided.
Except port-1 all other ports have alternate functions. (Port-1 can be used only for IO
operation.) When external memory is employed, port-0 functions as multiplexed low byte address
or data lines, and port-2 functions as high byte address lines. Therefore, for accessing external
memory the microcontroller uses a 16-bit address and accesses the memory in bytes. Hence the
addressable memory space is 64 kb (216 = 64 k). The 8031/8051 allows the external memory to be
organized as two banks of 64 kb, one for the program/code and the other for the data. The signal
PSEN is used as read control/enable for program memory. The port pin P3.7 functions as read
control (RD) and the port pin P3.6 functions as write control (WR) for data memory. When two
external memory banks are not desirable, the PSEN and RD should be externally ANDed to provided
a single read control signal. In such cases the controller will access a common memory space (of
maximum capacity 64 kb) for program and data.
In systems with external memory, the signal ALE is used to demultiplex the low byte address
or data using an external latch. The output signal on the ALE pin is the clock signal with a frequency
one-sixth of a crystal or internal clock frequency. The controller will output the ALE signal at a
constant rate (i.e., at one-sixth of internal clock) even when there is no external memory. Therefore
the ALE can also be used for external timing and clock source for peripherals or IO devices. In
EPROM/ROM version of 8051 family controllers, the programming pulse can be input through
ALE during programming of EPROM/ROM.
2. 6 MICROPROCESSORS AND MICROCONTROLLERS
Signal EA is used as an external program memory access control. The microcontroller will
access the program from external memory if EA pin is grounded. During programming mode of
internal EPROM/ROM, this pin is used to supply the programming voltage (+12-V).
Note : For programming the internal EPROM/ROM of 8051 family of microcontroller, a
separate programmer should be employed. The controllers listed in Table-2.2, does not have
ISP (In-system programmable) facility.
The XTAL1 and XTAL2 pins are provided for external quartz crystal connection, in order
to generate the required clock for the microcontroller. The maximum frequency of a quartz crystal
that can be connected to an 8031/8051 microcontroller is 12 MHz. (There are higher clock versions
of the 8051 family of microcontrollers. For details please refer to manufacturers data sheet.)
Alternatively the external clock can be supplied through an XTAL1 pin. The internal clock frequency
of an 8031/8051 microcontroller is same as a crystal frequency or externally supplied clock
frequency. When a crystal is connected between XTAL1 and XTAL2, the controller will output a
clock signal through XTAL2 whose frequency is same as crystal frequency and this clock signal
can be used for peripheral or IO devices.
The RST signal is used to reset the microcontroller in order to bring the controller to a
known state. For proper reset the RST pin should be held low for at least two machine cycles.
When the 8031/8051 controller is reset, all the internal registers are cleared except the port latches,
stack pointer and SBUF register. The internal RAM is not affected by reset. The content of the
various registers of 8031/8051 after a reset are listed in Table-2.5.
The 8031/8051 has five interrupts. In this two interrupts are external interrupts and the
remaining three are internal interrupts. The two external interrupts are interrupts initiated by applying
appropriate signals through the pins INT0 and INT1, and they are called external interrupt-0 and
external interrupt-1 respectively. The internal interrupts are initiated by timer-0, timer-1 and the
serial port. All the interrupts of 8031/8051 are maskable and vectored interrupts. The vector address
and the priorities of the interrupts of 8031/8051 are listed in Table-2.6. (The priorities of the
interrupts can also be altered by programming the IP register.)
C HAPTER 2 I NTRODUCTION T O M ICROCONTROLLERS 2. 7
The 8031/8051 has Harvard architecture in which the same address in different memory
devices or banks is used for program (or code) and data. Therefore the architecture has two
dedicated 16-bit address pointers, namely, Program Counter (PC) and Data Pointer (DPTR). The
PC is used as the address pointer to access program instructions and it is automatically incremented
after every byte of instruction fetch. The DPTR is used as address pointers to read/write data in
data memory and it is programmable using instructions.
Since the size of the address pointers are 16-bit they can address up to 216 = 64 k memory
locations. Hence 8031/8051 supports two memory banks of 64 kb each, one for program and the other
for data. In 8051, when the EA pin is tied to the VCC (logic-1), the first 4 kb of program memory address
space refers to 4 kb internal ROM and the remaining 60 kb refer to external (EPROM/RAM) memory.
In 8051, when the EA pin is tied to the ground (logic-0), the entire 64 kb of program address space
refers to external (EPROM/RAM) memory. In 8031 there is no internal ROM and so EA pin is always
grounded and in this case the entire program memory is external.
The 8031/8051 has separate 256 bytes internal RAM accessed by using an 8-bit address. In
this 256 bytes address space, the first 128 addresses are allotted to internal RAM and the next 128
bytes are allotted to SFR. The internal RAM/SFR can be accessed by using MOV instructions and
external data memory (RAM) can be accessed by using MOVX instruction.
The 8051 has four numbers of 8-bit ports, namely, port-0, port-1, port-2 and port-3. Each
port has a latch and driver (or buffer). When external memory is employed the port-0 lines will
function as multiplexed low byte address/data lines and port-2 lines will function as high byte
address lines. Also, the port pins P3.7 and P3.6 are used to output read and write control signals
respectively. In fact, each pin of port-3 has an alternate function which are listed in Table-2.4.
Port-1 is a dedicated IO port and does not have any alternate function. The ports are also
mapped as internal memory in the controller and so they can be addressed as memory locations for
8-bit operation.
2. 8 MICROPROCESSORS AND MICROCONTROLLERS
P0.7
P0.5
P0.4
P0.3
P0.6
P0.2
P0.1
P2.7
P0.0
P2.5
P2.4
P2.3
P2.6
P2.2
P2.1
P2.0
¯¯
¯ ¯
¯ ¯
¯¯
¯¯
¯ ¯
¯ ¯
¯¯
Port-0 Port-2
Drivers Drivers
¯ ¯
RAM Address RAM Port-0
Port-2 ¯
Register Memory Latch ROM Memory
Latch
¯ ¯ ¯ ¯
¯ ¯
Program
Address Register
¬
A-Register SP
(Stack Pointer)
¯ ¯
TEMP1 TMP1 ¨ Buffer ¨
¯ PCON SCON TMOD TCON
B-Register ¯ ¯ T2C0N Th0 TL0 Th1
TL1
PC
Incrementer
¨
ALU
SBUF IE IP
¯ Interrupt, Serial Port
PSW and Timer Blocks PC
(Program Counter)
¨
PSEN ¬
Timing
ALE ¬ Instruction ¯ ¯ ¯
and ¬ ® DPTR
¨
EA ® Control Register
(Data Pointer)
RST ® Unit
¯
¯
Port-3
PD
Port-1
Latch Latch
¯ ¯ ¯
Port-1 Port-3
Oscillator Drivers Drivers
¯ ¯
¯ ¯
¯
¯
¯ ¯
¯ ¯
¯ ¯
¯¯ ¯¯ ¯
P3.7
P1.7
P3.5
P3.4
P3.3
P1.5
P1.4
P1.3
P3.6
P3.2
P3.1
P1.6
P1.2
P1.1
P3.0
P1.0
VCC VSS
¯
XTAL1 XTAL2
The SFRs include 21 internal registers listed in Table-2.7. (A detailed explanation of SFRs is
presented in the next section.) The SFRs are mapped as internal data memory. The data memory
address space 80H to FFH are reserved for SFRs. Each register of SFR has one-byte address. Some
of the registers are both byte and bit-addressable. (The registers whose address ends with 0H or 8H
are bit-addressable.)
The 8051 has an 8-bit ALU which performs arithmetic and logical operations on binary data.
The A and B registers are used to hold the input data and the result of the ALU operation. Starting
from the address stored in the PC, the controller will fetch the instructions one by one, and store in
C HAPTER 2 I NTRODUCTION T O M ICROCONTROLLERS 2. 9
IR, which decodes the instructions and give information to the timing and control unit. Using the
information supplied by the IR unit, the control signals necessary for internal and external operations
are generated by the timing and control unit. The 8031/8051 has an internal oscillator and so it is
sufficient if an external quartz crystal is connected for clock generation.
The 8031/8051 has two 16-bit programmable timers/counters namely, timer-1 and timer-0.
In the counter mode of operation they can count the number of high to low transitions of the signal
applied to the timer pins. In timer mode of operation they can be independently programmed to
work in any one of the four operating modes. The timer operating modes are called mode-0,
mode-1, mode-2 and mode-3.
The 8051 family of microcontrollers has a full duplex serial port which can be programmed to
work in any one of the four operating modes, namely, mode-0, mode-1, mode-2 and mode-3. In
mode-0 the serial port can either receive or transmit at a fixed baud rate. In mode-2 it can simultaneously
transmit and receive at any one of the two selectable baud rates. In mode-1 and mode-3 it can work
as a full duplex serial port with variable baud rates which is programmed using timer-1.
TABLE - 2.7 : SPECIAL FUNCTION REGISTER (SFR)
QPI
7FH F0H* B-Register 0FFFH
Math Registers
7EH E0H* A-Register 0FFEH
7DH 7 0
QPI
D0H* PSW Flags
7 0
R7 - R0 83H DPH
18H Data Pointers
82H DPL
17H
Register Bank-2 7 0
R7 - R0 81H SP Stack Pointer
10H 7 0
0FH
QPI
Note : The hexa addresses marked with symbol * are bit - addressable.
In an 8031 microcontroller there is no internal ROM.
The first 32 bytes of internal RAM are organized as four groups of eight registers. Each group
is called a register bank and denoted as bank-0, bank-1, bank-2 and bank-3. The eight registers of a
bank are denoted as Rn where n takes values from 0 to 7. At any one time the controller can use any
one of the register bank as general purpose registers (or scratch pad registers). The selection of the
register bank depends on the value of bits RS0 and RS1 in the PSW register. After a reset the PSW
register is cleared and so the controller works with register bank-0.
The internal RAM locations in the address range 20H to 2FH are bit-addressable. The internal
RAM locations in the address range 2F H to 7FH can be used as general purpose RAM. The hexa
address of the bit-addressable RAM are listed in Table-2.8. The 16 RAM locations in the address
range 20H to 2FH has a capacity of 128 bits (16 × 8 = 128). Each bit in this RAM area can be
addressed by an 8-bit address in the range 00H to 7FH, as shown in Table-2.8.
20 07 06 05 04 03 02 01 00
21 0F 0E 0D 0C 0B 0A 09 08
22 17 16 15 14 13 12 11 10
23 1F 1E 1D 1C 1B 1A 19 18
24 27 26 25 24 23 22 21 20
25 2F 2E 2D 2C 2B 2A 29 28
26 37 36 35 34 33 32 31 30
27 3F 3E 3D 3C 3B 3A 39 38
28 47 46 45 44 43 42 41 40
29 4F 4E 4D 4C 4B 4A 49 48
2A 57 56 55 54 53 52 51 50
2B 5F 5E 5D 5C 5B 5A 59 58
2C 67 66 65 64 63 62 61 60
2D 6F 6E 6D 6C 6B 6A 69 68
2E 77 76 75 74 73 72 71 70
2F 7F 7E 7D 7C 7B 7A 79 78
A and B Registers
The A and B registers are called CPU registers. They are used to hold the data for most of the
CPU (ALU) operations. The size of A and B registers are 8-bit and they are mapped as on-chip data
memory with byte address E0H and F0H respectively. These registers are also bit-addressable.
In most of the ALU operations, the result is stored in the A-register and so, it is also known
as the accumulator.
Data Pointer (DPTR)
The data pointer is a 16-bit register used to hold the 16-bit address of data memory. The
16-bit data pointer can also be used as two numbers of 8-bit data pointer, namely, DPH and DPL.
The 8-bit data pointers are used for accessing internal RAM and SFR. The 16-bit data pointer is
used for accessing external data memory.
The 8-bit data pointer DPH and DPL are mapped as internal memory with byte address 83H
and 82H respectively. The contents of the data pointer are programmable using instructions.
Program Status Word (PSW)
The program status word store the status of the result of the ALU operations and some of the
status of the processor by means of a 1-bit status called flags. The PSW is also known as a flag
register. The flags are useful for the programmer to test the condition of the result and make decisions.
The format of the PSW of an 8031/8051 microcontroller is shown in Fig. 2.5. The PSW
consists of four math flags and two register bank select bits. The math flags are carry, auxiliary
carry, overflow and parity flags. These flags are altered after arithmetic and logical operations
depending on the result. The carry flag is set when the result has a carry. When there is a carry
from the lower nibble to the upper nibble, the auxiliary carry is set. When the result has even parity,
the parity flag is set. In certain mathematical operations if the size of the result exceeds the size of
the destination register then the overflow flag is set.
C HAPTER 2 I NTRODUCTION T O M ICROCONTROLLERS 2. 13
B7 B6 B5 B4 B3 B2 B1 B0
CF AF - RS1 RS0 OF - PF
® Parity Flag
® Overflow Flag
® Register Bank Select Bit 0
® Register Bank Select Bit 1
® Auxiliary Carry Flag
® Carry Flag
Fig. 2.5 : Format of PSW of an 8051 family of microcontrollers.
voltage) can be reduced to 2-V. The power down mode can be terminated only by a hardware reset
and during hardware reset the content of SFR are altered but the content of internal RAM are
preserved.
B7 B6 B5 B4 B3 B2 B1 B0
Note : The idle and power down mode are not available in NMOS version of 8031/8051.
The SMOD bit is used to decide the baud rate in serial port operating modes 1, 2 or 3. In
mode 2, if SMOD = 0, then the baud rate is 1/64 of oscillator frequency and if SMOD = 1, then the
baud rate is 1/32 of oscillator frequency. (In 8031/8051 the oscillator frequency and microcontroller
internal frequency are same.) In mode 1 and 3, the baud rate depends on the SMOD and timer-1
overflow rate.
SMOD
The baud rate in mode 1 or 3 = 2 32 × (Timer-1 overflow rate).
where, TH1 = Reload count value (8-bit) in higher order timer-1 count register.
The general purpose flag bits GF1 and GF0 can be used by the programmer to indicate the
status of certain events during program execution.
Serial Data Buffer (SBUF) Register
The SBUF register is used to hold the parallel data during transmission and reception. During
serial reception, the serial data is received via RxD pin and converted to parallel data and stored in
the receive buffer. During serial transmission, the parallel data is stored in the transmit buffer and
then converted to serial data to transmit via TxD pin.
The transmit and receive buffers are assigned the same internal address 99H but the transmit
buffer can be accessed only for write operation and the receive buffer can be accessed only for
read operation. When data is written to SBUF it goes to transmit buffer and when data is read from
SBUF it comes from the receive buffer.
C HAPTER 2 I NTRODUCTION T O M ICROCONTROLLERS 2. 15
1
1
0 - Mode-2
1 - Mode-3
® Transmitted 9 Bit (i.e., Bit B of Transmitted Data)
th
8
® Receive Enable
®Serial Mode Bit-2
Fig. 2.7 : Format of a SCON register of the 8051 family of microcontrollers.
The serial mode bit-2 (SM2) has no effect in mode-0 and when programmed for mode-0,
the SM2 should be equal to zero. In mode-1, SM2 is used to check a valid stop bit during reception.
In mode-1, if SM2 = 0, then Receive Interrupt (RI) is activated only when a valid stop bit is
received.
In mode-2 and mode-3 the SM2 bit is used to enable multiprocessor communication. In
multiprocessor communication, the serial port of a number of microcontrollers can be connected
to a common serial bus. One controller will act as a master and all other controllers will act as
slaves. A unique 8-bit address is assigned to each slave and the SM2 bit in all the slaves is set to 1.
When the SM2 bit is one, the slaves will consider the received byte as address and when the SM2
bit is zero, the slaves will consider the received byte as data. For communication with a slave the
master will first send an address byte and then a data byte.
The master initiates communication with a slave by sending the address of the slave on the
bus. The address byte will be received by all the slaves. Since SM2 = 1 initially in all the slaves, the
received byte will be considered as the address and the slaves will verify whether the received
address matches with the assigned address. The slave whose assigned address matches with the
received address will clear its SM2 bit. Now SM2 bit of only one of the slave will be zero. Next, the
master will send a data byte which is also received by all the slaves, but the data byte is accepted by
the slave whose SM2 = 0 and so the receive interrupt is activated only in one of the slaves whose
SM2 = 0. After reading the received data from the SBUF register, the SM2 bit of the slave should
be set to one again to receive the next data.
The REN bit of the SCON register can be used to enable or disable the serial reception.
When REN is set to one, the serial reception is enabled and when REN is cleared to zero, the serial
reception is disabled.
The bits TI and RI of the SCON register are a transmit interrupt flag and a receive interrupt flag
respectively. They are also called serial data interrupt flags. The controller will set the TI bit during the
transmission of stop bit of a data character in modes 1 to 3 and during the transmission of the 8th bit of
a data character in mode-0. Similarly, the controller will set the RI bit during the reception of stop bit of
a data character in modes 1 to 3 and during the reception of the 8th bit of a data character is mode-0.
These two flags are logically ORed internally and used as an internal interrupt signal (called serial port
interrupt) to interrupt the current program being executed by the controller. On receiving the serial port
interrupt the controller has to suspend the current program execution and begins to execute a subroutine
program to check the value of bits TI and RI of the SCON register. If TI is one, then the controller can
understand that the previous character has been transmitted and so the controller can execute another
subroutine to clear TI flag and load the next data in the SBUF register. If RI is one, the controller can
understand that a character has been received and so the controller can execute another subroutine to
clear the RI flag and read the data from the SBUF register.
Timer Mode Control (TMOD) Register
The TMOD register is used to select the operating mode and the timer/counter operation of
the timers. The format of a TMOD register is shown in Fig. 2.8. The lower four bits of the TMOD
register is used to control timer-0 and the upper four bits are used to control timer-1.
C HAPTER 2 I NTRODUCTION T O M ICROCONTROLLERS 2. 17
The two timers can be independently programmed to operate in various modes. The TMOD
register has two separate two bit field M0 and M1 to program the operating mode of timers. The
operating modes of timers are mode-0, mode-1, mode-2 and mode-3. In all these operating modes
the oscillator clock is divided by 12 and applied as the input clock to the timer.
Fig. 2.8 : Format of the TMOD register of the 8051 family of microcontroller.
Timer port mode-0:
In mode-0, the timer register is configured as a 13-bit register. For timer-1 the 8 bits of TH1
and lower 5 bits of TL1 are used to form the 13-bit register. For timer-0 the 8-bit of TH0
and lower 5 bits of TL0 are used to form the 13-bit register. (The upper three bits of the
TL registers are ignored.) For every clock input to timer the 13-bit timer register is incremented
by one. When the timer count rolls over from all 1s to all 0s, (i.e., 11111 1111 1111 to 00000
0000 0000) the timer interrupt flag in the TCON register is set to one.
Timer port mode-1:
Mode-1 is same as mode-0 except the size of the timer register. In mode-1, the TH and
TL registers are cascaded to form a 16-bit timer register.
Timer port mode-2:
In mode-2, the timers function as 8-bit timers with automatic reload feature. The
TL-register will function as an 8-bit timer count register and the TH-register will hold
an initial count value. When the timer is started, the initial value in TH is loaded to TL and
for each clock input to timer the 8-bit timer count register is incremented by one. When the
timer count rolls over from all 1s to all 0s (i.e., 1111 1111 to 0000 0000), the timer interrupt
flag in the TCON register is set to one and the content of the TH-register is reloaded in the
TL-register and the count process starts again from this initial value.
2. 18 MICROPROCESSORS AND MICROCONTROLLERS
The C/Tbit of the TMOD register is used to program the counter or timer operation of the
timer. When C/T bit is set to one, the timer will function as an event counter. The C/T bit
is programmed to zero for timer operation.
Normally the TR (Timer Run) bit in the TCON register is used to control the clock input to
the timer. In order to allow the clock input (Note : the timer will run only if the clock input is allowed),
the TR bit should be set to one. In addition, the GATE bit in the TMOD register will facilitate the
external signal applied to the INTpin to act as an additional control signal to allow or disallow the
clock input to the timer. When GATE = 1, the clock input to the timer is allowed only if the signal
at the INT pin is high (as well as TR should be set to one.) When GATE = 0, the signal at the INT
pin is ignored (but the TR should be set to one.)
Timer Control (TCON) Register
The TCON register consists of timer overflow flags, timer run control bits, external interrupt
flags and external interrupt type control bits. The format of a TCON register is shown in Fig. 2.9.
B7 B6 B5 B4 B3 B2 B1 B0
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
The timers in a 8031/8051 microcontroller are upcounters and keep on incrementing as long
as the clock is applied. Therefore, when the clock is applied after reaching the maximum value
(i.e., the content of the counter is all 1s), the content of the counter will become zero (i.e., all 0 s).
This condition is called timer overflow and it is also the end of timing which a program wants to
maintain by using the timer. The TCON register has a 1-bit flag, TF for each timer to indicate the
timer overflow or end of timing. Whenever the timer/counter overflows, the TF flag is set to one.
The TF flag is also used as an interrupt signal to initiate the execution of a subroutine. When the
controller vectors to subroutine, the TF flag is cleared.
The TR bit is used to start/stop the timer/counter. When the TR bit is set to one, the timer/
counter will start counting and continue the counting as long as the TR bit is one. The timer/
counter will stop counting when the TR bit is cleared to zero.
C HAPTER 2 I NTRODUCTION T O M ICROCONTROLLERS 2. 19
When a valid external interrupt signal is detected, the IE flag is set to one. When the controller
accepts the external interrupt and starts processing it, the IE flag is cleared to zero. The IT bit is
used to program the type of external interrupt signal to be recognized by the controller. The IT bit
is programmed as one to recognize the falling edge triggered external interrupt and it is programmed
as zero to recognize logic low level external interrupt.
Interrupt Enable (IE) Register
The IE-register is used to enable/disable the interrupts of an 8051. The interrupts are recognized
(or accepted) by the controller only if they are enabled. The IE-register can be programmed to
enable/disable all the five interrupts of an 8051 totally or individually. The format of an IE-register
is shown in Fig. 2.10. The EA bit of the IE-register can be programmed as zero, to disable all the
five interrupts of 8051. When EA bit is programmed as one, the interrupts are enabled provided
their individual enable bits are programmed as one. (The EA bit is also called global enable.)
B7 B6 B5 B4 B3 B2 B1 B0
EA X X ES ET1 EX1 ET0 EX0
QPI
Don’t Care
(Programmed
® Enable External Interrupt-0
as Zero) ® Enable Timer-0 Interrupt
® Enable External Interrupt-1
® Enable Timer-1 Interrupt
® Enable All ® Enable Serial Port Interrupt
Interrupts
Note : 1 -Enable ; 0 - Disable.
1 = Highest Priority
® Priority of External Interrupt-0
0 = Normal Priority
® Priority of Timer-0 Interrupt
® Priority of External Interrupt-1
Note : 1 - Enable ; 0 - Disable.
® Priority of Timer-1 Interrupt
® Priority of Serial Port Interrupt
Fig. 2.11 : Format of the IP-register of an 8051 family of microcontrollers.