1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

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

Discussion in 'Embedded' started by Mike Warren, May 7, 2008.

  1. Mike Warren

    Mike Warren Guest

    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 Warren, May 7, 2008
    #1
    1. Advertisements

  2. Mike Warren

    Mike Warren Guest

    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 Warren, May 7, 2008
    #2
    1. Advertisements

  3. Mike Warren

    Arlet Ottens Guest

    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
     
    Arlet Ottens, May 7, 2008
    #3
  4. Mike Warren

    Mike Warren Guest

    That looks great. Thank you.
     
    Mike Warren, May 7, 2008
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.