fbpx

Timer In 8051

The timer is used as a time delay generator.

  • 89C51 Has Two 16 Bit Timer.
    1.Timer 0,
    2.Timer 1.
  • Timer Divided Into Two 8bit Register.

Registers

  • TH0,TL0 = TIMER 0 [16 bit Register].
  • TH1,TL1 = TIMER1 [16 bit Register].
  • TMOD = Is a 8bit Register.
  • TCON = Is a 8bit Register.

Timer Mode (TMOD) Register

  • TMOD 8-bit Register.
  • Not bit-addressable.
  • Set Usage Mode For Two Timers
    Set lower 4 bits for Timer 0
    Set upper 4 bits for Timer 1.
TMOD Registor

TMOD

  • Gate
    1.Gate=0:
    Internal control.
    The start and stop of the timer are controlled by way of software.
    2.Gate =1:
    External control.
    The hardware way of starting and stopping the timer by software and an external source.
  • C/T = Counter / Timer
    When C/T = 1 It Act As Counter
    When C/T = 0 It Act As Timer.
  • Mode Selection

Timer Control Register

  • Timer control register: TCON
    Upper nibble for timer/counter, lower nibble for interrupts.
  • TF (timer flag, control flag)
    TF0 for timer/counter 0;
    TF1 for timer/counter 1.
    TF=0 : not reach
    TF=1: reach
    If we enable interrupt, TF=1 will trigger ISR.

TMOD

  • TR (run control bit)
    TR0 for Timer/counter 0; TR1 for Timer/counter 1.
    TR is set by programmer to turn timer/counter on/off.
    TR=0: off (stop)
    TR=1: on (start).

Timer Calculation

  • 89C51 Clock Frequency Is 11.0592 MHz
  • For One Machine Cycle 12 Instruction will Executed
    One Machine Cycle = 11.0592mhz.
  • To Execute 1 Instruction It Require 921.6 kHz
    1 Instruction = 11.0592 / 12 = 921.6 kHz.
  • Time Taken To Execute 1 Instruction Is Calcpated By
    Time = 1/921.6 kHz = 1.085 microsecond.
  • Timer Is 16 Bit ..(2)^16 = 65535.
  • 1 Second = 1000 mill sec
    • 1 mill sec = 1.000 microsecond.
  • To Calculate 1 millisecond
    1 millisecond / 1 machine cycle time
    1 millisecond / 1.085microsecond = 921.6 Dec.
  • Check Decimal To Hex Table
    For 921, Hex Value Is 399.
  • 16 Bit (2)^16 = 65535
    65535 from that 921 = 64614.
  • For 64614 Hex value Is FF66.
  • TH1=0XFF , TL1 = 66.
  • To Calculate 50 microsecond
    50 ms/ 1 machine cycle time
    50ms/1.085ms = 46 Dec
  • Check Decimal To Hex Table
    For 46, Hex Value Is 2E
  • 16 Bit ..(2)^16 = 65535
    65535 from that 46 = 65489.
  • For 65489 Hex value Is FCD1.
    TH1=0XFC , TL1 = D1.

Code

 #include <REGX51.H>
 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
 }
 void main()
 {
 unsigned int i;
 while(1)
 {
 P1_0=0;    // LED connected to 1st pin of port P1
 delay();    // Call delay
 P1_0=1;
 delay();    // Call delay
 }
 } 

Leave a Reply

Your email address will not be published. Required fields are marked *