beautypg.com

Rainbow Electronics ATmega64C1 User Manual

Page 161

background image

161

7647A–AVR–02/08

ATmega32/64/M1/C1

Note:

1. The example code assumes that the part specific header file is included.

Assembly Code Example

(1)

SPI_SlaveInit:

; Set MISO output, all others input

ldi

r17,(1<

out

DDR_SPI,r17

; Enable SPI

ldi

r17,(1<

out

SPCR,r17

ret

SPI_SlaveReceive:

; Wait for reception complete

sbis

SPSR,SPIF

rjmp

SPI_SlaveReceive

; Read received data and return

in

r16,SPDR

ret

C Code Example

(1)

void

SPI_SlaveInit(void)

{

/* Set MISO output, all others input */

DDR_SPI = (1<

/* Enable SPI */

SPCR = (1<

}

char

SPI_SlaveReceive(void)

{

/* Wait for reception complete */

while

(!(SPSR & (1<

;

/* Return data register */

return

SPDR;

}