for(;;){
putchar_spi0(1); // send the data one byte (test value 1).
time[0]=getchar_spi0(); // receive 3 bytes.
time[1]=getchar_spi0();
time[2]=getchar_spi0();
PTS_PTS3=~PTS_PTS3;
delay(20);
//time[3]=PTS;
for(i=0;i<3;i++)
tx_sci(time);
}
}
结果master的MOSI端输出波形正确,MISO也能将信号采进来,但是sck无输出,始终低电平,两mcu无法通信,master收到的全是FF。如果断掉slave,将master的MOSI和MISO连接起来则可以收到getchar函数里为了读数而发送的61,所以master里SPI移位时钟是好的,但SCK究竟为什么无法输出呢?
slave的程序如下:
#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
/************* @ INI_SPI *********************/
void ini_spi(void){
SPI0BR=0b01110111; //clock divisor is 2
SPI0CR2=0b00000010;// SS is input
SPI0CR1=0b01000000;// as slave
}
/*************** @@ SPI_WRITE *****************/
void putchar_spi0 (char cx)
{ unsigned char temp;
while(!(SPI0SR_SPTEF)); /* wait until write is permissible */
SPI0DR = cx; /* output the byte to the SPI */
while(!(SPI0SR_SPIF)); /* wait until write operation is complete */
// temp=SPI0SR;
temp=SPI0DR; // clear the spif flag.
}
/************** @@@ SPI_READ *******************/
char getchar_spi0(void)
{
unsigned char te;
while(!(SPI0SR_SPTEF)); /* wait until write is permissible */
SPI0DR = 0x00; /* trigger 8 SCK pulses to shift in data */
while(!(SPI0SR_SPIF)); /* wait until a byte has been shifted in */
// te=SPI0SR;
te=SPI0DR; // clear the spif flag.
return te; /* return the character */
}
/////////////////########*** MAIN ***###########/////////////////
void main(){
char test;
ini_spi();
for(;;){