Hey Shreyas,
What John has explained about timing is very correct, each machine cycle takes
500ns, so for 1uS u need to have 2 machine cycles or two timer count..
but your code will give more than 1 us delay,
This is you code
void delay(unsigned int us)
{
unsigned int x;
T1CON=0x0000;
for(x=0;x<us;x++)
{
TMR1=0x0000;
T1CON=0x8010; //timer 1 enable with prescale of 1:8
while(TMR1<0x02);
T1CON = 0x0000;
}
}
you dont need to configure time 1 everytime you enter the for loop this will take one machine cycle extra...
Also while(TMR1<0x02); will tale more than 2 machine cycle..
if you change above code in assemebly, this will check the condition everytime, which will ad extra machine cycle..
Your for loop will also take extra machine cycle..
since you want delay in uS, its is very critical case..
this routine will give you more than double delay, than you expect in the code..
try to go in steps for 10uS delay
In msec delay for loop delay is negligible, though it is present...
I hope you are getting what I want to say...
__
Amit
** If you find my answer useful, please click the "Helpful? Please click" icon. **