Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 10, 2016, 09:49:11 09:49


Login with username, password and session length


Pages: [1]
Print
Author Topic: Software implentation of comparator with hysteresis  (Read 4738 times)
0 Members and 1 Guest are viewing this topic.
max
Active Member
***
Offline Offline

Posts: 223

Thank You
-Given: 603
-Receive: 32


« on: June 01, 2013, 11:02:39 23: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 Offline

Posts: 1446

Thank You
-Given: 591
-Receive: 851



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

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

Posts: 153

Thank You
-Given: 132
-Receive: 197


« Reply #2 on: June 02, 2013, 02:25:21 02: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 02, 2013, 02:28:11 02:28 by bigtoy » Logged
solutions
Hero Member
*****
Offline Offline

Posts: 1446

Thank You
-Given: 591
-Receive: 851



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

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

Posts: 145

Thank You
-Given: 246
-Receive: 80


« Reply #4 on: June 03, 2013, 08:00:56 08: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 Offline

Posts: 579

Thank You
-Given: 101
-Receive: 151


« Reply #5 on: June 05, 2013, 06:15:10 06: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 Offline

Posts: 1

Thank You
-Given: 0
-Receive: 0


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

Posts: 579

Thank You
-Given: 101
-Receive: 151


« Reply #7 on: August 07, 2013, 05:08:00 05: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 Smiley 

 // 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]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC