Some random comments... 1. One of the top fundamental bits of knowledge on microcontrollers is how many clocks are required to execute one instruction. If you are using pics from the PIC12, 16 or 18 families, the answer is 4. This is in the data sheet - you should read it. So your pic (if it's one of the above pic families) takes 4 of the .279 usec clocks - or about 1.1 Mips. 2. To know what's going on, open the assembler listing your compiler generates - actually see what code instructions it makes. 3. I'm not a C guru but your loops will probably go faster if you preload and count down to zero. For example, you preload count with 80 and count up. The processor will take possibly several more instructions to do a compare. But the compare for zero is usually free; hitting zero, if done right, sets the Z flag so a bit test instruction can be used. 4. I think someone else may have mentioned to do a one second led blink to check your understanding of how it all works. This is a good idea too. 5. You probably use some sort of IDE; these often come with cycle counters. If you have one, use it. See how many cycles it says your code is using. Then try other code re-writes to attempt to improve it. On 2/22/2010 5:20 PM, Jason Hsu wrote: > Thanks for your responses. > > I tried writing a delay in C code, and the microcontroller doesn't > seem efficient in the counting process. The microcontroller can > increment a variable only 80 times in 1 msec. So that means that it > takes 12.5 usec to increment a variable just one time. With a 3.57945 > MHz crystal, a clock cycle is .279 usec. So incrementing a variable > just one time takes 45 clock cycles. I know that C isn't as efficient > as Assembly language, but I didn't expect it to be this inefficient. > > I understand now that delays are usually executed in Assembly code. > What is the proper way to insert Assembly language code into a C > program for PICC? I recall trying to do this once but having > difficulty getting it to compile, so I gave up. > > My new code snippit is: > > void delay_1_msec (void) > { > count = 0; > while (count<80) > { > count++; > } > } > > void delay_10_msec (void) > { > count_10_msec = 0; > while (count_10_msec<10) > { > delay_1_msec(); // 10 delays of 1 msec each. > count_10_msec++; > } > } > > void delay_1_sec (void) > { > count_1_sec = 0; > while (count_1_sec<100) > { > delay_10_msec(); // 100 delays of 10 msec each. > count_1_sec++; > } > } > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist