2

How to read from a register using software SPI, presumably shiftin()?

It works using the builtin SPI library + hardware SPI.

I seem to be able to write but not read using manual shiftin()/shiftout(). I need to do this because I need a version of this to work on a device that doesn't have hardware SPI. I always read out 0x00000000 using shiftin() as below, but the SPI hardware/library works as expected.

(yes, I've double checked the pin numbers). LIS_MISO_PIN = 12.

  // this works (write):
  SPI.transfer(WriteAddr);
  SPI.transfer(Data);

  // this works (write):
  shiftOut(LIS_MOSI_PIN, LIS_SCK_PIN, MSBFIRST, WriteAddr);
  shiftOut(LIS_MOSI_PIN, LIS_SCK_PIN, MSBFIRST, Data);

  // this works (read):
  SPI.transfer(Reg | 0x80);
  *input = SPI.transfer(0x00);

  // does NOT work, as well as various permutations:
  shiftOut(LIS_MOSI_PIN, LIS_SCK_PIN, MSBFIRST, Reg | 0x80);
  *input = shiftIn(LIS_MISO_PIN, LIS_SCK_PIN, MSBFIRST);

migrated from electronics.stackexchange.com Mar 23 '17 at 1:18

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • SPI reading is done one byte at a time, how would shiftIn handle that? – dandavis Mar 23 '17 at 6:39
  • Is your LIS_MISO_PIN set as INPUT? Does your device use SPI mode 0? – Edgar Bonet Mar 23 '17 at 9:26
  • How about using SoftSPI? ShiftIn/Out does not work. github.com/MajenkoLibraries/SoftSPI – Mikael Patel Mar 23 '17 at 17:11
  • How do you declare and initialize "input" ? This smells of uninitialized pointer... – next-hack Sep 2 '17 at 15:31
  • Could you please add a link to the software SPI that you have used. – sa_leinad Mar 8 '18 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy