unsigned unsigned unsigned unsigned
char char char char
rcvd[5]; send[]={1,2,3,4,5}; count=0; junk;
void spi_init(void) { SPCR=0xf2; SPSR=0x01;
// SPE, DORD, MSTR, CPOL, SPR1 Set // 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; interrupt is executed count++; } void runspi(void) { int i; for (i=0; i<5;i++) on the SPDR { execute on transmission complete, PORTC=send[i];
// SPIF cleared automatically when the
// Send out 5 bytes of data by loading it // Note that the STC interrupt will // thus we need not worry about losing
incoming data } }