C program to determine the time elapsed between two rising edges using the
HC12 input capture subsystem
C program:
#include "hc12.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 */
}
@interrupt void tic1_isr(void)
{
first = TC1;
TFLG1 = 0x02;
}
@interrupt void tic2_isr(void)
{
second = TC2;
done = TRUE;
TFLG1 = 0x04;
}
vector.c file:
/* 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 */
};