3

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);
sa_leinad
3,2182 gold badges23 silver badges51 bronze badges
asked Mar 23, 2017 at 1:15
5
  • SPI reading is done one byte at a time, how would shiftIn handle that? Commented Mar 23, 2017 at 6:39
  • Is your LIS_MISO_PIN set as INPUT? Does your device use SPI mode 0? Commented Mar 23, 2017 at 9:26
  • How about using SoftSPI? ShiftIn/Out does not work. github.com/MajenkoLibraries/SoftSPI Commented Mar 23, 2017 at 17:11
  • How do you declare and initialize "input" ? This smells of uninitialized pointer... Commented Sep 2, 2017 at 15:31
  • Could you please add a link to the software SPI that you have used. Commented Mar 8, 2018 at 14:38

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.