Unit3 Mode1
Unit3 Mode1
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
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
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