我的程序如下:
#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
void SPIInit(void){
DDRA=0xff; //PS7CS=1
PORTA=0xff;
SPI1BR =0x77; //clock divisor
SPI1CR2=0x00;//0b00000010; // SS is not used
SPI1CR1=0b01010000; //as master
}
void putchar_SPI1 (uchar cx)
{
char temp;
while(!(SPI1SR_SPTEF)); // wait until write is permissible
SPI1DR =cx; // output the byte to the SPI
while(!(SPI1SR_SPIF)); // wait until write operation is complete
temp=SPI1SR;
temp=SPI1DR; // clear the SPIf flag.
}
uint getchar_SPI1(void)
{
uchar te;
uint data;
while(!(SPI1SR_SPTEF)); /* wait until write is permissible */
SPI1DR = 0x00; /* trigger 8 SCK pulses to shift in data */
while(!(SPI1SR_SPIF)); /* wait until a byte has been shifted in */
te=SPI1SR;
data=SPI1DR<<8; // clear the SPIf flag.
while(!(SPI1SR_SPTEF)); /* wait until write is permissible */
SPI1DR = 0x00; /* trigger 8 SCK pulses to shift in data */
while(!(SPI1SR_SPIF)); /* wait until a byte has been shifted in */
te=SPI1SR;
data+=SPI1DR; // clear the SPIf flag.