Window Comparator and Direction


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2021
    Location
    Illinois, USA
    Posts
    10

    Default Window Comparator and Direction

    I know this has been asked and I looked at many of the replies but at 75 years young I am having a bit of trouble wrapping my head around the best way to do this. I have an adc value that I am trying to maintain in a window. I am comparing an adc reading to a reference and trying to decide if it is in a +/- 40 count range. If not, I need to determine the direction to run, i.e. Vadc - Vref is positive or negative result. What I am having trouble with is the possibility of the answer being negative (PBP returning 65536-value) and therefore looking greater than the reference because of the way negative numbers are handled and getting a wrong direction. I suppose I could use Long variables since I am using a PIC18F but I am just looking to understand the logic.
    Thanks for helping with my post-retirement education.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Window Comparator and Direction

    maybe like this , the msb of a var is the sign bit
    to convert between unsigned an signed use abs() or vice versa var=~var+1

    Code:
    Vadc var word
    Vref var word
    vDif var word
    aDif var word
    
    
    vDif = Vref -  Vadc ;signed difference
    adif= abs(Vref -  Vadc);absoloute unsigned difference
    
    
    if vDif.15 then
    do  negative stuff
    else
    do positive stuff
    endif
    Warning I'm not a teacher

  3. #3
    Join Date
    Dec 2021
    Location
    Illinois, USA
    Posts
    10


    Did you find this post helpful? Yes | No

    Default Re: Window Comparator and Direction

    Thanks Richard,

    it's the format of the ABS that has been confusing me. According to what I read in the manual, the ABS function for a word variable returns 65536 - value. So if I had a negative number and did the ABS() of that, it could return a number that is larger than the number I was taking the ABS value of. If my number was -40 for example. That would be 1111111111011000 in binary. (-40) If I do the ABS value function I think it returns 0000000000101000 which is 40! Wow, I was subtracting from 65535 not 65536. The light just went on! Thanks again Richard, sorry to ask such a dumb question!


  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Window Comparator and Direction

    If your ADC is the ADC inside the PIC then it is a possitive value.

    If you use external ADC then I guess it can be negative also.

    Ioannis

  5. #5
    Join Date
    Dec 2021
    Location
    Illinois, USA
    Posts
    10


    Did you find this post helpful? Yes | No

    Red face Re: Window Comparator and Direction

    Quote Originally Posted by Ioannis View Post
    If your ADC is the ADC inside the PIC then it is a possitive value.

    If you use external ADC then I guess it can be negative also.

    Ioannis
    I am using the internal ADC on the PIC18F45K80 but the question I was having was determining if I was inside a window of +/- 40 counts. I was concerned that RefPosition - CurPosition could give me a negative result but I see how to handle it now. Thanks for all the help.

Similar Threads

  1. Replies: 2
    Last Post: - 31st May 2013, 18:19
  2. Check within a 16 bytes window
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 29th November 2012, 11:54
  3. trying to do a scroll window
    By AvionicsMaster1 in forum Test Area
    Replies: 0
    Last Post: - 7th September 2011, 14:00
  4. Button Push within 3 second Window
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2005, 10:06
  5. Time window
    By Franko in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 9th August 2005, 16:42

Members who have read this thread : 2

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts