Timer Serial and Interrupt
Timer Serial and Interrupt
• The registers of Timers are loaded with some initial value. The value
of a Timer register increases by one after every machine cycle.
• One machine cycle duration is the 1/12th of the frequency of the
crystal attached to the controller.
• For example, if the frequency of the crystal is 12 MHz, then the
frequency for Timer will be 1MHz (1/12 of crystal frequency) and
hence the time (T = 1/f) taken by the Timer to count by one is 1µs
(1/1MHz).
• Similarly if an 11.0592 MHz crystal is used, operating frequency of
Timer is 921.6 KHz and the time period is 1.085 µs.
• If no value is loaded into the Timer, it starts counting from 0000H.
• When the Timer reaches FFFFH, it reloads to 0000H. This roll over
is communicated to the controller by raising a flag corresponding
to that Timer,
Starting or stopping a Timer
• If C/T is low, Timer is used for time keeping, i.e., Timer updates its value
automatically corresponding to 8051 clock source.
• When C/T is high, Timer is used as counter, i.e., it updates its value when
it receives pulse from outside the 8051 controller.
• M1 and M0 bits decide the Timer modes. There are four Timer modes
designated as Modes 0, 1, 2 and 3.
• Modes 1 and 2 are most commonly used while working with Timers.
TMOD = 0x01; sets the mode1 of Timer0 used for timing
TMOD = 0x20; sets the mode2 of Timer1 used for timing
Programming 8051 Timers
• Mode M1 M0
Operation
• Mode 0 0 0 13-bit
Timer
• Mode 1 0 1 16-bit
Timer
• Mode 2 1 0 8-bit
Auto Reload
• Mode 3 1 1 Split
Timer
Mode
Mode 0
• Mode 0 : 13-bit Timer
main()
{
unsigned int
while(1)
{
led=~led; // Toggle
for(i=0;i<1000;i++)
delay(); // Call de
}
}
• The Timer breaks into two 8-bit Timers that can count
from 00H up to FFH.
• The initial values are loaded into the higher byte and
lower byte of the Timer.
• The other Timer can be used in modes 0, 1 or 2 and is
updated automatically for every machine cycle.
• For example, if Timer0 is used in split mode, TH0 and
TL0 will become separate Timers. The start and flag
bits associated with Timer1 will now be associated
with the TH0.
• Split mode is useful when two Timers are required