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. 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-BThe 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. MCP3204The 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 PotentiometerThe 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 UpConnect 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, VerifyConnect 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:
Step 3: Initialization
This sets port D, pin 0 as an output, the rest of port D as inputs.
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.
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
First, bring PD0 low to enable communication.
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.
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
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 FurtherSo hopefully this tutorial has explained how to communicate effectively with an SPI device. The important things to mind when connecting a new device are:
Have fun! Let us know if you connect something new and cool to a Bumble-B using this technique!
(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. |
Software |
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