3 byte challenge for you

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello:

I have two bytes say ADC, & OLD_ADC. I'd like to compare them to see if they differ by more than value HYST (a constant) & take action if the difference (+/-) is more than HYST. Thus I need to take an absolute value of the difference. Any thoughts on an efficient method?

Hoyt

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just do it one step at a time:

Subtract one ADC value from the other.
Swap sign if the result is negative.
Compare with HYST.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please select a language and reveal your choice to us.

C? ASM? 4th?.....................

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I vote for FORTH!

adc C@ old_adc C@ - abs hys - 0>

Tom Pappano
Tulsa, Oklahoma

Tom Pappano
Tulsa, Oklahoma

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now I'm gonna have nightmares......

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the values are a single byte, it requires more than a simple subtraction of bytes to determine the difference. One way is to extend the subtraction to integer size. Here is another (in C):

if (ADC > OLD_ADC)
  return HYST > (ADC - OLD_ADC);
return HYST > (OLD_ADC - ADC);

Dave Raymond

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
      cp old_adc, new_adc  ; which is bigger? carry knows!
      mov temp, new_adc    ; don't mess with the adc value
      sub temp, old_adc    ; preserves carry
      brcs done            ; correct answer if new > old
      neg temp             ; if new < old flip result
done: cp temp, hyst

What do I win?
:P

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
return((abs(old_adc - new_adc))>hist) ;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mneary wrote:

      cp old_adc, new_adc  ; which is bigger? carry knows!
      mov temp, new_adc    ; don't mess with the adc value
      sub temp, old_adc    ; preserves carry
      brcs done            ; correct answer if new > old
      neg temp             ; if new < old flip result
done: cp temp, hyst

What do I win?
:P

I would say nothing :D
You are making temp negative no matter what, as you are branching on carry set. And you don't need the CP instruction at all, as SUB changes carry anyway, despite what you wrote in the comment.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:oops: I knew I should have simulated it. :oops:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Erm !!! Where was the challenge ? That was too easy.

Nigel

BTW I vote Pascal because I had to learn it at university and after that; ADA because everybody thought it was going to rule the programming world. (We didn't even have an ADA compiler though)