AdSense

Sunday, 26 May 2013

ATMega - Hardware SPI

(Deutsche Version) SPI communication can be very useful (e.g. to expand an Raspberry PI with ATMegas). To use the built in SPI Interface of an ATMega8, I hardly found any information. Here is the code which I finally found after a long time of researching:

void SPI_SlaveInit(void)
{
  DDRB |= (1<<PORTB4);//Set MISO to output
  SPCR = (1<<SPE);
}

char SPI_SlaveReceive(void)
{
  char data;
  while(!(SPSR & (1<<SPIF)))
  {}
  data = SPDR;
  SPDR = 0;
  return data;
}

void SPI_SlaveSend(char data)
{
  SPDR = data;
  while(!(SPSR & (1<<SPIF)))
  {}
  SPDR = 0;
}

No comments:

Post a Comment