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)
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?)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
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?
Bookmarks