Program to Capture Time Between Two Rising Edges





Program which polls to see when edges have been captured
#include <hc11.h>

unsigned int first, second, time;

main()
{
    TMSK2 = TMSK2 | 0x03;            /* Set prescaler for divide by 16 */

    /* Setup for IC1 */
    TCTL2 = (TCTL2 | 0x10) & ~0x20;  /* Capture Rising Edge */
    TFLG1 = 0x04;                    /* Clear IC1 Flag */

    /* Setup for IC2 */
    TCTL2 = (TCTL2 | 0x04) & ~0x08;  /* Capture Rising Edge */
    TFLG1 = 0x02;                    /* Clear IC2 Flag */

    while ((TFLG1 & 0x04) == 0) ;  /* Wait for 1st rising edge; */
    first = TIC1;                  /* Read time of 1st edge;    */

    while ((TFLG1 & 0x02) == 0) ;  /* Wait for 2nd rising edge; */
    second = TIC2;                 /* Read time of 2nd edge;    */

    time = second - first;         /* Calculate total time */
}


Program which captures edges on interrupts
#include <hc11.h>

#define TRUE  1
#define FALSE 0

void tic1_isr(void);
void tic2_isr(void);

unsigned int first, second, time, done;

main()
{
    done = FALSE;

    TMSK2 = TMSK2 | 0x03;            /* Set prescaler for divide by 16 */

    /* Setup for IC1 */
    TCTL2 = (TCTL2 | 0x10) & ~0x20;  /* Capture Rising Edge */
    TIC1_JMP = JMP_OP_CODE;          /* Set IC1 Interrupt Vector */
    TIC1_VEC = tic1_isr;
    TFLG1 = 0x04;                    /* Clear IC1 Flag */
    TMSK1 = TMSK1 | 0x04;            /* Enable IC1 Interrupt */

    /* Setup for IC2 */
    TCTL2 = (TCTL2 | 0x04) & ~0x08;  /* Capture Rising Edge */
    TIC2_JMP = JMP_OP_CODE;          /* Set IC2 Interrupt Vector */
    TIC2_VEC = tic2_isr;
    TFLG1 = 0x02;                    /* Clear IC2 Flag */
    TMSK1 = TMSK1 | 0x02;            /* Enable IC2 Interrupt */

    enable();

    while (!done) ;

    time = second - first;         /* Calculate total time */
}

#pragma interrupt_handler tic1_isr
void tic1_isr(void)
{
    first = TIC1;
    TFLG1 = 0x04;
}

#pragma interrupt_handler tic2_isr
void tic2_isr(void)
{
    second = TIC2;
    done = TRUE;
    TFLG1 = 0x02;
}


Program which captures edges on interrupts and prints result to screen
#include <hc11.h>

#define TRUE  1
#define FALSE 0

void tic1_isr(void);
void tic2_isr(void);
void putint(int i);

unsigned int first, second, time, done;

main()
{
    done = FALSE;

    TMSK2 = TMSK2 | 0x03;            /* Set prescaler for divide by 16 */

    /* Setup for IC1 */
    TCTL2 = (TCTL2 | 0x10) & ~0x20;  /* Capture Rising Edge */
    TIC1_JMP = JMP_OP_CODE;          /* Set IC1 Interrupt Vector */
    TIC1_VEC = tic1_isr;
    TFLG1 = 0x04;                    /* Clear IC1 Flag */
    TMSK1 = TMSK1 | 0x04;            /* Enable IC1 Interrupt */

    /* Setup for IC2 */
    TCTL2 = (TCTL2 | 0x04) & ~0x08;  /* Capture Rising Edge */
    TIC2_JMP = JMP_OP_CODE;          /* Set IC2 Interrupt Vector */
    TIC2_VEC = tic2_isr;
    TFLG1 = 0x02;                    /* Clear IC2 Flag */
    TMSK1 = TMSK1 | 0x02;            /* Enable IC2 Interrupt */

    enable();

    while (!done) ;

    time = second - first;         /* Calculate total time */
    putint(time);

}

#pragma interrupt_handler tic1_isr
void tic1_isr(void)
{
    first = TIC1;
    TFLG1 = 0x04;
}

#pragma interrupt_handler tic2_isr
void tic2_isr(void)
{
    second = TIC2;
    done = TRUE;
    TFLG1 = 0x02;
}

void putint(int i)
{
/* Call BUFFALO routines to write to terminal.                      *
 * Routine at 0xffc1 prints two bytes pointed to by X to terminal.  *
 * C compiler automatically sets X to address of i.                 *
 */
    asm("   jsr 0xffc1");

/* Routine at 0xffc4 prints new line to terminal.                   *
 */
    asm("   jsr 0xffc4");
}