ATmega128
166
The following code examples show how to initialize the SPI as a slave and how to perform a sim-
ple reception.
Assembly Code Example
SPI_SlaveInit:
; Set MISO output, all others input
r17,(1<<DD_MISO)
ldi
out
DDR_SPI,r17
; Enable SPI
ldi
r17,(1<<SPE)
out
SPCR,r17
ret
SPI_SlaveReceive:
; Wait for reception complete
sbis SPSR,SPIF
rjmp SPI_SlaveReceive
; Read received data and return
in
r16,SPDR
ret
(1)
C Code Example
void SPI_SlaveInit(void)
{
/* Set MISO output, all others input */
DDR_SPI = (1<<DD_MISO);
/* Enable SPI */
SPCR = (1<<SPE);
}
char SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return data register */
return SPDR;
}
Note:
1. See "About Code Examples" on page 9.
(1)
2467S–AVR–07/09
Need help?
Do you have a question about the ATmega128 and is the answer not in the manual?
Questions and answers