[go: up one dir, main page]

0% found this document useful (0 votes)
58 views10 pages

MAA Notes - Timer

Uploaded by

Majhe Guruji
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)
58 views10 pages

MAA Notes - Timer

Uploaded by

Majhe Guruji
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/ 10

8051 Timer/Counter

Timer Clock Frequency


The frequency for the timer is always 1/12th of the frequency of the crystal
attached to the 8051.

To The Timer
as per the timer mode

Basic diagram of Timer/Counter


8051 has two 16-bit programmable UP timers/counters. They can be
configured to operate either as timers or as event counters. The names of the
two counters are T0 and T1 respectively. The timer/counter is available in four
8-bit special function registers, these are TL0, TH0, TL1 and TH1 respectively.
In the "timer" function mode, the counter is incremented in every
machine cycle. Thus (up counting), it has counting machine cycles. Hence the
clock rate is 1/12th of the oscillator frequency.
In the "counter" function mode, the register is incremented in response to
a 1 to 0
transition at its corresponding external input pin (T0 or T1). It requires 2
machine cycles to detect a high to low transition. Hence maximum count rate is
1/24th of oscillator frequency.
The operation of the timers/counters is controlled by two special function
registers, TMOD and TCON respectively.
The TRx means If Timer 0 there is TR0 and If Timer 1 there is TR1
The INTx means If Timer 0 there is INT0 and If Timer 1 there is INT1
The Tx means If Timer 0 there is T0 and If Timer 1 there is T1
Mode 0:
• This mode allows a 13-bit timer or counter.
• The Mode0 register can hold values up to 1FFFH in TH-TL.
• When the timer reaches its maximum of 1FFFH, it rolls over to 0000, and
the TF (flag bit) is raised.
• Not used generally.
• Aside from these few differences, the rest is similar to Mode 1.
Mode 1:
• This mode enables a 16-bit timer or counter.
• The Mode 1 registers (TL and TH) can store values from 0000 to FFFFH.
• The timer must be started after loading TH and TL with a 16-bit initial
value.
• We can give initial values by using “SETB TR0” for timer0 and “SETB
TR1” for timer1.
• When the timer starts, it keeps counting until it reaches its limit of
FFFFH.
• Once the timer reaches its limit at 0000H, it sets a TF (timer flag) bit:
TF0 for timer0 and TF1 for timer1.
• Now we can stop the timer using “CLR TR0” for timer0 and “CLR TR1”
for timer1.
• To repeat the process, the original values must be reloaded in the TH and
TL registers, and the timer flag must be reset to 0.
Mode 2:
• This is an 8 bit auto reload mode
• This mode enables an 8-bit timer or counter.
• This timer first loads the values up to FFH in the TH register.
• When TH is loaded, 8051 copies the same 8-bit values to TL.
• The timer starts by setting “SETB TR0” and “SETB TR1” instructions for
timer0 and timer1, respectively.
• When the timer starts, it continues to increment the TL register until it
reaches FFH; after that, it rolls over to 00 and raises the TF flag. Then the
TL is automatically loaded with its original value which is stored in TH,
which is reserved by the register TH.
• The main feature of this mode is that it repeats the process. We just have
to clear the TF register, and it will load the original value into the register.
While in mode 1, the programmer reloads the values to TH and TL.
Mode 3:
• This mode is also known as a split timer mode.
• It splits timer0 and timer1 into two 8-bit timers.
• These 8-bit timers can count from 00H to FFH.
• This mode is used in applications where we require an additional 8-bit
timer or counter.
• TL0 sets TF0, and TH0 sets TF1.
• We can program timer0 and timer1 independently in modes 0, 1, and 2.
Each timer can have its own settings. But mode 3 can’t work
independently.
• If we choose mode 3 for timer0 and place timer1 in mode 3, it will cause
the timer to stop counting. Then timer 0 will use TR1 (timer1 register)
and TF1 (timer1 flag), i.e., timer 0 has to depend on timer 1.
Programming steps for Timer function – Mode 1

1. Load Tmod register value i.e. TMOD = 0x01 for Timer0 mode1

(16-bit timer mode).

2. Load calculated THx value i.e. here TH0 = .

3. Load calculated TLx value i.e. here TL0 = .

4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.

5. Poll TFx flag till it does not get set.

6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.

7. Clear timer flag TFx bit i.e. here TF0 = 0.

8. Repeat from step 1 to 7 for the delay again.


Program to generate a square wave of 5 kHz frequency on pin P1.0, clock
frequency =11.0592 MHz

Given:
Square wave frequency=5 kHz
clock frequency=11.0592 MHz

Step 1: Calculate the Time delay

T=1/f
=1/5 kHz
=0.2 ms
T=0.2 ms which is the period of square wave
T/2 =0.2/2
=0.1 ms delay for high and low i.e. for TON and TOFF

Step 2: Divide the clk frequency by 12


Timer Frequency = 11.0592 MHz/12
= 921.6 KHz
Timer time = 1/f
= 1/921.6 KHz
= 1.085 us
Count=0.1ms/1.085 us = 921

Step 3: Perform 65536 – n


= 65536-921=64615=FC67H
Step 4: Set TL = xx and TH = yy
Here xx=FC and yy=67. Hence,TH0=FCh,TL0=67h

MOV TMOD,#01 ;Timer 0, mode 1, 16-bitmode


AGAIN: MOV TL1,#67H ;TL1=67, low byte of timer
MOV TH1,#0FCH ;TH1=FC, the high byte
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CPL P1.0 ; compliment P1.0
CLR TR1 ;Stop the timer 1
CLR TF1 ;Clear tim er 1 flag
SJMP AGAIN ;Reload timer

You might also like