Suppose we wanted to measure the period of an arbitrary square wave.
Create a firstEdge flag.
Initialize stack, interrupts, etc…
; 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