beautypg.com

3 spi slave operation example, 4 two-wire mode – Rainbow Electronics ATtiny861_V User Manual

Page 130

background image

130

2588B–AVR–11/06

ATtiny261/461/861

17.3.3

SPI Slave Operation Example

The following code demonstrates how to use the USI module as a SPI Slave:

init:

ldi

r16,(1<

sts

USICR,r16

...

SlaveSPITransfer:

sts

USIDR,r16

ldi

r16,(1<

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 USI Data Register clock. The
loop is repeated until the USI Counter Overflow Flag is set.

17.3.4

Two-wire Mode

The USI Two-wire mode is compliant to the Inter IC (TWI) bus protocol, but without slew rate lim-
iting on outputs and input noise filtering. Pin names used by this mode are SCL and SDA.