Unix defines (see unistd.h) the function sleep(int msec) which waits the designated number of milliseconds before returning and usleep(int usec) which waits microseconds. The routines are not implemented yet for the PIC32.
One tradition for such functions is to have the cpu loop over some busy-work activity. There is some risk here that an optimizing compiler will judge the loop to be non-productive and eliminate it.
This example demonstrates a simplistic sleep routine (mysleep) and exercises it using the debug output routines.
Project files: timer1.zip.
timer1.c
01: /* timer1.c 02: */ 03: 04: #include <p32xxxx.h> 05: #include "db_utils.h" 06: 07: void mysleep(int n); 08: 09: 10: int main() 11: { 12: int i; 13: DBPRINTF("Basic Starter Kit Lab (" __DATE__ ", " __TIME__ ")\n"); 14: for (i=0; i<10; i++) { 15: DBPRINTF("Hello from PIC32 (%d)\n",i); 16: mysleep(1000); 17: } 18: DBPUTS("Program terminated. Click HALT and then RESET to stop the microcontroller. \n"); 19: return 0; 20: } 21: 22: void mysleep(int n) 23: { 24: int i,k,count; 25: const int DLY = 600; 26: for (i=0; i<n; i++) { 27: count = 0; 28: for (k=0; k<DLY; k++) count++; 29: } 30: }
The program prints out ten messages, waiting about a second between message.
Maintained by John Loomis, updated Tue Jul 29 16:50:33 2008