an electronics development environmentThis page describes a way to integrate three fantastic electronics tools using a bit of perl and shell scripting. installation$ sudo apt-get install xcircuit ngspice grace xcircuitXCircuit is a circuit editor that can generate Spice output. It has a peculiar user interface that takes time to get used to:
XCircuit doesn't use all the modern widgets and is thus frighteningly fast. using xcircuit with ngspiceIt's handy to have everything required for the simulation inside the circuit file:
The circuit above shows an explicit voltage source along with a spurious component to hang extra spice directives from. The crystal in the circuit above has an Info Pin containing: spice: Vin Vin GND DC 0 .op .dc Vin 0.0 5.0 0.01 .print DC V(Vout) ..which sweeps Vin from zero to five volts to produce:
..(with a BC557B transistor) Ensure that unused inputs and components are tied to GND otherwise ngspice fails or redlines trying to compute a stable initial state. simulateThis is the simulate script, which:
#!/bin/sh -e
CIRCUIT=$1 shift head -n1 $CIRCUIT | cat - library.spi $* $CIRCUIT | ngspice -b 2>/dev/null |./spice2grace >/tmp/grace-input COMMENT=`head -1 /tmp/grace-input` ARGS= for TOKEN in $COMMENT; do if [ "#" == "$TOKEN" ]; then STATE='expecting Y axis'; else if [ "expecting Y axis" == "$STATE" ]; then ARGS="xaxis label \"$TOKEN\"" STATE='expecting series'; SERIES=0 else if [ "expecting series" == "$STATE" ]; then ARGS="$ARGS; s$SERIES comment \"$TOKEN\"; s$SERIES line linewidth 2" SERIES=$((SERIES+1)) fi fi fi done cat /tmp/grace-input | gracebat -nxy - -legend load -hdevice PNM -printfile /tmp/spice.pnm -fixed 800 600 -pexec "title \"$CIRCUIT\"; s0 line color 9; $ARGS" && pnmtoxwd /tmp/spice.pnm 2>/dev/null | xwud [ -f /tmp/spice.pnm ] && rm /tmp/spice.pnm [ -f /tmp/grace-input ] && rm /tmp/grace-input spice2gracespice2grace is a slice of perl that converts the Spice tabular simulation output to a format that Grace recognises:
#!/usr/bin/perl
# # convert from spice PRINT output to grace NXY input # # spice output format is: # #... #----(x80) #Index v-sweep v(vout) #----(x80) #0 0.000000e+00 5.000000e+00 #1 1.000000e-02 5.000000e+00 #... #54 5.400000e-01 4.999883e+00 #^L #Index v-sweep v(vout) #-------------------------------------------------------------------------------- #55 5.500000e-01 4.999828e+00 #... #500 5.000000e+00 3.035277e-02 # #CPU time since last call: 0.079 seconds. #... # # grace input format is: # ## load smoothed-load smoother-load #2006-04-18T00:00 0.07 0.02 0.00 # use strict; my $past_intro = 0; my $printed_header = 0; while ( <> ) { $past_intro = 1 if m/-{80}/; next unless $past_intro; if ( m/^Index\s/ && ! $printed_header ) { my @tokens = split; shift @tokens; print "# ".join(' ', @tokens)."\n"; $printed_header = 1; } if ( m/^\d/ ) { s/^\d+\s+//; s/\t+/ /g; print; } } spice libraryThe Spice library is the place to put model parameters for your favourite components along with some fixup for the Spice that XCircuit produces: * tie the symbolic GND node used by xcircuit to ngspice ground (node 0) Rgnd GND 0 0.0 * provide a 5V EMF at the digital Vdd Vcc dVdd GND DC 5.0 ... (model parameters) automatic updatesXCircuit writes Spice on [alt]+[a]. It is possible to automatically run the Spice simulation and show the plot when XCircuit writes the Spice.
sentinel
#!/bin/sh -e
while true; do make; sleep 1; done Makefile%.d : %.spc ./simulate $< & echo -n > $@ .SILENT: all: transistor.d buck-converter.d $Revision: 1.3 $, $Date: 2006/04/19 08:51:34 $ |