Programming Of 8051 Timers
By: T.Nidhi Raj
Roll No: 23H51A04B7
Introduction to 8051 Timers
The 8051 microcontroller includes two
timers/counters, Timer 0 and Timer 1.
These timers are used for generating
delays, measuring time intervals, and
counting events.
Proper programming of these timers is
essential for accurate and efficient time-
based operations.
Features of 8051 Timer
Each timer can operate in different
modes to suit various applications.
Timers can function as timers (counting
external pulses) or counters (counting
internal clock cycles).
They have 16-bit registers for high-
resolution timing and counting.
Since the 8051 architecture is
fundamentally
8-bit, these 16-bit registers are split into
two separate 8-bit segments: a lower
byte and an upper byte.
Timer Registers in 8051
The primary registers involved are THx
(Timer High Byte) and TLx (Timer Low
Byte).
These registers are used to load initial
values and to read the current timer
count.
The Timer Control (TCON) and Timer
Mode (TMOD) registers configure timer
operation modes.
Timer Registers in 8051
The primary registers involved are THx (Timer High Byte) and TLx (Timer
Low Byte).
These registers are used to load initial values and to read the current timer
count.
The Timer Control (TCON) and Timer Mode (TMOD) registers configure
timer operation modes.
Timer Modes
The TMOD register sets the mode for Timer 0 and Timer 1 using 4 bits,
two bits per timer.
Proper configuration of TMOD is essential for the desired timing or
counting operation.
•C/T : 0 = Timer (internal clock)
1 = Counter (external pulses)
•M1 & M0 :Select one of the 4 timer modes
•If GATE = 1 Timer starts only when
→ external pin (INT0/INT1)
and start bit (TR0/TR1) are both ON.
•If GATE = 0 Timer starts with just the
→ start bit (TR0/TR1).
Starting and Stopping Timers
Timers are started by setting the
appropriate bits in the Timer Control
register (TCON).
To stop a timer, clear the corresponding
TRx bit.
Control over starting and stopping
timers allows precise management of
timing events.
Example: Generating a Timer 0 Delay program
MOV TMOD, #01H ; Timer 0, Mode 1 (16-
bit)
MOV TH0, #0FCH ; Load high byte
MOV TL0, #066H ; Load low byte
SETB TR0 ; Start Timer 0
WAIT: MOV A, TCON ; Copy TCON into A
ANL A, #20H ; Mask TF0 bit
JZ WAIT ; Loop until TF0 = 1
CLR TR0 ; Stop Timer
CLR TF0 ; Clear overflow flag
END
Example: Generating a Timer 0 Delay program
MOV TMOD, #01H ; Timer 0, Mode 1 (16-
bit)
MOV TH0, #0FCH ; Load high byte
MOV TL0, #066H ; Load low byte
SETB TR0 ; Start Timer 0
•Choose Timer Mode
MOV TMOD, #01H Sets
→ Timer 0 in 16-bit mode.
That means the timer can count from the value we load up to FFFFH.
→
•Load Starting Value
MOV TH0, #0FCH
MOV TL0, #066H
These values decide how long the delay will be before the timer overflows.
•Start the Timer
SETB TR0 Switches ON Timer 0.
→
Example: Generating a Timer 0 Delay program
WAIT: MOV A, TCON ; Copy TCON into A
ANL A, #20H ; Mask TF0 bit
JZ WAIT ; Loop until TF0 = 1
CLR TR0 ; Stop Timer
CLR TF0 ; Clear overflow flag
END
•Wait Until Overflow: which signals that the required number of counts has passed.
The timer keeps counting.
MOV A, TCON Copy the control register.
→
ANL A, #20H Check only the
→ TF0 flag (overflow flag).
JZ WAIT Keep waiting until overflow happens.
→
•Stop Timer
CLR TR0 Switches OFF the timer.
→
•Clear Flag
CLR TF0 Resets the overflow flag so the timer can be used again
→ .
Conclusion
•Timers are used for delays, counting, and event generation.
•Timer overflow signals the completion of a counting cycle.
•Timer 0 and Timer 1 can operate in different modes (8-bit/16-bit).
•By loading initial values and monitoring overflow, precise delays can be
generated.
•Timers are essential for real-time applications in microcontrollers.
introduction and features 8051 Microcontrollers programming

