Wednesday 6 March 2013

TIMERS IN 8051

Different modes of a Timer

There are four Timer modes designated as Modes 0, 1, 2 and 3. A particular mode is selected by configuring the M1 & M0 bits of TMOD register.
 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
 
(i) Mode 0 : 13-bit Timer
 
Mode 0 is a 13 bit Timer mode and uses 8 bits of high byte and 5 bit prescaler 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.
 
(ii) 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 ) ].
The period of one timer clock is 1.085 ยตs for a crystal of 11.0592 MHz frequency as discussed above.
 
Now to produce a desired delay, divide the required delay by the Timer clock period. Assume that the division yields a number NNNNN. This is the number of times Timer must be updated before it stops. Subtract this number from 65536 (binary equivalent of FFFFH) and convert the difference into hex. This will be the initial value to be loaded into the Timer to get the desired delay.
The calculator application in Windows can be a handy tool to carry out these calculations.

Example code
Time delay in Mode1 using polling method
// Use of Timer mode 1 for blinking LED using polling method
// XTAL frequency 11.0592MHz 
#include<reg51.h>
sbit led = P1^0;     // LED connected to 1st pin of port P1
void delay();

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

void delay()     // Delay generation using Timer 0 mode 1
{
 TMOD = 0x01;    // Mode1 of Timer0
 TH0= 0xFC;      // FC66 evaluated hex value for 1millisecond delay
 TL0 = 0x66;
 TR0 = 1;       // Start Timer
 while(TF0 == 0);    // Using polling method
 TR0 = 0;     // Stop Timer
 TF0 = 0;     // Clear flag
}
 
Example code
Time delay in Mode1 using interrupt method
// Use of Timer mode 1 for blinking LED with interrupt method
// XTAL frequency 11.0592MHz 
#include<reg51.h>
sbit LED = P1^0;     // LED connected to 1st pin of port P1
void Timer(void) interrupt 1    // Interrupt No.1 for Timer 0
{
 led=~led;     // Toggle LED on interrupt
}

main()
{
 TMOD = 0x01;     // Mode1 of Timer0
 TH0=0x00;    // Initial values loaded to Timer
 TL0=0x00;
 IE = 0x82;    // Enable interrupt
 TR0=1;     // Start Timer
 while(1);    // Do nothing 
}
 
(iii) 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
 
Example code
Time delay in Mode2 using polling method
// Use of Timer mode 2 for blinking LED with polling method
// XTAL frequency 11.0592MHz 
#include<reg51.h>
sbit led = P1^0;    // LED connected to 1st pin of port P1void delay();
main()
{
 unsigned int i;
 while(1)
 {
 led=~led;    // Toggle LED 
 for(i=0;i<1000;i++)
 delay();    // Call delay
 }
}

void delay()
{
 TMOD = 0x02;    // Mode1 of Timer0
 TH0= 0xA2;    // Initial value loaded to Timer
 TR0 = 1;    // Start Timer
 while(TF0 == 0);   // Polling for flag bit
 TR0 = 0;    // Stop Timer
 TF0 = 0;    // Clear flag
}
(iv) Mode 3 : Split Timer
In mode 3, also known as split mode, 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. In this case the start and flag bits associated with the other Timer are now associated with high byte Timer. So one cannot start or stop the other 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. Timer 1 cannot be stopped or started, but will be updated for every machine cycle.
Split mode is useful when two Timers are required in addition to a baud rate generator.

No comments:

Post a Comment