AtoD window test Demo

 Window Comparator using 16F876 - Friday, August 13, 2010 3:17 AM ( #1 ) 
Hi  

I have made a  window comparator to determine if a voltage is within a certain tolerance. 

Basically I sit in a loop, do A/D conversion, and switch on one of three LED's Hi, Lo or Ok 

I have two constant values, Upper Limit and Lower Limit 

Then in psuedo code, i do the following to turn on the correct LED: 

If A/D Result > Upper Limit 
     Turn On Hi Led 
Elseif A/D Result < Lower Limit 

     Turn On Lo LED 

Else 

     Turn On OK Led 

Endif 



The problem is that there may be a little bit of ripple, or something unstable with my A/D measurements, and if the voltage is near either the upper or lower limit, then TWO led's come on - i.e. it is I assume the result of successive conversions  falling in and out of the OK band.  


I thought of perhaps trying to take two measurements add them together, shift right (divide by two) and do this a couple of times before processing the LEDS? I am using the full 10Bits resolution, so even the 16-Bit subtraction to determine if Result > Limit etc is quite painful. Also I am running 5 channels each with 3 Leds. The voltages measured are from a SMPS, so I assume there will be some ripple there too. 


Is there perhaps a way to implement some sort of hysteresis so that if the Hi Led was on, it must drop like 2 a/d steps below the limit before the OK LED will turn on? If that makes sense? But if so how do I do that? 


Any Ideas? 



I've added Hysterisis to your psuedo code. 

If (  A/D Result +Hysterisis )  > Upper Limit 
     Turn On Hi Led 
     Hysterisis = A 

Elseif (  A/D Result +Hysterisis )< Lower Limit 

     Turn On Lo LED 
     Hysterisis = B 

Else 

     Turn On OK Led 
     Hysterisis = C 
  

Endif 


Will this work if you pick the correct value of A,B and C ? 

They could be positive or negative. 

Doug 

This impliments an AtoD window and uses a snapping hysterisys to provide a clean switch.

 var h =0;
 var A=10;
 var B=-10;
 var C=0;

function test( ip ){


  ledState="ok"; 

  ip = ip + Math.random()*9;
   
  if ( ip + h > high ) {
    ledState="HI";    
    h = A; 	
  } else {
 
    if ( ip + h < low ) {
      ledState="low";    
      h = B; 	
    } else {
      ledState="ok"; 
      h = C; 	
    }
  }
  self.document.write(""+ledState+","+h+","+ip+","+"\n
") }