Using Userial Tutorial

This tutorial explains how to connect a Bumble-B to an MCP3204 analog to digital converter and read the analog values using the userial program supplied with Bumble-B. The tutorial should work well for other boards that userial runs on, consult your local documentation for the correct pins.

Bumble-B and MCP3204 Breadboard Test Setup
Bumble-B and MCP3204 Breadboard Test Setup

Hopefully this tutorial will also give the basic idea of how to use the SPI features of userial more generally. This enables you to communicate between Bumble-B and interesting new devices without writing new software to test the peripheral.

Bumble-B / MCP3204 / Potentiometer Wiring
Materials needed:

  1. (1) Bumble-B or Atmel USB capable board supported by LUFA.
  2. (1) MCP3204 ADC
  3. (1) Breadboard (solderless or soldering type)
  4. (1) 10k Potentiometer
  5. Various lengths of hookup wire
  6. The Bumble-B patched userial firmware (latest source and binary can be found on the Bumble-B product page), or userial.

Bumble-B

The Bumble-B supplies the power and the means of reading the analog to digital converter and doing something useful with the value. The circuit can use bus power or external power (see note 1).

The Bumble-B userial firmware mentioned in this post ships on the Bumble-B by default. If you have not flashed a new firmware program on your Bumble-B, you have what you need already installed on it. If you have changed it, you can change it back using the source or binary package from the Bumble-B page.

MCP3204

The MCP3204 reads analog data while we communicate over SPI and returns the result. This device calls the MOSI line "Din" and the MISO line "Dout". It has separate analog and digital voltage supply and ground pins, but we will combine them into a single 5 volt supply.

10k Potentiometer

