Rainbow Electronics ATtiny43U User Manual

Page 103

Advertising
background image

103

8048B–AVR–03/09

<continued>

SPITransfer_loop:

out

USICR,r17

in

r16, USISR

sbrs

r16, USIOIF

rjmp

SPITransfer_loop

in

r16,USIDR

ret

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 DDRA. 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

Advertising