ATmega169P
19.2.3
SPI Slave Operation Example
The following code demonstrates how to use the USI module as a SPI Slave:
init:
ldi
sts
r16,(1<<USIWM0)|(1<<USICS1)
USICR,r16
...
SlaveSPITransfer:
sts
ldi
sts
USIDR,r16
r16,(1<<USIOIF)
USISR,r16
SlaveSPITransfer_loop:
lds
r16, USISR
sbrs
rjmp
lds
r16, USIOIF
SlaveSPITransfer_loop
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.
203
8018A–AVR–03/06