0
\$\begingroup\$

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
        return(SPDR);
    }

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

\$\endgroup\$
1
  • 5
    \$\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\$ Commented Oct 22, 2018 at 18:39

2 Answers 2

1
\$\begingroup\$

"What part of it sets the pin (MOSI) high and low to transfer bit."

This line:

SPDR = data;

sends the data out.

\$\endgroup\$
1
  • \$\begingroup\$ omg such an old question, but yeah i figured it already :) \$\endgroup\$ Commented Feb 10, 2020 at 20:33
-1
\$\begingroup\$

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

http://ww1.microchip.com/downloads/en/devicedoc/atmel-42735-8-bit-avr-microcontroller-atmega328-328p_datasheet.pdf

\$\endgroup\$

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.