Sonsivri

Electronics => Projects => Topic started by: max on June 01, 2013, 10:02:39 22:02



Title: Software implentation of comparator with hysteresis
Post by: max on June 01, 2013, 10:02:39 22:02
Hi friends,

I need to develop a led voltmeter to display 0-100v with 10v steps on 10 leds, using 16f676 uc,
to avoid the flickering of leds at change over point I need to implement a hysteresis function with
+/- 0.5v or +/- 1.0v hysteresis, need the idea how to implement this function in C language.
 
Regards


Title: Re: Software implentation of comparator with hysteresis
Post by: solutions on June 02, 2013, 12:40:49 00:40
Did you ever think of only updating the LEDs every second or two?


Title: Re: Software implentation of comparator with hysteresis
Post by: bigtoy on June 02, 2013, 01:25:21 01:25
Here's how I do it. Perhaps not the most elegant, but it gets the job done.

You have 2 thresholds: a high threshold and a low threshold. Plus you need to know if the LED is currently on or off. Pseudocode:

#define HIGH_THRESHOLD  1.1
#define LOW_THRESHOLD  0.9

if ( (led_state == ON) && (voltage < LOW_THRESHOLD) ) {
    led_off();
    led_state = OFF;
}
else if ( (led_state == OFF) && (voltage > HIGH_THRESHOLD) )
     led_on();
     led_state = ON;
}

In other words, if the LED is currently on but the voltage has dropped below the low threshold, turn the LED off.
If the LED is currently off and the voltage has risen above the high threshold, turn the LED on.
Otherwise, leave the LED alone.

You could also do this with a state machine (a state for the LED on, and a state for the LED off) but IMO the code above is simpler.

Solutions suggestion of just running the thing more slowly is also a good idea, if your application can tolerate the delayed response.


Title: Re: Software implentation of comparator with hysteresis
Post by: solutions on June 02, 2013, 08:21:43 08:21
If it can tolerate f'ing up the voltage reading it can tolerate the delay, IMO


Title: Re: Software implentation of comparator with hysteresis
Post by: bajrang on June 03, 2013, 07:00:56 07:00
Dear friend,
Here is a piece of code I wrote for the software hysteresis:
Again:    acttemp = ADIn 0   
      settemp = ADIn 1   
      stemp=quant*settemp
      atemp=quant*acttemp
      atemp1=(atemp+5)/0.20
      stemp1=(stemp+5)/0.20
      Print At 1,1, "Actual:"      
      Print At 1,10,Dec2 atemp1," C"
      Print At 2,1, "Set   :"   
      Print At 2,10,Dec2 stemp1," C"
      If PORTC.0=1 Then
      comp=settemp+width
      Else
      comp=settemp-width
      EndIf
      
      If acttemp < comp Then
      PORTC.0=1
      
      Else
      PORTC.0=0
      EndIf
            
      GoTo Again   
I hope it help.
regards,
bajrang


Title: Re: Software implentation of comparator with hysteresis
Post by: Gallymimu on June 05, 2013, 05:15:10 05:15
How about you use software PWM to give an intensity gradient so that you can see within those 10V steps.

i.e. 54 volts is 5 LEDs on 100% and 6th on 40%


Title: Re: Software implentation of comparator with hysteresis
Post by: Sparks on August 07, 2013, 01:13:45 01:13
Greetings!
What you need in this case is a first order digital low pass filter. Sounds complicated, but actually is quite simple. If you still haven't found a solution, p.m. me and I'll post one of my ready to use C functions for this.




Title: Re: Software implentation of comparator with hysteresis
Post by: Gallymimu on August 07, 2013, 04:08:00 04:08
Greetings!
What you need in this case is a first order digital low pass filter. Sounds complicated, but actually is quite simple. If you still haven't found a solution, p.m. me and I'll post one of my ready to use C functions for this.




how about you just post it instead of saying that you'll post it :) 

 // Return RC low-pass filter output samples, given input samples,
 // time interval dt, and time constant RC
 function lowpass(real[0..n] x, real dt, real RC)
   var real[0..n] y
   var real α := dt / (RC + dt)
   y[0] := x[0]
   for i from 1 to n
       y := α * x + (1-α) * y[i-1]
   return y

Here's a recursive implementation
http://www.edn.com/design/integrated-circuit-design/4323639/8-bit-microcontroller-implements-digital-lowpass-filter