PIC USB Interface

Microchip PIC16F877 to FTDI USB interface

Current Status:   Hardware Complete (software ongoing)
4th April 2006 hardware design
Creative Commons Attribution 3.0 Unported License/


This is a simple design that interfaces a PIC 16F877 microcontroller to the USB bus using the FTDI FT245 USB FIFO device.
It has been designed using a simple single sided PCB with only one surface mount device, the USB chip itself. All schematics,
 PCB layouts and software are available for download

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Update, May 2nd 2011.
A datalogger design is in progress based around this PCB design is currently in development.
You can also use the FTDI UM245R USB to FIFO module and a breadboard to implement this design.

This design uses the old, FT245BM device, which has been surpassed by the newer FT245R device. The FT245R incorporates the
configuration EEPROM, oscillator and USB config/pullup resistors, thus simplifying the design. Any new designs should use the updated device.

I have published details of my design based around the FT232R, USB to serial device with a PIC16F88 built-in.


Design overview
Accessing the USB device using a PIC
The hardware design
PCB layout
V1 to V2 PCB
First tests of assembled PCB
Configuring the USB device under windows
The PIC software required
Installing the drivers
Setting up hyperterminal
More example software

Design overview

As stated in the introduction the design uses the FTDI FT245BM USB FIFO device. This part handles all the details of
communicating via the USB bus, all it needs is a simple, 1KBit serial EEPROM to hold some USB configuration
information and you are ready to go.

The FT245 has the following interface to the microcontroller (taken from the databook)

 D0 to D7 comprise the 8 bit data bus interface, in this design it connects to
PORT D of the PIC micro controller.

RD# and WR are the Read and Write pins of the device, they connect totwo
output pins on PORT C of the PIC microcontroller Both signals are active low.

Finally, RXF# is the RX FIFO flag, when logic 0, it indicates there is data to be
read from the device. TXE is used to indicate that it is possible to write to the FIFO
device, logic 0 indicates it is OK to write data. Both of these signals connect to input
pins of PORT C of the PIC microcontroller.




Accessing the USB device using the PIC

For the PIC to talk to this device is reasonably straightforward. To write data, you set PORTD as outputs, 
present a byte of data on D0-D7, toggle WR from high to low, wait a bit then toggle WR high. The data gets
latched into the device on the rising edge of WR. Finally set PORT D as inputs (high impedance state)

To read data, set PORT D as inputs, toggle RD from high to low, wait then read the 8 bits of data presented on D0-D7. Toggle RD to high to
 complete the cycle.

The FT245 data sheet provides the full timing for the device but even with a 20 MHz PIC, as used in my design, an external cycle takes 200ns,
 4x the minimum 50ns cyles in the device data sheets, there will be no timing issues.

The hardware design

Before going into any further detail, I present the schematic of my design below, click to enlarge.

It is fairly straightforward, the USB interface is copied from one of the FTDI example designs and the PIC interface is straightforward enough.
Note: the FTDI USB to FIFO interface could also be implemented using the FTDI UM245R module, if you want to avoid any SMT soldering.

PCB layout

Note: The PCB design files in Eagle CAD and Gerber format are available for download

My principle design aims for this project were that the software is simple enough and that the hardware could be built on a single sided PCB.
The USB device is mounted on the solderside of the PCB and tracks very easily to the PIC microcontroller.

With some careful layout this was quite easy to achieve and the following design was manufactured:

There are only 4 wire links on this PCB and the prototype is 125mm long by 56mm wide.

V1 to V2 PCB layout

After building and testing the prototype PCB, a few issues came to light necessitating a V2 PCB design to be made. The issues are listed here

  • The spacing between the tracking and the ground copper fill is too close (without soldermask) and solder shorts happen too easily.
  • The ORG pin of the 93C46 was connected to ground when it should not have.
  • I could not connect an LCD to the PCB easily as I left out a few connections.
  • The lengths of the USB pair, D+ and D- were not tightly matched. The design works but this should be corrected.
  • The USB connector hangs partially off the edge of the PCB. This will affect the long term mechanical strength.
  • The decoupling capacitor size was smaller than the fitted parts.

With the above in mind, a new design has been created but to date (4th April 2006), has not been built.
It is planned to update the design to incorporate the newer FT245R device, add a PICKIT2 debug/programming connector and
support for Arduino shields?

Download PCB and Schematics

The schematics and PCB files in Eagle CAD format are available here, PIC_USB.zip  (88K)

First tests of assembled PCB

The first thing I did after assembling the PCB was to inspect for solder shorts. There were a few, mainly due to the close coupling of the
signal traces and the ground plane. Once this was done, I removed the PIC microcontroller and plugged the PCB + USB device + EEPROM
 into a USB hub on my PC. When Windows popped up a 'Found new Hardware' window, I was happy.

The fact Windows detected new hardware shows that everything was wired up correctly and the USB interface and crystal were working.
The next step was to configure the device. 

 Configuring the USB device under Windows (requires an update for the new FTDI FT_Prog software)

