Why is my code not generating SPI clock?

Im trying to read values from an SPI encoder. urrently i only have my Logic analyser connected. Thats the reason ther is nothing on MISO but he clock should be genrated by the Master, right? Maybe there is also an issue with my SPI code since it is my first time working with SPI...
Here is my code:

#include <SPI.h>

const int encoderNCS = 10; // Chip select (slave select) pin for the encoder
const int encoderMISO = 12; // MISO pin of the encoder
const int encoderMOSI = 11; // MOSI pin of the encoder
const int encoderSCK = 13; // SCK pin of the encoder

// Reading command: 00001100000000111111
byte command[] = {B0, B0, B0, B0, B1, B1, B0, B0, B0, B0, B0, B0, B0, B0, B1, B1, B1, B1, B1, B1}; //read command converted to bytes variable
byte receivedBit[] = {B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0, B0}; //initialize storage bytes

void setup() {
  Serial.begin(9600);
  SPI.begin();
  pinMode(encoderNCS, OUTPUT);
  digitalWrite(encoderNCS, HIGH); // Set NCS (CS) high initially
}

void loop() {

  // Enable the encoder by pulling NCS low
  digitalWrite(encoderNCS, LOW);
  delayMicroseconds(350); // At least 350ns between NCS falling edge and SCK rising edge

  SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE1)); // CPOL=0, CPHA=1
  // Simultaneously send and receive data
  for (int i = 0; i < 20; i++) {
    receivedBit[i] = SPI.transfer(command[i]);
    Serial.print(command[i]);
  }
  SPI.endTransaction();

  Serial.println(); // Print a new line after all 20 bits are received
  
  // Disable the encoder by pulling NCS high with sufficient high time
  digitalWrite(encoderNCS, HIGH);
  delayMicroseconds(350); // High time of NCS between two transmissions at least 350ns
}

Output of my logic analyser SCK is connected with Chanel0:

Why don't you print the received data?

All but the CS pins are common to the SPI bus. CS should be set LOW before and HIGH after every transaction.
Which Arduino are you using?

How do you connect the encoder and logic analyzer? Using a breadboard?

I currently only have to logic analyzer connected but as far as I know the clock signal should be generated from the master even if there is no slave device connected. That's also the reason I dont print the received data, bacause ther will be none. Im pulling NCS low and then high every time after sending + receiving 20 bits of data. And im using an Arduino UNO.

Right. I suspect a bad or wrong connection of your logic analyzer.

Transmitted are bytes, not single bits.

Okay thanks a lot for your help! It seems like my Pin 13 wich is the clock is not functioning properly, because i wrote a programm to manually turn the pin high and low but I see nothing on my analyzer. Then when i use a different pin it works as expected...

They probably shouldn't have had a pin 13, like no 13th floor in buildings…

a7

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.