ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 3149

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

50 4 Using the Synchronous Serial Interface SPI

SCK

MOSI

Tx shift register

Rx shift register

MISO

CLK

Control logic

Interrupt

request

Control

Status

Data

register

register

register

Internal bus

Fig. 4.2. General block diagram of the SPI

When the MASTER writes by software a byte into the transmitter’s data register, eight clocks are automatically generated to the SCK line, and the bi-directional transmission begins. Whatever is found at this moment in the SLAVE’s Tx Data Register is shifted into the MASTER’s Rx Data Register.

When more than one SLAVE device is connected to the SPI bus, only one SLAVE may be enabled at a certain time. An additional signal SS (SLAVE Select), active LOW, is used to select the active SLAVE device.

4.3 The SPI of HC11 Microcontrollers

The structure of the SPI of HC11 follows exactly the general structure of a peripheral interface, shown in Fig. 1.6. The registers of the interface are called SPDR (SPI Data Register), SPCR (SPI Control Register), and SPSR (SPI Status Register).

SPDR – SPI Data Register

When writing to the address of SPDR, data is directly written into the transmitter’s shift register. Reading from this address actually reads the last byte received in the receivers shift register. SPDR is double buffered for input, and single buffered for output. This means that SPDR can be read while a data transfer is in progress, but if a byte is written into SPDR before the transmission of the previous character is completed, an error condition occurs.

SPSR – SPI Status Register

The status register has the following structure:

SPSR

7

6

5

4

3

2

1

0

SPIF

WCOL

MODF

RESET

0

0

0

0

0

0

0

0


4.3 The SPI of HC11 Microcontrollers

51

SPIF – SPI transfer complete flag. This flag is set when a SPI transfer is complete

(after eight SCK cycles in a data transfer). It is cleared by reading SPSR (with SPIF = 1), then accessing (reading or writing) SPDR.

SPIF = 0 No SPI transfer complete or SPI transfer still in progress SPIF = 1 SPI transfer complete

WCOL – Write Collision. This is an error flag. It is set if the software tries to

write data into SPDR while SPI data transfer is in progress. Clear this flag by reading SPSR (WCOL = 1), then access SPDR.

WCOL = 0 No write collision WCOL = 1 Write collision

MODF – Mode Fault. This is another error flag. This error occurs when the input line SS is pulled LOW (the device is hardware configured as SLAVE) while the bit MSTR in the control register SPCR is set to 1.

MODF = 0

No mode fault

MODF = 1

Mode fault

This type of error occurs in multi-MASTER networks when more than one device is trying to become MASTER at a certain moment.

All other bits of SPSR are not implemented and always read 0. Out of RESET, SPSR is cleared.

SPCR – SPI Control Register

SPCR

7

6

5

4

3

2

1

0

SPIE

SPE

DWOM

MSTR

CPOL

CPHA

SPR1

SPR0

RESET

0

0

0

0

0

1

U

U

SPIE – SPI Interrupt Enable. When set to 1, this bit enables interrupt requests from the SPI system.

SPIE = 0 SPI system interrupts disabled SPIE = 1 SPI system interrupts enabled

SPI can generate interrupts in two situations:

Each time the SPIF bit in SPSR is 1 (a serial data transfer is completed).

Each time MODF is set.

SPE – Serial Peripheral System Enable

SPE = 0 SPI system disabled

SPE = 1 SPI system enabled

This control bit enables the whole SPI system and assigns port D bits 2, 3, 4, 5 to SPI. If the SPI works in master mode and DDRD bit 5 is set, then the port D bit 5 pin becomes a general-purpose output line instead of the SS input.


52 4 Using the Synchronous Serial Interface SPI

DWOM – Port D Wired-OR Mode. This bit selects the type of output on PORT D. DWOM affects all port D pins. This bit is not directly related to the SPI. It is placed here because the SPI uses most of the PORTD bits.

DWOM = 0 PORTD is configured as normal CMOS outputs

DWOM = 1 PORTD has open-drain outputs

