Fundamentals of Microcontrollers: Input Capture Timer Function Example - Prof. V. Giurgiut, Study notes of Mechanical Engineering

An example of using a microcontroller's timer function as an input capture device. It covers the use of the free running clock, tcnt, and its overflow flag, tof, as well as the input capture clock, tic1, and its event flag, ic1f. The document also demonstrates how to select the signal transition to be captured and calculate the actual time in microseconds from the timer readings and overflow count.

Typology: Study notes

Pre 2010

Uploaded on 10/01/2009

koofers-user-yd6
koofers-user-yd6 🇺🇸

5

(1)

9 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Fundamentals of Microcontrollers: Input Capture Timer Function Example - Prof. V. Giurgiut and more Study notes Mechanical Engineering in PDF only on Docsity!

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.

Instructions

i) Define variables:

a. Origin of time, T0 = 2 bytes

b. Capture time, T1 = 2 bytes

c. Overflow counter, NOF1 = 1

byte

ii) Initialize

iii) Initialize index X to REGBAS

iv) Initialize timer IC1 function: set

EDG1A in TCTL

v) Initialize SCI

vi) Wait for a keystroke reception

vii) Store initial time

viii) Zero overflow counter NOF

ix) Reset TOF

x) Wait for the input capture

xi) Check TOF

xii) Jump if no TOF; else

a. Increment overflow counter

b. Reset TOF

xiii) Check IC1F

xiv) Loop back

xv) After input capture

xvi) Load and store t

xvii) Reset IC1F by writing 1 to it

xviii) Loop back to iii) and do it again

Flowchart

SWI

 Initialize REGBAS in reg X

 Initialize timer IC function:

set EDG1A in TCTL

 Initialize SCI:

LABEL

Define variables:

 Origin of time, T0 = 2 bytes

 Capture time, T1 = 2 bytes

 Overflow counter, NOF1 = 1 byte

START

NOP

BEGIN

Check if RDRF is set

LABEL

 Load TCNT into T

 Zero NOF

 Reset TOF

 Increment NOF

 Reset TOF

LABEL

Check TOF

 Load and store measured

time from TIC1 into T

 Reset IC1F

Check IC1F

N

Y

N

Y

Code

ORG DATA

T0 RMB 2

T1 RMB 2

NOF1 RMB 1

ORG PROGRAM

START LDX #REGBAS

LDAA #%

STAA TCTL2,X

LDAA #%

STAA BAUD,X

LDAA #%

STAA SCCR1,X

LDAA #%

STAA SCCR2,X

BEGIN NOP

LABEL0 LDAA SCSR,X

ANDA #%

BEQ LABEL

LDAA SCDR,X

LDD TCNT,X

STD T

LDAA #$

STAA NOF

LDAA #%

STAA TFLG2,X

LABEL1 LDAA TFLG2,X

ANDA #%

BEQ LABEL

INC NOF

LDAA #%

STAA TFLG2,X

LABEL2 LDAA TFLG1,X

ANDA #%

BEQ LABEL

LDD TIC1,X

STD T

LDAA #%

STAA TFLG1,X

BRA BEGIN

SWI

a) Press the RUN button. The program should loop on LABEL0. Type letter T into the serial

transmitter and send. The program should exit the loop and stop at $c01a. At this point your

screen looks like this:

b) Step. You will notice that the contents of SCDR ($54, i.e. ASCII for T) has been loaded into accA

c) Step again. The screen looks like this:

You notice that the contents of TCNT (in this case, $101c) has been loaded into accD. (The double

accumulator accD is used, since the time values are in double precision, i.e., require 4 hex digits).

f) Step. The value in TFLG1 (%00000100) gets loaded into accA. Step again. AccA is AND-ed with

the IC1F mask (%00000100). The result is (%00000100), i.e., non-zero.

g) Step again. Since the result of the previous operation was non-zero, the BEQ operation does not

branch, and you proceed to line $c03b

h) Change display of accA to hex. Step. The value in TIC1 (for me, $2994, but you may get

something else!) is loaded in accD (The double accumulator accD is needed, since the timer

values are in double precision, i.e., four hex long)

i) Step again. The value in accD (for me, $2994, but you may have something else!) is stored as

the captured time in T1. Note that two memory location are used, and the storage is done as $

followed by $

j) You will do this now on automatic. Press the Run button. Then, press the send button in the

serial transmitter to start the input-capture process. Your program stops at the breakpoint $c01a.

k) Step. The TCNT value (mine is $cf13, yours may differ!) gets captured in accD.

l) Step two times. The TCNT value ends up in T0 (in my case, $cf13, yours may differ!).

m) Run. Toggle pin IC1 in port A window. As you toggle from 1 to 0, nothing happens, since you

only set EDG1A=1 in the initialization section. This means that you programmed the MCU to only

recognize raising edges on line IC1, i.e., transitions from 0 to 1.

n) Toggle again, now from 0 to 1. Note that the loop is exited, and the program stops at the

breakpoint $c03b.

o) Run again several times, and consolidate your understanding of the way the program runs. Try

sending different characters, and notice that the program is only sensitive to you sending a

character, but insensitive to what character you sent.

p) When you are content with your understanding, remove all breakpoint but SWI. Set a new

breakpoint at $c043. Run, go through the sending and the toggling, and, after it stops at the

breakpoint, write below the values of T0, T1, NOF:

My results Your results

T0 $49c

T1 $

NOF 0

Calculate the time taken between your sending of the character (start of the process) and your toggling

of IC1 from low to high (stop of the process).

t)

mine

= (T1-T0 + $10000NOF)0.5 s = ($6227 - $49c8 + $100000) 0.5 s = 3119 s = 3.1 ms

t)

yours

= (T1-T0 + $10000NOF)0.5 s = ($???? - $???? + $100000) 0.5 s = ???? s = ???.? ms

q) Try to wait for the NOF to change to 1 before you toggle the IC1 pin. write below the values of

T0, T1, NOF:

My results Your results

T0 $872e

T1 $366e

NOF 1

t)

mine

= (T1-T0 + $10000NOF)0.5 s = ($366e - $872e + $100001) 0.5 s = 55,200 s = 55.2 ms

t)

yours

= (T1-T0 + $10000NOF)0.5 s = ($???? - $???? + $100001) 0.5 s = ????? s = ???.? ms

Note: The results from u) and t) do not need to agree. They depend on human factor, like when you

decide to toggle the IC1 bit.

WHAT YOU HAVE LEARNED

In this example, you have learned:

 The use of MCU Timer function as an Input Capture (IC) device

 The use of the free running clock, TCNT, and it overflow flag, TOF

 The use of a input capture clock, TIC1, and its event flag, IC1F

 The selection of signal transition to be captured (here rising edge, EDG1A) and the fact that the

MCU is only sensitive to that particular transition.

 The calculation of actual time in s from the timer readings, T1, T0, and overflow count, NOF.