/**************************
文件所用資源
1.端口:P0.4,P0.5,P0.6,P0.7
2.調(diào)用delay_ms函數(shù)
**************************/
/*************************
模擬SPI接口I/O定義
*************************/
sbit spi_cs=P0^1;
sbit spi_di=P0^2;
sbit spi_clk=P0^3;
sbit spi_do=P0^4;
/*******************************
向SPI器件寫入一個字節(jié)數(shù)據(jù)
*******************************/
void spi_write(unsigned char spi_dat)
{
unsigned char i;
spi_cs=0;
for (i=0;i<8;i++)
{
spi_clk=0;
if((spi_dat & 0x80)==0x80)spi_di=1;
else spi_di=0;
spi_clk=1;
spi_dat=(spi_dat<<1);
}
spi_cs=1;
}
/********************************
從SPI器件讀出一個字節(jié)數(shù)據(jù)
********************************/
unsigned char spi_read()
{
unsigned char i,spi_dat;
spi_cs=0;
for (i=0;i<8;i++)
{
spi_clk=0;
spi_dat=(spi_dat<<1);
spi_clk=1;
if(spi_do==1)spi_dat|=0x01;
else spi_dat&=~0x01;
}
spi_cs=1;
return spi_dat;
}