I'm looking to create 250ms delay function with MikroC. In the code below, I don't understand what 165 in the 2nd for section does.
void MSDelay(unsigned int itime); // this is the prototype
void MSDelay(unsigned int itime) {
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++) {
for(j=0;j<165;j++); }
}
}
MikroC provides the built-in function Delay_ms for producing simple blocking software delays.
This should work unless you have other specific constraints.
The method you've shown is a bit of a hack. For some specific PIC with a specific clock an empty
forloop with 165 iteration likely takes about 1ms, so the outer loop simply counts milliseconds by running the inner loopitimetimes foritimemilliseconds.You should not use a method like this because it is highly specific to a particular PIC running at a particular clock speed and also depends on the compiler not simply optimizing away the whole loop. The built-in delay function should always just do the right thing no matter which PIC you're building for.