LTSPICE is offering very simple and straight forward way to create a symbol and connect it to subcircuit definition.
Every subcircuit that you want to use should have corresponding schematic symbol. Symbol is a drawing, used to represent a device, described by a subcircuit or a hierarchical block. You may use existing symbol if it’s pinout and functional drawing corresponds to your subcircuit, or you may create a new drawing to represent unique subcircuit definition that you just created.
Let’s create our own library. For this example I will create a dedicated folder “Subcircuits” inside LTspiceIV directory (installation directory of LTSPICE) and place all project files into it. You don’t have to do it this way and LTSPICE is offering a couple of different ways to save your subcircuit libraries, symbol drawings and schematics. We will cover these options later in this tutorial. But for now, it is the simplest way to reference all necessary files.
In this example we will create a text file with any text editor and name it “sc_example.lib“. Save this file into “Subcircuits” folder, that you just created.
Put definition of our simple subcircuit into “sc_example.lib“. You may use LTSPICE to edit this definition. Nice feature of LTSPICE editor is color highlighting, so that you can easily distinguish between comments, subcircuit declaration (header), SPICE commands and SPICE netlist. To do this just go to File → Open, select “all files” in Files of type: filter (by default, LTSPICE will not show you files with .lib extension) and open your sc_example.lib. Place the following subcircuit definition into this file and save it. It will represent simple amplifier with two parameters, gain and input impedance, that could be modified from schematic.
*****************************************
* ------------Gain Block-----------------
* Amplifier circuit to be used together
* with drawing symbol "amp.asy"
* input pin is called "AMP_IN" in subcircuit definition
* and is pin 1 in symbol drawing netlist order
* output pin is called "AMP_OUT" in subcircuit definition
* and is pin 2 in symbol drawing netlist order
.subckt GainBlock AMP_IN AMP_OUT PARAMS: gain=10 Rin_value=50
Rin AMP_IN 0 {Rin_value}
Eamp 1 0 AMP_IN 0 {gain}
.param Rout_value=100
Rout 1 AMP_OUT {Rout_value}
.ends GainBlock
*****************************************
I will shortly describe this subcircuit.
Name of subcircuit is GainBlock. It has 2 pins, AMP_IN and AMP_OUT. These names matters only for internal subcircuit net-list. For part symbol in the schematic, AMP_IN will have meaning as number 1 in netlist order and AMP_OUT will have meaning as number 2 in netlist order. There are 2 parameters that are declared and assigned initial values in the header. The third parameter is declared in the body of the netlist as an example of declaration of parameters, other than in the header.
The rest of subcircuit is simple netlist, containing few components. Resistor Rin is connected between AMP_IN pin and ground. Value of this resistor is Rin_value and is representing input impedance of the circuit. Eamp is voltage controlled voltage source, that is sensing voltage across Rin and amplifying it with the gain of 10 (default value, could be changed from schematic). It’s output, pin 1, is feeding AMP_OUT output of subcircuit through Rout, representing output impedance.
Next step would be to create a schematic symbol, representing this subcircuit.
To do this you have to open schematic editor. If you were editing your library in LTSPICE text editor, switch to schematic editor by opening any schematic or creating a new one. Otherwise you will not be able to see New Symbol option in File menu. Select File –> New Symbol to open symbol editor. Use Draw menu to select drawing tools and create your symbol drawing. Use Add PIN/PORT command from Edit menu to add pins to your part drawing. As soon as you select Add PIN/PORT command, you will see Pin/Port Properties dialogue. Enter Label name. Label name means nothing to subciruit and will determine this pin label in schematic. Then enter Netlist Order number. Netlist Order number should correspond to order of the pins in subcircuit header. This is what really matters and this is the way, how symbol and subcircuit are connected in LTSPICE.
Select Pin Label Justification check box to make label visible in your schematic and repeat pin adding process for all pins of your subcircuit.
Next step that you may take could be adding some necessary information to part drawing. Go to Edit –> Attributes –> Attribute Window and select between available attributes.
Attribute window fields display selected part properties on schematic.
You will need InstName field to be able to see refernce designator, like U1, U2,… Un on schematic. Type will represent symbol name, other attributes will show model and it’s parameters if you would like them to be also displayed in schematic. Select required attribute and click OK to be able to paste it on schematic.
Meaning of Attribute Window fields:
InstName – instance name, to display reference designator. Example: U is commonly used for IC.
Type – name of the symbol (file name).
SpiceModel – name of file including the spice model
Value, Value2, SpiceLine, SpiceLine2 – to display corresponding fields from Symbol Attributes, created in attribute editor
There are more symbol properties that should be assigned to the symbol, before it can represent our subcircuit. While we already added appropriate fields to be displayed, we have to assign values to these fields –> meaning that we have to assign attributes to the new symbol.
Assigning attributes to the new symbol:
Open the Symbol Attribute Editor and enter the appropriate information:
Meaning of Attribute Editor fields:
Prefix – determines the basic type of symbol. If the symbol is intended to represent a SPICE primitive, the symbol should have the appropriate prefix, R for resistor, C for capacitor, M for MOSFET, etc. The prefix should be ‘X‘ if you want to use the symbol to represent a subcircuit defined in a library
SpiceModel – name of file including the spice model. It is not the name of your model and in most common cases this field could be left blank. However, there is an exception for this rule, if you want to have drop-down menu for the library, describing similar parts (like transistors). In this case, SpiceModel field should contain the name of default model
Value – name that follows .subckt statement in the netlist, meaning the name of subcircuit
Value2 – is used to pass additional parameters to the subcircuit. If Value2 line is used, an instance of the symbol as a component on a schematic cannot be edited to have different attributes (there are some exceptions of this rule)
SpiceLine, SpiceLine2 – to pass parameters to the subcircuit
Description – to give the symbol a description, that would be visible in the symbol browser
ModelFile – name of a file to be included in the netlist as a library. If the symbol attribute SpiceModel exists and is the name of a subcircuit in the file specified as ModelFile then a drop list of all subcircuits names will be available when an instance of the symbol is edited on a schematic. Otherwise, could be left empty
Save the symbol, File –> Save As saves the file as a *.asy. We will save our symbol as amp.asy.
Where to save?
You have a couple of options.
We will consider that LTSPICE is installed in C:\Program Files\LTC\SwCADIII.
LTspice searches for the needed files as specified in the .lib or .include statement that is placed on the schematic or in a netlist. If no statement exists, LTspice looks in the SwCDIII\lib\cmp, SwCDIII\lib\sub, or the path of the schematic. It is recommended to always use the .lib or .include directive to avoid error messages.
Pingback: LTSpice LM13700 Simulation Error