[求助]MC9S12与TCL1543的SPI通信问题
- UID
- 162030
- 性别
- 男
|
[求助]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 */
}
|
|
|
|
|
|
- UID
- 104631
- 性别
- 男
|
|
|
|
|
|