micro second delay for 89V51RD2

Status
Not open for further replies.

chethankp

Member level 4
Joined
Jan 4, 2010
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
bangalore
Activity points
1,860
Hi,
I am using 89V51RD2 for some application .. I need to write delay code which can take create micro second delay ..

I wrote the below code

void delayMicroseconds(unsigned int i)
{

for(j=0;j<=i;j++)
{

for(k=0;k<256;k++);
}

}

But I do not think this is giving correct micro second delay .

Someone suggested to use the micro time for delay ..

I did not understand how exactly I can use the timer for it .
becoz I need to use the delayMicroseconds() from many places with different values .

like
delayMicroseconds(1);

delayMicroseconds(10)
delayMicroseconds(25)

Can somebody suggest a method to get exact micro second delay ..

?
 

hi there are lot of delay routines in lot of compilers with knowing the clock frequency... For creating exact delay in a C program without any inbuilt functions you have to work some more. . .
write the

Code C - [expand]
1
for(k=0;k<10;k++);


and decode it as assembly and find how many instructions written for it.. then change the value 10 according to your 1 us...
If you need more accuracy and less work, put a break point on this line and after this line now run and find how many instruction cycles taken for....
Or if you use keil _nop_ () function can give the delay of 1 instruction cycle with this you can create 1 us delay and loop it, but looping nop instruction for 1us is not recommended..
 
if you are using 20MHz crystal then try

Code:
void DelayMicroSeconds (int us)
{
        for( ; us>0 ; us--)
        {
          _nop_();               // if crystal is 20Mhz then delay is 10 microsecond
          _nop_();
          _nop_();
          _nop_();   
        }
}

and also try Venkadesh_M suggest as match perfect instruction cycle to put nop() in your routin..
 
Hi, the micro controller board which I bought has 11MHz ... and I don't have debugger to check the exact time for each instruction ...

Will adding two more _nop_ in to your suggested cide will create a 10micro second delay?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…