# The max clock speed of the simulate SPI port by IOs in Atmega328p or Atmeag2560

Did someone do a research on the max speed of simulate SPI port by IOs in Atmega328p or Atmeag2560?
recently I have a 1Mbps data to transfer by a wireless moudule, the hardware SPI port had used for the ethernet at Atmega328p board, I want to simulating a SPI port for the transfer moudule, WHAT is the MAX speed of the io simulated SPI ??
Thanks!

waysleehh:
Did someone do a research on the max speed of simulate SPI port by IOs in Atmega328p or Atmeag2560?
recently I have a 1Mbps data to transfer by a wireless moudule, the hardware SPI port had used for the ethernet at Atmega328p board, I want to simulating a SPI port for the transfer moudule, WHAT is the MAX speed of the io simulated SPI ??
Thanks!

By “simulated SPI” do you mean “software (bit-bang) SPI”? If so, it takes 371 microseconds to do an 8 bit send/receive sequence with a software SPI (tested with this code):

``````uint8_t spi_x (uint8_t data)
{
uint8_t bits = 8;

while (bits--) {
PORTB &= ~(1 << 4);
data & (1 << bits) ? PORTB |= (1 << 5) : PORTB &= ~(1 << 5);
PORTB |= (1 << 4);
PORTB & (1 << 5) ? data |= (1 << bits) : data &= ~(1 << bits);
}

return data;
}
``````

Therefore, the time per bit is 371 / 8 = 46.375 microseconds per bit which is (1 / 46.375) * 1e6 = 21563.34 (about 21.6 kbits/sec.)

Note that a random port and random bits were chosen for SCK, MISO and MOSI (but which bit it is doesn’t change the speed). This test, by the way, was run on a MEGA2560R3 at 16.0 MHz.

Hope this is what you wanted.

Krupski:
By “simulated SPI” do you mean “software (bit-bang) SPI”? If so, it takes 371 microseconds to do an 8 bit send/receive sequence with a software SPI (tested with this code):

``````uint8_t spi_x (uint8_t data)
``````

{
uint8_t bits = 8;

while (bits–) {
PORTB &= ~(1 << 4);
data & (1 << bits) ? PORTB |= (1 << 5) : PORTB &= ~(1 << 5);
PORTB |= (1 << 4);
PORTB & (1 << 5) ? data |= (1 << bits) : data &= ~(1 << bits);
}

return data;
}

``````

Therefore, the time per bit is 371 / 8 = 46.375 microseconds per bit which is (1 / 46.375) * 1e6 = 21563.34 (about 21.6 kbits/sec.)

Note that a random port and random bits were chosen for SCK, MISO and MOSI (but which bit it is doesn't change the speed). This test, by the way, was run on a MEGA2560R3 at 16.0 MHz.

Hope this is what you wanted.
``````

thanks a lot! you explained what i need.