






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







& '
% $
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)
the active edge of an external signal.
measurement to get the period.
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
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 (%)
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 #$
staa TCTL
;on rise of PA
ldd
std
First
;init global
clr
Done
8
& '
% $
Initialization for Period Measurement (cont) ldaa #$
;clear IC1F
ldaa TMSK1staa TFLG
;Old value
oraa #$
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
lddstaa TFLG1 ;Acknowledge [4]
subd First
std
Period
ldd
std
First
ldaa #$FF
;set flag
staa Done
rti
org
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
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
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
bclr $04,X,$
bset $04,X,$
First
brclr $23,X,$02,First
;Wait for first rising edge
ldy
;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
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