unsigned char rcvd[5]; unsigned char send[]={1,2,3,4,5}; unsigned char count=0; unsigned char junk; void spi_init(void) { SPCR=0xf2; // SPE, DORD, MSTR, CPOL, SPR1 Set SPSR=0x01; // SPI2x Set } <----------------------------------- Polling example ----------------------------------------------------------> void runspi(void) { int i; for (i=0; i<5;i++) // Send out 5 bytes of data by loading it on the SPDR { // Note that the STC interrupt will execute on transmission complete, PORTC=send[i]; // thus we need not worry about losing incoming data while(SPI_BSY); // Do nothing till the SPI section is busy junk = SPSR; // Necessary to read SPSR to clear SPIF after transmission and then rcvd[i]=SPDR; // read the SPI Data register. Both of these must be executed and in this order. } } <----------------------------------- Interrupt example ----------------------------------------------------------> ISR(SPI_STC_vect) { rcvd[count] = SPDR; // SPIF cleared automatically when the interrupt is executed count++; } void runspi(void) { int i; for (i=0; i<5;i++) // Send out 5 bytes of data by loading it on the SPDR { // Note that the STC interrupt will execute on transmission complete, PORTC=send[i]; // thus we need not worry about losing incoming data } }