Docsity

Computer Systems: Capturing Input, Recording Event Time and Measuring Periods, Study notes of Microcomputers

Partial preview of the text

Download Computer Systems: Capturing Input, Recording Event Time and Measuring Periods and more Study notes Microcomputers in PDF only on Docsity!

ECE 3120: Computer Systems

Chapter 8: Input Capture Function

Manjeera Jeedigunta

http://blogs.cae.tntech.edu/msjeedigun

Email: [email protected]

Tel: 931-372-6181, Prescott Hall 120

Rising edge

Falling edge

or

Figure 8.4 Events represented by signal edges

Input Capture Functions

(1 of 2)

†^

Physical time is often represented by the contents of the main timer.

†^

The occurrence of an event is represented by a signal edge (rising orfalling edge).

†^

The time when an event occurs can be recorded by latching the countof the main timer when a signal edge arrives as illustrated in Figure8.4.

†^

The HCS12 has eight input capture channels. Each channel has a 16-bit capture register, an input pin, edge-detection logic, and interruptgeneration logic.

†^

Input capture channels share most of the circuit with output comparefunctions. For this reason, they cannot be enabled simultaneously.

4

Timer Port Pins †

Each port pin can be used as a general I/O pinwhen timer function is not selected.

Pin 7 can be used as input capture 7, outputcompare 7 action, and pulse accumulatorinput.

When a timer port pin is used as a general I/Opin, its direction is configured by the DDRTregister.

5

ED G 7 B ED G 7 A ED G 6 B ED G 6 A ED G 5 B ED G 5 A ED G 4 B

ED G 4 A

valueafte r re s e t

Figure 8 .5 Tim e r c o ntro l re giste r 3 and 4 7

ED G 3 B ED G 3 A ED G 2 B ED G 2 A ED G 1 B ED G 1 A ED G 0 B ED G 0 A

(a) Tim e r c o ntro l

re gis te r 3 (TCTL3 )

(b) Tim e r c o ntro l

re gis te r 4 (TC TL4 )

ED G nB

ED G nA -- Edge c o nfiguratio n 0

0 : C apture dis able d 0

1 : C apture o n ris ing e dge s o nly 1

0 : C apture o n falling e dge s o nly 1

1 : C apture o n bo th e dge s

†^

The signal edgeto be captured isselected byTCTL3 andTCTL4.

†^

The edge to becaptured isselected by twobits. The usercan choose tocapture the risingedge, fallingedge, or bothedges.

Timer Control Register 3 and 4

7

7

6

5

4

3

2

1

0

C7F

C6F

C5F

C4F

C3F

C2F

C1F

C0F

reset:

0

0

0

0

0

0

0

0

Figure 8.8 Timer interrupt flag register 1 (TFLG1)

CnF: input capture/output compare interrupt flag bits

0 = interrupt condition has not occurred1 = interrupt condition has occurred

Timer Interrupt Flag 1 Register (TFLG1)

†

Whenever a signal edge arrives, the associatedtimer interrupt flag will be set to 1.

How to Clear a Timer Flag Bit^ †

In normal mode, write a 1 to the flag bit to be cleared.

†^

Method 1 „^

Use the BCLR instruction with a 0 at the bit position (s)corresponding to the flag (s) to be cleared. For example, BCLR TFLG1, $FE will clear the C0F flag.

†^

Method 2 „^

Use the movb instruction with a 1 at the bit position (s)corresponding to the flag (s) to be cleared. For example, movb

#$01,TFLG

will clear the C0F flag.

†^

When fast timer flag clear function is enabled, see Figure8.1.

-^

Time reference: often used in conjunction with an output compare function

Start ofinterval

End ofinterval

e^1

e^2

e^3

e^4

e^ i^

e^ j

Figure 8.11 Using an input-capture function for event counting

...^

...

Time t

0

Time of reference(set up by signal edge)

Time t

+ delay 0

Time to activateoutput signal

(set up by output compare)

Figure 8.12 A time reference application

†^

Interrupt generation: Each input capture function can be used as anedge-sensitive interrupt source.

†^

Event counting: count the number of signal edges arrived during aperiod

Input Capture

11

ΔT

T

duty cycle =

ΔT T^

Figure 8.13 Definition of duty cycle

Duty Cycle Measurement

Phase Difference Measurement

ΔT

T

signal S1 signal S

phase difference =

Δ T* 360T^

o

Figure 8.14 Phase difference definition for two signals

13

Start

Choose to capture the rising edgeSet the timer counter prescale factor to 16Enable the timer counter

Clear the C0F flag

C0F = 1?

yes

no

Saved the captured first edgeClear the C0F flag

C0F = 1?

no

yes

Take the difference of the second and the

first captured edges

Stop

Figure 8.16 Logic flow of period measurement program

Period Measurement

(2 of 2)

14

#include "c:\miniide\hcs12.inc"

org

$

edge

ds.b

2

; memory to hold the first edge

period

ds.b

2

; memory to store the period

org

$

movb

#$90,TSCR

; enable timer counter and enable fast timer flags clear

bclr

TIOS,IOS

; enable input-capture 0

movb

#$06,TSCR

; disable TCNT overflow interrupt, set prescaler to 64

movb

#$01,TCTL

; capture the rising edge of PT0 signal

movb

#$01,TFLG

; clear the C0F flag

brclr

TFLG1,C0F,*

; wait for the arrival of the first rising edge

ldd

TC

; save the first edge and clear the C0F flag

std

edge

movb

#$01,TFLG brclr

TFLG1,C0F,*

; wait for the arrival of the second edge

ldd

TC

subd

edge

; compute the period

std

period

swi end

Solution: Period Measurement

16

17

#include

"c:\miniide\hcs12.inc" org

edge

ds.b

overflow

ds.b

pulse_width ds.b

org

movw

#tov_isr,UserTimerOvf ; set up TCNT overflow interrupt vector

lds^

; set up stack pointer

movw

#0,overflow

movb

#$90,TSCR

; enable TCNT and fast timer flag clear

movb

#$05,TSCR

; disable TCNT interrupt, set prescaler to 32

bclr

TIOS,IOS

; select IC

movb

#$01,TCTL

; capture rising edge

movb

#C0F,TFLG

; clear C0F flag

wait

brclr

TFLG1,C0F,wait

; wait for the first rising edge

movw

TC0,edge

; save the first edge & clear the C0F flag

movb

#TOF,TFLG

; clear TOF flag

bset

TSCR2,$

; enable TCNT overflow interrupt

cli^

;^

movb

#$02,TCTL

; capture the falling edge on PT0 pin

wait

brclr

TFLG1,C0F,wait

; wait for the arrival of the falling edge

ldd^

TC

subd

edge

std^

pulse_width

bcc

next

; is the second edge smaller?

ldx^

overflow

; second edge is smaller, so decrement

dex

; overflow count by 1

stx^

overflow

;^

next

swi

tov_isr

movb

#TOF,TFLG

; clear TOF flag

ldx^

overflow

inx stx^

overflow

rti end