Note: In the future I will add a guide on driver installation, for this stage you need the FTDI CDM drivers installed.

Since the release of the FTDI Common Driver Model (CDM) drivers setup is so much easier. Before this release, you had to install
the D2XX drivers to use MPROG, then un-install them, install the VCP (Virtual COM Port) drivers and then run your application, what a pain.

On the FTDI website there is an easy to use configuration utility called MProg. This allows you to program the EEPROM on the PCB via Windows,
for my PCB I used the following settings:

Most of this screen should be straightforward, the device type is selected, FT245BM and USB version are easy enough. As this design
draws power from the USB bus, we must specify the maximum current drawn (USB allows upto 500mA for a self powered device), in
this instance, I have chosen 200mA.

The 'Product Description' box is just to put in whatever you want. Once this has been programmed into the EEPROM, when you plug this
board into another PC, the Windows found new hardware wizard shows, in this instance, that 'Ians USB Demo board' has been found.

Note: You only have to programme the EEPROM once.

The PIC software required (for a first test)

At this stage I had built the hardware, detected it under Windows and programmed the EEPROM, it was now time to get the PIC to do something.
As explained in the 'Accessing the USB device using the PIC' section, some basic software to read and write to the USB device was written.
This took the form of two functions, usbsend and usbrecv both of which were written using MPASM syntax and are listed below.

Note: In the near future I will provide full sample programmes, as the time of writing (4th April 2005) I am still cleaning up my test code
as there are a few debug tests to remove before publication.

This is the function to Receive USB data from the FT245BM, the byte of data received is stored in the variable, usbdatarx.

; A function to check for data received from the USB interface and if present, read it.
; USB is on port D, RC0=READ, RC3=RX_Flag
    btfsc  PORTC,RXFULL; See if RX_flag = 0 which indicates there is data in the FIFO
    gotonodata; Debug mode, toggle RC6 high
    ;If we got here there is data to read. Set PORT D = Input, READ=High, wait, READ=Low then read port D
    banksel  TRISD
    movlw   INPORT
    movwf   TRISD; PORT D now input, safe to start the read access.
    banksel PORTD
    bcf PORTC,READBIT; READ now active LOW so READ port D
    movf PORTD,W
    movwf   usbdatarx  ; usbdatarx holds the byte of data received
    bsf PORTC,READBIT; Store received data for now.
    bcf PORTC,6; PORTC 6 = low = data, high = nodata


nodata  bsf PORTC,7
        bcf PORTC,6
        retlw   0
This is the function to transmit a byte of data via the USB interface, the byte to be transmitted is stored
in the variable, usbdatatx
; A function to send a byte of information via the USB interface. Checks the TXF flag prior to sending.
; USB is on port D, RC1=Write, RC2=TX_Flag
; The data to send will be in usbdatatx

    movf PORTC,W; Read in PORT C for debug
    btfsc PORTC,TXFULL; See if TX_Flag = 0 which indicates OK to transmit.
    retlw0; Return with 0 if full how????
    ;If we get here we can transmit. Set PORT D = output, WRITE=High, Wait, WRITE=Low then Write Data, Write=High
    banksel TRISD
    movlw OUTPORT
    movwf TRISD; PORT D now output
    banksel PORTD
    bsf PORTC,WRITEBIT ; Make sure WR is high before data output.
    movf usbdatatx,W
    movwf PORTD
    bcf PORTC,WRITEBIT; Write now active
    bsf PORTC,WRITEBIT; All done, WR = high.
    ; Now tristate the databus, set PORT D to input.
    banksel TRISD
    movlw INPORT
    movwf TRISD; PORT D now input
    banksel PORTD; Return to page 0

Example programs to Download

The first program is called USB_Code_Test.asm, this sends a group of Hex codes to ensure that the PCB is wired up correctly and that there are
no stuck bits/short circuits. To view the output on the PC I used the program, DLPTest available from DLP design at http://www.dlpdesign.com/test.shtml.
This software displays all the hex codes sent by the software. although you could use Hyperterminal, it will not handle the non ASCII characters.

Hello world!
My second program just sends "Hello world" to the PC. By default my board uses the Virtual COM Port (VCP) driver so you can view the output using Hyperterminal
or similar program, I recommend PuTTY. The setup is quite simple, when I plug the PCB into my PC, it installs as COM3: (it will be different on your PC). In the terminal program
you select a Baud rate of 9600 BPS, 8 bits, no parity and 1 stop bit (standard settings) and connect using COM3: easy! This assumes your USB board is configured like this.
Download USB_Hello.asm here.

USB ADC test program
This USB ADC test program reads the voltage supplied on pin RA0 and outputs it to the screen.
Download USB ADC test.asm  here.


Show how to use the above software in a few example programs,
Demonstrate setting up a virtual COM port and using hyperterminal
Information on accessing the device via Visual Basic using virtual COM port and direct USB access

Updated 08 July 2017