C program using polling:
/* Program to determine the time between two rising edges using the * * HC12 Input Capture subsystem, and print the time on the terminal */ #include "hc12.h" #include "DBug12.h" unsigned int first, second, time; main() { TSCR = 0x80; /* Turn on timer subsystem */ TMSK2 = 0x05; /* Set prescaler for divide by 32 */ /* Setup for IC1 */ TIOS = TIOS & ~0x02; /* IOC1 set for Input Capture */ TCTL4 = (TCTL4 | 0x04) & ~0x08; /* Capture Rising Edge */ TFLG1 = 0x02; /* Clear IC1 Flag */ /* Setup for IC2 */ TIOS = TIOS & ~0x04; /* IOC2 set for Input Capture */ TCTL4 = (TCTL4 | 0x10) & ~0x20; /* Capture Rising Edge */ TFLG1 = 0x04; /* Clear IC2 Flag */ while ((TFLG1 & 0x02) == 0) ; /* Wait for 1st rising edge; */ first = TC1; /* Read time of 1st edge; */ while ((TFLG1 & 0x04) == 0) ; /* Wait for 2nd rising edge; */ second = TC2; /* Read time of 2nd edge; */ time = second - first; /* Calculate total time */ DBug12FNP->printf("time = %d\r\n",time) /* print to screen */; }C program using interrupts:
/* Program to determine the time between two rising edges using the * HC12 Input Capture subsystem. This program uses interrupts to * determine when the two edges have occurred. */ #include "hc12.h" #include "DBug12.h" #define TRUE 1 #define FALSE 0 volatile unsigned int first, second, time, done; main() { done = FALSE; /* Turn on timer subsystem */ TSCR = 0x80; /* Set prescaler to 32 */ TMSK2 = 0x05; /* Setup for IC1 */ TIOS = TIOS & ~0x02; /* Configure PT1 as IC */ TCTL4 = (TCTL4 | 0x04) & ~0x08; /* Capture Rising Edge */ TFLG1 = 0x02; /* Clear IC1 Flag */ TMSK1 = TMSK1 | 0x02; /* Enable IC1 Interrupt */ /* Setup for IC2 */ TIOS = TIOS & ~0x04; /* Configure PT2 as IC */ TCTL4 = (TCTL4 | 0x10) & ~0x20; /* Capture Rising Edge */ TFLG1 = 0x04; /* Clear IC2 Flag */ TMSK1 = TMSK1 | 0x04; /* Enable IC2 Interrupt */ enable(); while (!done) ; time = second - first; /* Calculate total time */ DBug12FNP->printf("time = %d\r\n",time) /* print to screen */; } @interrupt void tic1_isr(void) { first = TC1; TFLG1 = 0x02; } @interrupt void tic2_isr(void) { second = TC2; done = TRUE; TFLG1 = 0x04; }vector.c program setting up interrupt vectors
/* INTERRUPT VECTORS TABLE 68HC12 */ void tic1_isr(); void tic2_isr(); void (* const _vectab[])() = { /* 0x0B10 */ 0, /* BDLC */ 0, /* ATD */ 0, /* reserved */ 0, /* SCI0 */ 0, /* SPI */ 0, /* Pulse acc input */ 0, /* Pulse acc overf */ 0, /* Timer overf */ 0, /* Timer channel 7 */ 0, /* Timer channel 6 */ 0, /* Timer channel 5 */ 0, /* Timer channel 4 */ 0, /* Timer channel 3 */ tic2_isr, /* Timer channel 2 */ tic1_isr, /* Timer channel 1 */ 0, /* Timer channel 0 */ 0, /* Real time */ 0, /* IRQ */ 0, /* XIRQ */ 0, /* SWI */ 0, /* illegal */ 0, /* cop fail */ 0, /* cop clock fail */ (void *)0xff80, /* RESET */ };