1

When I used the library with MCP3008 ADC, this is the code that I used:

#include <stdio.h>
#include <time.h>
#include <bcm2835.h>

int main(int argc, char **argv) {
  float seconds;
  printf("Enter the run time (in seconds): ");
  scanf("%f", &seconds);
  FILE *f_0 = fopen("ltc_data.dat", "w"); 
  clock_t start, end;
  double time_taken;

  if (!bcm2835_init()){
    return 1;
  }
  bcm2835_spi_begin();
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //Data comes in on falling edge                
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);
  bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //Slave Select on CS0                    
  bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);   
  int ch0=0;
  start = clock();
  int i=0;
  for (;;) {
    char buf_0[] = {0x01,(0x08|ch0)<<4,0x00};
    char readBuf_0[3];
    bcm2835_spi_transfernb(buf_0,readBuf_0,3);
    fprintf(f_0, "%d\n", ((int)readBuf_0[1] & 0x03) << 8 | (int) readBuf_0[2]);

    end = clock();
    time_taken = ((double)(end-start)/CLOCKS_PER_SEC) * 1000;
    i++;
    if (time_taken > ((seconds * 1000) + 1)) {
        break;
    }
  }
  printf("%d", (int)(i/seconds));
  printf(" samples/s/channel\n");

  bcm2835_spi_end();
  bcm2835_close();
  return 0;

}

When I look at the datasheet of MCP3008, it clearly states the order of bits that needs to be sent to the ADC and the order that the ADC sends back. Because of this, I can send information to the ADC with the transfernb function with the buf_0 parameter and read what the output I get with the readBuf_0 parameter.

However, for the LTC 2365 ADC, it doesn't specify which bits I need to send to the ADC. I have it wired up correctly, I think:

ADC Pi

Vdd 3.3V

GND GND

Ain Input from function generator

CS CE0

SDO MISO

SCK SCLK

When I use the same code (which I probably shouldn't since LTC 2365 doesn't expect any input data), I see nothing (I literally see a line of 0s). What do I need to do to actually read this ADC?

Datasheet for the ADC:http://cds.linear.com/docs/en/datasheet/23656fb.pdf

  • 1
    What does the datasheet say? You can't use any old SPI code, you need code specific to the device which will be specified in the datasheet. – joan Nov 9 '17 at 21:04
  • If you're unclear what the data sheet is saying, then at least put a link to it here. – Brick Nov 9 '17 at 21:34
  • I've put the datasheet and also, changed the transfer code to: fprintf(f_0, "%d\n", (readBuf_0[0]));, and I changed the clock divider to _32. And it seems to be working fine, for the most part:. This is what I get for a 1 kHz sine wave from a function generator: ibb.co/j0qeTG. And this for a 25 kHz sine wave: ibb.co/g3RKTG. Why does it look so bad for the 1 kHz signal? – user76012 Nov 9 '17 at 23:41
  • Also for an ADC which is supposed to run at 1 MSPS gives me only 250 KSPS. How can I get it to at least 750 KSPS? – user76012 Nov 9 '17 at 23:48
1

Partial answer to this question:

What do I need to do to actually read this ADC?

Page 9 of the datasheet:

SDO: Three-State Serial Data Output. The A/D conversion result is shifted out on SDO as a serial data stream with MSB first. The data stream consists of two leading zeros followed by 12 bits of conversion data and two trailing zeros

So this is the expected data to read from SDO:

MSB of ADC conversion
          |
          |
          v
.----------------------------.
| Start 00dddddddddddd00 End |
'----------------------------'
                     ^
                     |
                     |
                     |
           LSB of ADC conversion

The above diagram is a simplification of the timing diagrams found on pages 14 and 15 of the datasheet. All the information you need to use the ADC is included on the datasheet.

  • So, I changed the transfer code to: fprintf(f_0, "%d\n", (readBuf_0[0]));, and I also changed the clock divider to _32. And it seems to be working fine, for the most part:. This is what I get for a 1 kHz sine wave from a function generator: ibb.co/j0qeTG. And this for a 25 kHz sine wave: ibb.co/g3RKTG. Why does it look so bad for the 1 kHz signal? – user76012 Nov 12 '17 at 22:26
  • Also, I get exactly what the ADC outputting, but I'm not too skilled at bit manipulation, shifting them and stuff. How do I actually take those bits and get the value that I want? – user76012 Nov 12 '17 at 23:04
  • So, I learned a bit about bit shifting and I am now using this: (readBuf_0[0] << 6) + (readBuf_0[1] >> 2) It seems to make sense, but my sampling rate is still at a pathetic 240-250KSPS – user76012 Nov 12 '17 at 23:43
  • I'm using an external RTC (ZS-042), I'm not sure if that has anything to do with it – user76012 Nov 13 '17 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.