Datasheet

111
ATtiny25/45/85 [DATASHEET]
2586Q–AVR–08/2013
The code is size optimized using only eight instructions (plus return). The code example assumes that the DO and
USCK pins have been enabled as outputs in DDRB. The value stored in register r16 prior to the function is called is
transferred to the slave device, and when the transfer is completed the data received from the slave is stored back
into the register r16.
The second and third instructions clear the USI Counter Overflow Flag and the USI counter value. The fourth and
fifth instructions set three-wire mode, positive edge clock, count at USITC strobe, and toggle USCK. The loop is
repeated 16 times.
The following code demonstrates how to use the USI as an SPI master with maximum speed (f
SCK
= f
CK
/2):
SPITransfer_Fast:
out USIDR,r16
ldi r16,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)
ldi r17,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)|(1<<USICLK)
out USICR,r16 ; MSB
out USICR,r17
out USICR,r16
out USICR,r17
out USICR,r16
out USICR,r17
out USICR,r16
out USICR,r17
out USICR,r16
out USICR,r17
out USICR,r16
out USICR,r17
out USICR,r16
out USICR,r17
out USICR,r16 ; LSB
out USICR,r17
in r16,USIDR
ret
15.3.3 SPI Slave Operation Example
The following code demonstrates how to use the USI as an SPI slave:
init:
ldi r16,(1<<USIWM0)|(1<<USICS1)
out USICR,r16
...
SlaveSPITransfer:
out USIDR,r16
ldi r16,(1<<USIOIF)
out USISR,r16
SlaveSPITransfer_loop:
in r16, USISR