On Feb 21, 2010, at 10:11 PM, Jason Hsu wrote: > As you can see in the code snippet below, I have created algorithms > for a 1 sec delay, 10 msec delay, 100 usec delay, and 1 usec delay. 1) You have to be very careful when writing delay "loops" in a high- level language like C. Compilers have been known to notice that you're not actually doing anything inside the loop, and optimize it all away in favor of setting equivalent end values. 2) You're using the same "count" variable for all the delay functions, and each "inside" function will overwrite whatever value the outside function had. Ie, delay_10_nsec returns with count == 200, so the delay_1_usec() loop will immediately end. 3) each PIC instruction takes 4 clock cycles, or a little over 1 microsecond at 3.xx MHz, so you're starting off with some invalid assumptions. 4) most C compilers will include some "delay" primitives to address many of these issues, so that you don't have to reinvent the wheel... > In applications for which timing is critical, how do you make sure > the timing is correct? Code in assembler. Perhaps inline assembler within C code. Aside from all the above issues, code generation can change from one version of a compiler to the next, rendering any time spent getting the C code "just write" useless... BillW -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist