首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

spi

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

返回列表