My Extech EX330 multimeter is reading 3.439Mhz on the pin 13 on my Arduino Uno running the following code. Is the reading correct? If the divider is 2, shouldn't I get 8 Mhz (16Mhz / 2)?

    #include <SPI.h> 
void setup()
  pinMode(13, OUTPUT);

  digitalWrite(13, HIGH);

void loop()

  byte pot = B0;                
  SPI.transfer(pot);                        //  Do SPI transfer of variable pot

Yes, the clock speed should be (and most probably is) 8 MHz. However, you're not generating an 8 MHz clock.

What you are in fact generating is 8 clock pulses of 8 MHz, then you have a delay while it deals with the rest of your code, and then it generates another 8 clock cycles of 8MHz.

The overall frequency is evidently sensed as 3.439 MHz.

To generate a pure 8 MHz clock you would have to use a different transfer method. I don't know if the 328P has DMA available, but if it does, that would give you much faster transfers. Also, you could write a more non-blocking version of SPI.transfer().

The normal SPI.transfer() first starts the transfer, then waits for the transfer to finish, and returns the results of the transfer. If you're only interested in transmitting, and not receiving, or the transmitted and received data don't need to c correlate completely, you can instead change the order of how it works:

  1. Wait for there to be no transfer in operation
  2. Store the result of the last transfer
  3. Start the new transfer
  4. Return the results of the last transfer.

That way it runs much smoother as it's doing the current transfer at the same time as running your other code.

| improve this answer | |

No, because neither the allocation and assignment of pot nor the call to SPI.transfer() come for free. And the time taken by both slows the average frequency down to about the measured value.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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