| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| Guest 
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				| Making hysteresis to A/D. |  
				|  Posted: Fri Dec 19, 2008 9:11 am |   |  
				| 
 |  
				| Using 8 bit A/D 0-255, want to use a pot. as a selector. 
 I want 4 step and don't want toggling between the step, therefore I want to use some hysteresis maybe 5-10 count.
 
 Step without hysteresis.
 0-63  step 1
 64-127 step2
 128-191 step3
 192-255 step4
 
 How to make the smartest implementation?
 |  | 
	
		|  | 
	
		| libor 
 
 
 Joined: 14 Dec 2004
 Posts: 288
 Location: Hungary
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Dec 19, 2008 10:28 am |   |  
				| 
 |  
				| I would just insert some 'neutral zones' between the 'steps'. 
 0-59 valid step 1
 60 - 67  neutral zone
 68-123 valid step2
 124-128 neutral zone
 129-191 valid step3
 ...etc
 
 a reading in any neutral zone would result a no-change in the 'step'. (the previous state remains selected)  a simple sequence of   if()...  structures will do.
 However upon power-on you should initialize the step to the closest valid range.
 |  | 
	
		|  | 
	
		| Guest 
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				|  |  
				|  Posted: Fri Dec 19, 2008 11:01 am |   |  
				| 
 |  
				| Like this... 
 
  	  | Code: |  	  | void readad(){ const int8 hyst=5;
 int8 res;
 
 res=(int8)read_adc()&~0x03; //cutoff 2 bit
 
 if (res>0        && res<42+hyst){}  else
 if (res>42-hyst  && res<85+hyst){}  else
 if (res>85-hyst  && res<127+hyst){} else
 ........
 }
 
 | 
 |  | 
	
		|  | 
	
		| RLScott 
 
 
 Joined: 10 Jul 2007
 Posts: 465
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Dec 19, 2008 1:20 pm |   |  
				| 
 |  
				|  	  | Anonymous wrote: |  	  | Like this... 
 
  	  | Code: |  	  | void readad(){ const int8 hyst=5;
 int8 res;
 
 res=(int8)read_adc()&~0x03; //cutoff 2 bit
 
 if (res>0        && res<42+hyst){}  else
 if (res>42-hyst  && res<85+hyst){}  else
 if (res>85-hyst  && res<127+hyst){} else
 ........
 }
 
 | 
 | 
 No, I'm afraid this code will not implement hysteresis.  The first "if" guarantees that the second "if" will never see a value of res<47, so there is no point in looking for "res>42-5".  To implement hystersis, there must be some form of static memory of the previous range you were in, such as the variable "Range" in the following:
 
  	  | Code: |  	  | void readad(){
 const int8 hyst=5;
 int8 res;
 
 res=(int8)read_adc()&~0x03; //cutoff 2 bit
 
 if (res<42-hyst)      Range = 0;
 else if(res<42+hyst)  Range = (Range<1) ? 0 : 1;
 else if(res<85-hyst)  Range = 1;
 else if(res<85+hyst)  Range = (Range<2) ? 1 : 2;
 else if(res<127-hyst) Range = 2;
 else if(res<127+hyst) Range = (Range<3) ? 2 : 3;
 . . . .
 switch(Range)
 {
 case 0:  {} break;
 case 1:  {} break;
 case 2:  {} break;
 case 3:  {} break;
 . . . .
 }
 
 
 | 
 _________________
 Robert Scott
 Real-Time Specialties
 Embedded Systems Consulting
 |  | 
	
		|  | 
	
		| Guest 
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				|  |  
				|  Posted: Sat Dec 20, 2008 2:47 pm |   |  
				| 
 |  
				| Nice code, thanks :-) |  | 
	
		|  | 
	
		|  |