Comparator - tracking the high low condition more robustly.


Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default Comparator - tracking the high low condition more robustly.

    I've set a PIC Internal comparator up to interrupt when a signal goes below a preset VRef level.

    Here's what happens in the interrupt routine...

    (in the code below, 'above_threshold' is a variable to monitor whether comparator is above or below the preset Vref threshold)

    Code:
    Comp1_Int:
    @ INT_DISABLE CMP1_INT
    If above_threshold = 1 then ; 
    above_threshold = 0             
    else 
    above_threshold = 1
    endif
    
    @ INT_ENABLE CMP1_INT
    @ INT_RETURN
    Essentially it's toggling between a 1 or a 0 each time the comparator interrupts. (btw: please excuse the n00b type coding - is there a more elegant way to toggle a variable?)

    Have I got the jist of how this should be approached? I'm actually now thinking as I've typed this out, that maybe the comparator only trips/interrupts in one direction?

    It seems to be working, but occasionally it gets 180 degrees out of sync ...ie if the signal is above threshold it's variable is a 0 (wrong!) & if below it's a 1 (also wrong!)

    How can I make this routine more robust?
    Last edited by HankMcSpank; - 4th February 2011 at 10:16.

Members who have read this thread : 0

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

Posting Permissions

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