|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
FT2303RL, FTDI的USB-to-uart 芯片,可以使用在上位机调用API控制gpio的高,低电平。
// ftdi-gpio.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ftd2xx.h"
int ftdi_set_gpio()
{
FT_STATUS status = 0;
FT_HANDLE handle = 0;
DWORD numDevs = 0;
FT_DEVICE_LIST_INFO_NODE * devInfo;
UCHAR gpio_mask = 0, gpio_mode = 0;
// detect device info.
status = FT_CreateDeviceInfoList(&numDevs);
if (status == FT_OK)
{
printf("1: created device info.\n");
}
else
{
printf("1: Error, can't create device info.\n");
return -1;
}
devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*(numDevs));
memset(devInfo, 0, sizeof(FT_DEVICE_LIST_INFO_NODE));
printf("malloc devInfo.\n");
status = FT_GetDeviceInfoList(devInfo, &numDevs);
if (status == FT_OK)
{
printf("2: get device info list: %d, numDevs = %u\n", status, numDevs);
}
else
{
printf("2: Error, can't get device info list\n");
return -1;
}
for (int i = 0; i < (int)numDevs; i++)
{
printf("Dev %d:\n", i);
printf(" Flags=0x%x\n", devInfo.Flags);
printf(" Type=0x%x\n", devInfo.Type);
printf(" ID=0x%x\n", devInfo.ID);
printf(" LocId=0x%x\n", devInfo.LocId);
printf(" SerialNumber=%s\n", devInfo.SerialNumber);
printf(" Description=%s\n", devInfo.Description);
printf(" ftHandle=0x%x\n", devInfo.ftHandle);
}
status = FT_OpenEx(devInfo[0].SerialNumber, FT_OPEN_BY_SERIAL_NUMBER, &handle);
printf("open: %d\n", status);
free(devInfo);
printf("free devInfo.\n");
gpio_mask = 0xFF; // all output, all = 1
gpio_mask = 0xF1; // all output, all = 0
gpio_mode = 0x20; //
status = FT_SetBitMode(handle, gpio_mask, gpio_mode);
// close it.
status = FT_Close(handle);
printf("close: %d\n", status);
return status;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("ftdi gpio example.\n");
ftdi_set_gpio();
return 0;
} |
|