i am Generating 1 second delay using timers in 8051
my concept is like this
iam using 11.0592 crystal frequency
time required for 1 machine cycle is1.085 micro second
if timer is set to 0000h then time required for timer to run once is 65536*1.085= 7.11 ms
now if 7.11*142(times the loop runs) = 1000ms=1sec
so here is my code
but im not getting any 1 second delay..please correct me where iam wrong
my concept is like this
iam using 11.0592 crystal frequency
time required for 1 machine cycle is1.085 micro second
if timer is set to 0000h then time required for timer to run once is 65536*1.085= 7.11 ms
now if 7.11*142(times the loop runs) = 1000ms=1sec
so here is my code
Rich (BB code):
#include<reg51.h>
void delay(void);
void main()
{
while(1)
{
P2=0x00; // led off
delay(); // delay for 1sec
P2=0xFF; // led on
delay(); // delay for 1 sec
}
}
void delay(void)
{
int i;
TMOD=0x01; // timer 0 in mode 1
for(i=0;i<142;i++)
{
TL0=0x00; // starting value from 0
TH0=0x00;
TR0=1; // sart timer
while(TF0==0); // polling TF flag for high
TR0=0; // stop timer
TF0=0; // clear flag TF0
}
}
Last edited by a moderator: