Skip to main content
Engineering LibreTexts

19.2: Programming Languages for Hardware

  • Page ID
    43119
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    This section deals with software that is used for hardware design or access. Within this discussion there is a small discussion on html to help demonstrate a point about another language. In no way is learning html part of this course, it is just a means to an end herein.

    SPICE and digital design languages

    SPICE is the language of circuit design. It is the basis for all analog circuit simulators. There are both paid for products and open source products all based off the original Spice program. SPICE stands for Simulation Program for Integrated Circuit Emphasis. Here we will give a historical outline of SPICE following along the lines of our other history for programs.

    • Created at UC-Berkely around 1975
      • Originally written in Fortran
      • Last version (SPICE3) was written in C
    • Derived from CANCER (Computer Analysis of Non-linear Circuits, Excluding Radiation)
      • Radiation was excluded because of laws that would make anything dealing with radiation a domain of the Department of Defense (DOD)
        • Think 1960s and flower children
      • From a 1971 paper by Ronald A. Rohrer
    • Uses a modified nodal analysis
      • The modified method allowed for circuits without an ideal source
      • Linear simultaneous equations (How would we solve that in say Octave? You answer.)
    • Uses Newton-Raphson for non-linear components
    • Transient analysis uses trapezoidal (or Gear if chosen) integration
      • Numerical methods course should be taken so you can understand these concepts...(hint)

    Spice simulators

    This is a small selection of SPICE simulators available for "free" or for a price.

    • SPICE — Spice3 from the original source
    • ngspice — a part of the gEDA (GPL Electronic Design Automation tools)
    • LTSpice
      • While a product of a business this program is free
      • Lots of preloaded elements are from Analog Devices but they offer it for free, so...
    • TINA-TI
      • Two versions, one free the other paid for, but the free version is pretty loaded so you may not need the paid for one
      • From Texas Instruments
    • CIRCUITMOD
    • MacSpice 3
      • Version of SPICE 3 for Macs
    • Circuit Construction Kit
      • This is for kids
      • Simulation through PhET at University of Colorado
        • PhET has lots of useful educational simulations
        • PhET simulations are for all ages (the one here is for kids but others are more advanced)
    • MultiSim
      • National Instrument's circuit analysis program
      • Not free
    • PSpice (OrCAD)
      • This is one of the more popular SPICE products
      • Not free

    Example

    Defibrillators are an important medical device that resuscitates a stopped heart (in certain conditions). Scientists and engineers worked on various versions of the defibrillator in the early 1900s but the origins of the device we use today comes from an electrical engineering in the 1930s. The device works by sending electric shocks to the heart, but why this works is still a matter of research. Since the simple defibrillator can be modeled rather simply by an electrical circuit we will use that as an example for Spice. Today there are two types of defibrillators in use: a monophasic and a biphasic defibrillator. The biphasic is considered to be more advanced but the monophasic is still used in situations where it is difficult to use the biphasic.

    A monophasic defibrillator just sends current one way through the paddles that are on different sides of the heart whereas the biphasic sends current both ways. A sketch of the wave forms are show above. In our simulation we will try and produce a wave form that emulates the defibrillator. Since the somewhat square wave form of the biphasics will need additional circuitry that will obscure the programming message we will produce a more monophasic circuit and simulation (though it is not quite monophasic either).
    A monophasic defibrillator just sends current one way through the paddles that are on different sides of the heart whereas the biphasic sends current both ways. A sketch of the wave forms are show above. In our simulation we will try and produce a wave form that emulates the defibrillator. Since the somewhat square wave form of the biphasics will need additional circuitry that will obscure the programming message we will produce a more monophasic circuit and simulation (though it is not quite monophasic either).

    Since a defibrillator would be used on a human, we will need to simulate the human as a resistor. Outlined below are some real world considerations.

    • Human resistance (or chest impedance)
      • A dry human is about a 100,000 \(\Omega\) resistor
        • Too large a resistance
        • In order to make the defibrillator work we would burn the patient (in direct skin contact) at this resistance
        • Of course, we could open the patient and apply the paddles directly to the heart which was done in the past, but let's not go back to the past...the future is brighter if we want it to be
      • A wet human is about a 1000 \(\Omega\) resistor
        • Better but still pretty large and this sounds dangerous
      • Old defibrillator concept inserted the lead into the human to reduce the resistance
        • A punctured human is about a 500 \(\Omega\) resistor
        • Hmmm...still a large resistance and we are trying to avoid the past
      • A special conductive liquid could be put on the paddles to reduce the resistance
        • This can achieve a resistance of less than 100 \(\Omega\)
        • This is the way
          • You might have seen in TV and movie medical dramas medical personal applying a wet gel on the paddles (though now there are gel pads and other advances)
          • With this we can simulate our human to be about a 50 \(\Omega\) resistor

    Let us now present a circuit that we believe will be good for our simple defibrillator.

    A circuit for a simple defibrillator. The battery charges the capacitor which then acts as the power source (a capacitor will discharge fast). The human (cartoon) is represented by a 50 Omega resistor above. This is a circuit design for the Spice simulator. It is a simple RLC circuit (that is taught to everyone who takes a serious circuits course). We will take note of the voltage at node 2. That will be the voltage inputted into our human who is represented by a resistor. The ic=1000V means that the capacitor will have an initial condition of a 1000V charge. This avoids the switches and the battery in the circuit to the left as that initial condition will simulate those.
    A circuit for a simple defibrillator. The battery charges the capacitor which then acts as the power source (a capacitor will discharge fast). The human (cartoon) is represented by a 50 \(\Omega\) resistor above. This is a circuit design for the Spice simulator. It is a simple RLC circuit (that is taught to everyone who takes a serious circuits course). We will take note of the voltage at node 2. That will be the voltage inputted into our human who is represented by a resistor. The ic=1000V means that the capacitor will have an initial condition of a 1000V charge. This avoids the switches and the battery in the circuit to the left as that initial condition will simulate those.

    And now for the actual program and its execution.

    spice_program_defib.png Image of the spice simulation that produces a signal that is consistent with the waveforms of the defibrillator discussed before. It should be noted that this is an under-damped waveform which will be discussed in your electrical circuits course (if you take one).
    This is the Spice program that represents the circuit discussed above. As you see it has a capacitor, inductor, and resistor (c1,l1, and r1). This is a run of the program to the left. As you see it produces a signal that is consistent with the waveforms of the defibrillator discussed before. It should be noted that this is an under-damped waveform which will be discussed in your electrical circuits course (if you take one).

    Final thoughts for Spice

    You might have noted that we simulated a human by a resistor. This idea which is reminiscence of the control system discussions in the Fundamentals section where physical objects can be represented by elements of a circuit. Given this it would be possible to use Spice to simulate any engineering system assuming we can represent it by elements of a circuit which BTW we can do for most physical objects. Of course, Spice is not the only simulator for electrical circuits. Spice is for the analog circuits but todays circuits have a lot of digital components, so Hardware Description Languages (HDLs) were developed for digital circuit simulation. For complete circuit simulators the HDL engine is combine with the Spice engine.

    Hardware Description Languages (HDLs)

    HTML (as a prelude to HDLs)

    HTML (HyperText Markup Language) has nothing to do with science and engineering, but it is a useful idea to examine in anticipation of a discussion on Hardware Description Languages (HDL). HTML is basically just text surrounded by tags that tell a software program (a browser) how a web page should be displayed on the screen. This is a lot like HDLs that describe the hardware for a program to simulate. That is text that has a specific syntax structure to emulate hardware (hence hardware description).

    • Example of HTML
    simplehtml.png simplehtml_code.png
    A simple browser page that is produce by the HTML code on the right. More complicated browser pages would have css templates and maybe javascript. The HTML code that is read by the browser to produce the simple browser page on the left. You will see that the tags are relatively simple and the text is just standard ascii text.

    HDLs are used for modeling primarily digital circuits. Most programs meant to work with HDLs can also simulate the digital circuits. Analog components are generally simulated with Spice that can usually be linked to these programs. One program that used HDLs is the program Electric which is an open source program for use with VLSI and digital logic.

    Let's take a look at the two most popular HDLs (note there are many HDL languages).

    VHDL

    VHDL is one a very popular HDL for modeling circuits (in particular digital circuits). Here is an outline of this language.

    • VHSIC Hardware Description Language (VHDL)
      • Very High-Speed Integrated Circuits (VHSIC)
        • Useful for VLSI (Very Large Scale Integration)
        • Useful for ASIC (Application Specific Integrated Circuit)
      • A DOD standard originally to describe ASIC circuits (kind of like a hardware psuedo-code)
      • Borrows from the DOD product, Ada programming language
        • Heavy typing (can be difficult)
        • Case insensitive

    VHDL is the default language of Electric.

    Verilog

    Verilog is a popular HDL because of its ease of use. Here is an outline of this language.

    • Verilog is an HDL that was developed at about the same time as VHDL. Verilog is used to model electronic systems and to do verification of the circuit.
      • Portmanteau1 of verification and logic
      • Borrows from C
      • Case sensitive

    Verilog can be easily used by Electric, but is not default. Other HDLs are much harder to use with Electric.

    VHDL example

    Here we will give a simple example of the VHDL language. We will do this with a very simple logic circuit which is commonly used in digital circuit analysis.

    This is a simple logic circuit that would be could be the basis of a digital circuit. It has two inverters (0 to 1 or 1 to 0) for inputs X2 and X4, two ANDS for X1 and ~X2 plus X3 and ~X4 and an OR for the results of the AND. This circuit could also be used in a logic class as well, btw. You can learn more about this in your digital circuits class if you chose to take that course.
    This is a simple logic circuit that would be could be the basis of a digital circuit. It has two inverters (0 to 1 or 1 to 0) for inputs X2 and X4, two ANDS for X1 and ~X2 plus X3 and ~X4 and an OR for the results of the AND. This circuit could also be used in a logic class as well, btw. You can learn more about this in your digital circuits class if you chose to take that course.

    This simple logic can be written in VHDL in many ways.

    VHDL_simple_brute.png VHDL_simple_truthtable.png simple_logic_verilog.png
    VHDL of simple logic circuit presented as is...brute force if you wish. The NOT, AND, and OR are incorporated in the equation. The output is F. This is very consistent with the ADA programming language. VHDL of simple logic circuit presented in another way. This is more like a truth table, though compacted to the essentials. Verilog of the same simple logic circuit for comparison. This is consistent with the C programming language.

    Still another way of expressing the VHDL is the generated VHDL from the following electric example of this simple logic circuit.

    This is the simple logic circuit created in the program Electric (linked to previously). The waveforms for of the circuit are presented here as well for completeness. The VHDL generated by Electric is shown in the upper right corner. It is more "wordy" then the hand written VHDL presented before. This is usually the case with program generated code.  Electric is a program that can be used to learn VHDL by example.
    This is the simple logic circuit created in the program Electric (linked to previously). The waveforms for of the circuit are presented here as well for completeness. The VHDL generated by Electric is shown in the upper right corner. It is more "wordy" then the hand written VHDL presented before. This is usually the case with program generated code. Electric is a program that can be used to learn VHDL by example.

    LABVIEW (versus Simulink or XCOS)

    National Instrument's LabVIEW is a IDE for general controllers generally in a professional setting. LabVIEW takes a graphical approach to programming (technically it is a Visual Programming Language (VPL) like Logo). LabVIEW and the G programming language (not to be confused with G-code which is very old command control language) are synonymous. LabVIEW is a preferred language of engineers for interacting wiht controllers and measurement devices through the General Purpose Interface Bus (GPIB or IEEE-488) because of its ease of use and extensive options. While LabVIEW is not the only programming language that can do this as you could directly use C/C++, Fortran, or use a package from Octave/MATLAB (Instrument Control) or Python (pyVISA), it is the easiest.

    Simulink and XCOS are for modeling control systems graphically (VPL) rather than directly interacting with controllers. However using these modeling system with MATLAB or Scilab and then using their separate packages for instrument control may be desirable for some. Note in Octave you can do all this but it will not be graphical, but it is free.

    Example LabVIEW

    This example is an example of controlling the input into a diode and reading the output.

    • Before showing the program we will show the set-up for a testing of a Zener diode using instruments controlled by the LabVIEW program.
    Here is the equipment that the LabVIEW program controlled for the simple Zener diode test. The actual circuit is behind the computer on a breadboard. Note: These instruments are overkill for this type of test as this test was meant to ensure operation, not the actual work.
    Here is the equipment that the LabVIEW program controlled for the simple Zener diode test. The actual circuit is behind the computer on a breadboard. Note: These instruments are overkill for this type of test as this test was meant to ensure operation, not the actual work.
    • LabVIEW offers the ability to design your own GUI for your control panel where you set different parameters.
    The instrument panel designed on LabVIEW (not programming, just a pretty control panel). The results (I-V curve) of a successful test are displayed here.
    The instrument panel designed on LabVIEW (not programming, just a pretty control panel). The results (I-V curve) of a successful test are displayed here.
    • And now the program which is too large to see unless you click on it and examine it with zoom. For convenience a small interesting piece of it is shown to the right.
    The LabVIEW program which you can see is graphical (VPL). In this graphical program there is input/output, conditionals, and loops. The thin lined box represents a loop in the above image and the thicker lined boxes are either conditionals (case statements or while loops) or sequence structures. This is a small piece of the program on the left. It shows the VPL functions to build a graph where you send it an x and y input and then the output is sent to the XY Graph icon.
    The LabVIEW program which you can see is graphical (VPL). In this graphical program there is input/output, conditionals, and loops. The thin lined box represents a loop in the above image and the thicker lined boxes are either conditionals (case statements or while loops) or sequence structures. This is a small piece of the program on the left. It shows the VPL functions to build a graph where you send it an x and y input and then the output is sent to the XY Graph icon.

    If you want to learn more about the LabVIEW programming language the LabVIEW Wiki is a good source of information and easy to navigate as well. There are really no open source alternatives to LabVIEW, a German product mostly in German and Spanish called MyOpenLab is the closest at present (2021). Of course you could use traditional languages to do what LabVIEW does but you will sacrifice ease of use.

    Scilab Xcos

    Scilab's Xcos (open source) is a control simulation programming language that is open source very much like MATLAB's Simulink. This are graphical languages that have blocks that represent control actions like summing, integrating, differentiating, and other actions. This might best be describes as the blocks represent transfer function.

    • In the following figure is a cartoon of how this blocks might look like in these types of control simulation programming languages.
    Different control simulation languages have slightly different versions of the control block elements sketched above. Note that these blocks are just like they would have been done by hand before computers.
    Different control simulation languages have slightly different versions of the control block elements sketched above. Note that these blocks are just like they would have been done by hand before computers.

    Scilab's Xcos has many examples which can be access in the Scilab program (or if you wish not to download Scilab they have some of those examples on the web page itself; this is true of MATLAB's Simulink as well).

    • Xcos' examples are called demonstrations and are accessible through the Xcos' help
    The snapshot above shows one of the demonstrations in Scilab's Xcos. It is a good example of a PID controller. Also in the picture are some of the blocks in Scilab. Of course since this product is open source you can go check this out for yourself. The results of the demonstration on left are shown here in the graph. What are some interesting engineering/control systems conclusions you can come up with by looking at this simulation? In this artistic screenshot you will see that the way to access the Xcos demonstrations is through the help in Xcos (the question mark). There are many examples which you can "play" with to learn more about engineering.
    The snapshot above shows one of the demonstrations in Scilab's Xcos. It is a good example of a PID controller. Also in the picture are some of the blocks in Scilab. Of course since this product is open source you can go check this out for yourself. The results of the demonstration on left are shown here in the graph. What are some interesting engineering/control systems conclusions you can come up with by looking at this simulation? In this artistic screenshot you will see that the way to access the Xcos demonstrations is through the help in Xcos (the question mark). There are many examples which you can "play" with to learn more about engineering.

    VPLs are a great aid in programming more complex ideas and functions, but keep in mind that you can do the same thing in traditional languages. Why use VPSs? Well ease of use of course.

    Arduino and "micro-computers"

    Arduino is not the first microcontrollers/microprocessors but it's popularity has increased the awareness of these type of microprocessor. The uses for these microcontrollers range from miniature drones to wearable light shows controlled by this miniature devices. A comparison of some of the more popular microprocessors show a range of operating systems (or lack thereof), languages, and usage.

      Arduino Raspberry Pi Basic Stamp Processor Beagleboard
    Computer or not Not a full-fledge computer Full-fledged computer Not a full-fledged computer Full-fledged computer
    Operating system None Linux OS (Debian - Raspberry Pi OS) None Linux OS (Ubuntu if you wish)
    Primary Language C++ Python (and others) Unique (Basic Stamp 2) kind of like an Assembler language but more advanced (1.5 level?) Python (and others)
    Types Many different types; many different sizes; more than 20 (not including retired products that are still widely available) Two types One module thought there are four different evolutions of it Many different types; many different sizes
    Website https://www.arduino.cc https://www.raspberrypi.org Parallax https://beagleboard.org
    Additional Info
    • The most popular of these type of controllers/processors
    • Good for beginners
    • Good for small projects
    • Lots of choices
    • No ports
    • Idea for this product came from the BASIC Stamp microcontroller
    • Direct competitor to Arduino though technically they are really in different markets, so not really
    • More computer like
    • Lots of ports (Ethernet port; Video port, etc.)
    • Note: Rasberry Pi Pico has no ports and is more like Arduino
    The BASIC Stamp microcontroller (or now the BASIC Stamp 2) was the most popular before Arduino. This has evolved into the Propeller microcontroller. Arduino (circa 2005) is more like this product (circa 1992).
    • Open source
    • Not well known
    • Original product
      • More expensive
      • More features
      • Kind of like a merge of Arduino and Raspberry Pi
      • Ports (Ethernet port; etc.),
    • Note: PocketBeagle has no ports is more like Arduino plus now they have more Arduino (Or is it BASIC Stamp?) like products

    These microcontrollers/microprocessors have add a new dimension to engineering and hence programming as well. With that we will give a simple example of Arduino from the starter kit.

    Example

    The Arduino is meant to be plugged into your computer for you to program it. Here is an example of one of the projects in the Starter Kit, The Light Theremin2.

    The simple light Theremin that is described in the Arduino Starter Kit. It is a take on the actual Theremin which does not use light. It is very simple, fun, and a great way to start your adventures with Arduino (or others microcontrollers as this is very simple and can easily be migrated to practically any electronic kit). The cable going out of the picture is attached to one of the authors' laptops.
    The simple light Theremin that is described in the Arduino Starter Kit. It is a take on the actual Theremin which does not use light. It is very simple, fun, and a great way to start your adventures with Arduino (or others microcontrollers as this is very simple and can easily be migrated to practically any electronic kit). The cable going out of the picture is attached to one of the authors' laptops.

    The hardware is programming using the Arduino IDE software as shown here with the public domain Thermin program.

    arduino_ide_software.png
    The software which is open source to program the hardware for the Arduino. The Light Theremin example is public domain and can be modified to improve the sound or even expand it's abilities by adding additional hardware and reprogramming. Just search the web and you will find many Theremin examples (light or otherwise).

    Lastly we show the full code of the the Light Theremin project as our programming example.

    light_theremin_arduino_program.png
    This is the Light Theremin program used in the Arduino project. This is clearly a C/C++ program however you will note the more hardware like aspects to it.

    This completes the discussion of computer programming, now it is time to produce a team project. The next section will detail that assignment. This will be a group project.


    1Portmanteau in English is a traveling bag that was generally made of stiff leather and generally opens into two compartments. The word evolved into a "portmanteau word" which is a blended word believed to be first used by Lewis Carroll in the Jabberwocky poem of Alice in Wonderland: Through the Looking-Glass. Of course, Alice in Wonderland is one of the greatest works of "Englit."

    2The Theremin is an invention of the modern world and is played without actually touching the equipment. The professional instrument is an example of heterodyning in radio engineering. It is basically an RLC circuit with the hand as ground. Sound from the Theremin can be heard in such great movies as The Day The Earth Stood Still (1951 Version) and Spellbound. For an utterly delightful theme song using a Theremin then you must watch Midsomer Murders. Though not originally done by a Theremin the theme song of the original Star Trek was so close to the sound of a Theremin that many people believed it actually was done by a Theremin. Today there are many Theremin versions of the Star Trek theme song on YouTube.


    19.2: Programming Languages for Hardware is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.