8051 Timer and Counter
8051 Timer and Counter
A counter is a device that stores (and sometimes displays) the number of times a
particular event or process occurred, with respect to a clock signal. It is used to count
the events happening outside the microcontroller. In electronics, counters can be
implemented quite easily using register-type circuits such as a flip-flop.
Timer Counter
Maximum count rate is 1/12 of Maximum count rate is 1/24 of the oscillator
the oscillator frequency. frequency.
Timer 0 Register
The 16-bit register of Timer 0 is accessed as low- and high-byte. The low-byte register is
called TL0 (Timer 0 low byte) and the high-byte register is called TH0 (Timer 0 high
byte). These registers can be accessed like any other register. For example, the
instruction MOV TL0, #4H moves the value into the low-byte of Timer #0.
https://www.tutorialspoint.com/embedded_systems/es_timer_counter.htm 1/5
2/25/25, 12:24 PM Embedded Systems - Timer/Counter
Timer 1 Register
The 16-bit register of Timer 1 is accessed as low- and high-byte. The low-byte register is
called TL1 (Timer 1 low byte) and the high-byte register is called TH1 (Timer 1 high
byte). These registers can be accessed like any other register. For example, the
instruction MOV TL1, #4H moves the value into the low-byte of Timer 1.
Both Timer 0 and Timer 1 use the same register to set the various timer operation
modes. It is an 8-bit register in which the lower 4 bits are set aside for Timer 0 and the
upper four bits for Timers. In each case, the lower 2 bits are used to set the timer mode
in advance and the upper 2 bits are used to specify the location.
Gate − When set, the timer only runs while INT(0,1) is high.
M1 − Mode bit 1.
M0 − Mode bit 0.
GATE
Every timer has a means of starting and stopping. Some timers do this by software,
some by hardware, and some have both software and hardware controls. 8051 timers
https://www.tutorialspoint.com/embedded_systems/es_timer_counter.htm 2/5
2/25/25, 12:24 PM Embedded Systems - Timer/Counter
have both software and hardware controls. The start and stop of a timer is controlled by
software using the instruction SETB TR1 and CLR TR1 for timer 1, and SETB TR0 and
CLR TR0 for timer 0.
The SETB instruction is used to start it and it is stopped by the CLR instruction. These
instructions start and stop the timers as long as GATE = 0 in the TMOD register. Timers
can be started and stopped by an external source by making GATE = 1 in the TMOD
register.
This bit in the TMOD register is used to decide whether a timer is used as a delay
generator or an event manager. If C/T = 0, it is used as a timer for timer delay
generation. The clock source to create the time delay is the crystal frequency of the
8051. If C/T = 0, the crystal frequency attached to the 8051 also decides the speed at
which the 8051 timer ticks at a regular interval.
Timer frequency is always 1/12th of the frequency of the crystal attached to the 8051.
Although various 8051 based systems have an XTAL frequency of 10 MHz to 40 MHz, we
normally work with the XTAL frequency of 11.0592 MHz. It is because the baud rate for
serial communication of the 8051.XTAL = 11.0592 allows the 8051 system to
communicate with the PC with no errors.
M1 / M2
M1 M2 Mode
1 1 Spilt mode.
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
https://www.tutorialspoint.com/embedded_systems/es_timer_counter.htm 3/5
2/25/25, 12:24 PM Embedded Systems - Timer/Counter
flag TF1 is set when the count rolls over from all 1s to all 0s. Mode 0 operation is the
same for Timer 0 as it is for Timer 1.
The benefit of auto-reload mode is that you can have the timer to always contain a value
from 200 to 255. If you use mode 0 or 1, you would have to check in the code to see the
overflow and, in that case, reset the timer to 200. In this case, precious instructions
check the value and/or get reloaded. In mode 2, the microcontroller takes care of this.
Once you have configured a timer in mode 2, you don't have to worry about checking to
see if the timer has overflowed, nor do you have to worry about resetting the value
because the microcontroller hardware will do it all for you. The auto-reload mode is used
for establishing a common baud rate.
When Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be set in modes
0, 1 or 2, but it cannot be started/stopped as the bits that do that are now linked to
TH0. The real timer 1 will be incremented with every machine cycle.
Initializing a Timer
Decide the timer mode. Consider a 16-bit timer that runs continuously, and is
independent of any external pins.
Initialize the TMOD SFR. Use the lowest 4 bits of TMOD and consider Timer 0. Keep the
two bits, GATE 0 and C/T 0, as 0, since we want the timer to be independent of the
https://www.tutorialspoint.com/embedded_systems/es_timer_counter.htm 4/5
2/25/25, 12:24 PM Embedded Systems - Timer/Counter
external pins. As 16-bit mode is timer mode 1, clear T0M1 and set T0M0. Effectively, the
only bit to turn on is bit 0 of TMOD. Now execute the following instruction −
MOV TMOD,#01h
Now, Timer 0 is in 16-bit timer mode, but the timer is not running. To start the timer in
running mode, set the TR0 bit by executing the following instruction −
SETB TR0
Now, Timer 0 will immediately start counting, being incremented once every machine
cycle.
Reading a Timer
A 16-bit timer can be read in two ways. Either read the actual value of the timer as a
16-bit number, or you detect when the timer has overflowed.
https://www.tutorialspoint.com/embedded_systems/es_timer_counter.htm 5/5