
EMCH 367 Fundamentals of Microcontrollers Example INPUT CAPTURE TIMER FUNCTION
EXAMPLE INPUT CAPTURE TIMER FUNCTION
OBJECTIVE
This example has the following objectives:
Review the use of MCU Timer function as an Input Capture (IC) device
Review the use of the free running clock, TCNT, and it overflow flag, TOF
Review the use of a input capture clock, TIC1, and its event flag, IC1F
Demonstrated how the selection of signal transition to be captured is made (here rising edge,
EDG1A) and that the MCU is only sensitive to that particular transition.
Show the calculation of actual time in s from the timer readings, T1, T0, and overflow count,
NOF.
PROGRAM EX_IC
This program is an example of timer input capture. The process is started when a keystroke is received.
Then, the time is measured until a low-high transition is captured on line IC1. In this process, the initial
time, T0, and the capture time, T1, as well as the number of overflows are recorded. After the input
capture, the IC1F flag is reset, the overflow counter is zeroed, and the process is repeated.
FLOWCHART AND CODE
The program flowchart is show to the right of the program instructions. Note the variable definition block
in which T0, T1, and NOF are defined. Next, the initialization block contains reg. X initialization, timer IC
initialization, and SCI initialization. The program loop starts at label BEGIN with the overflow counter,
NOF, being zeroed. First, the RDRF (reception data register full) flag is checked in a loop to verify if a
keystroke has been received. When keystroke was received, the time counter is read and stored in T0.
Then, the programs loops on LABEL1 until and input capture IC1 is recorded. In this loop, TOF is first
check to verify if timer overflow takes place. When timer overflow is detected, the overflow counter,
NOF, is incremented and TOF is reset. Next, IC1F is check to verify if input capture on IC1 took place. If
input capture is not detected, the program returns to LABEL1. When input capture is detected, the
program exits the loop, loads the IC1 timer from TIC1 and stores it in the capture time variable, T1. The
program loops back to the beginning and wait for a new keystroke to restart the process.
The essential code for the program is shown to the right of the program flowchart. This essential code
was incorporated into the standard asm template to generate the file Ex_IC.asm.
Dr. Victor Giurgiutiu Page 111/30/2020