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

how add hysteresis in my code
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tanveerriaz



Joined: 04 May 2012
Posts: 7

View user's profile Send private message

how add hysteresis in my code
PostPosted: Sun Apr 28, 2013 12:19 am     Reply with quote

how i add 5v hysteresis in this code

Code:
  while(1)
{
 
   set_adc_channel(0);
    delay_ms(1);
   adc_u=read_adc();
 
  if (adc_u>= 170 && adc_u<= 240)
output_high(pin_a5);

else
output_low(pin_a5);
 
   
    }
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Apr 28, 2013 1:31 am     Reply with quote

The state of the output pin is used to modify the reference values.

Mike
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun Apr 28, 2013 8:47 am     Reply with quote

Hi,

Mike's answer is spot-on, but it may not be clear what he means? Basically, to add 'hysteresis', you've got to adjust the shift point depending on the current state. As an example, lets suppose to want to change the output state at 170. When you are below that value, transition will occur at 170, but when you are above that value, transition should occur at a slightly lower value, say 165. In this way, you won't constantly toggle the output if the input hovers about a single, fixed transition point.

Did I make this more clear, or just muddy the waters?

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Sun Apr 28, 2013 1:51 pm     Reply with quote

It is difficult to have traditional hysteresis, since you are testing two thresholds at once. However for a single threshold, something like:
Code:

int1 state=0;
int8 thresholds[2] = {170,165};

   set_adc_channel(0); //only needs to be done once...
   while (TRUE)
   {
      delay_ms(1);
      adc_u=read_adc();

       if (adc_u>thresholds[state])
       {
           output_high(PIN_A5);
           state=1;
       {
       else
       {
           output_low(PIN_A5);
           state=0;
       {
    }

When the output is turned on, it lowers the threshold needed to stay on, and vice versa.

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Apr 28, 2013 3:17 pm     Reply with quote

I don't know exactly what you're trying to do. (You've not explained)

It appears, when ADC reading is between thresholds, you want pin_a5 high, else low.

Basically:-

If pin_a5 is low, you make the gap between the thresholds narrower, i.e. raise the lower threshold and reduce the upper one.
If pin_a5 is high, you make the gap between the thresholds wider, i.e. reduce the lower threshold and raise the upper one.

The shift required in the thresholds depends on your noise levels, and lots of other details you're keeping from us.

John's explained the rationale, Ttelmah's shown you how, for the more usual single threshold case.

Mike
necati



Joined: 12 Sep 2003
Posts: 37
Location: istanbul

View user's profile Send private message

adc
PostPosted: Sun Apr 28, 2013 4:12 pm     Reply with quote

while(1){

set_adc_channel(0);
delay_ms(1);
adc_u=read_adc();

if (adc_u>= 170 || adc_u<= 240){output_high(pin_a5);
else{output_low(pin_a5);}
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Re: adc
PostPosted: Mon Apr 29, 2013 2:41 pm     Reply with quote

necati wrote:
while(1){

set_adc_channel(0);
delay_ms(1);
adc_u=read_adc();

if (adc_u>= 170 || adc_u<= 240){output_high(pin_a5);
else{output_low(pin_a5);}
}

Should produce interesting results.

Mike
tanveerriaz



Joined: 04 May 2012
Posts: 7

View user's profile Send private message

PostPosted: Mon Apr 29, 2013 8:24 pm     Reply with quote

i make a man voltage protecution circuit on a relay 170v to 240 voltage.
when voltage low 170 and up to 240 off the load. i need tune code for avoid chattering of relay.
hare is my code
Code:


#include <16F676.h>
#device adc=10
#FUSES NOWDT ,INTRC_IO ,NOMCLR  ,NOBROWNOUT   
#use delay(clock=4000000)
#use fast_io(c)


int16 adc_u;


void main()
{
      setup_adc_ports(sAN0|VSS_VREF);
      setup_adc(ADC_CLOCK_INTERNAL);
 
      setup_comparator(NC_NC);
      set_tris_a(0xff);
      port_a_pullups(0x38);
      set_tris_c(0);
     
delay_ms(1000);
   output_high(pin_a4);
 delay_ms(1000);
 delay_ms(1000);
 output_low(pin_a4);

  while(1)
{
 
   set_adc_channel(0);
   delay_us(200);
   adc_u=read_adc();
 
  if (adc_u>= 170 && adc_u<= 240)
output_high(pin_a5);

else
output_low(pin_a5);
 
   

    }
 
    }
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Apr 30, 2013 1:41 am     Reply with quote

1) Do you have real hardware?
2) You have to assess the change in trip levels needed to stop chattering.
3) Is your input AC or DC?
4) Maybe we need to see your schematic.
5) Ttelmah has shown a way to do it.
6) You need to extend his idea to 2 thresholds.
7) Necati's suggestion is interesting.
8) When you're in-band the thresholds need to be further apart than when you're out-of-band.
9) Better indenting makes code more readable, and easier to follow.

Mike
dorinm



Joined: 07 Jan 2006
Posts: 38

View user's profile Send private message

PostPosted: Tue Apr 30, 2013 3:19 am     Reply with quote

Code:
if(adc_u<230 && adc_u>190)
output_high(pin_a5);
else if(adc_u>240 || adc_u<170)
output_low(pin_a5);


I assume that normal voltage should be between 190 and 230, and under 170 or over 240 is out of specs ...so you have hyst. a 230-240 and 170-190
tanveerriaz



Joined: 04 May 2012
Posts: 7

View user's profile Send private message

PostPosted: Tue Apr 30, 2013 10:33 pm     Reply with quote

I need a circuit to protect my house electrical equipment
from main power up and down.
I have a prototype board for check my circuit.
I use 230v ac > dc and a resistance divider network to down voltage for adc input.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed May 01, 2013 1:36 am     Reply with quote

tanveerriaz wrote:
I need a circuit to protect my house electrical equipment
from main power up and down.
I have a prototype bord for check my circuit.
I use 230v ac > dc and a resistance divider network to down voltage for adc input.

I/we need to see your schematic.

Mike
dorinm



Joined: 07 Jan 2006
Posts: 38

View user's profile Send private message

PostPosted: Wed May 01, 2013 3:02 pm     Reply with quote

tanveerriaz wrote:
I need a circuit to protect my house electrical equipment
from main power up and down.
I have a prototype board for check my circuit.
I use 230v ac > dc and a resistance divider network to down voltage for adc input.

...so you are so faaar away... actually you don't have any idea on "how" to do it and you are asking here Smile)... you have just discovered that pic only accepts up to Vdd DC on analog input so you rectified (/filtered?) the mains; what you don't see is that if you're not filtering, a simple ADC won't help, and if you filter, the mains could actually go well to 0V long time before you will know;

what are you trying to achieve is a basic task for comparators, yet the analog interface is not straightforward; I suggest that you first should develop some (analog) electronic skills (theory) before you go forward.
tanveerriaz



Joined: 04 May 2012
Posts: 7

View user's profile Send private message

PostPosted: Thu May 02, 2013 8:17 pm     Reply with quote

Here is my uncompleted work. When its complete I show it here.
I add a 3310 lcd to show voltage.
http://picturepush.com/public/12835620
http://picturepush.com/public/12835639
http://picturepush.com/public/12837662
http://picturepush.com/public/12837667
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri May 03, 2013 2:26 am     Reply with quote

A few comments.

1) You're not showing values for components, so we can't make any judgement.
2) The LEDs are wrong way round as shown on schematic.
3) There's a risk of destroying the relay driver device.
4) I hope you understand the risks working on live mains.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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