Thursday, April 30, 2015

89C51 Timers for generation of Desired Delay


Related Notes: Timer Configurations and Modes with examples:
            The 8051 microcontroller has two independent 16 bit up counting timers named Timer 0 and Timer 1 and this article is about generating time delays using the 8051 timers. Generating delay using pure software loops have been already discussed here but such delays are poor in accuracy and cannot be used in sensitive applications. Delay using timer is the most accurate and surely the best method.
            A timer can be generalized as a multi-bit counter which increments/decrements itself on receiving a clock signal and produces an interrupt signal up on roll over. When the counter is running on the processor’s clock , it is called a “Timer”, which counts a predefined number of processor clock pulses and  generates a programmable delay. When the counter is running on an external clock source (may be a periodic or aperiodic external signal) it is called a “Counter” itself and it can be used for counting external events.
            In  8051, the oscillator output is divided by 12 using a divide by 12 network and then fed to the Timer as the clock signal. That means for an 8051 running at 12MHz, the timer clock input will be  1MHz. That means the the timer advances once in every 1uS and the maximum time delay possible using a single 8051 timer is ( 2^16) x (1µS) = 65536µS. Delays longer than this can be implemented by writing up a basic delay program using timer and then looping it for a required number of time. We will see all these in detail in next sections of this article.
Designing a delay program using 8051 timers.

While designing delay programs in 8051, calculating the initial value that has to be loaded into TH and TL registers forms a very important thing.

  • Assume the processor is clocked by a 12MHz crystal.
  • That means, the timer clock input will be 12MHz/12 = 1MHz
  • That means, the time taken for the timer to make one increment = 1/1MHz = 1uS
  • For a time delay of “X” uS the timer has to make “X” increments.
  • 2^16 = 65536 is the maximim number of counts possible for a 16 bit timer.
  • Let TH be the value value that has to be loaded to TH registed and TL be the value that has to be loaded to TL register.
  • Then, THTL =  Hexadecimal equivalent of (65536-X) where (65536-X) is considered in decimal.
Example:
Let the required delay be 1000uS (ie; 1mS).
That means X = 1000
65536 – X =  65536 – 1000 = 64536.
64536 is considered in decimal and converting it T0 hexadecimal gives FC18
That means THTL = FC18

Therefore TH1=0xFC and TL1=0x18

The program shown below can be used for generating 1mS delay and it is written as a subroutine so that you can call it anywhere in the program. Also you can put this in a loop for creating longer time delays (multiples of 1mS). Here Timer 1 of 8051 is used and it is operating in MODE1 (16 bit timer). TMOD=0x10

Program in Keil:
/* Internal Timer                                */
/* 2nd Dec 2005                                  */
/* Copyright 2005 Circuits4You                   */ 
/* WWW - http://www.circuits4you.com             */
/* Email - info@circuits4you.com                 */

#include <reg51.h>
// Set Crystal Frequency in Project>>Options for Target>>Target >> Xtal=12 
void delay1ms();
//=======================================================
//    Main Function
//=======================================================
void main()
{
 while(1)
 {
  P1=0;  //Add breack point and check time1
  delay1ms();
  P1=1;  //Add breack point and check time2

 }
}

//=======================================================
//    1ms Timer Delay Function
//=======================================================
void delay1ms()
{
 TMOD=0x10;  //Configure timer 1 in 16-bit mode 1
 TH1=0xFC;  //Timer count for 1 msec delay at 12MHz
 TL1=0x18;  //Count=65536-1000
 TR1=1;   //Start the timer
 while(TF1==0); //Wait until Timer overflow flag sets
 TF1=0;   //Clear Timer overflow flag
}


No comments:

Post a Comment