Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ftdi.h>
  3.  
  4. #define HC595_CT (1) // number of '595 chips
  5.  
  6. int main(int argc, char **argv)
  7. {
  8.     struct ftdi_context ftdic;
  9.     int f,i, err;
  10.  
  11.     unsigned char buf[2*8*HC595_CT+1]; // latch pulse, 8*HC595_CT clock pulses
  12.  
  13.     if ((f=ftdi_init(&ftdic)) < 0)
  14.     {
  15.         fprintf(stderr, "ftdi_init failed\n");
  16.         return f;
  17.     }
  18.  
  19.  
  20.     f = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
  21.  
  22.     if (f < 0 && f != -5)
  23.     {
  24.         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
  25.         exit(-1);
  26.     }
  27.  
  28.     printf("ftdi open succeeded: %d\n",f);
  29.  
  30.     // FTDI cable assignments:
  31.     #define BIT_DATA (1<<0)  // 1: TXD, "data"
  32.     #define BIT_CLOCK (1<<2) // 4: RTS, "clock"
  33.     #define BIT_LATCH (1<<3) // 8: CTS, "latch"
  34.  
  35.     ftdi_set_bitmode(&ftdic, BIT_DATA | BIT_CLOCK | BIT_LATCH, BITMODE_BITBANG);
  36.  
  37.     // set zero
  38.     *buf=0;
  39.     f = ftdi_write_data(&ftdic, buf, 1);
  40.  
  41.     unsigned char *b=buf;
  42.     unsigned char data;
  43.     unsigned char state;
  44.  
  45.     if (argc == 2) {
  46.       data=atof(argv[1]);
  47.     } else {
  48.       data=0x5a;
  49.     }
  50.  
  51.     printf("sending data %d\n",data);
  52.  
  53.     for (i=0; i<8; i++) {
  54.       state=(data & (128L>>i))?BIT_DATA:0;
  55.       *b++=state;
  56.       state |= BIT_CLOCK;
  57.       *b++=state;
  58.     }
  59.     *b++=BIT_LATCH;
  60.  
  61.     f = ftdi_write_data(&ftdic, buf, (b-buf));
  62.  
  63.     ftdi_disable_bitbang(&ftdic);
  64.     ftdi_usb_close(&ftdic);
  65.     ftdi_deinit(&ftdic);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement