Hey folks,
I have always had problems working with bitwise operators and need some help. I am reading 32 bits from an LTC2440 ADC using the following code (seems to work) and which I found here: sample code for LTC2440 24-bit ADC
unsigned long SpiRead(void) {
unsigned long result = 0.0;
byte sig = 0; // sign bit
byte b;
unsigned char incoming_adc[10];
while (digitalReadFast(BUSYPIN)==HIGH) { // wait until busy pin goes low and ADC result is ready
}
digitalWriteFast(SLAVESELECT,LOW); // take the SS pin low to select the chip
delayMicroseconds(1); // probably not needed, only need 25 nsec delay
b = SPI.transfer(0xff); // B3
if ((b & 0x20) == 0) sig=1; // is input negative ?
b &=0x1f; // discard bits 25..31
result = b;
result <<= 8;
b = SPI.transfer(0xff); // B2
result |= b;
result = result<<8;
b = SPI.transfer(0xff); // B1
result |= b;
result = result<<8;
b = SPI.transfer(0xff); // B0
result |= b;
digitalWriteFast(SLAVESELECT,HIGH); // take the SS pin high to bring MISO to hi-Z and start new conversion
if (sig) result |= 0xf0000000; // if input is negative, insert sign bit (0xf0.. or 0xe0... ?)
result = result / 16.0; // scale result down , last 4 bits are "sub-LSBs"
return(result);
}
Could anyone possibly explain the bitwise operator parts of the code for me in detail, I would VERY much appreciate it since I have tried understanding it but it is proving very difficult. I am using an arduino uno though, so I'm not sure if it can store 32 bits and it might have to be split up into a set of 16 bits?
Here are the bit descriptions of the bits from the LTC2440 datasheet:
http://cds.linear.com/docs/en/datasheet/2440fd.pdf pg. 11
Bit 31 (first output bit) is the end of conversion (EOC) indicator.
This bit is available at the SDO pin during the conversion and sleep states whenever the CS pin is LOW.
This bit is HIGH during the conversion and goes LOW when the conversion is complete.Bit 30 (second output bit) is a dummy bit (DMY) and is
always LOW.Bit 29 (third output bit) is the conversion result sign indicator (SIG).
If V_IN is >0, this bit is HIGH.
If V_IN is <0, this bit is LOW.Bit 28 (fourth output bit) is the most significant bit (MSB) of the result.
This bit in conjunction with Bit 29 also provides the underrange or overrange indication.
If both Bit 29 and Bit 28 are HIGH, the differential input voltage is above +FS.
If both Bit 29 and Bit 28 are LOW, the differential input voltage is below –FS.Bits ranging from 28 to 5 are the 24-bit conversion result MSB first.
Bit 5 is the Least Signifi cant Bit (LSB).
Bits ranging from 4 to 0 are sub LSBs below the 24-bit level.
Bits 4 to bit 0 may be included in averaging or dis-carded without loss of resolution.
Thank you!