From: Mike Warren on
Can someone please offer a suggestion for a way to check if a value
has changed up or down by at least some threshold. It doesn't matter
how long it takes but code size and register use are important.

I'm not sure how to handle values lower than ChangeThreshold or
higher then 255 - ChangeThreshold.

..equ ChangeThreshold = 6
..def OldValue = R16
..def NewValue = R17

Available registers R10, R11, R12, R13, R19, R24

CheckForChange:
; Determine if NewValue > OldValue + ChangeThreshold
; or NewValue < OldValue - ChangeThreshold
rjmp HasChanged


--
-Mike
From: Mike Warren on
This is what I have so far. Does it seem reasonable?

ChOld should contain the new value(ChNew) if ChNew is different to
ChOld by at least TrigThresh.

..def A = R19
..def B = R24

CheckForChange:
push A
push B

cp ChNew,ChOld
brlo CheckForLower

CheckForHigher:
ldi A,TrigThresh
add A,ChOld
; skip if overflowed
cpi A,TrigThresh+1
brlo EndCheckForChange
; test higher
cp A,ChNew
brlo HasChanged
rjmp EndCheckForChange

CheckForLower:
ldi B,TrigThresh
mov A,ChOld
sub A,B
; skip if underflowed
cpi A,255-TrigThresh
brsh EndCheckForChange
; test lower
cp ChNew,A
brlo HasChanged
rjmp EndCheckForChange

HasChanged:
mov ChOld,ChNew
EndCheckForChange:
pop B
pop A
ret

--
-Mike