Board logo

标题: [求助]MC9S12与TCL1543的SPI通信问题 [打印本页]

作者: huaning88    时间: 2007-6-13 10:24     标题: [求助]MC9S12与TCL1543的SPI通信问题

我的程序如下:
#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.

return data; /* return the character */
}

void da1(uchar d1){
uint addata=0;
DDRB=0xff;
PORTB=0xff;
PORTA=0x00;
putchar_SPI1(d1);
addata=getchar_SPI1();
PORTA=0xff;
PORTB=addata;
}



void main(void) {


/* put your own code here */
EnableInterrupts;

SPIInit();

da1(0x0b);

for(;;) {} /* wait forever */
/* please make sure that you never leave this function */
}


作者: seuafu2005    时间: 2007-6-14 14:38

这个程序是好是坏?




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0