e009044.pdf

(802 KB) Pobierz
000079-UK USB-interface
COM PUTER
USB Interface
Experimenting with the Universal Serial Bus
Design by B. Kainka
Everybody is talking about the USB. In the long term, this new serial
interface could replace many of the PC interfaces that have been used up
to now. This is reason enough for looking at it more closely.
Before getting into practical details, let’s first
look at the basic features of the Universal Ser-
ial Bus. In USB Version 1.0, there are
‘lowspeed’ devices with a data rate of
1.5 Mb/s, and ‘fullspeed’ devices with a data
rate of 12 Mb/s. Even the lower data transfer
rate is several times faster than the possible
baud rates of a serial interface.
Up to now, we have often been faced with
unpleasant situations in which all of the PC
interfaces are already in use. The
advantage of the USB here is that
you can always make four new ports
available by connecting a supple-
mentary bus distributor (hub). Up to
127 devices in total can be con-
nected to the bus.
The USB connection also provides
the operating voltage for small
devices. Up to 100 mA can be drawn
from the interface without any spe-
cial provisions. With a special
demand login, this can be increased
to 500 mA. Typical laboratory appli-
cations often need significantly less
than this. Eliminating the extra
power cable for each device helps in
the struggle against the general
‘cable salad’.
USB devices can always be con-
nected and disconnected while the
system is operating. The operating
system automatically loads the nec-
essary driver. This extended Plug-
and-Play capability considerably
simplifies working with multiple
devices. A bus number is automati-
cally assigned to each newly con-
nected device (enumeration). The
operating system first reads a cer-
tain amount of information from the
device and then automatically loads
a suitable driver.
This means that every USB device
is already significantly more intelli-
gent than a device with an RS232
interface. It must make itself known
to the operating system by means of
a series of description tables (descrip-
tors), in order that the correct driver
can be selected. With the very first
connection, the operating system
requests a diskette with the driver.
For all subsequent reconnections, the
driver is loaded automatically, so that
the user does not have to take any
action. If the device is disconnected
from the PC, the driver is automati-
138912474.037.png
COM PUTER
cally deleted from memory.
Unfortunately, USB can presently be
used only with PCs that use the Win-
dows 98 operating system. Although
USB can in principle be used under
Windows 95 from version SR2
onwards, this is not completely reli-
able. Consequently, Windows 98 is
recommended for use with the USB
Interface. Windows NT5 and Win-
dows 2000 support USB, but Win-
dows NT4 does not support USB at
all. However, the software for this
project has not been tested with
Windows 2000.
6 MHz
RESONATOR
R/C EXT
CY7C63001
OSC
INSTANT-ON
NOW TM
RAM
128 Byte
8-bit
Timer
EPROM
2K/4K Byte
RAM
128 Byte
Power
on
Reset
Interupt
Controller
USB
Engine
PORT
0
PORT
1
Watch
Dog
Timer
The circuit
D+, D-
V CC / V SS
P0.0 - P0.7
P1.0 - P1.3
000079 - 12
The USB Interface is based on the
Cypress CY7C6300 microcontroller.
This IC was primarily developed for
small USB devices, such as mice and
joystick ports, and it contains a com-
plete USB engine for the lowspeed
USB. The two data lines (D+ and D–)
are connected directly to the micro-
controller. Figure 1 shows the block
diagram of the IC. In addition to a
RISC processor, it contains an OTP
ROM for the operating software
(firmware) and two ports with a total
of twelve output lines, as well as
RAM and a timer.
The USB Interface presented here
is based on a Cypress application.
Cypress made their USB thermome-
ter available in the CY3640 starter
kit. This kit was intended mainly to
promote familiarity with the proces-
sor and the circuit technology. It is
no longer available in the original
form, but we have resurrected it in
this Elektor project. You can use the
USB Interface to get acquainted with
the USB, and you can also use it for
many different types of serious appli-
cations.
The USB Interface includes the
following functions:
Figure 1. Block diagram of the CY7C63001.
5V
R1
R2
R3
C1
IC2
R4
8
100n
7
TH
DQ
1
6
2
TL
CLK
R5
D 1
D 2
S1
5
3
C2
TC
RST
100n
4
DS1620
12
K2
JP1
JP2
1
VCC
10
P0.0
P0.1
P0.2
P0.3
P0.4
P0.5
P0.6
P0.7
XI
2
3
4
X1
K1
IC1
K3
USB Type B
1
11
XO
20
19
18
17
6MHz
1
2
CY7
D –
13
C63001
3
4
14
A CP
D +
5
P1.0
P1.1
P1.2
P1.3
16
9
CEXT
6
15
10
VSS VPP
7
8
– temperature measurement
– polling a pushbutton switch
– setting the brightness of a LED
000079 - 11
Figure 2. Complete schematic diagram of the USB Interface with temperature sensor.
138912474.038.png 138912474.039.png 138912474.040.png 138912474.001.png 138912474.002.png 138912474.003.png 138912474.004.png 138912474.005.png 138912474.006.png 138912474.007.png 138912474.008.png 138912474.009.png 138912474.010.png
COM PUTER
1
COMPONENTS LIST
000079-1
H2
R4
H5
Resistors:
R1 = 47
R2 = 270
R3 = 10k
R4 = 47
R5 = 1k 5
+
K1
0
K3
C1
X1
JP2
Capacitors:
C1,C2 = 100nF
R1
R2
R3
C2
D1
D2
Semiconductors:
D1 = LED, red,
D2 = LED, green
IC1 = CY7C63001ACP
(programmed, order code
000079-41 )
IC2 = DS1620
JP1
H4
S1
Figure 4. Screen display of the original
Cypress demo program.
Figure 3. The USB Interface can be build using
this printed circuit board.
Miscellaneous:
JP1,JP2 = 2-way pinheader with
jumper
K1 = 10-way PCB terminal block
K2 = solder pin
K3 = USB socket, PCB mount,
type B (Farnell #153-503)
S1 = pushbutton 1 make contact
X1 = 6MHz ceramic resonator,
(Murata CSA6.00MG (Farnell #
295-292), or Newport
ZTA6.00MT
Enclosure: approx. size 61x22x80
mm (Conrad Electronics
#522848)
Project disk,
order code 000079-11
PCB, order code 000079-1
Figure 5. The program allows access
to all port functions of the
microcontroller, and also displays the
temperature.
– nine freely-accessible I/O ports
The pushbutton toggles an internal
program register, which can be
polled via the USB.
The two bus lines, D+ and D–, are
led to a Type B USB connector. If you
cannot come up with such a connec-
tor, you may solder the USB cable
directly to the circuit board. The USB
Interface takes its operating voltage
(+5 V and earth) from the bus. The
second (red) LED, D1, which should
be a low-power type, indicates the
presence of the supply voltage. This
voltage can also be used for other
purposes via the solder posts K2.
The 47-k
capacitors are already integrated
into the IC.
The USB Interface is fully compatible with the
original Cypress thermometer application.
This means that you can use all sample pro-
grams and the thermometer driver. However,
in the original application, the port accesses
were not fully functional. Consequently, the
firmware has been modified, and it also
includes additional functions.
Figure 2 shows the circuit diagram. The
temperature is sensed by a Dallas DS1620 IC,
which occupies three port lines (P0.0–P0.2).
The remaining nine lines (P0.3–P0.7 and
P1.0–P1.3) are led out to terminal strip K1, for
general use. In addition, a green LED (not a
low-current type!) can be connected to port
connection P1.3 via jumper JP1. This LED
indicates successful enumeration, and it can
be set to 16 different brightness levels. How-
ever, this works only if pushbutton switch S1
is connected to port P1.2 via jumper JP21.
Construction and testing
resistor ensures that no
more than the allowed 100 mA can
be drawn from the USB connection
in case of a short circuit.
The microcontroller is clocked by
a 6-MHz ceramic resonator with two
(and not three!) leads. The necessary
The construction of the small printed
circuit board shown in Figure 3
should not present any problems.
Sockets may be used for both ICs.
Once everything has been assem-
bled and there are no obvious con-
struction errors, there comes the
moment of truth. Connect the USB
Interface to a PC using a type A–B
USB cable. After a moment, Win-
dows will recognise that new hard-
ware has been connected. A win-
dow will appear with a request to
insert a diskette with a suitable dri-
ver. You can comply by inserting the
diskette labelled 000079-11 in drive
A. Here the system will find the file
138912474.011.png 138912474.012.png 138912474.013.png 138912474.014.png 138912474.015.png 138912474.016.png 138912474.017.png 138912474.018.png 138912474.019.png 138912474.020.png 138912474.021.png 138912474.022.png 138912474.023.png 138912474.024.png 138912474.025.png 138912474.026.png 138912474.027.png 138912474.028.png 138912474.029.png 138912474.030.png 138912474.031.png 138912474.032.png 138912474.033.png 138912474.034.png
COM PUTER
Info on the Internet
Fahrenheit or Celsius degrees for the temper-
ature display. In addition, the brightness of
the green LED can be altered. The microcon-
troller includes a 4-bit D/A converter that can
be used to set the level of the current sunk by
the Port 1 outputs. However, the software
only allows Port P1.3 to be set. Figure 4
shows how the temperature is displayed on
the monitor.
Microsoft VB5
www.microsoft.com/msdownload/vbcce.htm
Cypress CY7C63001ACP microcontroller
http://www.cypress.com/cypress/prodgate/usb/cy7c63001.html
Dallas temperature sensor
http://www.dalsemi.com/datasheets/pdfs/1620.pdf
Programming
in Visual BASIC
An interface is not especially interesting if
you cannot drive it using your own programs.
You can access the device via a small Visual
BASIC program. We use V5BCCB, which
Microsoft allows to be downloaded free of
charge and which is fully functional, but
which cannot generate executable stand-
alone applications.
CypressSemiconductorsCYPRESS.INF
, which describes the device and the
driver. Following this, it loads the
actual driver USBTherm.sys . As soon
as this process is completed, the
device is enumerated, which means
that it is logged on to the system.
Now, not only the red LED is lit (if
JP2 is installed) but also the green
LED.
The Cypress demo software on
the diskette, Thermometer.exe ,
implements a thermometer with a
graphic display of temperature ver-
sus time. If JP1 is installed, the user
can use the keyboard to select
Listing 1. The USB1.BAS module with declarations
Attribute VB_Name = “Module1”
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Declare Function CreateFile Lib “kernel32” Alias “CreateFileA”
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long,
ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES,
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long,
ByVal hTemplateFile As Long) As Long
Declare Function DeviceIoControl Lib “kernel32” (ByVal hDevice As Long,
ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize
As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long,
lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
Declare Function CloseHandle Lib “kernel32” (ByVal hObject As Long) As Long
Public Security As SECURITY_ATTRIBUTES
Public gOverlapped As OVERLAPPED
Public hgDrvrHnd As Long
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_WRITE = &H2
Public Const FILE_SHARE_READ = &H1
Public Const OPEN_EXISTING = &H3
138912474.035.png
 
COM PUTER
Listing 2. Accessing all ports and functions
Dim sFileName As String
Dim htemp As Long
Dim lIn As Long, lInSize As Long, lOut As Long, lOut-
Size As Long, lSize As Long
Dim lTemp As Long
Sub USB_IO()
sFileName = “\\.\Thermometer_0”
hgDrvrHnd = CreateFile(sFileName, GENERIC_WRITE Or
GENERIC_READ, FILE_SHARE_WRITE Or FILE_SHARE_READ,
Security, OPEN_EXISTING, 0, 0)
lTemp = DeviceIoControl(hgDrvrHnd, 4&, lIn, lInSize,
lOut, lOutSize, lSize, gOverlapped)
htemp = CloseHandle(hgDrvrHnd)
End Sub
USB_IO
End Sub
Private Sub HScroll1_Change()
Wert = HScroll1.Value * 8 + 7
WrRAM 46, Wert
Label4.Caption = Str$(Wert)
End Sub
Private Sub HScroll2_Change()
WrRAM 47, HScroll2.Value
Label5.Caption = HScroll2.Value
End Sub
Sub Brightness(Level)
lIn = Level * 256 + 14
lInSize = 2
lOutSize = 1
USB_IO
End Sub
Private Sub HScroll3_Change()
Brightness HScroll3.Value
Label6 = HScroll3.Value
End Sub
Private Sub Timer1_Timer()
lIn = 11
lInSize = 1
lOutSize = 3
USB_IO
Temp = ((lOut \ 256) And 255) / 2
Minus = (lOut \ 65536) And 255
If Minus > 0 Then Temp = Temp * -1
Button = (lOut \ 16777216) And 255
Label7.Caption = Str$(Temp)
Label8.Caption = Str$(Button)
Label9.Caption = Str$(RdPort(0))
Label10.Caption = Str$((RdPort(1)) And 15)
End Sub
Function RdPort(Port) As Integer
lIn = Port * 256 + 20
lInSize = 2
lOutSize = 2
USB_IO
RdPort = (lOut / 256) And 255
End Function
Sub WrRAM(Adresse, Wert)
lIn = 65536 * Wert + Adresse * 256 + 23
lInSize = 3
lOutSize = 1
Functions
15h: Write Port
lIn: Value (0-255), Port (0,1), 15h (Length: 3 Bytes)
iOut: Status (Length: 1 Byte)
0Bh: Read Thermometer
lIn: 0Bh (Länge: 1 Byte)
iOut: Button, Sign, Temp, Status (Length: 4 Bytes)
16h: Read RAM
lIn: Address (0-255), 16h (Length: 2 Bytes)
iOut: Value, Status (Length: 2 Bytes)
0Eh: Set LED Brightness
lIn: Brightness (0-15), 0Eh (Length: 2 Bytes)
iOut: Status (Length: 1 Byte)
17h: Write RAM
lIn: Value (0-255), Address (0-255), 17h (Length: 3 Bytes)
iOut: Status (Length: 1 Byte)
14h: Read Port
lIn: Port (0,1), 14h (Length: 2 Bytes)
iOut: Wert, Status (Length: 2 Bytes)
18h: Read ROM
lIn: Index, Address (0-255), 18h (Length: 3 Bytes)
iOut: Value, Status (Length: 2 Bytes)
138912474.036.png
Zgłoś jeśli naruszono regulamin