Personal tools
You are here: Home Teaching EE20N: Hacking Stuff (Wtr'08) Lecture 9

Lecture 9

Summary: example programs to interface with a gyro and a USB-drive using a microcontroller.

Using a gyroscope

A gyroscope measures the rate of rotation about a certain axis. Before people built mechanical structures out of silicon, these used to be complicated and bulky mechanical or photonic devices. Now, you can buy a chip with a microscale gyro on it. In this lecture, I used a board you can buy from Sparkfun Electronics, that has a chip with 2 gyros on it (it also has 3 acellerometers). The BASIC program shown here will measure the analog output of one of the gyros (see the datasheet of the gyro-chip and schematic of the board) on AD5 and integrate the signal to turn a rate of rotation into an absolute angle.

Files:

Using a USB-stick

You might at some point need to store a lot of data (e.g. track information, images, sonar data, etc.) and the RAM on the microcontroller is of limited size. A convenient solution is to store it on a file on a USB flash drive. There is an easy way to do this using a VDRIVE adapter from Vinculum. This adapter allows you to treat a USB-stick as a drive. You can create files and write to them. Later on, these files can be read by a PC if needed. There's an example on the Coridium website here. This adapter needs 5V and GND and you need to connect 4 signal wires (CTS, TXD, RXD and RTS - no need to connect RI) to your microcontroller as shown in the VDRIVE2 user guide:

Connections.jpg

 

There are example programs on the Coridium website here. Basically, you need to put this file in the same directory as your program.
 You can then access a USB drive connected to the VDRIVE2 by doing the following in your BASIC program:

At the top of your program, you include this statement:

#include "vdriveSERIAL.bas"    ' this makes sure that this file is also processed by the compiler

Before you type any code, define your main program by the label:

main:              ' defines start of your code

Before you call any VDRIVE-specific subroutines, you need to state which pins you used on the microcontroller for the 4 signal wires and initialize the VDRIVE:

vdriveCTS = 4
vdriveTXD = 5
vdriveRXD = 6
vdriveRTS = 7

gosub vdriveInit

You can delete a file, open a file for writing, write to a file, and close a file as in the following example code:

vdriveString$ = "sonar.txt"        ' erase any pre-existing file
gosub vdriveFileDelete
vdriveString$ = "sonar.txt"     ' open a file for writing and send some data to it
gosub vdriveFileOpenWrite
FOR j=0 TO 71
 vdriveString$ = STR(j) + " " + STR(R$(j)) + chr(13)
 gosub vdriveFileWriteString        ' write a line to the file with data
NEXT
vdriveString$ = "sonar.txt"     ' close file
gosub vdriveFileCloss

This particular example produces the following file: sonar.txt. This is data from the sonar program demonstrated in Lecture 8. You can then use a plotting program to plot the data. Here are 3 consecutive sonar diagrams (red, green, and then blue) acquired while I was walking around my car. The part that is changing is me standing near the car.

 

 sonar.png

Files:

  • vdriveSERIAL.bas - this is the file you need to #include to use the VDRIVE
  • sonar2usb.bas - example program that realizes a sonar and saves data to VDRIVE


 

 

Document Actions