0% found this document useful (0 votes)
9 views11 pages

Unit3 Mode1

Mode 1 programming involves using a 16-bit timer that counts from 0000 to FFFFH, setting a timer flag (TF) upon overflow. To generate a time delay, the timer must be configured, started, and monitored until the TF is raised, after which it can be reset for repeated use. An example program demonstrates generating a square wave using Timer 0 with specific initial values and calculations for time delay based on the timer's frequency.

Uploaded by

ramolabhamare19
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)
9 views11 pages

Unit3 Mode1

Mode 1 programming involves using a 16-bit timer that counts from 0000 to FFFFH, setting a timer flag (TF) upon overflow. To generate a time delay, the timer must be configured, started, and monitored until the TF is raised, after which it can be reset for repeated use. An example program demonstrates generating a square wave using Timer 0 with specific initial values and calculations for time delay based on the timer's frequency.

Uploaded by

ramolabhamare19
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/ 11

Mode 1 Programming

❑ The following are the characteristics and operations of Mode1:


1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to
be loaded into the timer’s register TL and TH
2. After TH and TL are loaded with a 16-bit initial value, the timer
must be started
▪ This is done by SETB TR0 for timer 0 and
SETB TR1 for timer 1

XTAL TH TL TF
÷12
oscillator
TF goes high Overflow
TR
C/T = 0 when FFFF → 0 flag

April 8, 2025 1
Mode 1 Programming
3. After the timer is started, it starts to count up
▪ It counts up until it reaches its limit of FFFFH
▪ When it rolls over from FFFFH to 0000, it sets high a flag bit called
TF (timer flag)
– Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1
– This timer flag can be monitored
▪ When this timer flag is raised, one option would be to stop the timer
with the instructions CLR TR0 or CLR TR1, for timer 0 and timer 1,
respectively
XTAL TH TL TF
÷12
oscillator
TF goes high Overflow
TR
C/T = 0 when FFFF → 0 flag

April 8, 2025 2
Mode 1 Programming
4. 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 reloaded to 0

XTAL TH TL TF
÷12
oscillator
TF goes high Overflow
TR
C/T = 0 when FFFF → 0 flag

April 8, 2025 3
Mode 1 Programming
❑ To generate a time delay
1. Load the TMOD value register indicating which timer (timer 0 or
timer 1) is to be used and which timer mode (0 or 1) is
selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB TFx,target
instruction to see if it is raised
▪ Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
April 8, 2025 4
Mode 1 Programming
In the following program, we create a 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. Analyze the program
MOV TMOD, #01 ;Timer 0, mode 1 (16-bit mode)
HERE: MOVTL0, #0F2H ;TL0=F2H, the low byte
MOVTH0, #0FFH ;TH0=FFH, the high byte
CPLP1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE

In the above program notice the following steps.


1. TMOD is loaded.
2. FFF2H is loaded into TH0-TL0.
3. P1.5 is toggled for the high and low portions of the pulse.

April 8, 2025 5
Mode 1 Programming
DELAY: SETB TR0 ;start the timer 0
AGAIN: JNBTF0, AGAIN ;monitor timer flag 0
;until it rolls over
CLRTR0 ;Stop timer 0
CLRTF0 ;clear timer flag
RET
FFF2 FFF3 FFF4 FFFF 0000
TF=0 TF=0 TF=0 TF=1
TF=0

4. The DELAY subroutine using the timer is called.


5. In the DELAY subroutine, timer 0 is started by the SETB TR0 instruction.
6. Timer 0 counts up with the passing of each clock, which is provided by the
crystal oscillator. As the timer counts up, it goes through the states of FFF3,
FFF4, FFF5, and so on until it reaches FFFFH. One more clock rolls it to 0,
raising the timer flag (TF0=1). At that point, the JNB instruction falls through.

April 8, 2025 6
Mode 1 Programming
Calculate the amount of time delay in the DELAY subroutine generated by the
timer. Assume XTAL = 11.0592 MHz.

The timer works with a clock frequency of 1/12 of the XTAL frequency;
therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the timer frequency.
As a result, each clock has a period of T = 1/921.6kHz = 1.085µs.
In other words, Timer 0 counts up each 1.085 µs resulting in
delay = number of counts X 1.085µs.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13 decimal).
However, we add one to 13 because of the extra clock needed when it rolls
over from FFFF to 0 and raise the TF flag. This gives 14 X 1.085µs = 15.19µs
for half the pulse. For the entire period it is T = 2 X 15.19µs = 30.38µs as the
time delay generated by the timer.

April 8, 2025 7
Mode 1 Programming
(b) in decimal
(a) in hex
Convert YYXX values of
(FFFF – YYXX + 1) X 1.085 µs,
the TH, TL register to
where YYXX are TH, TL initial
decimal to get a NNNNN
values respectively.
decimal, then
Notice that value YYXX are in
(65536 - NNNNN) X
hex.
1.085 µs

April 8, 2025 8
Mode 1 Programming
Calculate the frequency of the square wave generated on pin P1.5.
Solution:
In the timer delay calculation, we did not include the overhead due to
instruction in the loop. To get a more accurate timing, we need to add
clock cycles due to this instructions in the loop. To do that, we use the
machine cycles

April 8, 2025 9
April 8, 2025 10
Basic Timer Delay Generation (1 ms delay)
MOV TMOD, #01 ; Timer 0, Mode 1 (16-bit mode)
AGAIN: MOV TL0, #67H ; Load low byte
MOV TH0, #0FCH ; Load high byte
SETB TR0 ; Start Timer 0
WAIT: JNB TF0, WAIT ; Wait until Timer Overflows
CLR TR0 ; Stop Timer 0
CLR TF0 ; Clear Timer Overflow Flag
SJMP AGAIN ; Repeat forever

April 8, 2025 11

You might also like