Spi slave operation example, Attiny2313 – Rainbow Electronics ATtiny2313 User Manual

Page 143

Advertising
background image

143

ATtiny2313

2543A–AVR–08/03

The following code demonstrates how to use the USI module as a SPI Master with max-
imum speed (fsck = fck/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

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)

out

USICR,r16

...

SlaveSPITransfer:

out

USIDR,r16

ldi

r16,(1<<USIOIF)

out

USISR,r16

SlaveSPITransfer_loop:

sbis

USISR,USIOIF

rjmp

SlaveSPITransfer_loop

in

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 trans-
ferred to the master device, and when the transfer is completed the data received from
the Master is stored back into the r16 Register.

Advertising