void SpiSentByte(U08 bb)
{
U08 i;
for(i=0;i<8;i++){
SCK=1;
CpuDelayUS(20);
if((bb&0x80)==0)
MOSI="0";
else
MOSI="1";
bb<<=1;
CpuDelayUS(20);
SCK=0;
CpuDelayUS(20);
}
}
U08 SpiReceiveByte(void)
{
U08 i,bb=0;
for(i=0;i<8;i++){
SCK="1";
CpuDelayUS(20);
bb<<=1;
if (MISO==1)
bb|=0x01;
CpuDelayUS(20);
SCK=0;
CpuDelayUS(20);
}
return bb;
}
void Enable7758(void)
{
CS=0;
SCK=1;
CS="0";
}
void Disable7758(void)
{
CS=1;
SCK=1;
CS=1;
}
void Read7758(U08 addr, U08 *pnt,U08 cnt)
{
U08 i;
Enable7758();
SpiSentByte(addr);
CpuDelayUS(4);
for(i=0;i<cnt;i++)
*pnt++=SpiReceiveByte();
Disable7758();
}
void Write7758(U08 addr,U08 *pnt,U08 cnt)
{
U08 i;
Enable7758();
SpiSentByte(addr|0x80);
for(i=0;i<cnt;i++) SpiSentByte(*pnt++);
Disable7758();
}
用户461316 2008-9-9 19:50