Sending and Receiving Data //Function to send and receive data for both master and slave

unsigned char spi_tranceiver (unsigned char data)

        // Load data into the buffer
        SPDR = data;

        //Wait until transmission complete
        while(!(SPSR & (1<<SPIF) ));

        // Return received data

What part of it sets the pin (MOSI) high and low to transfer bit. All i see is the awaiting for the interrupt bit (SPIF).

Does AVR MCU automatically handles all routine regarding pusling pins just by setting SPE bit to 1 ?

"To send data, load the data into the SPI Data Register (SPDR), and then, wait until the SPIF flag is set. When the SPIF flag is set, the data to be transmitted is already transmitted and is replaced by the received data. So, simply return the value of the SPI Data Register (SPDR) to receive data."

Another line from the same post - i do not expect that mcu will start transmitting as soon as i put data in data register

  • 2
    \$\begingroup\$ "i do not expect that mcu will start transmitting as soon as i put data in data register" well, that's exactly what happens, if things are correctly configured. \$\endgroup\$ – Chris Stratton Oct 22 '18 at 18:39

The system is single buffered in the transmit direction and double buffered in the receive direction. This means that bytes to be transmitted cannot be written to the SPI Data Register before the entire shift cycle is completed. When receiving data, however, a received character must be read from the SPI Data Register before the next character has been completely shifted in. Otherwise, the first byte is lost.

In SPI Slave mode, the control logic will sample the incoming signal of the SCK pin. To ensure correct sampling of the clock signal, the minimum low and high periods should be longer than two CPU clock cycles.

When the SPI is enabled, the data direction of the MOSI, MISO, SCK, and SS pins is overridden according to the table below. For more details on automatic port overrides, refer to the IO Port description. Table 23-1. SPI Pin Ove



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.