Docsity

Lecture Slides on Input Capture | ECE 6960, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
Slide 1
'
&
$
%
ECE/CS 3720: Embedded System Design
(ECE 6960/2 and CS 6968)
Chris J. Myers
Lecture 13: Input Capture
Slide 2
'
&
$
%
Basic Principles of Input Capture
•Input capture can be used to measure the period or pulse
width of TTL-level signals.
•Can also trigger interrupts on rising or falling transitions
of external signals.
•Each input capture module has:
–An external input pin, ICn
–A flag bit
–Two edge control bits, EDGnB and EDGnA
–An interrupt mask bit (arm)
–A 16-bit input capture register
1
Slide 3
'
&
$
%
Basic Components of Input Capture
Slide 4
'
&
$
%
Basic Principles of Input Capture (cont)
•Two or three actions result from a capture event:
1. Current TCNT copied into input capture register.
2. The input capture flag is set.
3. An interrupt is requested if the mask is 1.
•The input capture mechanism has many uses:
1. Arm the flag bit so that an interrupt is requested on
the active edge of an external signal.
2. Perform two rising edge captures and subtract the
measurement to get the period.
3. Perform a rising edge capture, then a falling edge
capture, and subtract to get pulse width.
2
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Slides on Input Capture | ECE 6960 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

& '

% $

ECE/CS 3720: Embedded System Design

(ECE 6960/2 and CS 6968)

Chris J. Myers

Lecture 13: Input Capture

& '

% $

Basic Principles of Input Capture

Input capture

can be used to measure the period or pulse

width of TTL-level signals.

of external signals.Can also trigger interrupts on rising or falling transitions

Each input capture module has:

An external input pin, ICn

A flag bit

Two edge control bits, EDGnB and EDGnA

An interrupt mask bit (arm)

A 16-bit input capture register

1

Slide 3

& '

Basic Components of Input Capture

Slide 4

& '

Basic Principles of Input Capture (cont)

  1. An interrupt is requested if the mask is 1.2. The input capture flag is set.1. Current TCNT copied into input capture register.Two or three actions result from a capture event:
  1. Arm the flag bit so that an interrupt is requested onThe input capture mechanism has many uses:

the active edge of an external signal.

  1. Perform two rising edge captures and subtract the

measurement to get the period.

  1. Perform a rising edge capture, then a falling edge

capture, and subtract to get pulse width.

2

& '

% $

Input Capture Interface on 68HC

& '

% $

Control Bits and Flags

EDGnB

EDGnA

Active edge

0

0

None

0

1

Capture on rising

1

0

Capture on falling

1

1

Capture on both rising and falling

3

Slide 7

& '

Control Bits and Flags

PR

PR

Divide by

TCNT clock

500 ns

μ s

μ s

μ s

Slide 8

& '

Setting the TFLG1 Register

Care must be taken when clearing the TFLG1 register.

ldyThe following works:

TFLG1 = 0x01;

staa $23,Yldaa #$

ldxThe following does not:

TFLG1 |= 0x01;

bset $23,X,$

4

& '

% $

Period Measurement

Resolution

of a period measurement is the smallest

change in period that can be detected.

Resolution of TCNT is from 500ns to 4

μ s.

Resolution is also the units of measurement.

Precision

is the number of separate and distinguishable

measurements.

Precision of TCNT is 65,536 different periods (16-bit).

Range

is min and max values that can be measured.

overflows, and when there is no period.Good measurement systems should detect under and

& '

% $

Period Measurement Example

7

Slide 15

& '

Period Measurement Resolution

Component

Process the interrupt (cycles,

μ s)

μ s

Execute the entire handler (cycles,

μ s)

μ s

Minimum period (cycles,

μ s)

μ s

Period (

μ s)

Cycles/interrupt

Time in handler (%)

P

3200/P

Slide 16

& '

Initialization for Period Measurement

Period rmb

;units 500 ns

First

rmb

;TCNT at first edge

Done

rmb

;set each rising

Init sei

;make atomic

ldaa TCTL

;Old value

anda #$CF

;Clear EDG1B=

oraa #$

;EDG1BA =

staa TCTL

;on rise of PA

ldd

TCNT

std

First

;init global

clr

Done

8

& '

% $

Initialization for Period Measurement (cont) ldaa #$

;clear IC1F

ldaa TMSK1staa TFLG

;Old value

oraa #$

;IC1I=

staa TMSK

;Arm IC1F

cli

;enable

rts

& '

% $

Initialization for Period Measurement in C

unsigned int Period;

// units of 500 ns

unsigned int First;

// TCNT first edge

unsigned char Done;

// Set each rising

void Ritual(void){

asm(" sei");

// make atomic

First = TCNT;TCTL2 = (TCTL2&0xCF)|0x10; // rising

// first will be wrong

Done=0;

// set on subsequent

TFLG1 = 0x04;

// Clear IC1F

TMSK1 |= 0x04;

// Arm IC

asm(" cli");}

9

Slide 19

& '

ISR for Period Measurement

IC1Han ldaa #$

;clear IC3F

