Skip to main content

Messages

This section allows you to view all Messages made by this member. Note that you can only see Messages made in areas you currently have access to.

Messages - Jonno

3
Breakout boards / FTDI FT2232H - Problem reading two bytes from I2C device
Hi All,

I am using visual c++ to communicate with three ICs using I2C, from a PC via usb using a FT2232H.

The devices I am connected via I2C are 2x AD5252 and 1x AD7993. The AD7993 contains 16 bit registers which I am having trouble reading from. I have successfully written and read from the 8bit registers on all three devices, but always get incorrect results when reading the 16bit registers.

Below is a copy of the code I use for reading the 8bit registers in the AD7993 AtoD converter. The code is written in Microsoft Visual C++ 2008 Express Edition and based on code from the I2CTEST example code found at the ftdichip website


I would appreciate it if anyone could point me to how to modify the code for reading two bytes?

Code: [Select]
    //////////////////
    ///READ SECTION
    ///////////////////
    //Purge USB receive buffer first before read operation
    ftStatus = FT_GetQueueStatus(ftHandle, &dwNumInputBuffer); // Get the number of bytes in the device receive buffer
    if ((ftStatus == FT_OK) && (dwNumInputBuffer > 0))
    FT_Read(ftHandle, &InputBuffer, dwNumInputBuffer, &dwNumBytesRead); //Read out all the data from receive buffer
    //////////////////////////////////////////////////////////
    // Set I2C start condition
    //////////////////////////////////////////////////////////
    HighSpeedSetI2CStart();
    bSucceed = SendByteAndCheckACK(0x58); // Address of AD5252 with LSB=0 for write command
    bSucceed = SendByteAndCheckACK(0x01); // Register RDAC1 for W1
    HighSpeedSetI2CStart();
    bSucceed = SendByteAndCheckACK(0x59); // Address of AD5252 with LSB=1 for read command
    //////////////////////////////////////////////////////////
    // Read the data from I2C device
    //////////////////////////////////////////////////////////
    //dwNumBytesToSend = 0; //Clear output buffer
    OutputBuffer[dwNumBytesToSend++] = 'x80'; //Command to set directions of lower 8 pins and force value on bits set as output
    OutputBuffer[dwNumBytesToSend++] = 'x00'; //Set SCL low, WP disabled by SK, GPIOL0 at bit ¡®0¡¯
    OutputBuffer[dwNumBytesToSend++] = 'x11'; //Set SK, GPIOL0 pins as output with bit ¡¯1¡¯, DO and other pins as input with bit ¡®0¡¯
    OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_IN; //Command to clock data byte in on ¨Cve Clock    Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = 'x00';
    OutputBuffer[dwNumBytesToSend++] = 'x00'; //Data length of 0x0000 means 1 byte data to clock in
    //OutputBuffer[dwNumBytesToSend++] = 'x87'; //Send answer back immediate command

    OutputBuffer[dwNumBytesToSend++] = MSB_RISING_EDGE_CLOCK_BIT_IN; //Command to scan in acknowledge bit , -ve clock    Edge MSB first
    OutputBuffer[dwNumBytesToSend++] = 'x0'; //Length of 0 means to scan in 1 bit
    OutputBuffer[dwNumBytesToSend++] = 'x87'; //Send answer back immediate command
    ftStatus = FT_Write(ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent); //Send off the commands
    dwNumBytesToSend = 0; //Clear output buffer
    ftStatus = FT_Read(ftHandle, InputBuffer, 2, &dwNumBytesRead);//Read one byte from device receive buffer, may need try more times

    ByteDataRead = InputBuffer[0]; //return the data read from EEPROM

    OutputBuffer[dwNumBytesToSend++] = 'x80'; //Command to set directions of lower 8 pins and force value on bits set as output
    OutputBuffer[dwNumBytesToSend++] = 'x02'; //Set SDA high, SCL low, WP disabled by SK at bit '0', DO, GPIOL0 at bit '1'
    OutputBuffer[dwNumBytesToSend++] = 'x13'; //Set SK,DO,GPIOL0 pins as output with bit ¡¯1¡¯, other pins as input with bit ¡®0¡¯
    HighSpeedSetI2CStop();
    ftStatus = FT_Write(ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent); //Send off the commands
    dwNumBytesToSend = 0; //Clear output buffer

Jonathan

( ! ) Fatal error: Uncaught exception 'Elk_Exception' with message 'Please try again. If you come back to this error screen, report the error to an administrator.' in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
( ! ) Elk_Exception: Please try again. If you come back to this error screen, report the error to an administrator. in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
Call Stack
#TimeMemoryFunctionLocation
10.01292293016session_write_close ( )...(null):0
20.01322424616ElkArte\sources\subs\SessionHandler\DatabaseHandler->write( )...(null):0
30.01322425392Database_MySQL->query( ).../DatabaseHandler.php:119
40.05842564136Database_MySQL->error( ).../Db-mysql.class.php:273