USB to 8 bit Parallel port Or Asynchronous Bit Bang mode of FT232
And finally ,after all those lecturing (for those who had the patience to go through all those previous articles ) about the IDE's,setting up a connection and so on, we have reached where the real action is.In this section we are going to convert the pins of the FT232 (TXD,RXD,RTS,DTS...RI) to an 8 bit parallel port using the good old D2XX library.One use of Asynchronous Bit bang mode is the replacement of legacy parallel port based devices using FT232 chip.
FT232 chip has a special mode called Bit Bang Mode(BM) which turns the 8 I/O lines into a 8 Bit bidirectional parallel port .There are 3 different types of bit bang modes
- Asynchronous Bit Bang Mode
- Synchronous Bit Bang Mode
- CBUS Bit Bang Mode
Here we are going to talk only about Asynchronous Bit Bang Mode and CBUS Bit Bang Mode.Please note that to enable the CBUS Bit Bang Mode the corresponding bits in the EEPROM should be set using FT Prog (Asynchronous Bit Bang Mode has no such requirements ) .
Source Code and Library
All the C sourefiles along with D2XX library used in this tutorial can be downloaded from our GitHub Page.For this Tutorial use the " Asynch_BBM " Directory inside the code repository.
Here i am using USB2SERIAL board ,in which all the relevant pins for Asynchronous Bit Bang Mode are brought out to male bergs for easy access.
Any FT232RL breakout board with access to the required pins (TXD,RXD,RTS,DTS...RI) can be used or you can use the USB2SERIAL breakout board.Keep the circuit mentioned in the previous article handy as it can be of great help in visualizing the data from the port.The 8 bit parallel port signals (D0 -D7) are marked in blue in the image below.Ground of FT232 can be taken from either P4 ( 2 pins are ground ) or from GND pin of K2.
Pin numbers for the corresponding data ports of FT232RL are given below.
Connect the Data ports D0-D7 to LED's using 1K resistors as shown in the figure.Make the grounds common.
If you are using the USB2SERIAL board you can take Ground from Connector P4 (both pins) or from GND pin of K2.
Asynchronous Bit Bang mode is a special mode of the FT232 chip which turns the pins (TXD,RXD,RTS,DTS...RI) into an 8 bit bidirectional parallel port.Any data written to the device will be self clocked to the output pins.The rate at which the data is clocked is controlled by the baudrate generator.Each pin (D0-D7) can be independently set as input or output.When no new data is written the pins hold the previous data.
For setting up the Asynchronous Bit Bang mode you will need the following functions
- FT_Open( );
- FT_SetBitMode( );
- FT_SetBaudRate( );
- FT_Write( );
- FT_Close( );
Both FT_Open() and FT_Close() are explained in the previous articles and is used for openning and closing the connection to the chip.
FT_SetBitMode() is an extended API function in the D2XX library.It is used to select the various modes of FT232 chip like Asynchronous Bit Bang mode,Synchronous Bit Bang mode and CBUS Bit Bang mode.
FT_handle is the same as in the FT_Open() function and is used as a handle to access the selected chip.
Mask is an 8 bit unsigned char used to control the direction of port pins (input /output).A bit value of 0 sets the corresponding pin to input and a bit value of 1 sets the corresponding pin to output.
|Mask = 0x00 // all input
Mask = 0xFF // all output
Mask = 0x0F // upper nibble input,lower nibble output
Mode is used to select the required mode for the FT232 chip .
|Mode = 0x00 // Resets the chip
Mode = 0x01 // Selects the Asynchronous Bit Bang Mode
Mode = 0x20 // Selects the CBUS Bit Bang Mode
The function is used to set the baudrate at which data is clocked out of the device.You can use standard values like 4800,9600,19200 etc.
BaudRate = 9600;
Used to write data to a device identified by the handle.
FT_Write( FT_handle, // handle to the chip
&Buffer is a pointer to the variable Buffer.Buffer contains the data to be written to to the port (here 0xAA).
sizeof(Buffer) is used to determine the size of the data type (no of bytes).Here we are using the C sizeof()operator to determine the no of bytes occupied by the variable Buffer.
&BytesWritten is a pointer to the variable BytesWritten which will return the number of bytes written to the device.
Now open up a text editor and type out the code given below.
UCHAR Mask; // for selecting which pins are input/output
FT_status = FT_SetBitMode(FT_handle,Mask,Mode); //sets Asynchronous BBM
FT_status = FT_SetBaudRate(FT_handle,BaudRate); //Sets FT232 to 9600 baud
getchar(); // Press any key to reset the chip
Save it as "asynch_BBM.c" and compile it using gcc.Please make sure that ftd2xx.lib, ftd2xx.h and your source file are in the same directory.Connect the port pins to the LED's .Run the resulting executable .The led's will glow alternatively to show the pattern 0xAA.
I have added a few extra lines of codes to the above C program to make it more descriptive.The below image shows how to compile and execute the code on Windows 7 PC using USB2SERIAL development board.
You can download the code files along with ftd2xx library and header files from here.
Below image shows the connection between USB2SERIAL board and LED's showing 0xAA being written.