Thanks to manufactures like FTDI, adding USB to your electronics is now very easy.
The following links will give you pointers on getting USB to work with the Parallax Stamp, and can be easily modified for your favourite Microcontroller or programing language.
| USB to RS232 converter | Got legacy equipment, but your new Mac or PC is USB based? You can still use your old equipment with my Adapter. |
| USB on a Stamp | A simple example showing how to get USB working with a Stamp. Followed by a practical USB based LCD Display project. |
In the pipeline: (please check back): USB OEM Stamp
Contents:
Parts required:
- Parallax Stamp BS2
- FTDI USBMOD2. (The USBMOD2 can be found here).
Above: The USBMOD2
Note: You can power this circuit from the USB bus. No need for an external power supply.
This project was approached in four steps:
Step 1: One direction - USB to RS232.
Idea is to be able to press a Key on a USB based Mac, and have the Key's character displayed on a Windows PC.
How it works:
When the USBMOD2 has data in it's Receive Buffer, it sends /RXF low to tell the microcontroller there is data available to read.
The Microcontroller then has to send /RD low in order for the first byte of that data to be presented on the bus so it can be read. When /RD is sent high, if data is in the buffer, the next byte will be made available and presented on the bus at the next high to low transistion of /RD.
The Software:
To test, make sure you have the FTDI USB extension installed on your Mac. Use a Datacoms program like Clarisworks and select the USB port in the program's prefs. On the Windows side, use Hyperterminal, set to 9600,8,N,1.
This is the code:
|
'{$STAMP BS2} '*****DECLARE
VARIABLES***** readUSB CON 9 'P9 = /RD on the USB module '*****MAIN PROGRAM***** HIGH readUSB ReadFIFO: |
Idea is to be able to press a Key on a Windows PC, and have the Key's character displayed on a Mac.
How it works:
When the USBMOD2's Transmit Buffer is empty, it sends /TXE low to tell the microcontroller that it can send data to the USBMOD2.
The Microcontroller then has to place a byte on the bus, and write it to the USBMOD2's Transmit buffer by pulsing /WR low then high. As long as /TXE is low, the Micro can write to the USBMOD2.
The Software:
To test, make sure you have the FTDI USB extension installed on your Mac. Use a Datacoms program like Clarisworks and select the USB port in the program's prefs. On the Windows side, use Hyperterminal, set to 9600,8,N,1.
This is the code:
|
'{$STAMP BS2} '*****DECLARE
VARIABLES***** writeUSB CON 10 'VARIABLES '*****MAIN
PROGRAM***** |
Idea is to be able to type on the Mac or Windows PC and have the characters turn up on either.
Due to the nature of the SERIN command, I found that I had to put a timeout variable in the code, otherwise, we would never get to receive anything from the Mac.
This is the code. Set Hyperterminal to 9600,8,N,1. The original version of the code for this section gave me a throughput of 160bps max. But, after a little fine tuning of the code, I got up to 3200bps...
|
'{$stamp BS2} ' [DECLARE VARIABLES] '[MAIN CODE
BEGINS] ReadPorts: writeUSB: |
Transfer files to/from the Mac and PC.
Using the code in step 3, this works perfectly when transferring a file from the Mac to the PC. I managed to transfer the picture up the top of this page with no errors at all. Make sure both Terminal programs on your Mac and PC are using the same file transfer protocols with same settings.
However, it isn't working correctly going from the PC to the Mac. I'm sure it is to do with SERIN again. A limitation of the Stamp - not having a buffer on it's serial port.
Here is a simple test application for the Mac.
Note: You need to make sure you have the FTDI extension installed on your Mac.
The Application is written in Real Basic. It simply sends the message that is typed in the editfield to the USBMOD2, and using the program in step 1, that message will be displayed in Hyperterminal on the PC.
The program begins by checking your Mac to see what serial ports it has (including the VPC provided by the FTDI extension). If it doesn't find the USBMOD2, it will quit.
The source code for the application is below.
|
Window1.Open: Window1.PushButton1.Action: |
Adding USB to an LCD is easy. Matrix Orbital have a range of USB based displays already, that also use FTDI chips. Theirs uses the FT232 USB<->serial chip. We'll use the same circuit above, with an HD44780 based LCD attached, and the code modified so you can send commands or data to the display. This example will not have as many features as the Matrix Orbital range (as I don't want to out smart them - hey they gave me free samples after all!!!), but it will be useable on either RS232 or USB. My example uses the FT245 USB <=> Parallel chip.
<circuit here>
This code is not complete yet. The final version will allow you to choose between RS232 or USB, support various commands, etc. This code is a simple example showing that the idea works.
|
'{$STAMP BS2} '*****DECLARE VARIABLES***** ' Variables '*****MAIN PROGRAM***** HIGH readUSB Initialize_LCD: ReadFIFO: 'Listening to USB now GetUSBdata: LCD_Command: LCD_Write: |
<how it works here>