这是 读写pcf2127a的程序。用spi接口。
cpu 是51兼容系列 cy7c68013
#define spi_read 0x20
#define spi_write 0xa0
void spi_start(void)
{ OEE|=spi_ce+spi_di+spi_clk;
OEE &= ~spi_do;
SYNCDELAY;
IOE&=~(spi_ce|spi_clk);
SYNCDELAY;
}
void spi_stop(void)
{
IOE |=spi_ce;
SYNCDELAY;
}
void spi_wr(unsigned char command)
{
char i;
for (i=0;i<8;i++)
{
if((command&0x80)==0x80) {IOE |=spi_di;}else {IOE &=~spi_di;}
command<<=1;
SYNCDELAY;
IOE |=spi_clk;
SYNCDELAY;
IOE &=~spi_clk;
SYNCDELAY;
SYNCDELAY;
}
}
unsigned char spi_rd(void)
{
unsigned char bret;
char i;
bret=0;
for (i=0;i<8;i++)
{IOE |=spi_clk;
bret<<=1;
SYNCDELAY;
SYNCDELAY;
if((IOE&spi_do)==spi_do){ bret++;}
IOE &= ~spi_clk;
SYNCDELAY;
SYNCDELAY;
}
return bret;
}
void spi_writedata(unsigned char address,unsigned char command)
{
address&=0x1f;
address|=0x20;
spi_start();
spi_wr(address);
SYNCDELAY;
spi_wr(command);
spi_stop();
}
unsigned char spi_readdata(unsigned char address)
{unsigned char bret;
address&=0x1f;
address |=0xa0;
spi_start();
spi_wr(address);
SYNCDELAY;
bret=spi_rd();
spi_stop();
return bret;
}
void read_time(unsigned char address)
{spi_start();
spi_wr(address|0xa0);
EP0BUF[8] =spi_rd();//秒
EP0BUF[9]= spi_rd();//分
EP0BUF[10]= spi_rd();//时
EP0BUF[11]= spi_rd();//日
EP0BUF[12]= spi_rd();
hEP0BUF[13]= spi_rd();//月
EP0BUF[14]= spi_rd();//年
spi_stop();
}
void write_ram(unsigned int address,unsigned int command)
{unsigned char addh,addl,commandh,commandl;
addh=(address&0xff00)>>8;
addl=address&0x00ff;
commandh=(command&0xff00)>>8;
commandl=command&0x00ff;
spi_writedata(0x1a,addh);
spi_writedata(0x1b,addl);
spi_writedata(0x1c,commandh);
spi_writedata(0x1c,commandl);
}
unsigned int read_ram(unsigned int address)
{
unsigned char addh,addl,commandh,commandl;
unsigned int command;
addh=(address&0xff00)>>8;
addl=address&0x00ff;
spi_writedata(0x1a,addh);
spi_writedata(0x1b,addl);
commandl=spi_readdata(0x1d);
commandh=spi_readdata(0x1d);
command=(commandh<<8)+commandl;
return command;
}
SPI协议中接收和发送数据是一体的,数据传输的线路为一个循环链路(loop)如果主机发送数据给从机,则必须从从机读取数据(即从机也发送数据到主机)同理,如果主机读取从机发来的数据,也必须同时发送数据给从机总之,同一个时钟周期内,读和写2个 *** 作都必须执行
以上就是关于我想写个C51与FPGA的SPI接口的C程序,我知道51要用模拟的SPI,哪位大贤能帮帮忙,让我学习一下。全部的内容,包括:我想写个C51与FPGA的SPI接口的C程序,我知道51要用模拟的SPI,哪位大贤能帮帮忙,让我学习一下。、根据SPI协议,写一字节数据到从机,为什么还要读出一字节的数据示例程序如下:、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)