Tuesday, January 22, 2013

R-2R success! (or "How To Use SPICE")

I stopped trying to figure out equations to predict voltage levels for my R-2R experiments and just broke down and learned enough of SPICE to make it show me the answers itself.

Both of these are exactly the same except for the top resistor.  Here is the SPICE program to compute the voltages for the left configuration:


My dang problem
v1 1 0 dc 5
rA 1 2 220
rB 1 3 220
rC 2 0 47
rD 3 0 220
rE 2 3 110

.DC v1 5 5 1
.PRINT DC v(1) v(2) v(3)

.end

It outputs this line:

--------------------------------------------------------------------------------
Index   v-sweep         v(1)            v(2)            v(3)            
--------------------------------------------------------------------------------
0 5.000000e+00 5.000000e+00 1.122611e+00 1.811306e+00

As you can see, v2 (1.122V) and v3 (1.811) match up to my physical readings quite nicely.

Now if I change the SPICE program so that rC is 115 ohms instead of 47, I get this:

--------------------------------------------------------------------------------
Index   v-sweep         v(1)            v(2)            v(3)            
--------------------------------------------------------------------------------
0 5.000000e+00 5.000000e+00 1.916667e+00 2.208333e+00

Again, v2 (1.9166) and v3 (2.21) match up with my results (on the right-hand side of the picture) quite nicely.

Phew!  What a pain in the neck!  I am logging this here so I don't ever have to figure it out again.

To execute these programs, you do the following command:

ngspice -b your_src_file_here.txt

And here is a variation on the program:

My dang problem
v1 1 0 dc 5
rA 1 2 220
rB 1 3 220
rC 2 0 115
rD 3 0 220
rE 2 3 110

.control
set hcopypscolor=1
tran 1us 500us
hardcopy test-tran.ps v(1) v(2) v(3)
.endc

.end

This dumps out the following text:

Initial Transient Solution
--------------------------

Node                                   Voltage
----                                   -------
1                                            5
2                                      1.91667
3                                      2.20833
v1#branch                           -0.0267045

It also writes out a postscript file called test-trans.ps which graphs the voltages, which is kind of useless for my purposes since my voltage does not change.  But it might prove handy later.  The "initial transient solution" data that it prints out is handy nonetheless and in a slightly different format (ie better) than what is printed out in the first program.

To convert the postscript file to a .png, do this command:

convert -background white -layers flatten test-tran.ps test-tran.ps.png

See https://wiki.ubuntu.com/From_PSpice_to_ngspice-gEDA for some more help and some decent "getting started" type explanations.

No comments:

Post a Comment