Hardware Reference
In-Depth Information
The following assembly function reads a string from the SPI0 interface:
getsSPI0 tstb
; check the byte count
beq
donegs0
; return when byte count is zero
jsr
getcspi0
; call subroutine to read a byte
staa 1,x 1
; save the returned byte in the buffer
decb
; decrement the byte count
bra
getsspi0
donegs0 clr
0,x
; terminate the string with a NULL character
rts
These four assembly functions are stored in the file spi0util.asm and are included on the
complementary CD.
The C language versions of the previous four functions are as follows:
void putcspi0 (char cx)
{
char
temp;
while(!(SPI0SR & SPTEF));
// wait until write is permissible
SPI0DR 5 cx;
// output the byte to the SPI
while(!(SPI0SR & SPIF));
// wait until write operation is complete
temp 5 SPI0DR;
// clear the SPIF flag
}
void putsspi0(char *ptr)
{
while(*ptr) {
// continue until all characters have been outputted
putcspi0(*ptr);
ptr 11 ;
}
}
char getcspi0(void)
{
while(!(SPI0SR & SPTEF));
// wait until write is permissible
SPI0DR 5 0x00;
// trigger eight SCK pulses to shift in data
while(!(SPI0SR & SPIF));
// wait until a byte has been shifted in
return SPI0DR;
// return the character
}
void getsspi0(char *ptr, char count)
{
while(count) {
/* continue while byte count is nonzero */
*ptr 11 5 getcspi0(); /* get a byte and save it in buffer */
count 22 ;
}
*ptr 5 0;
/* terminate the string with a NULL */
}
These four C functions are stored in the file spi0util.c and are included in the complemen-
tary CD.
 
Search WWH ::




Custom Search