Hi all, thanks in advance for your help.
As stated in the title, I am experiencing some strange behavior regarding my SPI outputs on an Arduino Nano 33 BLE. I have looked at quite a few forum posts regarding SPI, and have read through the almighty Gammon's webpage on SPI as well. I'm hoping it's just a silly mistake/misunderstanding on my part, but I can't seem find anything.
In a nutshell, the issue is that my SPI_MODEx call does not seem to change the phase/polarity of the output.
Here is my sketch:
#include <SPI.h>
// Define the digital pin for the SPI Interface.
const int slaveSelectPin1 = 10;
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
// Cycle the slave select pin so the accelerometer switches to SPI mode
pinMode(slaveSelectPin1, OUTPUT);
digitalWrite(slaveSelectPin1, LOW);
digitalWrite(slaveSelectPin1, HIGH);
// Initialize Serial Communication for debugging
Serial.begin(9600);
while (!Serial);
delay(1000);
// Pause so I can open the serial monitor
Serial.println("Here We Go:");
}
void loop() {
// put your main code here, to run repeatedly:
SPI.begin(); // init SPI
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0)); //set parameters
digitalWrite(slaveSelectPin1, LOW);
// Transfer something easy to read
SPI.transfer(0b10101010);
delay(1000);
SPI.transfer(0b10101010);
delay(1000);
SPI.transfer(0b10101010);
delay(1000);
digitalWrite(slaveSelectPin1, HIGH);
SPI.endTransaction();
SPI.end();
delay(1000);
}
To test my code, I connected oscilloscope channel 1 to the SCK pin and channel 2 to the MOSI pin. All GND connections are shared. I wanted to test the output to make sure I was setting everything up correctly before connecting anything to my sensor (we can talk about that later if need be). Just to be clear, the sensor is not connected as of right now.
In loop(), I call the SPI.beginTransaction() function and give the usual: a speed, a bit order, and an SPI mode. Below, I have linked 4 sets of images, one for each mode number. Each set consists of:
-
"Zoomed out view" - I delete the delays between each SPI.transfer() so I can display all three on the same oscilloscope screen.
-
"First Transmission" - 1s Delays are added again and this is the detail view of the first transmission.
-
"First Transmission" - Detail view of the second transmission.
-
"First Transmission" - Detail view of the third transmission.
The scope is pretty old, hopefully the "screen shots" are clear enough for you read. I'm happy to clarify anything if necessary.
Mode0
Code:
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0));
Photos (external link due to file limits)
Mode1
Code:
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE1));
Photos (external link due to file limits)
Mode2
Code:
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE2));
Photos (external link due to file limits)
Mode3
Code:
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE3));
Photos (external link due to file limits)
Hopefully that covers what I am experiencing well enough for you to get my confusion -- as they all (to my eye) look identical!
What I was expecting was something more like Nick Gammon's explanation on his website where for each mode the the SCK/MOSI idle voltages vary.
Like I said, I really hope someone spots a simple mistake I made or can point me in the direction of the right forum post.