Breaking it down – The .SUBCKT
September 25, 2010 23 Comments
Having done so many simulation examples, ever wondered how complex actual circuits can be? When you go on to design a circuit with some application it won’t be just one amplifier or one rectifier. It’ll have dozens of blocks. How do you fit all that in one schematic? Well you can’t – at least not without getting totally mixed up with where each block is. Thats where hierarchical design comes in. This means that you break down a system into blocks. Break these blocks down even further till you get the simplest ones – like a tree branching out and having leaves at the end. The advantage of this is that most of the time, these “leaves” will repeat across your circuit and with a hierarchical design, these circuits need to be defined just once and reused again.
I hope you have read my previous tutorial on symbol creation in gschem as well as other tutorials on SPICE. You can get these tutorials by pointing your mouse to the Tutorials menu at the top and then selecting SPICE. You need the symbol of the previous tutorial to go through this one. Click Here to go to the symbols creation tutorial.
We will first look at a few op-amp circuits. I will be using LM741 for the examples. You can get the model (and many others) from Texas Instruments. Next we’ll build a two-stage BJT amplifier using a single BJT amplifier as a building block. Just like last time, I will explain only what is new. So lets begin, with my favorite part of analog circuits – operational amplifiers :)
Example – 1 The Inverting Amplifier
First, download the LM741 model from the National Semiconductor website. Once you have downloaded it, open it with your text editor and notice this line:
Take a look at the order in which pins of the op-amp appear. Now go back to the symbol we created and check the sequence numbers of the pins. See the connection? Thats why we used that order. Change the name of the subcircuit to LM741 since thats the name we used in the symbol. Lets now build the circuit. Here is the finished schematic:
We’ve looked at all the components before. Only the op-amp (which we created ourselves :) ) is new . Im sure that by now you can figure what we need to add here – the model-name. So go ahead and set it to LM741. Create the cmd file:
op-amp-inv-amp: .TRAN 100u 200m 100m
Finally create the netlist using gnetlist and run the simulation using ngspice. Open gwave with the raw file generated and plot the voltages at the input and output. This is what you should get (click for larger, better picture).
All done? Next are my favorite op-amp circuits – active filters. Lets move on :)
Exampler – 2 First Order Active Low Pass Filter
A simple circuit – Passive Low pass filter + voltage follower. Here’s the schematic:
And the cmd:
.AC DEC 10 1 1Meg
Now get the netlist, run the simulation and open the raw file in gwave. Plot the voltage at the output of the circuit and remember to set the X-Axis scale as Log. Here is the output:
Now lets look at the other extreme – a high pass filter.
Example -3 Second Order Active High Pass Filter
Different name but all the same – Just two passive high pass circuits followed by a voltage follower. This what the circuit looks like:
Add a cmd file:
.AC DEC 20 1K 200K
and get the netlist, run the simulation and plot the output waveform using gwave. Again, remember to set the X-Axis scale to Log. Here is the output:
Alright thats it for op-amps. Now lets see what subcircuits can really do.
Example – 4 Two Stage BJT Amplifier
Lets begin by capturing a single stage amplifier. Then all we need to do is use this twice and we’re done. Here is what this looks like:
Most of the circuit should be familiar to you. Before we look at the subckt specific sections, noticed that I’ve used Q2N3904 as the model name. This is because for some reason ngspice doesn’t support model names beginning with a number in subcircuits. So make sure you make a copy of the model file for this BJT and change the filename and model name accordingly.
There are two symbols here which are specific to a subcircuit. The first of these is spice-subcircuit-LL-1.sym. You can get this from the SPICE simulation elements section. This component tells gnetlist that the circuit we’re making should be translated to a subckt and not a netlist. Select and place this symbol and set its model-name to amp.
The second symbol is spice-subcircuit-IO-1.sym, also available in the same section. This defines the inputs and outputs of the circuit. We need these to show how a higher level circuit connects to this subcircuit. We need four of these – Input signal, Vcc, gnd and output signal. So place four of them like in the circuit. Pay attention to the order of the refdes of each of these. This is important because we’ll be creating a symbol and the symbol’s pin sequence numbers are connected to the refdes of these IO components. You do not need to change or add any attributes to these pins. Save this file and exit. Run the following command in terminal (from the directory where this file is saved):
gnetlist -g spice-sdb -o amp.mod amp.sch
This will create a model file. You can move this to your models directory but for this tutorial, keep it in the current directory itself. Now we need a symbol so open gschem again and create the following symbol:
Make sure the pin sequence numbers are in the right order. The device attribute is amp and the refdes, as you can see, is X?. All subcircuit reference designators must begin with X. Save this to the symbols directory we created last time and exit.
Now for the actual amplifier circuit. Take a look at the circuit and see for yourself how simple it looks after using a hierarchical approach :)
Don’t think I need to explain much here. You know what settings to put :). The amp symbols just need the model-name to be set to amp. Later on, as you create more amplifier circuits, use the same interface (i.e. input – Vcc – gnd – output) and the same symbol. The holds for any other symbols and subcircuits you make. Simplifies things a lot doesn’t it? :). Lets have a look at one component we’re coming across for the first time – vcc-1.sym. This is available under the Power Rails section – same place where you get gnd-1.sym. All you need to do is set its value by connecting it to a voltage source as shown. Then connect it to all the Vcc inputs in your circuit like I have done for this circuit.
Once you are done with the schematic capture (thats what they call it), save and exit. Then use the following cmd:
.TRAN 10u 10m
Create the netlist, run the simulation and plot the output waveform (click the image for a better view):
And thats it for subcircuits. Next time we will look at the interactive interpreter – a really cool feature in ngspice. Hope you liked this tutorial. Thanks for reading! :)