0% found this document useful (0 votes)
77 views

Timer Serial and Interrupt

The document discusses timers in microcontrollers. It explains that timers are used to generate precise time delays and counting. Timers can operate in different modes like mode 0, 1, 2 and 3. Mode 1 is a 16-bit timer most commonly used to generate delays. Timers can be programmed using polling or interrupt methods. Registers like TMOD, THx and TLx are used to configure and control timers.

Uploaded by

Ayan Acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Timer Serial and Interrupt

The document discusses timers in microcontrollers. It explains that timers are used to generate precise time delays and counting. Timers can operate in different modes like mode 0, 1, 2 and 3. Mode 1 is a 16-bit timer most commonly used to generate delays. Timers can be programmed using polling or interrupt methods. Registers like TMOD, THx and TLx are used to configure and control timers.

Uploaded by

Ayan Acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

What is a Timer

• Timer is a clock that controls the sequence of


an event while counting in fixed intervals of
time.
• Timer is used for producing precise time delay.
• It can be used to repeat or initiate an action
after/at a known period of time.
• An example could be setting up an alarm which
triggers at a point of time or after a period of
time.
Timers in a controller
• Most of the microcontrollers have inbuilt Timers.
• Timers in a controller not only generate time delays
but they can also be used as counters.
• Used to count an action or event.
• The value of counter increases by one, every time its
corresponding action or event occurs.
• Timers in a controller are inbuilt chips that are
controlled by special function registers (SFRs) assigned
for Timer operations.
• These SFRs are used to configure Timers in different
modes of operations.
Timers in a controller
• While working with microcontrollers, it is more
than often required to generate time delays.
• There are two possible ways of generating time
delays.
– First is by using the code, like using for or while loops
in a C program.
– However, the delays provided by the software are not
very precise.
– The other method is to use Timers.
– Timers provide time delays that are very precise and
accurate.
8051 Timers and registers

• AT89C51 microcontroller has two Timers -Timer0 and


Timer1.
• The value of a Timer register increases by one every time a
timer counts.
• This means that the maximum number of times a timer
can count without repeating is 216, i.e., 65536.
• The maximum allowed counts in value of Timer registers
can be from 0000H to FFFFH.
• 8051 Timers are accessed as two different registers; one for
lower byte and other for higher byte.
• Register of Timer0 is accessed as TL0 for lower byte and
TH0 for higher byte. Similarly TL1 and TH1 are registers
assigned to Timer 1.
How a Timer functions

• 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

• For every Timer, there is a corresponding Timer control


bit which can be set or cleared by the program to start
or stop the Timer. TR0 and TR1 are the control bits for
Timers 0 and 1 respectively. Setting the control bit
would start the Timer.
• TR0 = 1; starts Timer 0
• TR1 = 1; starts Timer 1

• Clearing the control bit would stop the Timer.
• TR0 = 0; stops Timer 0
• TR1 = 0; stops Timer1
Configuring a Timer
TMOD
• When GATE is high, the corresponding Timer is enabled only when there
is an interrupt at corresponding INTx pin of AT89C51 controller and Timer
control bit is high.

• 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

• The programming of 8051 Timers can be done by using either


polling method or by using interrupt.
• In polling, the microcontroller keeps monitoring the status of
Timer flag. While doing so, it does no other operation and
consumes all its processing time in checking the Timer flag
until it is raised on a rollover.
• In interrupt method controller responds to only when the
Timer flag is raised. The interrupt method prevents the
wastage of controller’s processing time unlike polling method.
• Polling is mostly used for time delay generation
• Interrupt method is more useful when waveforms are to be
generated or some action has to be repeated in fixed delays.
Polling Method

The polling method involves the following algorithm:


1. Configure the Timer mode by passing a hex value into the TMOD
register.
2. Load the initial values in the Timer low TLx and high THx byte. (x =
0/1)
3. Start the Timer by setting TRx bit.
4. Wait while the Timer flag TFx is raised.
5. Clear the Timer flag. The Timer flag is raised when Timer rolls over
from FFFFH to 0000H.
– If the Timer is not stopped, it will start updating from 0000H in case of modes
0 & 1 while with initial value in case of mode 2.
6. Stop the Timer by clearing TRx bit. If TRx bit is not cleared the Timer
will restart updating from 0000H after the rollover in case of modes 0 and
1 while with initial value in case of mode 2.
Interrupt Method

• The interrupt method makes use of a register


called Interrupt Enable (IE) register.
• An 8051 microcontroller has 6 hardware
interrupts.
• The interrupts refer to a notification,
communicated to the controller, by a
hardware device or software.
Different modes of a Timer

• 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

• Mode 0 is a 13 bit Timer mode and uses 8 bits of


high byte and 5 bit of low byte.
• The value that the Timer can update in mode0 is
from 0000H to 1FFFH. The 5 bits of lower byte
append with the bits of higher byte.
• The Timer rolls over from 1FFFH to 0000H to
raise the Timer flag.
Mode 1 : 16-bit Timer

• Mode1 is one of the most commonly used Timer modes. It


allows all 16 bits to be used for the Timer and so it allows
values to vary from 0000H to FFFFH.
• If a value, say YYXXH, is loaded into the Timer bytes, then
the delay produced by the Timer will be equal to the
product :
[ ( FFFFH – YYXXH +1 ) x ( period of one timer clock ) ].

• It can also be considered as follows: convert YYXXH into


decimal, say NNNNN, then delay will be equal to the
product :
• [ ( 65536-NNNNN ) x ( period of one timer clock ) ].
Time delay in Mode1 using polling method
// Use of Timer mode 1 for blinking LED using polling met
// XTAL frequency 11.0592MHz
#include<reg51.h>
sbit led = P1^0; // LED connected to 1st pin of port
void delay

main()
{
unsigned int
while(1)
{
led=~led; // Toggle
for(i=0;i<1000;i++)
delay(); // Call de
}
}

void delay() // Delay generation using Timer 0 mode


{
TMOD = 0x01; // Mode1 of Tim
TH0= 0xFC; // FC66 evaluated hex value for 1millisecond de
TL0 = 0x
TR0 = 1; // Start Ti
while(TF0 == 0); // Using polling met
TR0 = 0; // Stop Ti
TF0 = 0; // Clear f
}
Mode 2 : 8-bit Auto Reload

• Mode 2 is an 8 bit mode. The initial value is


loaded into the higher byte. A copy of the
same is passed to the lower byte.
• The Timer can update from 00H to FFH. The
Timer rolls over from FFH to initial value
automatically.
• Mode 2 is commonly used for setting baud
rates for serial communication.
Mode 2 : 8-bit Auto Reload
• void delay()
{
TMOD = 0x02; // Mode1 of Timer0
TH0= 0xA4; // Initial value loaded to
Timer
TR0 = 1; // Start Timer
while(TF0 == 0); // Polling for flag bit
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
Mode 3 : Split Timer

• 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

You might also like