Entire Forum This board This topic Members Entire Site
 Welcome, Guest. Please login or register.Did you miss your activation email? April 22, 2018, 11:13:43 11:13 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length
 Pages: [1]
 Author Topic: Software implentation of comparator with hysteresis  (Read 6041 times) 0 Members and 1 Guest are viewing this topic.
max
Active Member

Offline

Posts: 234

Thank You
-Given: 846

 « on: June 01, 2013, 04:02:39 16: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
 Logged

Fate arrived and made the conscious unconscious It silenced the activity of life.
solutions
Hero Member

Offline

Posts: 1770

Thank You
-Given: 616

 « Reply #1 on: June 01, 2013, 06:40:49 18:40 »

Did you ever think of only updating the LEDs every second or two?
 Logged
bigtoy
Active Member

Offline

Posts: 184

Thank You
-Given: 180

 « Reply #2 on: June 01, 2013, 07:25:21 19: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.
 « Last Edit: June 01, 2013, 07:28:11 19:28 by bigtoy » Logged
solutions
Hero Member

Offline

Posts: 1770

Thank You
-Given: 616

 « Reply #3 on: June 02, 2013, 02:21:43 02:21 »

If it can tolerate f'ing up the voltage reading it can tolerate the delay, IMO
 Logged
bajrang
Active Member

Muted
Offline

Posts: 145

Thank You
-Given: 245

 « Reply #4 on: June 03, 2013, 01:00:56 01: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
 Logged
Gallymimu
Hero Member

Offline

Posts: 664

Thank You
-Given: 148

 « Reply #5 on: June 04, 2013, 11:15:10 23: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%
 Logged
Sparks
Inactive

Muted
Offline

Posts: 1

Thank You
-Given: 0

 « Reply #6 on: August 06, 2013, 07:13:45 19: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.

 Logged
Gallymimu
Hero Member

Offline

Posts: 664

Thank You
-Given: 148

 « Reply #7 on: August 06, 2013, 10:08:00 22: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

 Logged
 Pages: [1]