Board logo

标题: spi [打印本页]

作者: subtle33cn    时间: 2007-7-20 16:34     标题: spi

void SPIInit(void){
SPI0BR =0b00000001;//clock divisor is 2
SPI0CR2=0b00000010; // SS is not used
SPI0CR1=0b01010000; //as master
}

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 */
}

void putchar_spi0 (char cx)
{
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.
}


void main(void) {
char temp;

/* put your own code here */
EnableInterrupts;

SPIInit();

while(1)
{
putchar_spi0(0x0f);

temp=getchar_spi0();
}

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






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