pylon
21
If you want to have 16bits of integer information and the sign information you need to change to the 32bit variant:
void loop() {
delay(2000);
digitalWrite(CSPin, LOW);
delay(1);
byte1 = SPI.transfer(0);
byte2 = SPI.transfer(0);
byte3 = SPI.transfer(0);
delay(1);
digitalWrite(CSPin, HIGH);
int32_t v = byte1;
v <<= 11;
v |= ((int) byte2) << 3;
v |= byte3 >> 5;
if (byte1 & 0x20) {
v = -v;
}
}
There's something I do not understand.
My ADC is 16 bit it should give 65535 in decimal.
If I use your old code I get an output from unloaded stain gauge: 1340
uint16_t v = byte1;
v <<= 11;
v |= ((uint16_t) byte2) << 3;
v |= byte3 >> 5;
Serial.println(v);
If I use your new code I get an output from unloaded stain gauge: 66862
int32_t v = byte1;
v <<= 11;
v |= ((int) byte2) << 3;
v |= byte3 >> 5;
Serial.println(v);
pylon
23
We need to mask out the flag bits in the first byte:
int32_t v = byte1 & 0x1F;
v <<= 11;
v |= ((int) byte2) << 3;
v |= byte3 >> 5;
Serial.println(v);