[2]

lddstaa TFLG1 ;Acknowledge [4]

TIC

[5]

subd First

[6]

std

Period

[5]

ldd

TIC

[5]

std

First

[5]

ldaa #$FF

;set flag

[2]

staa Done

[4]

rti

[12]

org

$FFEE

fdb

IC1Han

Slide 20

& '

ISR for Period Measurement in C

void TIC1handler(void){ #pragma interrupt_handler TIC1handler()

First=TIC1;Period=TIC1-First;

// Setup for next

TFLG1=0x04;

// ack by clearing IC1F

Done=0xFF;}

10

& '

% $

Initialization for 32-Bit Period Measurement

void Ritual(void){unsigned char Mode;unsigned int Count;unsigned int First; unsigned int MsPeriod,LsPeriod;

asm(" sei");

// make atomic

TFLG1 = 0x04;

// Clear IC1F

TMSK1 |= 0x04;

// Arm

IC

TCTL2 = (TCTL2&0xCF)|0x10;

// rising

TFLG2 = 0x80;

// Clear TOF

TMSK2 |= 0x80;

// Arm TOF

asm(" cli"); }Mode=0;

& '

% $

Input Capture ISR for Period Measurement

void TIC1handler(void){ #pragma interrupt_handler TIC1handler()

if(Mode==0){

if(((TIC1&0x8000)==0)First = TIC1; Count=0; Mode=1;

&&(TFLG2&0x80)) Count--;

} else {

if(((TIC1&0x8000)==0)

&&(TFLG2&0x80))Count++;

TMSK1=0x00; TMSK2=0x00;} // Disarmif (TIC1<First) MsPeriod--;LsPeriod=TIC1-First;MsPeriod=Count; Mode=2;

TFLG1=0x04;}

// ack, clear IC1F

13

Slide 27

& '

Timer Overflow ISR for 32-Bit Period Measurement

void TOhandler(void){ #pragma interrupt_handler TOhandler()

if(Count==65535){ // 35 minutesCount++;TFLG2=0x80;

TMSK2=0x00;TMSK1=0x00;MsPeriod=LsPeriod=65535;

// Disarm

Mode=2;}}

Slide 28

& '

Measure Resistance Using Pulse Width

14

& '

% $

Initialization for Gadfly Pulse-Width Measurement

; B=PB7,

Q=PA1/IC

Init

ldx

#$1000 ;I/O registers

ldaa #$

;gadfly

rtsstaa $22,X ;TMSK1 IC2I=

& '

% $

Gadfly Pulse-Width Measurement in Assembly

Rising equ ; return Reg D as R in Kohm

;First TCNT

Meas

ldx

#$1000 ;I/O registers

ldaa #$

;Rising edge

staa $21,X

;Set TCTL

bclr $23,X,$FD

;IC2F=

bclr $04,X,$

;PB7=

bset $04,X,$

;PB7=

First

brclr $23,X,$02,First

;Wait for first rising edge

ldy

$12,X

;TCNT at rising

ldaa #$

;Falling edge

staa $21,X

;Set TCTL

15

Slide 31

& '

Gadfly Pulse-Width Measurement in Assembly (cont)

bclr $23,X,$FD

;IC2F=

pshy

;Save on stack

;Wait for next falling edgeSecond brclr $23,X,$02,Second

ldd

$12,X ;TCNT at falling

subd Rising,Ytsy

;RegD=pulse width 1000 to 2000 cyc

subd #

;0<=R<=1000Kohm

rtspuly

Slide 32

& '

Gadfly Pulse-Width Measurement in C

void Init(void){ TMSK1=0x00;}

// no interrupts

unsigned int Rising;unsigned int Measure(void) { TFLG1=0x02;TCTL2=(TCTL2&0xF3)|0x04; // Rising edge

// clear IC2F

PORTB|=0x80;PORTB&=0x7F;

// rising edge on PB

Rising=TIC2;while(TFLG1&0x02==0){}; // wait rise

// TCNT at rising edge

TFLG1=0x02;TCTL2=(TCTL2&0xF3)|0x08; // Falling edge

// clear IC2F

return(TIC2-Rising-1000); }while(TFLG1&0x02==0){}; // wait fall

16

& '

Pulse-Width Measurement Using Two Channels

unsigned int PW;

// units of 500 ns

unsigned char Done;

// Set each falling

void Ritual(void){

asm(" sei");

// make atomic

TCTL2=(TCTL2&0xCF)|0x20;

// falling edges of IC1, TCNT->TIC

TCTL2=(TCTL2&0xF3)|0x04;

// rising edges of IC2, TCNT->TIC

Done=0;

// set on the falling edge

TFLG1 = 0x04;

// Clear IC1F

TMSK1|= 0x04;

// Arm IC1, not IC

asm(" cli");}

& '

Pulse-Width Measurement Using Two Channels

void TIC1handler(void){ #pragma interrupt_handler TIC1handler()

TFLG1=0x04;} // ack by clearing IC1FDone=0xFF;PW=TIC1-TIC2; // time from rise to fall

19