introduction and features 8051 Microcontrollers programming

  • 1.
    Programming Of 8051Timers By: T.Nidhi Raj Roll No: 23H51A04B7
  • 2.
    Introduction to 8051Timers The 8051 microcontroller includes two timers/counters, Timer 0 and Timer 1. These timers are used for generating delays, measuring time intervals, and counting events. Proper programming of these timers is essential for accurate and efficient time- based operations.
  • 3.
    Features of 8051Timer Each timer can operate in different modes to suit various applications. Timers can function as timers (counting external pulses) or counters (counting internal clock cycles). They have 16-bit registers for high- resolution timing and counting. Since the 8051 architecture is fundamentally 8-bit, these 16-bit registers are split into two separate 8-bit segments: a lower byte and an upper byte.
  • 4.
    Timer Registers in8051 The primary registers involved are THx (Timer High Byte) and TLx (Timer Low Byte). These registers are used to load initial values and to read the current timer count. The Timer Control (TCON) and Timer Mode (TMOD) registers configure timer operation modes.
  • 5.
    Timer Registers in8051 The primary registers involved are THx (Timer High Byte) and TLx (Timer Low Byte). These registers are used to load initial values and to read the current timer count. The Timer Control (TCON) and Timer Mode (TMOD) registers configure timer operation modes.
  • 6.
    Timer Modes The TMODregister sets the mode for Timer 0 and Timer 1 using 4 bits, two bits per timer. Proper configuration of TMOD is essential for the desired timing or counting operation. •C/T : 0 = Timer (internal clock) 1 = Counter (external pulses) •M1 & M0 :Select one of the 4 timer modes •If GATE = 1 Timer starts only when → external pin (INT0/INT1) and start bit (TR0/TR1) are both ON. •If GATE = 0 Timer starts with just the → start bit (TR0/TR1).
  • 7.
    Starting and StoppingTimers Timers are started by setting the appropriate bits in the Timer Control register (TCON). To stop a timer, clear the corresponding TRx bit. Control over starting and stopping timers allows precise management of timing events.
  • 8.
    Example: Generating aTimer 0 Delay program MOV TMOD, #01H ; Timer 0, Mode 1 (16- bit) MOV TH0, #0FCH ; Load high byte MOV TL0, #066H ; Load low byte SETB TR0 ; Start Timer 0 WAIT: MOV A, TCON ; Copy TCON into A ANL A, #20H ; Mask TF0 bit JZ WAIT ; Loop until TF0 = 1 CLR TR0 ; Stop Timer CLR TF0 ; Clear overflow flag END
  • 9.
    Example: Generating aTimer 0 Delay program MOV TMOD, #01H ; Timer 0, Mode 1 (16- bit) MOV TH0, #0FCH ; Load high byte MOV TL0, #066H ; Load low byte SETB TR0 ; Start Timer 0 •Choose Timer Mode MOV TMOD, #01H Sets → Timer 0 in 16-bit mode. That means the timer can count from the value we load up to FFFFH. → •Load Starting Value MOV TH0, #0FCH MOV TL0, #066H These values decide how long the delay will be before the timer overflows. •Start the Timer SETB TR0 Switches ON Timer 0. →
  • 10.
    Example: Generating aTimer 0 Delay program WAIT: MOV A, TCON ; Copy TCON into A ANL A, #20H ; Mask TF0 bit JZ WAIT ; Loop until TF0 = 1 CLR TR0 ; Stop Timer CLR TF0 ; Clear overflow flag END •Wait Until Overflow: which signals that the required number of counts has passed. The timer keeps counting. MOV A, TCON Copy the control register. → ANL A, #20H Check only the → TF0 flag (overflow flag). JZ WAIT Keep waiting until overflow happens. → •Stop Timer CLR TR0 Switches OFF the timer. → •Clear Flag CLR TF0 Resets the overflow flag so the timer can be used again → .
  • 11.
    Conclusion •Timers are usedfor delays, counting, and event generation. •Timer overflow signals the completion of a counting cycle. •Timer 0 and Timer 1 can operate in different modes (8-bit/16-bit). •By loading initial values and monitoring overflow, precise delays can be generated. •Timers are essential for real-time applications in microcontrollers.