Delay in C

Status
Not open for further replies.

savnik

Member
I have this:

//clock=20000000 //one instruction=0.2us
delay_cycles(59); how much is the delay; is 118;

If i change the xtal from 20 to 4 mhz how much is the delay now ;
 
Seems pretty self explanatory to me. Judgeing by the delay being .2us and the frequency being 20mhz you're using a pic (the time constant of 20mhz is actually .05us but the PIC instruction cycle is four clocks) So just calculate it that way. 1/Clock frequency * 4 = single cycle delay time. I don't believe such a routine can delay for just a couple cycles as the branching alone takes a certain number of instructions. Delays of only a few instruction cycles would best be achieved by using inline assembly NOP's, there may even be a C function for it, but if you need controlling of timeing that severe you should be programming in assembly anyways.
 
Last edited:
Using the instruction cycle time for doing delays, more than a couple of cycles, is a really really poor practice in my opinion. Especially considering how many better ways there are to do it.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…