Quantcast
Channel: xanthium enterprises - robotics and embedded system for all
Viewing all articles
Browse latest Browse all 134

USB to 8 bit Parallel port Or Asynchronous Bit Bang mode of FT232

$
0
0

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 BangMode has no such requirements) .

Source codes of this tutorial can be downloaded from here.

Hardware 

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 positions in USB2SERIAL port

Pin numbers for the corresponding data ports of FT232RL are given below.

table for pin numbers of FT232RL

Connect the Data ports D0-D7 to LED's using 1K resistors as shown in the figure.Make the grounds common.

connecting LED's to FT232 data port in asynchronous bit bang mode

 If you are using the USB2SERIAL board you can take Ground from Connector P4 (both pins) or from GND pin of K2.

 

Asynchronous BBM

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( )
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 FT_handle;
UCHAR Mask;
UCHAR Mode;

FT_SetBitMode(FT_handle,Mask,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

Modeis 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

FT_SetBaudRate( )
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.

DWORD BaudRate;
BaudRate = 9600;
FT_SetBaudRate(FT_handle,BaudRate);

FT_Write( )
Used to write data to a device identified by the handle.

FT_HANDLE FT_handle;
UCHAR     Buffer = 0xAA
DWORD     BytesWritten = 0;

FT_Write( FT_handle,      // handle to the chip
            &Buffer,      // address of the- 
                          //-variable containing data

          sizeof(Buffer)// Size of the Buffer
          &BytesWritten   // Number of bytes written 
         );

&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 theCsizeof()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.

Code

Now open up a text editor and type out the code given below.

#include <stdio.h>
#include <windows.h>  // for windows specific keywords in ftd2xx.h
#include "ftd2xx.h"  // Header file for ftd2xx.lib 
int main(void)
    {
        FT_HANDLE FT_handle;    // handle to FT 232 chip
        FT_STATUS FT_status;    // status of the FT 232 chip

        UCHAR Mask;             // for selecting which pins are input/output
        UCHAR Mode;             // Selects the mode of operation for the chip
        DWORD BaudRate;         // for selecting the Baud Rate
        UCHAR Buffer = 0xAA;    // data to be written to the port
        DWORD BytesWritten = 0// No of bytes written by FT_Write()
              
        FT_status = FT_Open(0,&FT_handle); // Open  a connection to FT232RL
        
        Mode0x01;              // Select Chip mode as Asynchronous Bit Bang
        Mask0xff;              // 8 bit port all output

        FT_status = FT_SetBitMode(FT_handle,Mask,Mode); //sets Asynchronous BBM
        
        BaudRate = 9600;       // Setting the Baud rate to 9600

        FT_status = FT_SetBaudRate(FT_handle,BaudRate); //Sets FT232 to 9600 baud
        
        FT_status = FT_Write( FT_handle,      // handle to the chip
                              &Buffer,        // address of the data
                              sizeof(Buffer), // Size of the Buffer
                              &BytesWritten   // Number of bytes written 
                            );

         getchar();          // Press any key to reset the chip 
        
         Mode = 0x00;         // Mask = 0x00 will reset the chip and-
                              // -get it out of Asynchronous mode
         FT_status = FT_SetBitMode(FT_handle,Mask,Mode); // Reset the chip 
        
         FT_Close(FT_handle); // Close the Serial port connection

     }

 

 

Save it as "asynch_BBM.c" and compile it using gcc.Please make sure that ftd2xx.libftd2xx.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.

USB2SERIAL  board interfaced with LED's  in Asynchronous bit bang mode of FT232

 

Previous Tutorial                                      Main Page                                         Next Tutorial

 

 


Viewing all articles
Browse latest Browse all 134

Trending Articles