
 1 void main() {
 2 
 3     unsigned startTime;
 4     unsigned startTimeLo;
 5     unsigned startTimeHi;
 6     unsigned endTime;
 7     unsigned endTimeLo;
 8     unsigned endTimeHi;
 9     char i;
10 
11     InitializeHardware();
12     while (1) {                         /* Repeat forever */
13         getch();                        /* Wait for an asynchronous event */
14         EA = 0;                         /* No interrupts for a while */
15         CCF0 = 0;                       /* Clear PCA capture flag */
16         startTimeLo = PCA0L;            /* Read PCA counter, LSB */
17         startTimeHi = PCA0H;            /*  first per the data sheet */
18         i = 50; while (--i) ;           /* Spin here for a while */
19         P1.0 = 1;                       /* Assert PCA input line */
20         P1.0 = 1;                       /* Be sure to meet the pulse */
21         P1.0 = 1;                       /*  width requirement of two */
22         P1.0 = 1;                       /*  SYSCLK clock periods */
23         P1.0 = 0;                       /* End the pulse */
24         EA = 1;                         /* Interrupts okay now */
25         if (CCF0) {                     /* Good capture */
26             endTimeLo = PCA0CPL0;       /* Get the capture register */
27             endTimeHi = PCA0CPH0;
28 
29             startTime = startTimeLo + (startTimeHi << 8);
30             endTime   = endTimeLo   + (endTimeHi   << 8);
31             printf("start: %5u (0x%04X), end: %5u (0x%04X), elapsed: %5u\n",
32                 startTime, startTime, endTime, endTime, endTime - startTime);
33 
34             }                           /* End 'good capture' */
35         else {                          /* Bad capture */
36             printf("Bad capture\n");    /* Scream and holler */
37             }                           /* End 'bad capture' */
38         }                               /* End 'repeat forever' */
39     }                                   /* End main() */
