View previous topic :: View next topic |
Author |
Message |
tanveerriaz
Joined: 04 May 2012 Posts: 7
|
how add hysteresis in my code |
Posted: Sun Apr 28, 2013 12:19 am |
|
|
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
|
|
Posted: Sun Apr 28, 2013 1:31 am |
|
|
The state of the output pin is used to modify the reference values.
Mike |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sun Apr 28, 2013 8:47 am |
|
|
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
|
|
Posted: Sun Apr 28, 2013 1:51 pm |
|
|
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
|
|
Posted: Sun Apr 28, 2013 3:17 pm |
|
|
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
|
adc |
Posted: Sun Apr 28, 2013 4:12 pm |
|
|
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
|
Re: adc |
Posted: Mon Apr 29, 2013 2:41 pm |
|
|
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
|
|
Posted: Mon Apr 29, 2013 8:24 pm |
|
|
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
|
|
Posted: Tue Apr 30, 2013 1:41 am |
|
|
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
|
|
Posted: Tue Apr 30, 2013 3:19 am |
|
|
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
|
|
Posted: Tue Apr 30, 2013 10:33 pm |
|
|
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
|
|
Posted: Wed May 01, 2013 1:36 am |
|
|
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
|
|
Posted: Wed May 01, 2013 3:02 pm |
|
|
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 )... 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
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri May 03, 2013 2:26 am |
|
|
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 |
|
|
|