8051 timers || Tutorial with AT89S52 timer program program

Before going to this tutorial please refer 8051 timers.

Let us see how to configure a timer in 8051 with AT89S52 micro-controller.
After going through the 8051 manual we can implement the same timer logic in this c program to simulate the timer output we can use the LED.
Program:
#include<at89x52.h>
/*
Crystal freq = 11.0592MHz
Fcpu=11.0592MHz
12 Cycles required for one machine cycle
Finstruction=(11.0592/12) MHz => 921.6 KHz
Time period of each machine cycle = 1/921.6 msec.
so... Tmachine cycle = 1.085 uSec
Desired delay = No of counts x 1.085 usec
*/
void configTimer()
{
/*
We need delay for 30mSec Second
So, 30x10^-3 = No of counts x 1.085x10^-6
No of counts = ((30 x 10^-3 x 10^6)/1.085) = 27649 = 0x6C01
Initial timer value = 0xFFFF - 0x6C01 = 0x93FE
THO = 0x93, TL0 = 0xFE
*/
TMOD = 0x1; // Opearating timer 0 in 16bit mode
TH0 = 0x93;
TL0 = 0xFE;
TR0 = 1;//Turn on timer 0
}

void delay()
{
configTimer();
while(TF0 == 0); //wait for timer 0 to overflow
TR0 = 0; //Turn off timer 0
TF0 = 0;//Clear timer 0 overflow flag
}

int main()
{
P1_0 = 0;  //Making 0th pin in port-0 to low i.e LED off
P1_1 = 0;  //Making 1st pin in port-0 to low i.e LED off
while(1)   //Super loop
{
  delay();
  P1_1 = 0; //LED off
  P1_0 = 1; //LED On
  delay();
  P1_0 = 0; //LED off
  P1_1 = 1; //LED on
}
return 0;
}

Connection:


Output Video:



Cheers!!!!
-> Let Us Embed <-