8051 Timers Programming in C

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

8051 Timers

● Basic Registers of the timer


● Timer 0 registers
● Timer 1 registers

Both the registers are 16 bits wide but can be


accessed as 8 bit registers separately.
PROGRAMMING 8051 TIMERS

Timer 0 registers
TL0 ( timer 0 low byte )
TH0 ( timer 0 high byte )
Timer 1 registers

TL1 ( timer 1 low byte )


TH1 ( timer 1 high byte )
Timer Interrupt & Optimize
TMOD Register - Operation Control
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
TIMER1
GATETIMER2
Gating Control
When Set Timer/Counter “x”is enabled
Only while “INTx”pin is high and “TRx” control pin is set
When Cleared Timer “x” is enabled
C/T Timer or Counter“TRx”
Whenever Selector
control bit is set
Cleared for Timer operation (input from internal system clock).
Set for Counter operation (input from “Tx” input pin)
TMOD Register - Operating Mode Control (0 , 1, 2 )
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
TIMER1
TIMER2
M1 M0 Function
0 0 13-Bit timer Mode.

0 1 16-bit Timer/Counter : “THx” and “TLx” are cascaded


8-bit auto reload
1 0
“THx” hold a value which is to be
Reloaded into “TLx” each time it overflows
Clock Source for timer

● The crystal frequency attached to the 8051


timer is the source of the clock for the timer.
● The frequency for the timer is always 1/12th the
frequency of the crystal attached to the 8051
Mode 1 programming

● It is a 16-bit timer; so it allows values of 0000 to FFFFH to


be loaded into the timer’s registers TL and TH
● After TH and TL are loaded with a 16 bit initial value, the
timer must be started
● It starts counting up until it reaches its limit of FFFFH. When
it rolls over from FFFFH to 0000, it sets high TF (Timer flag)
● Stop the timer
● After the timer reaches its limit and rolls over, in order to
repeat the process TH and TL must be reloaded with the
original value, and TF must be reset to 0
Mode 1 programming
1.Loaded value into TL and TH
2. Turn On Timer by TR0=1 for timer 0 ; TR1 =1 for timer 1
3.If TF (timer flag) = high, TR0=0
4.Reload TH and TL value, TF reset to 0
Steps to program in mode 1
1.Load the TMOD value
2.Load registers TL and TH
3.Start the timer ( TR0= or TR1=1)
4.Keep monitoring the timer flag (TF)
5.Stop the timer ( TR0=0 or TR1=0)
6.Clear the TF flag
7.Go back to step 2
Calculate Timer Delay
Finding values to be loaded into the timer
Assuming XTAL =11.0592MHz from Example 9-10

1.Divide the desired time delay by 1.085μs


2.Perform 65536-n, where n is the decimal value we got in
Step 1
3.Convert the result of Step 2 to hex, where yyxx is the
initial hex value to be loaded into the timer’s registers
4.Set TL = xx and TH = yy

You might also like