CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Making hysteresis to A/D.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Making hysteresis to A/D.
PostPosted: Fri Dec 19, 2008 9:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 19, 2008 10:28 am     Reply with quote

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








PostPosted: Fri Dec 19, 2008 11:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 19, 2008 1:20 pm     Reply with quote

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








PostPosted: Sat Dec 20, 2008 2:47 pm     Reply with quote

Nice code, thanks :-)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group