/*********************************************
模拟SPI接口I/O定义
*********************************************/
sbit spi_cs=P1^2;
sbit spi_di=P1^0;
sbit spi_clk=P1^1;
sbit spi_do=P1^3;
#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
}
/*********************************************
向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);
}
上一篇:4*4矩阵键盘C51程序
下一篇:DS18B20多点温度检测系统
推荐阅读最新更新时间:2024-03-16 15:17