MSTR – Master Mode Select. This bit must be set by software to configure the device to work as MASTER for the SPI link.

MSTR = 0

Slave mode

MSTR = 1

Master mode

CPOL – Clock Polarity

CPOL = 0 SCK line idles LOW CPOL = 1 SCK line idles HIGH

CPHA – Clock Phase

CPHA = 0 SS must go HIGH between successive characters transmitted. CPHA = 1 SS can be maintained LOW for all transfers

CPOL and CPHA must be programmed with identical values for MASTER and SLAVE

SPR[1:0] – SPI Clock Rate Selects

When the device is configured as MASTER, these two bits select how the SPI clock (SCK) is obtained by dividing the main clock E, as described in Table 4.1. When the device is configured as SLAVE, these bits have no effect, because the MASTER generates the clock SCK.

Table 4.1. The effect of programming [SPR1–SPR0]

SPR1-SPR0

SCK is E clock divided by:

00

2

01

4

10

16

11

32

Initialization of the Interface

Obviously, the initialization sequence is different for MASTER and SLAVE. Below is an example of an initialization sequence. Note that the SPI system interrupts are enabled at the SLAVE device.

4.4

The SPI of the AVR Microcontrollers

53

*MASTER SPI initialization routine

SPI_INITM

LDAA

#$20

;configure PORTD bit 5 as

;general purpose output

STAA

DDRD

BCLR

PORTD,$20

;make SS=0

LDAA

#$56

;SPI enable, Master,

CPHA=1

;clock:32

STAA

SPCR

RTS

* SLAVE SPI initialization routine

SPI_INITS

LDAA

#$C6

;SPIE, SPE, Slave, CHPA=1

;clock:32

STAA

SPCR

RTS

4.4 The SPI of the AVR Microcontrollers

The registers of the SPI interface on AVRs are almost identical with those described for HC11. Even the names of the registers are identical: SPDR, SPSR, SPCR. Note that some AVR microcontrollers have a so-called Universal Serial Interface (USI), instead of SPI. The registers of USI are called USIDR, USISR, USICR. This paragraph describes the SPI registers of AT90S8515/8535.

SPSR – SPI Status Register

The status register has the following structure:

SPSR

7

6

5

4

3

2

1

0

SPIF

WCOL

RESET

0

0

0

0

0

0

0

0

Only two bits are implemented in SPSR: SPIF and WCOL. They have identical functions with the homologous bits of HC11. MODF is not implemented at the AVR, because the behavior of the AVR is slightly different. If the SS input is pulled LOW when the device is software configured as MASTER, the MSTR bit in SPCR is automatically cleared.

SPCR – SPI Control Register

SPCR

7

6

5

4

3

2

1

0

SPIE

SPE

DORD

MSTR

CPOL

CPHA

SPR1

SPR0

RESET

0

0

0

0

0

0

0

0


54 4 Using the Synchronous Serial Interface SPI

DWOM is replaced here by DORD (Data Order).

DORD = 1 then the LSB of the data word is transmitted first DORD = 0 then the MSB of the data word is transmitted first.

The effect of SPR1–SPR0 is also different, as described in Table 4.2.

Table 4.2. The effect of programming [SPR1–SPR0]

SPR1-SPR0

SCK is the system clock divided by:

00

4

01

16

10

64

11

128

Initialization of the SPI Interface

INIT_SPIM:

;MASTER initialization

SBI

DDRB,5

;configure portb, bit 5 as

;output (MOSI)

SBI

DDRB,7

;configure portb, bit 7 as

output (SCK)

SBI

DDRB,4

;configure portb, bit 4 as

;output

LDI

R16,0X54

;master, no interrupts,

;fastest clock

OUT

SPCR,R16

RET

;return to main program

INIT_SPIS:

;SLAVE initialization

CBI

DDRB,5

;configure portb, bit 5 as

;input (MOSI)

SBI

DDRB,6

;portb, bit 6 as output

(MISO)

CBI

DDRB,7

;portb, bit 7 as input

(SCK)

CBI

DDRB,4

