Arduino: int32_t from two’s complement

  32-bit, arduino, c++, type-conversion

I’m trying to achieve a simple thing: read 4 bytes of data from ADC via SPI and combine them into signed (int32_t) value, but I’m getting unexpected results (see plot). My function for reading the ADC is as follows:

int32_t readDataContinuous() {
  //  delayMicroseconds(100);
  while (digitalRead(DRDYPin) == HIGH) {
    //do nothing (waiting for the conversion to finish)
  }
  int32_t resp;
  digitalWrite(CSPin, LOW);
  SPI.beginTransaction(SPISettings(40000000, MSBFIRST, SPI_MODE1));
  resp = SPI.transfer(0x00);
  resp <<= 8;
  resp |= SPI.transfer(0x00);
  resp <<= 8;
  resp |= SPI.transfer(0x00);
  resp <<= 8;
  resp |= SPI.transfer(0x00);
  SPI.endTransaction();
  digitalWrite(CSPin, HIGH);
  SerialBT.println(resp);
  return resp;
}

see plot

Any ideas?

Source: Windows Questions C++

LEAVE A COMMENT