an electronics development environment

This 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

xcircuit

XCircuit is a circuit editor that can generate Spice output. It has a peculiar user interface that takes time to get used to:

  • left button, click: start drawing a path or continue operation
  • left button, drag: move element(s)
  • right button, click: unselect, select last or undo depending on context
  • middle button, click: adds the clicked element to the selection or finish operation
  • middle button, drag: adds elements within the bounding box to the selection

XCircuit doesn't use all the modern widgets and is thus frighteningly fast.

using xcircuit with ngspice

It's handy to have everything required for the simulation inside the circuit file:

voltage source circuit

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:

example output

..(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.

simulate

This is the simulate script, which:

  • splices together the Spice library and the Spice output from XCircuit
  • invokes Spice and converts its output with spice2grace
  • invokes Grace with our favourite parameters to produce a PNM
  • invokes pnmtoxwd to produce an XWD
  • invokes xwud to show the XWD. xwud is a viewer that loads quickly, is uncluttered and terminates with a click anywhere on the plot
#!/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

spice2grace

spice2grace 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 library

The 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 updates

XCircuit writes Spice on [alt]+[a]. It is possible to automatically run the Spice simulation and show the plot when XCircuit writes the Spice.

  1. A sentinel script is run to watch the *.spc files
  2. XCircuit writes the blah.spc file
  3. The sentinel invokes simulate blah.spc

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 $

Valid HTML 4.01 Transitional