USB on a Stamp

by shahroz094 in Workshop > Energy

3211 Views, 5 Favorites, 0 Comments

USB on a Stamp

usbmod2.jpg
usb.jpg
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:
1.Parts and Circuit Diagram
2.Procedure
3.Macintosh Test Application
4.A USB based LCD (in progress)

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


Step 1: One Direction - USB to RS232.

untitled.bmp
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.

Step 2: One Direction - RS232 to USB.

untitled.bmp
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.

Step 3: Bidirectional

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...

Step 4: File Transfer

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.

Macintosh Test Application

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:
Sub Open()
Dim i as Integer, port as string
'Check what serial ports are available including
'VPC's like the one provided by the FTDI extension
for i = 0 to System.serialPortCount - 1
port = System.serialport(i).Name
if port = "USBHighSpeedSerialConverter" then
serial1.SerialPort=system.SerialPort(i)
goto endFTDIcheck
end if
next
if port <> "USBHighSpeedSerialConverter" then
msgbox "The USBMOD2 is not connected, now quitting"
quit
end if
endFTDIcheck:
End Sub

Window1.PushButton1.Action:
Sub Action()
serial1.write editField1.text
End Sub

USB Based LCD

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*****
'CONSTANTS
EnableLCD CON 8 ' LCD Enable pin (1 = enabled)
RW CON 9
RS CON 10 ' Register Select (1 = char)
readUSB CON 11 'P11 = /RD on the USB module
ClrLCD CON $01 ' clear the LCD
CrsrHm CON $02 ' move cursor to home position
CrsrLf CON $10 ' move cursor left
CrsrRt CON $14 ' move cursor right
DispLf CON $18 ' shift displayed chars left
DispRt CON $1C ' shift displayed chars right
DDRam CON $80 ' Display Data RAM control
CGRam CON $40 ' Custom character RAM
Line1 CON $80 ' DDRAM address of line 1
Line2 CON $C0 ' DDRAM address of line 2

' Variables
char VAR Byte ' character sent to LCD

'*****MAIN PROGRAM*****

HIGH readUSB
LOW RW
HIGH RS
LOW EnableLCD

Initialize_LCD:
DIRS=%0001111111111111
LCD_Init:
PAUSE 500 ' let the LCD settle
char = 56 ' multi-line mode
GOSUB LCD_Command
PAUSE 5
PULSOUT EnableLCD, 1
PULSOUT EnableLCD, 1
char = 12 ' disp on, crsr off, blink off
GOSUB LCD_Command
char = 6 ' inc crsr, no disp shift
GOSUB LCD_Command
char = ClrLCD
GOSUB LCD_Command

ReadFIFO: 'Listening to USB now
IF IN14 = 0 then GetUSBdata 'if /RXF is low, Data is available
goto ReadFIFO

GetUSBdata:
DIRS=%0001111100000000
LOW readUSB
char = INL
HIGH readUSB
DIRS=%0001111111111111
GOSUB LCD_Write
GOTO ReadFIFO

LCD_Command:
LOW RS ' enter command mode

LCD_Write:
OUTL = char
PULSOUT EnableLCD, 1 ' strobe the Enable line
HIGH RS ' return to character mode (if not already in character mode
RETURN