/* * Program to solve Part 1 of Lab 6 * Use Input Capture 2 Interrupt to measure time between two falling edges */ #include <hc11.h> #define TRUE 1 void tic2_isr(void); unsigned int first_edge; /* Time of first edge */ unsigned int second_edge; /* Time of second edge */ unsigned int time_diff; /* Time difference between edges */ unsigned int which_edge; /* Used to let ISR know which time to set */ main() { TMSK2 = (TMSK2 | 0x03); /* 8 us clock to timer subsystem */ /* * Set up Input Capture 2 */ TCTL2 = (TCTL2 | 0x08) & ~0x04; /* Capture falling edge on TIC2 */ TIC2_JMP = JMP_OP_CODE; /* Set the TIC2 interrupt vector */ TIC2_VEC = tic2_isr; /* to point to the tic2_isr() routine */ TMSK1 = TMSK1 | 0x02; /* Set Bit 1 (IC2I bit) in TMSK1 */ /* to enable the TIC2 interrupt */ TFLG1 = 0x02; /* Clear TIC2 flag before enabling ints */ /* * Done with Input Capture 2 setup */ which_edge = 1; /* 1st time in interrupt, capture first edge */ enable(); /* Enable interrupts (clear I bit in CCR) */ while (TRUE) { if (which_edge == 3) /* Got both edges if which_edge == 3 */ { time_diff = second_edge - first_edge; /* Calculate time diff */ which_edge = 1; /* Next time in will be for first edge */ } } } #pragma interrupt_handler tic2_isr void tic2_isr(void) { if (which_edge == 1) /* Get time of first edge if which_edge == 1 */ { first_edge = TIC2; /* get time */ which_edge = 2; /* Next time will grab time of second edge */ } else if (which_edge == 2) /* Get time of second edge if which_edge == 2 */ { second_edge = TIC2; /* get time */ which_edge = 3; /* Tell main program time got both edges */ } TFLG1 = 0x02; /* Clear source of interrupt */ }