The 10k pot provides a range of analog values so we can vary the input and hopefully receive the correct values through USB. The middle "wiper" pin should be connected to CH0 of the ADC. The other two pins are connected to the voltage supply and ground. The V+ and GND wires of the pot can be connected in either direction. This simply affects which end of the pot will have a value near 0x0000 and which is near 0x0FFF (we're using a 12 bit ADC).

Step 1: Wire It Up

Connect all pins as shown in the above schematic. The different colors show networks who's wires are connected. The ground and voltage supply line connect to several points, as indicated by dotted connections on the schematic.

Step 2: Connect To Computer, Verify

Connect the Bumble-B to a USB port on your computer. If the userial firmware is loaded, in Windows you will get a new COM serial port (see note 2). In Linux, a new device will be created in the /dev directory, likely named /dev/ttyACM0. Check the output of `dmesg` after plugging the device to see what /dev entry was created.

The port settings are 115200/8/N/1, no hardware flow control.

Using your favorite terminal program (or sometimes in Linux, `echo` and `cat` can be used directly on the device file) open the communications port described above. In Linux, options include minicom and gtkterm. In Windows, hyperterm or realterm can be used. PuTTY will run on both platforms.

Point your terminal to the com port, change settings if necessary, and run the "V" command:

V
Bumble-B 0.4 (userial 1.6)

Step 3: Initialization

GCDIIIIIIIO
GCDIIIIIIIO

This sets port D, pin 0 as an output, the rest of port D as inputs.

GBDxxxxxxx1
GBD11111111

Set PD0 high. This is the chip select (CS) line of the MCP3204. This line should normally be high (1) and be set low (0) during communication. The 'x' tells userial that these other bits of port D should not be altered.

SC0M007D
SC0M007D

Here we tell userial to use SPI mode (0) (shift on the falling edge, capture on the rising edge), (M)ost significant bit first, and try to use hexidecimal speed value of (007D) (this works out to F_OSC / 128).

So the return value shows us the settings again "SC0M007D" - mode 0, MSB first, 0x007D speed.

Step 4: Reading

GBDxxxxxxx0
GBD11111110

First, bring PD0 low to enable communication.

SW060000
SWFFECBC

This is the entire SPI transaction, as described on page 21 of the MCP3204 datasheet, FIGURE 6-1: SPI Communication using 8-bit segments (Mode 0,0: SCLK idles low).

Three bytes are written. The addressing command for selecting a channel to read is encoded into the first two bytes and the last is padding. We are addressing CH0, single ended: SGL/DIFF=1, D2=0, D1=0, D0=0.

  1. 0b00000110 = 0x06
    The bottom three bits of byte 1 are: [start bit (1)] [single-ended (1) / differential (0)] [bit two of channel address - D2]. The top 5 bits should be zero.
  2. 0b0000000 = 0x00
    The top two bits of byte 2 are: [bit one of channel address - D1] [bit zero of channel address - D0]. The bottom 7 bits the ADC will ignore, so set to zero. D0 and D1 are both zero which makes the whole value a zero.
  3. 0b0000000 = 0x00
    The last byte sent is a zero and is padding - during this zero byte, the bottom 8 bits of the ADC value are transferred.

In the returned value, the first byte should be completely ignored. Return bytes two and three contain the twelve bit sample (0xECBC in the example above).

The upper four bits of byte two should be ignored. The bottom four bits of byte two are the top four bits of the twelve bit sample. (0xEC & 0x0F) = 0x0C.

The last byte contains the bottom eight bits of the sample. The example above contained 0xBC in this byte. ((0x0C << 8) + 0xBC) = ADC sample value of 0x0CBC.

Step 5: Cleanup

GBDxxxxxxx1
GDB10001001

Bring PD0 high again, to end communications, release the SPI bus, and stop sampling.

To read another sample, start again at step 4.

Step 6: Going Further

So hopefully this tutorial has explained how to communicate effectively with an SPI device. The important things to mind when connecting a new device are:

  1. Pinouts
    Make sure you are correctly connecting SPI, and toggling the perhiperal CS (or SS - Chip Select or sometimes called Slave Select) line before communicating. Some manufacturers call the connections of SPI names other than MOSI/MISO/SCLK, so pay attention when mapping these pins.
  2. SPI Communications Format
    Check your local datasheet for communication details. Userial can handle the various modes and speeds of many SPI devices, provided they can be accessed in 8 bit boundaries - a common limitation of hardware SPI implementations.

Have fun! Let us know if you connect something new and cool to a Bumble-B using this technique!


(1) NOTE: The power supply must be in configuration 2 or 3 as described on page 4 of the Bumble-B datasheet.

(2) NOTE: Windows will ask for a driver the first time you plug the Bumble-B. It does not need any actual binary drivers, those are built into Windows. It does however need a .inf file describing the device. (Why? Who knows. Blame Microsoft...)

Extract the source or binary Bumble-B firmware package, and point Windows manually to the Bumble-B the extracted directory with the file 'userial.inf' inside it. userial.inf contains a description of Bumble-B that Windows uses to make a virtual COM port.

Update: If you're looking for C code for this chip, here is MCP3204 AVR Driver version 0.1.

 
 
 
 

Re: FLIP

You will need the Atmel driver inside of the Flip directory. When Windows asks for a driver, point it to C:\Program Files\Flip\usb. I'm going off the top of my head, that last directory might be named something slightly different. Look for a directory with an .inf in it.

Once you've located that, follow the directions in the Bumble-B datasheet for getting into programming mode. It involves tying HWB and RESET to ground temporarily. HWB -> GND; RESET -> GND; lift RESET; lift HWB. Windows will now ask for a new driver. This is where you point it to Flip.

Now, open Flip. Change the MCU to at90usb162. Do "Open USB" (Ctrl-U). You can now erase (always erase first!), flash, and verify.

During C language development time, I normally attach 2 tactile buttons to HWB/RESET -> GND so that it's just a quick 1-2 button press to get back to DFU programming mode.

Hope that helps!

 
 
 

FLIP

Having great difficulty getting bumble b to work
with flip. Works well with hyperterm. Does not
pick off the usb90 driver as best as i can tell.
The auto mode does not put device in SP1 mode.

Any hints, suggestions

 
 

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options