Timer/Counter Subsystem Input Capture

A Dr. Taylor Tutorial

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
.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
     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
     com   firstFlag
     sts   firstFlagLoc, firstFlag
     pop   r3
     pop   r2
     pop   r1
     pop   r0
     pop   temp
     out   SREG, temp
     pop   temp


Tutorials for Other Courses

Additional Links

Site Mirror

This is a mirror to be used when http://myweb.msoe.edu/taylor/, (EDU instead of US) is not available.