Hardware Reference
In-Depth Information
The same setting can be achieved by the following C statements:
SPI0BR 5 0x10;
/* set baud rate to 6 MHz */
SPI0CR1 5 0x50;
/* enable SPI, data shift on SCK's rising edge, master mode */
SPI0CR2 5 0x02;
/* disable bidirectional mode, SPI stops in wait mode */
WOMS 5 0;
/* enable Port S pull-up */
Sending and reading data using the SPI interface is very straightforward. The following four
functions are used most often:
1. putcSPIx ( x 5 0, 1, or 2). This function sends out a character through the SPI interface.
In assembly language, the character to be output is passed in accumulator A.
2. putsSPIx ( x 5 0, 1, or 2). This function outputs a NULL-terminated string through the
SPI interface. In assembly language, the string is pointed to by index register X.
3. getcSPIx ( x 5 0, 1, or 2). This function reads in a character from the SPI interface. In
assembly language, the character is returned in accumulator A.
4. getsSPIx ( x 5 0, 1, or 2). This function reads in a string from the SPI interface. The
buffer to hold the string is pointed to by index register X, whereas the number of
characters to read is specified in accumulator B.
Example 10.4
Write the four common SPI data transfer functions in the assembly and C languages.
Solution: Before writing a byte to the SPI data register, the user must make sure that the SPTEF
flag is 1. To make sure that the SPI transfer is completed, the program must wait until the SPIF
flag is set to 1.
The following assembly function outputs the character in accumulator A to SPI0:
putcspi0 brclr SPI0SR,SPTEF,*
; wait until write operation is permissible
staa SPI0DR
; output the character to SPI0
brclr SPI0SR,SPIF,*
; wait until the byte is shifted out
ldaa SPI0DR
; clear the SPIF flag
rts
The following assembly function outputs a NULL-terminated string pointed to by index
register X:
putsspi0 ldaa 1,x 1
; get 1 byte to be output to SPI port
beq
doneps0
; reach the end of the string?
jsr
putcspi0
; call subroutine to output the byte
bra
putsspi0
; continue to output
doneps0 rts
To read a byte from the SPI interface, a byte must be written into the SPI x DR register to
trigger eight clock pulses to be sent out from the SCK pin. The following assembly function
reads a character from the SPI0 interface and returns the character in accumulator A:
getcspi0
brclr SPI0SR,SPTEF,*
; wait until write operation is permissible
staa SPI0DR
; trigger eight clock pulses for SPI transfer
brclr SPI0SR,SPIF,*
; wait until a byte has been shifted in
ldaa SPI0DR
; return the byte in A and clear the SPIF flag
rts
Search WWH ::




Custom Search