Hello
I am trying to interface with an MCP3208 (12-bit, 8 channel ADC) over SPI bus. I attach a 0-5V potentiometer to pin 1 (analog input 0) of the MCP3208. I measure with a multimeter that I am getting a range from 0-5Volts.
Datasheet: https://www.microchip.com/en-us/product/MCP3208
I have 2 questions:
- I have a problem understanding is the addressing of the SPI bus. When I do this below, everything works great (12-bit resolution):
int MCP3208_Read_SingleCh(byte readAddress)
{
int Return_Value = 0;
byte dataMSB = 0;
byte dataLSB = 0;
int CS_Pin = 9;
readAddress = 0b01100000 | ((readAddress & 0b111) << 2);
SPI.beginTransaction(MCP3208_Set);
digitalWrite(CS_Pin, LOW);
SPI.transfer(readAddress);
dataMSB = SPI.transfer(0x00);
dataLSB = SPI.transfer(0x00);
digitalWrite(CS_Pin, HIGH);
SPI.endTransaction();
//Return_Value = ((dataMSB << 8) | dataLSB);
Return_Value = (dataMSB << 4) | (dataLSB >> 2);
return RtnVal;
}
However when I insert a line of code to shift the address over by a bit, my resolution goes to 11-bit.
int MCP3208_Read_SingleCh(byte readAddress)
{
int Return_Value = 0;
byte dataMSB = 0;
byte dataLSB = 0;
int CS_Pin = 9;
readAddress = 0b01100000 | ((readAddress & 0b111) << 2);
/* NEW CODE HERE*/
readAddress = readAddress >> 1;
SPI.beginTransaction(MCP3208_Set);
digitalWrite(CS_Pin, LOW);
SPI.transfer(readAddress);
dataMSB = SPI.transfer(0x00);
dataLSB = SPI.transfer(0x00);
digitalWrite(CS_Pin, HIGH);
SPI.endTransaction();
//Return_Value = ((dataMSB << 8) | dataLSB);
Return_Value = (dataMSB << 4) | (dataLSB >> 2);
return RtnVal;
}
And if I bit shift again (shift by 2), the resolution goes down to 10-bit. Reading the datasheet, I cannot figure out how this addressing works - anyone care to share some knowledge?
- Can someone explain how/why we bit-shift the LeastSignificantByte (dataLSB) by 2 bits to the right?
Thank You!!