How to use Table in ideal comparator model

Hi all,
Since I am having trouble with TI.com supplied models for (quad) comparators I am trying to use an ideal model for a comparator, but with an interpolated (so more smooth) changeover around zero input.
The comparator output is resistance very low (open collector) for in+ < in- and very high for in+ > in- . I like to use 1 Ohm and 1 MOhm for the low and high values.
I cannot find a description of the right syntax and ngspice does not accept the following:

*
* A quad comparator ngspice model
* file name: LM339.lib
.subckt LM339 2out 1out vcc+ 1in- 1in+ 2in- 2in+ 4in- 4in+ 3in- 3in+ vcc- 3out 4out
XU1A 1in+ 1in- vcc+ vcc- 1out Comparator
XU1B 2in+ 2in- vcc+ vcc- 2out Comparator
XU1C 3in+ 3in- vcc+ vcc- 3out Comparator
XU1D 4in+ 4in- vcc+ vcc- 4out Comparator
.ends
.SUBCKT Comparator 1 2 3 4 5
RComp 4 5 R = table(V(1,2), -1mV, 1, 1mV, 1M) ;Behavioural resistor.
.ends Comparator

Can someone tell me how this should be done?
Thanks a lot,
Jan

There are two issues:

ngspice is case insensitive, that is:

1m == 1M == 1e-3
1Meg == 1e6

You are using the TABLE function with 2 points only. Within -1m, 1m the reaction is obvious.

Outside of this range it is not! What will happen if ngspice just defines a straight line between the two points and extrapolates it outside of this range?

To ensure that that will not happen, you have to give another two points, to direct the extrapolation towards a constant output, like

RComp 4 5 R = table(V(1,2), -2m, 1, -1m, 1, 1m, 1Meg, 2m, 1Meg)  ; Behavioural resistor.

Thanks a lot, Holger; however ngspice still objects as follows:

Note: Compatibility modes selected: ps lt a
Circuit: KiCad schematic
Warning: Value of resistor rf4 is too small, set to 1.000000e-12
Error: no such function 'table' at line 12
from file
"C:/Users/jjmcu/OneDrive/Activator_Kicad/Project4/Spice models/LM339/LM339 with resistor table.lib
Error: ngspice.dll cannot recover and awaits to be reset or detached
Warning: can't find the initialization file spinit.

N.B. in some other example it was said that table would use (the same) constant values outside the range, but better to make sure indeed.

TABLE is not supported, but

RComp 4 5 R = PWL(V(1,2), -2m, 1, -1m, 1, 1m, 1Meg, 2m, 1Meg)  ; Behavioural resistor.

is (see manual chapter 5.1.4).

R behav
.SUBCKT Comparator 1 2 3 4 5
* RComp 4 5 R = table(V(1,2), -2m, 1, -1m, 1, 1m, 1Meg, 2m, 1Meg)  ; Behavioural resistor.
RComp 4 5 R = PWL(V(1,2), -2m, 1, -1m, 1, 1m, 1Meg, 2m, 1Meg)  ; Behavioural resistor.
.ends
XR in 0 nn out 0  Comparator
VR in 0 0
Vout out 0 -1
.control
dc VR -5m 5m 0.1m
set xbrushwidth=2
plot log10(i(Vout))
.endc 
.end

Thanks Holger, this is accepted by ngspice.
Just for your info: Table does seem to be supported, see 5.2.3 in the ngspice manual, but maybe not as I used it.
Further: in 9.0.0 after selecting a model file, the model selection is not reflected in the selection slot, though the selection works. And the analysis tab does not seem to be (always) remembered as before.
Thanks again,
Jan

I am not using NGSpice but to me, this is a “poster child” for engineering notation (1e-3 is the example) to reduce ambiguity.

1 Like