原文:
http://yuweixian4230.blog.163.com/blog/static/1324686752010361326372/
http://zgqdwrs.blog.163.com/blog/static/104128350200947703047/
include
/*********************************************
模拟SPI接口I/O定义
*********************************************/
sbit spi_cs=P1^2;
sbit spi_di=P1^0;
sbit spi_clk=P1^1;
sbit spi_do=P1^3;
#define set_spi_cs spi_cs=1
#define clr_spi_cs spi_cs=0
#define set_spi_clk spi_clk=1
#define clr_spi_clk spi_clk=0
#define set_spi_di spi_di=1
#define clr_spi_di spi_di=0
#define read_spi_do spi_do
/*********************************************
1ms delay ,22.1184MHZ,STC
*********************************************/
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i
for(j=0;j<1947;j++);
}
/*********************************************
向SPI器件写入一个字节数据
*********************************************/
void spi_write(unsigned char spi_dat)
{
unsigned char i;
for (i=0;i<8;i++)
{
clr_spi_clk;
if((spi_dat & 0x80)==0x80)
set_spi_di;
else
clr_spi_di;
set_spi_clk;
spi_dat=(spi_dat<<1);
}
}
/*********************************************
从SPI器件读出一个字节数据
*********************************************/
unsigned char spi_read()
{
unsigned char i,spi_dat;
clr_spi_cs;
for (i=0;i<8;i++)
{
clr_spi_clk;
spi_dat=(spi_dat<<1);
set_spi_clk;
if(read_spi_do==1)
spi_dat|=0x01;
else
spi_dat&=~0x01;
}
return spi_dat;
}
/*********************************************
模拟SPI接口测试主程序
*********************************************/
main()
{
unsigned int i;
set_spi_cs;
spi_do=0;
delay_ms(1);
for(i=0;i<1;i++)
{
clr_spi_cs;
spi_write(0x00);
spi_write(0xff);
set_spi_cs;
delay_ms(100);
}
while(1);
}
阅读(3694) | 评论(0) | 转发(0) |