How to pass parameters to subcircuits in LTSpice with the include statement

ltspicesubcircuit

Let's say I create a block with resistors R1 and R2. I want to use this block as a subcircuit in other designs. The typical way to pass in the values for R1 and R2 is to right-click on the block and type in the values as shown in the image below.

How do I pass the parameters to the block X1 as a spice command instead of right-clicking and typing the parameters manually? I would like to put all my parameters in a file and use the .include statement to import the values.

Best Answer

In the way you constructed your block with the two resitors, in that same way you can pass parameters to subcircuits using your block:

* a subcircuit/block with two resistors whose
* values need to be defined externally

.subckt r2d2 a b
r1 a 1 {r1}
r2 1 b {r2}
.ends r2d2

* another subcircuit that makes use of r2d2, and
* who needs, in turn, the value for its own,
* internal resistor, defined externally:

.subckt 3po x y
xu1 x 1 r2d2 r1={p} r2=1k
r1 1 y {q}
.ends 3po

* using 3po in some schematic/netlist with
* externally defined values :

xu1 m n 3po p=2k q={a}
.param a=3k

As you can see, not all subcircuit parameters need to be defined in .param statements, the same way not all need to be defined inside the instance of the subcircuit; they can be mixed. Using an .inc can be done the usual way from this point forward.


Just to be sure it's clear: subcircuits are like a scope and anything that happens inside a subcircuit stays within that subcircuit, unless it's made available to the outside from within the subcircuit. That means either passing parameters to the subcircuit (what you're showing in your picture) or external pins that make available internal quantities (voltage, current) to the outside world.

Also see this answer.

If your purpose is to have the .param statemens residing in an external file to be included with .inc then the only way to change them is through assignment with {}, which implies editing by hand -- one or another. The only other shortcut to defining each parameter, separately, is to have all the parameters related through a function, in which case, instead of a long .param line you can have a .func. The values will then be f(1), f(2), etc, and the function .func f(x) {2*x+1} (for example).