All Syllabus

Home About Search Unit 1 Unit 2 Unit 3 Unit 4 Unit 5 Unit 6 Unit 7 Unit 8 Unit 9      

8051 Timer Programming in Assembly and C

Programming 8051 Timers

Mode 1 Programming

Finding values to be loaded into the timer

Mode 2 Programming

Counter Programming

Programming Timer0 and 1 in 8051 C

Timers 0 and 1 Delay Using Mode 2 (8-bit Auto-reload)

8051 Timer Programming in Assembly and C

Programming 8051 Timers

The 8051 has two timers/counters; they can be used either as Timers to generate a time delay
or as event counters to count events happening outside the microcontroller.

Basic Timers of 8051:


Both Timer 0 and Timer 1 are 16 bits wide. Since 8051 has an 8-bit architecture, each 16-bits
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. These registers can be
accessed like any other register. For example:
 MOV TL0,#4FH
 MOV R5,TH0

TMOD (timer mode) Register:


Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various
timer operation modes. TMOD is an 8-bit register. The lower 4 bits are for Timer 0 and the
upper 4 bits are for Timer 1. In each case, the lower 2 bits are used to set the timer mode the
upper 2 bits to specify the operation. The TMOD register is as shown in figure 2 below:

Timers of 8051 do starting and stopping by either software or hardware control. In using
software to start and stop the timer where GATE=0. The start and stop of the timer are
controlled by way of software by the TR (timer start) bits TR0 and TR1. The SETB
instruction starts 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. The hardware way of starting and
stopping the timer by an external source is achieved by making GATE=1 in the TMOD
register.