2. Introduction
The 8051 microcontroller has two 16-bit timer/counter registers, Timer 0
(P3.4) and Timer 1(P3.5). They can be used either as Timers to generate a time
delay or as Event counters to count events happening outside the
microcontroller.
Application of Timers & Counters
•Timers:
• Generating accurate delays
• PWM signal generation
• Event scheduling
•Counters:
• Counting external pulses (e.g., sensor triggers)
• Measuring the frequency of external signals
• Counting events in automation systems
5. Timer 0 and Timer 1
Timer 0 and Timer 1 are 16-bits wide. Since 8051 has an 8-bit architecture, each
16-bit timer is accessed as two separate registers of low byte and high byte.
The low-byte register is called TL0/TL1 and The high-byte register is called
TH0/TH1. They can be accessed like any other register.
7. Timer/Counter Registers
Timer/Counter Registers
•TMOD (Timer Mode Register): Configures the mode and operation of Timer 0
and Timer 1.
•TCON (Timer Control Register): Contains control flags for starting, stopping,
and checking overflow.
•TH & TL (Timer High and Timer Low Registers): Store the timer/counter
values.
9. Timer Control (TCON) Register
• TCON register is also one of the registers whose bits are directly in
control of timer operation.
• Only 4 bits of this register are used for this purpose, while rest of them is
used for interrupt control to be discussed later.
10. TF1 bit is automatically set on the Timer 1 overflow.
TR1 bit enables the Timer 1.
• 1 – Timer 1 is enabled.
• 0 – Timer 1 is disabled.
TF0 bit is automatically set on the Timer 0 overflow.
TR0 bit enables the timer 0.
• 1 – Timer 0 is enabled.
• 0 – Timer 0 is disabled.
Timer Control (TCON) Register
11. TMOD Register (Timer Mode)
• The TMOD register selects the operational mode of the timers
T0 and T1.
• The low 4 bits (bit0 – bit3) refer to the timer 0, while the high
4 bits (bit4 – bit7) refer to the timer 1.
12. • GATE1 enables and disables Timer 1 by means of a signal
brought to the INT1 pin (P3.3):
• 1 – Timer 1 operates only if the INT1 bit is set.
• 0 – Timer 1 operates regardless of the logic state of the
INT1 bit.
• C/T1 selects pulses to be counted up by the timer/counter 1:
• 1 – Timer counts pulses brought to the T1 pin (P3.5).
• 0 – Timer counts pulses from internal oscillator.
• T1M1, T1M0 These two bits select the operational mode of the
Timer 1.
TMOD Register (Timer Mode)
14. GATE0 enables and disables Timer 0 using a signal brought to
the INT0 pin (P3.2):
• 1 – Timer 0 operates only if the INT0 bit is set.
• 0 – Timer 0 operates regardless of the logic state of the
INT0 bit.
• C/T0 selects pulses to be counted up by the timer/counter 0:
• 1 – Timer counts pulses brought to the T0 pin (P3.4).
• 0 – Timer counts pulses from internal oscillator.
• T0M1, T0M0 These two bits select the operational mode of the
Timer 0.
TMOD Register (Timer Mode)
24. How to load the Timer registers with the required time delay
Mode 1 (16 bit timer/counter)
Step:1
Value to be loaded in the Timer = FFFF – (delay Value) + 1
Step:2
load into THx and TLx register
Mode 0 (13 bit timer/counter)
Step:1
Value to be loaded in Timer = 1FFF – (Delay Value) +1
Step:2
load into THx and TLx register
25. Steps for programming timers in 8051
Load the TMOD value register indicating which timer (0 or 1) is to be used
and which timer mode is selected.
Load registers TL and TH with initial count values.
Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1
for timer 1.
Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction to
see if it is raised.
Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and
timer 1, respectively.
Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR
TF1”, for timer 0 and timer 1, respectively.
Go back to step 2 to load TH and TL again.
27. Data Types and Time Delay in the 8051 using C
Why Program the 8051 using C Language???
Compilers produce hex files that is downloaded to ROM of
microcontroller
The size of hex file is the main concern
Microcontrollers have limited on-chip ROM
Code space for 8051 is limited to 64K bytes
C programming is less time consuming, but has larger hex file size
The reasons for writing programs in C
It is easier and less time consuming to write in C than Assembly
C is easier to modify and update
We can use code available in function libraries
C code is portable to other microcontroller with little modification
28. A good understanding of C data types for 8051 can help programmers
to create smaller hex files
Unsigned char
Signed char
Unsigned int
Signed int
Sbit (single bit)
Bit and sfr
Data Types and Time Delay in the 8051 using C