-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcomp.comp
More file actions
33 lines (29 loc) · 1.06 KB
/
comp.comp
File metadata and controls
33 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
component comp "Two input comparator with hysteresis";
pin in float in0 "Inverting input to the comparator";
pin in float in1 "Non-inverting input to the comparator";
pin out bit out "Normal output. True when *in1* > *in0* (see parameter *hyst* for details)";
pin out bit equal "Match output. True when difference between *in1* and *in0* is less than *hyst*/2";
param rw float hyst=0.0 """Hysteresis of the comparator (default 0.0)
With zero hysteresis, the output is true when *in1* > *in0*. With nonzero
hysteresis, the output switches on and off at two different values,
separated by distance *hyst* around the point where *in1* = *in0*.
Keep in mind that floating point calculations are never absolute
and it is wise to always set *hyst* if you intend to use equal """;
option period no;
function _ "Update the comparator";
license "GPL";
author "Jeff Epler";
;;
FUNCTION(_) {
double tmp = in1 - in0;
double halfhyst = 0.5 * hyst;
if(tmp < -halfhyst) {
out = 0;
equal = 0;
} else if(tmp > halfhyst){
out = 1;
equal = 0;
} else {
equal = 1;
}
}