我想写个C51与FPGA的SPI接口的C程序,我知道51要用模拟的SPI,哪位大贤能帮帮忙,让我学习一下。

我想写个C51与FPGA的SPI接口的C程序,我知道51要用模拟的SPI,哪位大贤能帮帮忙,让我学习一下。,第1张

这是 读写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=0i<8i++)

{

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=0i<8i++)

{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

}

这个很明显嘛,上面的错误是:(send_data)>>num,右移num位之后,还是非0的数,所以DOUT就为1,0x80,就是10000000嘛,移7次还是非0,所以全发送为1

下面这个就对了,把要发送的数据,一位胡册一位的放到CY里了,当然就发对了

还有岁册很多方法可以做到这个,比如先把send_data放到ACC里,然后对ACC>>num,DOUT=ACC.0,或裤雀宏者是:

if((send_data>>num)&0x01)

DOUT=1

else

DOUT=0

这样也可以,主要是bit和byte的转换关系要搞清楚

希望能帮到你


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12525674.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存