void delay (u32 interval)
interval : Number of milliseconds to pause |
Pauses execution for interval milliseconds. Please be aware that the accuracy of this function may vary and should not be used in situations where precise timing is needed.
#include "moacon500.h" void cmain(void) { portInit(2,0); //Initialize ports 20~27 for output while(1) //Run forever { portOut(20,1); //Set port 20 High delay(100); //Wait for 100ms portOut(20,0); //Set port 20 Low delay(100); //Wait for 100ms } }
To wait for times less than 1ms, or to achieve a higher precision, a “spin-wait” function can be used. The CPU will spin in a loop for a specified number of iterations.
void spinWait(volatile u32 countdown) // The `volatile` is important here. { for (;countdown > 0; countdown--); // Loop until countdown reaches 0 { } }
Each iteration will consume a very small amount of time, so this method can be used to simulate a very short delay, or a delay with higher precision.