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
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