0% found this document useful (0 votes)
20 views16 pages

timers-of-8051

Uploaded by

franck n1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views16 pages

timers-of-8051

Uploaded by

franck n1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

8051 TIMERS AND COUNTERS

PROGRAMMING
• The 8051 has two 16-bit Programmable timers / counters (Timer 0 – Timer 1).
• Which can be used either as timer to generate a time delay or
as counter to count events happening outside the microcontroller.
• Timer Mode
• Increments every machine cycle (12 clock cycles-internal)
• Counter mode
• Increments when T0/T1 go from 1 - 0 (External signal)
• Timer can cause an interrupt
• Timer 1 can be used to provide programmable
baud rate for serial communications
• Timer/Counter operation
• Mode control register (TMOD)
• Control register (TCON)
Basic registers of the 8051 Timers
• Timer 0 and Timer 1 are 16 bits wide
• Each 16-bit timer is accessed as two separate registers of low byte and high byte.

Timer 0 registers
Low byte register is called TL0 and
the High byte register is referred to as TH0

Timer 1 registers
Low byte register is called TL1 and
the High byte register is referred to as TH1

These Timer registers be accessed like any other


register, such as A, B, R0, R1, R2, etc.
MOV TL0, #4Fh moves the value 4F into TL0
MOV R5, TH0 saves TH0 in R5
Timer Mode – TMOD Register

Timers 0 and 1 use TMOD register to set operation modes


• 8-bit register
• Lower 4 bits are for Timer 0 M1 M0 OPERATION MODE
• Upper 4 bits are for Timer 1 0 0 Mode 0 ( 13-bit Timer)
0 1 Mode 1 ( 16-bit Timer)
• Lower 2 bits are used to set the timer mode
1 0 Mode 2 (8 bit Auto Reload)
• Upper 2 bits to specify the operation
1 1 Mode 3 ( Split Timer Mode)
Timer Configuration Register - TCON
GATE: The TMOD registers of both timers 0 and 1 have the GATE bit.
• Every timer has means of starting and stopping.
• Some timers do this by software, some by hardware - The timers in the 8051 have both.
• The TRx (ie), TR0 and TR1 (Timer Run) is used to Start and Stop the Timers/Counters.

• SETB TR0  Start Timer-0; and CLR TR0  Stop Timer 0


• Similarly , SETB TR1 and CLR TR1 used for Timer 1
• These instructions start and stop the timers as long as GATE=0 in the TMOD register.

GATE0 enables and disables Timer 0 using a signal brought to the INT0 pin (P3.2): ( INT1 P3.3 for Timer 1)
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). (P3.5 For Timer 1)
0 - Timer counts pulses from internal oscillator.
• C/T’ = 0 ; Timer operation [input from internal system clock –
increments for every mc (12 clock cycles) ]
• C/T = 1 ; Counter operation – Input from Tx input Pin (External)
Programming the Timers
• Clock source for timer
• Timer needs a clock pulse to tick

• if C/T’ = 0, the crystal frequency attached to the 8051 is the source of the clock for the timer

• Frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051
(XTAL = 11.0592 MHz allows the 8051 system to communicate with the PC with no errors)

• In our case, the timer frequency is 1MHz since our crystal frequency is 12MHz
Mode 1 programming

• 16-bit timer, values of 0000 to FFFFH


• TH and TL are loaded with a 16-bit initial value
• Timer started by "SETB TR0" for Timer 0 and "SETB TR1" for Timer1
• Timer count ups until it reaches its limit of FFFFH
• Tolls over from FFFFH to 0000H
• Sets TF (timer flag)
• When this timer flag is raised, can stop the timer with "CLR TR0" or
"CLR TR1“
• After the timer reaches its limit and rolls over, the registers TH and TL
must be reloaded with the original value and TF must be reset to 0
Timer/Counter Mode 0

• Mode 1 same as Mode 0, but uses all 16 bits

CSE 477 8051 Overview 8


Example
Generation of square wave of 50% duty cycle (with equal portions high and low) on the P1.5 bit.
Timer 0 is used to generate the time delay
Example
The following program generates a square wave on pin P 1.5 continuously using
Timer 1 for a time delay. Find the frequency of the square wave if XTAL = 11.0592
MHz
Finding values to be loaded into the timer
• XTAL = 11.0592 MHz (12MHz)
• divide the desired time delay by 1.085ms (1ms) to get n
• 65536 – n = N , Convert N to hex XXYY
• set TL = XX and TH = YY
Example
Assuming XTAL = 11.0592 MHz, write a program to generate a square wave of 50
Hz frequency on pin P2.3.

T = 1/50 Hz = 20 ms
1/2 of it for the high and low portions of the pulse = 10 ms
10 ms / 1.085 us = 9216
65536 - 9216 = 56320 in decimal = DC00H
TL = 00 and TH = DCH
The calculation for 12MHz crystal uses the same steps
Example
Examine the following program and find the time delay in seconds.
Exclude the time delay due to the instructions in the loop.
This is one of the rarities being kept only for the purpose of compatibility with the previuos versions
of microcontrollers. This mode configures timer 0 as a 13-bit timer which consists of all 8 bits of TH0
and the lower 5 bits of TL0. As a result, the Timer 0 uses only 13 of 16 bits. How does it operate?
Each coming pulse causes the lower register bits to change their states. After receiving 32 pulses,
this register is loaded and automatically cleared, while the higher byte (TH0) is incremented by 1.
This process is repeated until registers count up 8192 pulses. After that, both registers are cleared
and counting starts from 0.

You might also like