chipKIT® Development Platform

Inspired by Arduino™

SPI Lib, setBitOrder does not work

Created Sat, 26 Oct 2013 17:15:46 +0000 by YOBE.


Sat, 26 Oct 2013 17:15:46 +0000


Really need your help. No clue what I do wrong. I have used SPI.h as library and tried to send data with LSB FIRST mode.

But what ever I do, Chipkit keeps sending the MSB FIRST on the data line.

Please some advice. Spend more then 2 hours trying to find what is wrong. I added a screen shot from my logic analyser, with "transfer" of "01" and you see the 1 is at the end, so MSB FIRST????!!!!!!!!

Even tried to put a delay after SPI.begin, but same result. Stays in MSBFIRST


SPI.setClockDivider(SPI_CLOCK_DIV4); SPI.setBitOrder(LSBFIRST); SPI.setDataMode(SPI_MODE3);


Sat, 26 Oct 2013 17:25:20 +0000

The PIC32 SPI peripheral doesn't support LSB first communications. In fact, I'm not even sure that the SPI protocol specification even allows LSB first.

If you want to do LSB communication you will have to use software - try the shiftOut() function.


Sun, 27 Oct 2013 01:55:12 +0000


That would be very strange. If you look to the content of the SPI library, this is the info:



Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first).



So, to my opnion, the library should support the LSBFIRST command!!! Or is this only the case for ARDUINO and not relevant for CHIPKIT???? This would be a real issue.

Thanks for feedback anyhow.


Sun, 27 Oct 2013 10:56:06 +0000

setBitOrder is an Arduino specific thing. I have never known an SPI peripheral that requires LSB first mode, and indeed the Wikipedia article on SPI says that normally it's MSB first.

The whole SPI library is really there for Arduino compatibility and isn't recommended for new projects. Instead use the DSPI library which supports multiple SPI ports.

What is it that you're doing that requires LSB first mode?


Sun, 27 Oct 2013 15:05:19 +0000

What is it that you're doing that requires LSB first mode?

I am working on a wireless PS2 controller on my Chipkit UNO32. The controller uses LSB format on SPI But in the meanwhile i solved it with:


Perhaps there is also an better/faster way to solve this?

Problem is solved, but confusing if you read the Arduino reference guide. "setBitOrder" does not work on Chipkit it seems now.

Thanks anyhow for the feedback.


Mon, 28 Oct 2013 02:20:32 +0000

Not sure if you are using the PS2X library or not, but I just rewrote the constants in reversed bit order used for communication with the PS controller so that I could use the hardware SPI.

I can post a copy of my modified library if you want to try it out. I'm assuming that it is the standard PS2 interface.



Mon, 28 Oct 2013 02:49:16 +0000

@ Jason

Hello, I wrote the communication to the PS2 controller based on some "older" PIC software. It works.

BUT, would be great to see your lirary!!! Can you post it please. The only library I could find was the one from Bill Porter (Arduino), but during compiling it gave errors on Chipkit platform. So I could not use it.

You use the SPI protocol in your Library?

Thanks in advance,



Mon, 28 Oct 2013 22:52:31 +0000

I'm using the Hardware SPI with the DSPI library, so in your sketch you have to include the DSPI.H file. It is the same library as the Bill Porter library that was ported by someone else on this forum, so it should look familiar to you.

I haven't used it in a while and I know MPIDE has gone through some changes since so let me know if it doesn't work for you. Just remember to include the DSPI.H in your sketch.

If you want to change the SPI module you have to change it in the CPP file as I haven't gone any further on making it changeable in the sketch file.




Tue, 29 Oct 2013 04:22:35 +0000


thanks for the zip file, but I get the same error as when I used the Bill Porter fille. I have the impression its linked to the MPIDE but I cannot figure out what is the problem.

Do you have any idea what this could be? I use 20120903, I think the latest version.

This is the error code (part of it)

At the end but I think it is related to previous errors:

Perhaps I should better start a new topic on this issue?


Tue, 29 Oct 2013 22:04:44 +0000

I'm using the same version of MPIDE as you 20120903, so it should work for you. I'm not sure that the example sketches are setup to work since I created my own niche' sketch so I'll attach the sketch that works for me so you can see if this compiles for you.

Which board do you have selected? I'm currently using the UNO32, but I'm pretty sure I've used this on both the MAX and UNO.

Let me know how it turns out for you.


Wed, 30 Oct 2013 00:30:04 +0000


Now it compiles :D :D Root cause: in the examples that are included in the updated library for Chipkit, this is defined:

#include <PS2X_lib.h>

BUT it should be

#include <DSPI.h> #include <PS2X_lib_SPI.h>