Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ftdi.h>
- #define HC595_CT (1) // number of '595 chips
- int main(int argc, char **argv)
- {
- struct ftdi_context ftdic;
- int f,i, err;
- unsigned char buf[2*8*HC595_CT+1]; // latch pulse, 8*HC595_CT clock pulses
- if ((f=ftdi_init(&ftdic)) < 0)
- {
- fprintf(stderr, "ftdi_init failed\n");
- return f;
- }
- f = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
- if (f < 0 && f != -5)
- {
- fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
- exit(-1);
- }
- printf("ftdi open succeeded: %d\n",f);
- // FTDI cable assignments:
- #define BIT_DATA (1<<0) // 1: TXD, "data"
- #define BIT_CLOCK (1<<2) // 4: RTS, "clock"
- #define BIT_LATCH (1<<3) // 8: CTS, "latch"
- ftdi_set_bitmode(&ftdic, BIT_DATA | BIT_CLOCK | BIT_LATCH, BITMODE_BITBANG);
- // set zero
- *buf=0;
- f = ftdi_write_data(&ftdic, buf, 1);
- unsigned char *b=buf;
- unsigned char data;
- unsigned char state;
- if (argc == 2) {
- data=atof(argv[1]);
- } else {
- data=0x5a;
- }
- printf("sending data %d\n",data);
- for (i=0; i<8; i++) {
- state=(data & (128L>>i))?BIT_DATA:0;
- *b++=state;
- state |= BIT_CLOCK;
- *b++=state;
- }
- *b++=BIT_LATCH;
- f = ftdi_write_data(&ftdic, buf, (b-buf));
- ftdi_disable_bitbang(&ftdic);
- ftdi_usb_close(&ftdic);
- ftdi_deinit(&ftdic);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement