USB Based Stepper Motor Controller

Download (PDF, 1.98MB)


The Universal Serial Bus is one of the most common interfaces used in electronic consumer products today,
including PCs, cameras, GPS devices, MP3 players, modems, printers, and scanners etc. The USB was
originally developed by Compaq, Microsoft, Intel, and NEC, and later by Hewlett- Packard, Lucent, and Philips as
well. These compa- nies eventually formed the nonprofit corporation USB Implementers Forum Inc. to organize
the development and publication of USB specifications. In this article we shows how to control a stepper motor
with a USB port of a PC with the help of PIC microcontroller.
The USB is a high-speed serial interface that can also provide power to devices connected to it. A USB bus
supports up to 127 devices connected through a four-wire serial cable of up to three or even five meters in length.
Many USB devices can be connected to the same bus with hubs, which can have 4, 8, or even 16 ports. A device
can be plugged into a hub which is plugged into another hub, and so on.

USB Signal

USB signals are bi-phase, and signals are sent from the host computer using the NRZI data encoding technique.
In this technique, the signal levels inverted for each change to a logic 0. The signal level for a logic 1 is not
changed. A 0 bit is “stuffed” after every six consecutive ones in the data stream to make the data dynamic (this is
called bit stuffing because the extra bit lengthens the data stream). A packet of data transmitted by the host is sent to every device connected to the bus, traveling downward through the chain of hubs. All the devices receive the
signal, but only one of them, the addressed one, accepts the data. Conversely, only one device at any time can
transmit to the host, and the data travels upward through the chain of hubs until it reaches the host. USB devices
attached to the bus may be full-custom devices, requiring a full-custom device driver, or they may belong to a
device class.
Deviced classes enable the same device driver to be used for several devices having similar functionalities. For
example, a printer device has the device class 0x07, and most printers use drivers of this type.
Endpoint: An endpoint is either a source or a sink of data. A single USB device can have a number of endpoints,
the limit being sixteen IN and sixteen OUT endpoints.
Transaction: A transaction is a transfer of data on the bus.
Pipe: A pipe is a logical data connection between the host and an endpoint.

USB States

Idle: The bus is in idle state when the pulled-up line is high and the other line is low. This is the state of the lines
before and after a packet transmission. Detached: When no device is connected to the bus, the host sees both
lines as low.
Attached: When a device is connected to the bus, the host sees either D+ or D_ go to logic high, which means a
device has been plugged in.
J state: The same as idle state.
K state: The opposite of J state.
SE0: The single ended zero state, where both lines on the bus are pulled low.
SE1: The single ended one state, where both lines on the bus are high. SE1 is an illegal condition on the bus; it
must never be in this state.
Reset: When the host wants to communicate with a device on the bus, it first sends a “reset” condition by pulling
low both data lines (SE0 state) for at least10ms.
EOP: The end of packet state, which is basically an SE0 state for 2 bit times, followed by a J state for 1 bit time.
Keep alive: The state achieved by EOP. Keep alive is sent at least once every millisecond to keep the device from
suspending.
Suspend: Used to save power, suspend is implemented by not sending anything to a device for 3ms. A suspended device draws less than 0.5mA from the bus and must recognize reset and resume signals.
Resume: A suspended device is woken up by reversing the polarity of the signal on the data lines for at least
20ms, followed by a low-speed EOP signal.

Data Transmission

Data is transmitted on a USB bus in packets. A packet starts with a sync pattern to allow the receiver clock to
synchronize with the data. A packet identifier (PID) byte immediately follows the sync field of every USB packet. A
PID itself is 4 bits long, and the 4 bits are repeated in a comple- mented form. There are seventeen different PID
values There are four packet formats, based on which PID is at the start of the packet: token packets, data
packets, handshake packets, and special packets.
Enumeration
When a device is plugged into a USB bus, it becomes known to the host through a process called enumeration.
The steps of enumeration are, When a device is plugged in, the host becomes aware of it. The host sends a USB reset signal to the device to place the device in a known state. The reset device responds to address 0. The host
sends a request on address 0 to the device to find out its maximum packet size using a Get Descriptor command.
The device responds by sending a small portion of the device descriptor. The host sends a USB reset again. The
host assigns a unique address to the device and sends a Set Address request to the device. After the request is
completed, the device assumes the new address. At this point the host is free to reset any other newly plugged-in
devices on the bus. The host sends a Get Device Descriptor request to retrieve the complete device descriptor,
gathering information such as manufacturer, type of device, and maximum control packet size. The host sends a
Get Configuration Descriptors request to receive the device’s configuration data, such as power requirements and the types and number of interfaces supported. The host may request any additional descriptors from the device.
The most common USB descriptors are: Device descriptors, Configuration descriptors Interface descriptors, HID
descriptors, Endpoint descriptors. Here i cannot discuss all these things because of space limitation.
PIC18F4550 microcontroller is use for USB communication with PC. PORTC pins RC4 (pin 23) and RC5 (pin 24) are used for USB interface. RC4 is the USB data D_ pin, and RC5 is the USB data D+ pin. Internal pull-up resistors are provided which can be disabled (setting UPUEN =0) if desired and external pull- up resistors can be used instead. For full-speed operation an internal or external resistor should be connected to data pin D+, and for low-speed opera- tion an internal or external resistor should be connected to data pin D-. Operation of the USB module is configured using three control registers, and a total of twenty-two registers are used to manage the actual USB transactions The operation of the USB link requires the microcontroller to keep the connection alive by sending keep-alive messages to the PC every several
milliseconds. This is achieved by setting up a timer interrupt service routine using TIMER 0. Timer TMR0L is
reloaded and timer interrupts are re-enabled just before returning from the interrupt service routine. Inside the main program PORTB is defined as digital I/O and TRISB is cleared to 0 so all PORTB pins are outputs. All the interrupt registers are then set to their power-on- reset values for safety. The timer interrupts are then set up. The timer is operated in 8-bit mode with a prescaler of 256. Although the crystal clock frequency is 8MHZ, the CPU is operated with a 48MHz clock, by enabling its PLL. Selecting a timer value of TMR0L = 100 with a 48MHz clock
gives timer interrupt intervals of 3.3ms.
Circuit Diagram of USB controlled Stepper Motor Interface is show in fig. 1. PCB layout of USB controlled Stepper
Motor Interface is show in fig. 2. Component layout of USB controlled Stepper Motor Interface is show in fig. 3.

Subscribe Newsletter