English
Language : 

ATMEGA165PV_14 Datasheet, PDF (192/364 Pages) ATMEL Corporation – High Endurance Non-volatile Memory segments
ATmega165P
19.3.3
SPI Slave Operation Example
The following code demonstrates how to use the USI module as a SPI Slave:
init:
ldi r16,(1<<USIWM0)|(1<<USICS1)
sts USICR,r16
...
SlaveSPITransfer:
sts USIDR,r16
ldi r16,(1<<USIOIF)
sts USISR,r16
SlaveSPITransfer_loop:
lds r16, USISR
sbrs r16, USIOIF
rjmp SlaveSPITransfer_loop
lds r16,USIDR
ret
The code is size optimized using only eight instructions (+ ret). The code example assumes that
the DO is configured as output and USCK pin is configured as input in the DDR Register. The
value stored in register r16 prior to the function is called is transferred to the master device, and
when the transfer is completed the data received from the Master is stored back into the r16
Register.
Note that the first two instructions is for initialization only and needs only to be executed
once.These instructions sets Three-wire mode and positive edge Shift Register clock. The loop
is repeated until the USI Counter Overflow Flag is set.
8019K–AVR–11/10
192