1

I am trying to verify sck output on my arduino uno. I don't have oscilloscope so I loopbacked miso to sck and tried Executing int rx0=SPI.transfer(5) while printing this using Serial.printrx0) it is giving 255.Whether this is expected output?

3
  • I'd expect you'll get all bit with the same value. You can try other SPI_MODES and you probably get 0x00 for two of them.. However you could use loopback between MOSI a MISO (you can expect 1B delay)
    – KIIV
    Commented Nov 28, 2021 at 6:47
  • Hi KIIV, THANKS for responding.. I tried all the SPI mode and I got 255 as output, means all the bit as 1 in the byte. Whether it is right result?whether I can say my sck output is normal? Thanks Commented Nov 28, 2021 at 7:13
  • 1
    You could instead send SCK to T1 (pin 5), configure Timer 1 as a counter, and use it to count the clock pulses. Commented Nov 28, 2021 at 9:04

1 Answer 1

3

As suggested by KIIV and myself, you can test the SPI port by:

  • connecting MOSI to MISO and checking if you receive what you send
  • connecting SCK to the timer input T1 and counting the clock pulses.

Below is a sketch that performs both tests:

/*
 * Test the SPI port.
 *
 * Connect:
 *   MOSI (11) to MISO (12)
 *   SCK (13) to T1 (5)
 */

void setup() {
    Serial.begin(9600);

    // Configure SPI port.
    DDRB |= _BV(PB2)   // SS (digital 10) as output
          | _BV(PB3)   // MOSI (digital 11) as output
          | _BV(PB5);  // SCK (digital 13) as output
    SPCR = _BV(SPE)    // enable port
         | _BV(MSTR);  // master mode

    // Configure Timer 1.
    TCCR1A = 0;          // normal mode
    TCCR1B = _BV(CS10)   // count rising edges of T1 = digital 3
           | _BV(CS11)   // ditto
           | _BV(CS12);  // ditto
}

void loop() {
    TCNT1 = 0;  // clear timer
    uint8_t data = random(256);
    Serial.print(data);
    Serial.print(" -> ");
    SPDR = data;  // send
    delay(1);
    if (bit_is_set(SPSR, SPIF)) {  // transfer complete?
        Serial.println(SPDR);
    } else {
        Serial.println("nothing received");
    }
    Serial.print("    clock pulses: ");
    Serial.println(TCNT1);
    delay(1000);
}

It should print:

167 -> 167
    clock pulses: 8
241 -> 241
    clock pulses: 8
217 -> 217
    clock pulses: 8
[...]
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.