AVR Assembler (Mega8) - Test if a value has changed more than a given amount

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
Reply to
Mike Warren
Loading thread data ...

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
Reply to
Mike Warren

how about this:

sub r17, r16 ; diff = new - old brcc new_bigger ; neg r17 ; if (new < old) diff = old - new new_bigger: cpi r17, 6 ; if (diff >= 6 ) brsh HasChanged ; has changed

Reply to
Arlet Ottens

That looks great. Thank you.

--
-Mike
Reply to
Mike Warren

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.