# SPI ATMEGA328P does not send back on MISO

I am using ATMega328P and programming in Atmel Studio 7 and I am trying to initialize the SPI. I initialize it and send some data, checked with the oscilloscope, the data is sending on MOSI line but I get nothing on MISO from the MFRC522 RFID sensor that is the slave.

I am working in master mode, this is the code:

void spi_masterinit()
{
DDRB = 0xEF; //initializare port
DDRB |= (1<<3)|(1<<5)|(1<<2); // MOSI , SCK OUTPUT ,SS
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);//prescaler 16 , Master mode  , SPI enable
DDRB &= ~(1<<4); //MISO INPUT
}

uint8_t  spi_mastertransmit( uint8_t data)
{
SPDR = data; // loading data in buffer
while(!(SPSR & (1<<SPIF)));  // wait for the flag SPIF to be 1
return SPDR; // return data
}


This is the code for init and transmit and this is the main:

int main(void)
{
uint8_t data = 0;
_delay_ms(50);
spi_masterinit();
_delay_ms(1000);
mfrc522_init();
_delay_ms(50);

while(1)
{
PORTB &= ~(1<<2); //cs active
spi_mastertransmit(data++);
PORTB |= (1<<2);
_delay_ms(1000); //disable cs
}
return 0;
}

• Please make an effort to format your post a little. :) I formatted your code but would be nice to post here text not in "chat format". – Duck Dodgers Jul 24 at 9:23
• Welcome to the site. Please realise this is not a free design house, homework-answering service or an on-line technical encyclopedia, copied out to you on demand. People will help you take the next step if your question shows you've done as much as you possibly could on your own - which yours doesn't, I'm afraid. Please edit your question and greatly improve it. Show your work and findings so far in considerable detail with a schematic. The schematic editor here is a breeze to use. The better the quality of question, the better the quality of the answers you will attract. Again, a warm welcome – TonyM Jul 24 at 12:21
• Consider starting with a known-good Arduino example for this combination to validate the hardware and gain some understanding of the needed operations. Then re-visit your custom bare-metal direct-to-registers solution informed by that. – Chris Stratton Jul 24 at 16:18

## 2 Answers

Single byte transaction on SPI bus usually don't allow the slave to respond anything back. Use the protocol required by the chip, like sending a command and transferring response back during a single transaction defined by CS being low.

• That's not always the case. There are SPI slave devices that return a status register value for the first byte read value, for example. It is true for the MFRC522 so upvoted, even though I suspect you hadn't checked :-) – TonyM Jul 24 at 9:42
• i really don't know how to do this , i searched all over the internet ,i think the problem is the main function but i don't know what should i change – Gabi Zaharia Jul 24 at 10:03

Correct me if I'm wrong, but does not Studio 7 also use the SPI for programming the Atmega? If so, then possibly you are having some bus clash between your programmer ((SPI Master) and Atmega in master mode.

Try unplugging your programmer before running your code, and see what happens.