|
51单片机 I/O 口模拟 SPI 读写 EEPROM
[复制链接]
void Spi_Data_Out( uchr length, uchr d )
{
for ( ; length > 0 ; length -- )
{
Spi_sck = 0;
Spi_Delay(0xf);
if( d & 0x80 )
Spi_out_port = 1;
else
Spi_out_port = 0;
d <<= 1;
Spi_Delay(0xf);
Spi_sck = 1;
Spi_Delay(0xf);
}
Spi_sck = 0;
}
void Spi_Char_Wr( uchr addr, uchr d )
{
Spi_cs = SPI_ENABLE;
Spi_Delay(0xff);
Spi_Data_Out(8,CMD_WRITE);
Spi_Data_Out(8,addr);
Spi_Data_Out(8,d);
Spi_cs = SPI_DISABLE;
Spi_Delay(0xff);
}
///////////输出数据的代码没问题,在proteus中可以看到结果
uchr Spi_Char_Rd( uchr addr)
{
uchr r = 0;
Spi_cs = SPI_ENABLE;
Spi_Delay(0xff);
Spi_Data_Out(8,CMD_READ);
Spi_Data_Out(8,addr);
Spi_cs = SPI_DISABLE;
Spi_Delay(0xff);
r = Spi_Data_In(8);
Spi_cs = SPI_DISABLE;
Spi_Delay(0xff);
return r;
}
uchr Spi_Data_In( uchr length )
{
uchr in = 0;
Spi_sck = 1;
Spi_Delay(0xff);
Spi_cs = SPI_ENABLE;
Spi_Delay(0xff);
for ( ; length > 0 ; length -- )
{
in <<= 1;
Spi_sck = 0;
Spi_Delay(0xff);
if ( Spi_in_port )
in |= 0x01;
else
in &= 0xfe;
Spi_sck = 1;
Spi_Delay(0xff);
}
return in;
}
这个是仿照别人的代码写的程序,读取时钟返回0xff ,请高手帮帮忙呀。。。
|
|