Ss pin functionality, Slave mode, Atmega128(l) – Rainbow Electronics ATmega128L User Manual

Page 160

Advertising
background image

160

ATmega128(L)

2467B–09/01

The following code examples show how to initialize the SPI as a slave and how to per-
form a simple reception.

Note:

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

SS Pin Functionality

Slave Mode

When the SPI is configured as a slave, the Slave Select (SS) pin is always input. When
SS is held low, the SPI is activated, and MISO becomes an output if configured so by
the user. All other pins are inputs. When SS is driven high, all pins are inputs, and the
SPI is passive, which means that it will not receive incoming data. Note that the SPI
logic will be reset once the SS pin is driven high.

The SS pin is useful for packet/byte synchronization to keep the slave bit counter syn-
chronous with the master clock generator. When the SS pin is driven high, the SPI slave

Assembly Code Example

(1)

SPI_SlaveInit:

; Set MISO output, all others input

ldi

r17,(1<<DD_MISO)

out

DDR_SPI,r17

; Enable SPI

ldi

r17,(1<<SPE)

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<<DD_MISO);

/* Enable SPI */

SPCR = (1<<SPE);

}

char SPI_SlaveReceive(void)

{

/* Wait for reception complete */

while(!(SPSR & (1<<SPIF)))

;

/* Return data register */

return SPDR;

}

Advertising