t-nissieの日記: 【電脳】FTDIで遊んでみた
FTDIで遊んでみた
/*
hello-ftdi.c: flash LED 3 times.
This example uses the libftdi API.
Reference: http://hackaday.com/2009/09/22/introduction-to-ftdi-bitbang-mode/
Usage:
$ su
# ./hello-ftdi
*/
#include <stdio.h>
#include <ftdi.h>
#define PIN_TX 0x01
#define PIX_RX 0x02
#define PIN_RTS 0x04
#define PIN_CTS 0x08
#define PIN_DTR 0x10
#define PIN_DSR 0x20
#define PIN_DCD 0x40
#define PIN_RI 0x80
#define LED PIN_TX
int main()
{
unsigned char c = 0;
struct ftdi_context ftdic;
/* Initialize context for subsequent function calls */
ftdi_init(&ftdic);
/* Open FTDI device based on FT232R vendor & product IDs */
if(ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) {
puts("Can't open device");
return 1;
}
/* Enable bitbang mode with a single output line */
ftdi_set_bitmode(&ftdic, LED, BITMODE_BITBANG);
/* Endless loop: invert LED state, write output, pause 1 second */
c ^= LED; ftdi_write_data(&ftdic, &c, 1); sleep(1);
c ^= LED; ftdi_write_data(&ftdic, &c, 1); sleep(1);
c ^= LED; ftdi_write_data(&ftdic, &c, 1); sleep(1);
c ^= LED; ftdi_write_data(&ftdic, &c, 1); sleep(1);
c ^= LED; ftdi_write_data(&ftdic, &c, 1); sleep(1);
c ^= LED; ftdi_write_data(&ftdic, &c, 1);
}
/*
local variables:
compile-command: "gcc -o hello-ftdi hello-ftdi.c -lftdi"
End:
*/
間違ってた。偶然動いてた。
誤 ftdi_set_bitmode(&ftdic, BITMODE_BITBANG, LED);
正 ftdi_set_bitmode(&ftdic, LED, BITMODE_BITBANG);