Good afternoon!
Does anybody have any experience of connecting an external ADC (like MCP3008) to Arduino over SPI using SPI built-in library?
The problem is:
When I manage SPI by hand (bit banging on the lines) the resulting ADC value for a sensor is correct and nearly to a single bit the same as the one from the on-board ADC (A0).
But, if I try to use the standard SPI library the resulting value is way different from the value of A0.
Here is the variant with .transfer(byte). .transfer(buffer, 3) gives the same result:
uint16_t queryMCP(int chan) {
byte cmd, val;
uint16_t adc = 0;
cmd = (0x18 | chan) << 3;
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
digitalWrite(chipSelect, LOW);
val = SPI.transfer(cmd);
adc = (val & 3) << 9;
val = SPI.transfer(0);
adc |= (val & 0xff) << 1;
val = SPI.transfer(0);
adc |= (val >> 7) & 0x1;
digitalWrite(chipSelect, HIGH);
SPI.endTransaction();
return (adc & 0x3ff);
}
Kit: Arduino Uno, MCP3008.
P.S.: 6 A ports is not enough for my project