bTop-2 Basics

An introduction to analog and digital I/O with the bTop-2 lab benchtop.

March 19, 2009

Introduction

To begin using the bTop with Mathematica, load the bTopTools package. (If you don't have this installed yet, download the cross-platform Mathematica drivers from http://perfsci.com/bTop-software.asp#mathematica.) Loading the package will launch an accessory program to mediate communications between the bTop and Mathematica.

"bTopBasics_1.gif"

BTopConnections[] is a good way to make sure your bTop is properly connected.

"bTopBasics_2.gif"

"bTopBasics_3.gif"

If you only have one bTop device connected, it should be device #1. Let's name this 'bt' for convenience.

"bTopBasics_4.gif"

"bTopBasics_5.gif"

Double-click the arrows to the right of each section to see more about the ways Mathematica can interact with the bTop-2.

Digital Out

The bTop has three sets of digital IO ports, each 8 bits wide. The A port is ordinary and powered over the USB bus; the B port is made up of switched relays carrying power from the AC adapter; the D port is only accessible internally. The three ports correspond to PAx, PBx, and PDx on the bTop wiring diagram.

Here, we'll be using the A port to demostrate writing digital values.

Use the BTopDigitalIOPort[] function to get the index for the port you're interested in:

"bTopBasics_6.gif"

To demonstrate digital writing, we want to set each pin as an output pin. The function in question is BTopSetDigitalIODirections[devices, ports, directions].
- devices is a list of bTop device identifiers. Here, we have only one bTop.
- ports is a list of ports on that bTop. Here, we'll just be using digital port A.
- directions is a list of directions to apply to the named ports. Each direction is an 8-bit integer (from 0 to 255, inclusive) where bit n indicates whether pin PXn will be used as an input (0) or output (1) port. That is, to enable all pins as output, we would use 255, since its binary representation is all ones.

"bTopBasics_7.gif"

"bTopBasics_8.gif"

(Note that pin 0 is the rightmost, or least-significant digit.)
So, to set all the digital pins on port A as output, we use this command:

"bTopBasics_9.gif"

Or, more verbosely,

"bTopBasics_10.gif"

Now we can use the BTopDigital[] function to update the values on the pins. This function works very similarly. We specify the bTop board to address, the digital port to use, and set its value equal to an integer which indicates the state of each pin in a bitwise fashion.

So, to turn on all the pins we call the function like this:

"bTopBasics_11.gif"

To turn on just the first pin, we could use:

"bTopBasics_12.gif"

To turn on just the second,

"bTopBasics_13.gif"

And to turn on the first and the second,

"bTopBasics_14.gif"

In general, to control pins individually, use Mathematica's binary digit functions. For example, to turn on every other pin we could use

"bTopBasics_15.gif"

"bTopBasics_16.gif"

"bTopBasics_17.gif"

If we connect the 8 digital out pins to 8 LEDs, we can make a mini light-show:

"bTopBasics_18.gif"

"bTopBasics_19.gif"

"bTopBasics_20.gif"

(Use command-. to stop the show.)

Or we can make a simple binary counter:

"bTopBasics_21.gif"

"bTopBasics_22.gif"

"bTopBasics_23.gif"

Digital In

Using the bTop's digital input ability is very similar to using the digital outs (see last section). Again, there are three digital IO ports; the A and B ports are accessible from the front panel. Here we'll be using the A port to read digital values.

"bTopBasics_24.gif"

Recall that the third argument of BTopSetDigitalIODirections is a bitwise indicator of which pins are used for input (0) or output (1). To use all 8 pins in the A port as inputs, we set the direction to 0:

"bTopBasics_25.gif"

To read the digital IO pins, we use the BTopDigital function as in the output section, but without assigning a value.

"bTopBasics_26.gif"

"bTopBasics_27.gif"

The result of this function is a bitwise representation of the state of each pin in the designated port. The digital input pins will read 0 when they are connected to the digital ground port (DGND on the panel diagram); otherwise, they read 1. The input 255 indicates that none of the pins in port A are connected to ground.

If we connect the first pin to ground, we get a different reading:

"bTopBasics_28.gif"

"bTopBasics_29.gif"

and a completely different reading when connecting the last pin:

"bTopBasics_30.gif"

"bTopBasics_31.gif"

We can determine the state of each pin individually by unpacking this integer into bits:

"bTopBasics_32.gif"

"bTopBasics_33.gif"

Here I've connected some pins to ground.

"bTopBasics_34.gif"

"bTopBasics_35.gif"

"bTopBasics_36.gif"

"bTopBasics_37.gif"

To read only the value of a few pins, take the result of the BTopDigital function and use the bitwise AND to compare to the values you're interested in. Here's the value of only the first three pins:

"bTopBasics_38.gif"

"bTopBasics_39.gif"

"bTopBasics_40.gif"

"bTopBasics_41.gif"

"bTopBasics_42.gif"

You might find it more convenient in your application to address the pins PXn by number, rather than a bitmask.

"bTopBasics_43.gif"

Here all four odd-numbered pins are grounded:

"bTopBasics_44.gif"

"bTopBasics_45.gif"

and all the even-numbered pins are connected to voltage:

"bTopBasics_46.gif"

"bTopBasics_47.gif"

Analog I/O

As well as digital in and out, the bTop is capable of analog input through its A-D convertor. The bTop can read 1,000 samples per second from its A-D port, and has a resolution of 12 bits.

In the examples below, we've connected a the AD3 port of our bTop to the output of an analog function generator.

The function BTopAnalog works like BTopDigital. You can use it to read the value of an analog channel.

"bTopBasics_48.gif"

"bTopBasics_49.gif"

By adding a few extra arguments, we can use this function to take time samples, returning a matrix of readings from the specified ports. The other arguments are the number of samples to collect, and the speed at which to operate the AD convertor (up to 1,000 samples per second).

Note that you can use the BTopGetAD[] and BTopGetADTimeSamples[] to get digital values from the channels (from 0 to 4096), instead of voltages:

"bTopBasics_50.gif"

"bTopBasics_51.gif"

Here, our function generator is producing a sine wave at approximately 5Hz:

"bTopBasics_52.gif"

"bTopBasics_53.gif"

Note that, since we're reading voltages in the unipolar mode, we only see positive amplitudes. To see the whole sine wave, we can read bipolar values:

"bTopBasics_54.gif"

"bTopBasics_55.gif"

One wrinkle is that BTopAnalog returns unsigned values. To make this into a real sine wave, we need to convert these values to signed:

"bTopBasics_56.gif"

"bTopBasics_57.gif"

"bTopBasics_58.gif"

Naturally, we can define helper functions in Mathematica for common tasks.

"bTopBasics_59.gif"

Here's a 5Hz square wave from our function generator,

"bTopBasics_60.gif"

"bTopBasics_61.gif"

and a triangle wave,

"bTopBasics_62.gif"

"bTopBasics_63.gif"

Analysis

Since BTopTools returns data in standard Mathematica lists, it is straightforward to perform signal analysis on the results. For example, here is the Fourier spectrum for a 50Hz sine wave:

"bTopBasics_64.gif"

"bTopBasics_65.gif"

This is a good place for another convenience function:

"bTopBasics_66.gif"

"bTopBasics_67.gif"

Here's the fourier spectrum for a triangle wave:

"bTopBasics_68.gif"

"bTopBasics_69.gif"

and a square wave (note the harmonics):

"bTopBasics_70.gif"

"bTopBasics_71.gif"

The limit of the fourier expansion of a pure tone, as the number of samples goes to infinity, is the delta function. We can observe this in the spectrum for a sine wave. For a 300Hz sine wave, taking a small number of samples gives the spike a wide "belt":

"bTopBasics_72.gif"

"bTopBasics_73.gif"

As we increase the sampling duration, the spike grows more pronounced:

"bTopBasics_74.gif"

"bTopBasics_75.gif"

"bTopBasics_76.gif"

"bTopBasics_77.gif"

The same phenomenon can be observed with the harmonics of a square waves Here, we increase the number of samples of a 100Hz square wave from 100 to 12,800:

"bTopBasics_78.gif"

"bTopBasics_79.gif"

"bTopBasics_80.gif"

Interaction

Again, since the BTopTools are so well-integrated with the Mathematica library, we can use Mathematica's interaction tools to make an oscilloscope of sorts.

"bTopBasics_81.gif"

Here's a 0.1s sample of a 50Hz sine wave:

"bTopBasics_82.gif"

"bTopBasics_83.gif"

We can use Mathematica's Dynamic and Refresh functions to make this display update in real-time:

"bTopBasics_84.gif"

"oscilloscope.gif"

We can even make a dynamic fourier spectrum:

"bTopBasics_85.gif"

"fourier.gif"

Analog Out

The D-A convertor, like the A-D convertor, operates at 1KHz. It allows you to set the voltage outputs of the DA port to unsigned 8-bit digital values (0 to 255).

Like digital IO, we can use the same function to get and set analog values; unlike digital IO, the input and ouput ports are in different physical locations. Here we've connect DA0 to AD3:

"bTopBasics_86.gif"

"bTopBasics_87.gif"

"bTopBasics_88.gif"

"bTopBasics_89.gif"

We can use the analog output to send Mathematica functions out over the DA port:

"bTopBasics_90.gif"

"bTopBasics_91.gif"

As with collecting time samples, you can use BTopSetDA[] to set digital values, from 0 to 255.

"bTopBasics_92.gif"

"bTopBasics_93.gif"

"bTopBasics_94.gif"

Spikey Created with Wolfram Mathematica 6