Figure 1.1 ioexpander schematic
Figure 1.2 ioexpander board
注:IO0~1无拉,IO2上拉,IO3~4下拉
Test Code
VC新建C++ windows console项目,添加依赖于
Resource#13
ftd2xx.h
libMPSSE_i2c.h
WinTypes.h
libMPSSE.lib
libMPSSE.dll
TEST CODE
#include "pch.h"
#include <stdio.h>
#include <Windows.h>
#include "../libMPSSE_i2c.h"
//#include <iostream>
void print_and_quit(const char cstring[]) {
printf("%s\n", cstring);
getc(stdin);
exit(1);
}
int main(int argc, char **argv)
{
Init_libMPSSE();
FT_STATUS status;
FT_DEVICE_LIST_INFO_NODE channelInfo;
FT_HANDLE handle;
uint8 buffer[256];
uint32 bytesTransfered;
uint32 channelCount = 0;
status = I2C_GetNumChannels(&channelCount);
if (status != FT_OK)
print_and_quit("Error while checking the number of available MPSSE channels");
else if(channelCount<1)
print_and_quit("Error: no MPSSE channels are available");
printf("There are %d channels available. \n\n", channelCount);
for (int i=0; i < channelCount; i++) {
status = I2C_GetChannelInfo(i, &channelInfo);
if (status != FT_OK)
print_and_quit("Error while getting details for an MPSSE channel.");
printf("Channel number: %d\n", i);
printf("Description: %s\n", channelInfo.Description);
printf("Serial Number: %d\n", channelInfo.SerialNumber);
}
status = I2C_OpenChannel(0, &handle);
if (status != FT_OK)
print_and_quit("Error while opening the MPSSE channel.");
ChannelConfig channelconfig;
channelconfig.ClockRate = I2C_CLOCK_FAST_MODE;
channelconfig.LatencyTimer = 75;
status = I2C_InitChannel(handle, &channelconfig);
if (status != FT_OK)
print_and_quit("Error while initializing the MPSSE channel");
buffer[0] = 0x1; //IO bank 1
bytesTransfered = 0;
printf("result: %d\n", buffer[0]);
status = I2C_DeviceWrite(handle, 0x20, 1, buffer, &bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
if (status != FT_OK)
print_and_quit("Error while writing command.");
bytesTransfered = 0;
status = I2C_DeviceRead(handle, 0x20, 1, buffer, &bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT);
if (status != FT_OK)
print_and_quit("Error while reading data.");
printf("result: %d\n", buffer[0]);
Cleanup_libMPSSE();
return 0;
}