On Thu, 23 Mar 2000, Tony Nixon wrote:
{Quote hidden}> Reginald Neale wrote:
> >
> > Esteemed PIC Gurus:
> >
> > Does anyone have a favorite way to do a window comparison on an A/D
> > result? Seems to me there was one here a week ago, but it has eluded
> > me.
> >
> > What I want to do is to insert code in an existing loop that will set
> > an alarm output pin if the A/D result falls outside the window; set
> > another pin high if it is below the hard-coded lower limit, set a
> > different pin if it is above the upper limit, and continue on without
> > setting any of the three if the result is within the window.
> >
> > TIA,
> > Reg Neale
>
> movf ADres,w
> sublw UpLimit
> btfss status,c
> goto TooHigh
>
> movlw LoLimit
> subwf ADres,w
> btfss status,c
> goto TooLow
>
If you're comparing against constants, why not do this:
movf ADres,w
sublw UpLimit ;W = UpLimit - ADres
skpc
goto TooHigh
sublw UpLimit-LoLimit ;W = ADres - LoLimit
skpc
goto TooLow
(You save a whole instruction!)
Example:
ADres LoLimit UpLimit (Up-AD) (AD-Lo)
------------------------------------------------------
9 4 10 10-9=>1,C=1 6-1=>5,C=1
11 4 10 10-11=>ff,C=0 ----
2 4 10 10-2=>8,C=1 6-8=>fe,C=0
C-Code:
GoldiLocks(unsigned char ADres)
{
if(ADres>HiLimit)
TooHigh();
else if(ADres < LoLimit)
TooLow();
else
JustRight();
}
Scott