SPI speed not making sense, seems very slow (MEGA2560)

Hey All,

I'm trying to use the hardware SPI with also using the fastDigitalWrite (using constant pin names) to try and write to 2 devices on the SPI bus (2 MCP4922). So basically, I am managing my CS for both SPI slaves.

What I do is:
-Set CS_0 low
-Or an uint8_t with another uint8_t
-transfer result on SPI
-or an uint8_t with another uint8_t
-transfer result on SPI
-Set CS_0 high, then set CS_0 low (DAC has 2 channels, so I need to toggle CS to latch 1st DAC 16-bit data)
-Or an uint8_t with another uint8_t
-transfer result on SPI
-or an uint8_t with another uint8_t
-transfer result on SPI
-Set CS_0 high

I do this all again with CS_1 (since I have 2 SPI devices I am writing to.

I set the clock divider to 2 so I would think that sets my speed to 8Mhz. or basically 8Mbps

From what you see above, I have 64 bits that need to be written out, and 8 digitalWrites. At 8Mhz, I would think that would give me each of those 64 bits would take 0.125uS. If I assume worst case for the digitalWrites that would be like 4uS I would think, so overall that's 8uS + 32uS or 40uS total. The scope is showing me 250uS (I am alternating the DAC from high to low) to transition from high to low. I don't understand why I am loosing 210uS.

Rick

I answered my own question, and so no one else makes the same mistake, this:

SPI.setClockDivider(2);

DOES NOT =

SPI.setClockDivider(SPI_CLOCK_DIV2);

:slight_smile:

Now I see a change every ~19uS (which means the digital writes are much faster) which comes out to 52k changes per second at the DAC outputs when driving 4 channels on 2 SPI devices. Much better.