我不知道SS按上面接法对不对。
我根据
http://bbs.eccn.com/dispbbs.asp?boardid=3&rootid=118351&id=118351&star=
所述重新写了master程序如下:
#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
/************ @ INI_SCI ********************/
void ini_sci(void) {
SCI0BD=52;
SCI0CR1=0x00;
SCI0CR2=0x0c;
}
/************ @@ TX_SCI ******************/
void tx_sci(char i){
while(SCI0SR1_TC!=1);
SCI0DRL=i;
}
/************* @@@ INI_SPI *********************/
void ini_spi(void){
SPI0BR=0b01110111; //clock divisor is 2
SPI0CR2=0b00000010;// SS is not used(in master mode ,MODFEN=0)
SPI0CR1=0b01010000;// as master
}
/*************** @@@@ 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 = 0x61; /* 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 */
}
/**************** @@@@@@DELAY ********************/
void delay (unsigned char i){
word temp;
for(;i>0;i--){
for(temp=0;temp<1000;)temp++;
}
}
/////////////////########*** MAIN ***###########/////////////////
void main(){
char i,time[3];
ini_sci();
ini_spi();
//DDRS_DDRS6=1;
DDRS_DDRS3=1;
MODRR_MODRR4=1;
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(;;){
test=getchar_spi0();
if(test==1){
putchar_spi0(222);
putchar_spi0(111);
putchar_spi0(123);
}
}
}
|