;portb, bit 4 as input

(SS)

LDI

R16,0XC4

;slave, interrupts

enabled,

;fastest clock

OUT

SPCR,R16

RET

;return to main program

Note that the AVR microcontrollers do not automatically configure the I/O lines used by the SPI as input or output lines.


4.5 Examples of Using The SPI

55

4.5 Examples of Using The SPI

4.5.1 Using the SPI To Connect Two Microcontrollers

The SPI is a fast data communication link. It is not designed to work over long distances. SPI can be used to create networks with two or more microcontrollers, when processing tasks are distributed between two or more microcontrollers for faster processing, or to connect special memory or peripheral devices.

A typical hardware SPI connection between two HC11E9 microcontrollers is shown in Fig. 4.3. In all situations, it is important to define a communication protocol between the two devices.

If, for instance, the SLAVE device is used to expand the MASTER’s I/O space with two input ports, then the MASTER should send al least three bytes through the SCI: the first byte is an opcode needed to inform the SLAVE about the intention of the MASTER to read data from the SLAVE’s ports, and two other dummy bytes must be sent in order to generate the clock pulses needed to read the two data bytes from SLAVE.

Note that the SLAVE device does not have any initiative in a SPI link – it’s the MASTER that decides when to start the communication, and how many data bytes are to be exchanged between the two devices.

A convenient way to write the software is to write an interrupt-driven SPI communication routine for the SLAVE, like in the example below:

PC5 14

PC6 15

PC7 16

PD0 20

PD1 21

PD2 22 MISO

PD3 23 MOSI PD4 24 SCK PD5 25 SS

MODA 3

E 5

AS 4

R/W\ 6

14 PC5 15 PC7PC6 16

PD0 20 PD1 21 PD2 22 PD3 23 PD4 24 PD5 25

MODA 3

E 5

AS 4 R/W\ 6

Fig. 4.3. SPI link between two 68HC11E9 microcontrollers

56 4 Using the Synchronous Serial Interface SPI

*SPI_SENDCH - sends character stored at XSPI over SPI

*returns the character received form SLAVE in RSPI

SPI_SENDCH

LDAA

SPSR

;wait for SPIF

ANDA

#$80

;isolate SPIF bit

BEQ

SPI_SENDCH

;if zero, wait

LDAA

XSPI

;get character to send

STAA

SPDR

;send it

SPS05

LDAA

SPSR

;wait for end of

;transmission

ANDA

#$80

BEQ

SPS05

LDAA

SPDR

;read the byte just

;received

STAA

RSPI

;save it to RSPI, and

;return

RTS

SPISVC

LDAA

SPSR

;read SPSR to clear SPIF

LDAA

SPDR

STAA

SPIRDATA

;a RAM location to save

;received byte

INC

QSPI

;true QSPI

LDAA

SPIXDATA

;get the character to be

;sent next time

STAA

SPDR

;place it in SPDR

RTI

;return from interrupt

Note that, at each interrupt, the SLAVE prepares the data to be delivered to MASTER at the next interrupt. The main program is responsible for preparing the transmission data in the variable SPIXDATA, according to the value received from MASTER in SPIRDATA. QSPI is a flag indicating that a character has been received and placed in SPIRDATA by the interrupt service routine.

4.5.2 Expanding the I/O Space Using the SPI

In some cases, when the number of MCU I/O lines is insufficient for a certain application, a possible solution to expand the I/O space is to use shift registers connected to the SPI. The following example shows how the I/O space of an ATMega8 AVR is expanded with one 8-bit input port, and one 8-bit output port. For the input port, a parallel-input serial-output shift register (74165) is used. The output port is implemented using a 4094 serial-input parallel-output register. The schematic of the circuit is presented in Fig. 4.4.

Obviously, in this SPI link, the MASTER device is the microcontroller. The serial data input of register IC2 is connected to the MOSI line of the MCU. The shift clock for this register is generated by the microcontroller on the SCK line. The load (LD) input for the 4094 register is driven by an extra output line of the micrcontroller (PB2).