Table of Contents

Timer/Counter Subsystem Input Capture

Relevant I/O Registers

WGM13 WGM12 Mode of Operation TOP Update of OCR1x TOV1 Flag Set
0 0 Normal 0xFFFF Immediate MAX
0 1 CTC OCR1A Immediate MAX
1 0 PWM, Phase and Freq Correct ICR1 BOTTOM BOTTOM
1 1 CTC ICR1 Immediate MAX

Sample Code

Measure Waveform Period with Input Capture

; ISR for input capture pin with Timer/Counter1 Subsystem
; This ISR assumes that the following data memory reservations have been made:
;   firstFlagLoc -- 1 byte
;   periodLoc -- 2 bytes
;   firstTimeLoc -- 2 bytes
timeCaptureISR:
.def temp = r16
.def firstFlag = r17
     push  temp
     in    temp, SREG
     push  temp
     push  r0
     push  r1
     push  r2
     push  r3
     lds   firstFlag, firstFlagLoc
     tst   firstFlag
     brne  secondEdge
     in    r0, ICR1L                ; Must read low byte first
     in    r1, ICR1H
     sts   firstTimeLoc, r0
     sts   firstTimeLoc+1, r1
     rjmp  tcISRdone
secondEdge:
     in    r2, ICR1L
     in    r3, ICR1H
     lds   r0, firstTimeLoc
     lds   r1, firstTimeLoc+1
     sub   r2, r0                   ; Subtract low byte
     sbc   r3, r1                   ; Subtract high byte
     sts   periodLoc, r2
     sts   periodLoc+1, r3
tcISRdone:
     com   firstFlag
     sts   firstFlagLoc, firstFlag
     pop   r3
     pop   r2
     pop   r1
     pop   r0
     pop   temp
     out   SREG, temp
     pop   temp
     reti

Applications