0

I'm simulating my code on Proteus and trying to send data between two ATmega32 devices via SPI as master and slave. However I keep getting the warning "SPDR Write Collision. Written data is ignored" for the slave device for each character of the string I'm trying to send.

#define F_CPU 8000000UL

#include <avr/io.h>

void PORT_INIT(void);
void SPI_SLAVE_INIT(void);
void SPI_SEND_STRING(void);

char *transmit = "Hello there!";        ///< Data to be sent.

int main(void){
    DDRA = 0xFF;
    
    PORT_INIT();
    SPI_SLAVE_INIT();
    
    SPI_SEND_STRING();
}

/*!
 *  @brief Initialize ports.
 */

void PORT_INIT(void){
    DDRB &= ~((1<<PB4)|(1<<PB5)|(1<<PB7));          ///< Set MOSI, SCK and SS pins to input.
    DDRB |= (1<<PB6);                               ///< Set MISO pin to output.
}

/*!
 *  @brief Initialize SPI in slave mode.
 */

void SPI_SLAVE_INIT(void){
    SPCR = (1<<SPE);            ///< Enable SPI in Slave SPI mode.
}

/*!
 *  @brief Transmit a message over SPI.
 */

void SPI_SEND_STRING(void){
    for(uint8_t i=0;transmit[i]!=0;i++){
        SPDR = transmit[i];         ///< Load the data byte to the SPDR shift register to transmit.
        while(!(SPSR & (1<<SPIF)));     ///< Wait until the full data byte is received.
        PORTA = SPDR;
    }
}

Am I missing something here? The master device clock frequency is Fosc/16.

  • Hey, I didn't work with Atmega at all, but here an educated guess; in most architectures there is only one spi data register, so Between two writes to it you need to read the recieved data, otherwise you wil get an overrun error. (maybe it is the write collison in your case) +Since this is the slave device, I would suggest you wait for reception of the first byte before writing to DR. – İlkerK May 02 '21 at 10:26
  • I modified the code as you said but I still get the same error – Asitha Navaratne May 02 '21 at 10:44
  • Seems like people have faced the same problems for Simulating SPI in Proteus. https://www.avrfreaks.net/forum/avr-spi-write-collision – İlkerK May 02 '21 at 10:58
  • I think that is something to be done with UART unfortunately. I don't use it here – Asitha Navaratne May 03 '21 at 01:46

0 Answers0