芯片ORG引脚接VCC,16位数据,地址为A9---A0
主要代码如下:
// Write enable must precede all programming modes.
void EEPROM_Ewen(void)
{
u16 i,InData;
EEPROM_CS_0( );
EEPROM_SCLK_0( );
delay_us(100);
EEPROM_CS_1( );
InData=0x4C0; // 100 11XX XXXX
for(i = 0; i < 11; i++)
{ // 9
if(InData&0x400)
{
EEPROM_MOSI_1( );
}
else
EEPROM_MOSI_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
EEPROM_SCLK_0( );
delay_us(100);
InData <<= 1;
}
EEPROM_CS_0( );
}
// Write enable must precede all programming modes.
void EEPROM_Ewds(void)
{
unsigned int i,InData;
EEPROM_CS_0( );
EEPROM_SCLK_0( );
delay_us(100);
EEPROM_CS_1( );
InData=0x400; // 100 00XX XXXX
for(i = 0; i < 11; i++)
{ // 9
if(InData & 0x400)
{
EEPROM_MOSI_1( );
}
else EEPROM_MOSI_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
EEPROM_SCLK_0( );
delay_us(100);
InData <<= 1;
}
EEPROM_CS_0( );
}
void EEPROM_Earse(u16 address)
{
unsigned char i;
u16 result = 0;
EEPROM_SCLK_0( );
EEPROM_CS_0( );
delay_us(100);
EEPROM_CS_1( );
address=(address&0x3FF)|0x1C00;
for(i = 0; i < 13; i++ )
{ // 8
if(address & 0x1000) EEPROM_MOSI_1( );
else EEPROM_MOSI_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
EEPROM_SCLK_0( );
delay_us(100);
address <<= 1;
}
delay_us(100);
EEPROM_CS_0( );
//EEPROM_SCLK_0( );
delay_us(1); // 1
EEPROM_CS_1( );
delay_us(1);
//while(!EEPROM_MISO_IS_HIGH())
while(!PBin(4))
{ // busy test
EEPROM_SCLK_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
}
EEPROM_SCLK_0( );
EEPROM_CS_0( );
}
// Reads data stored in memory, at specified address.
u16 EEPROM_Read(u16 address)
{
unsigned char i;
u16 result = 0;
EEPROM_Ewen();
EEPROM_SCLK_0( );
EEPROM_CS_0( );
delay_us(100);
EEPROM_CS_1( );
address=(address&0x3FF)|0x1800;
for(i = 0; i < 13; i++ )
{ // 8
if(address & 0x1000) EEPROM_MOSI_1( );
else EEPROM_MOSI_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
EEPROM_SCLK_0( );
delay_us(100);
address <<= 1;
}
EEPROM_SCLK_1( );
delay_us(100);
EEPROM_SCLK_0( );
delay_us(100); // 1
//DO=1;
delay_us(100);
for(i = 0; i < 16; i++)
{ // 16
EEPROM_SCLK_1( );
delay_us(100);
result = result << 1;
if (EEPROM_MISO_IS_HIGH())
{
result++;
}
EEPROM_SCLK_0( );
delay_us(100);
}
EEPROM_CS_0( );
EEPROM_Ewds();
return(result);
}
// Writes memory location An - A0.
void EEPROM_Write(u16 address,u16 InData)
{
unsigned char i;
EEPROM_Ewen(); //3õê¼»ˉê±Ö′DDò»′ξíDDáË£¬
delay_us(5);
// 101 A9-A0
EEPROM_Earse(address);//¼ó2»¼óÕaDD½á1û¶¼ò»Ñù
delay_us(5);
EEPROM_CS_0( );
EEPROM_SCLK_0( );
delay_us(100);
EEPROM_CS_1( );
address=(address&0x3FF)|0x1400;
for(i = 0; i < 13; i++)
{ // 8
if(address & 0x1000)
EEPROM_MOSI_1( );
else
EEPROM_MOSI_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
EEPROM_SCLK_0( );
delay_us(100);
address<<=1;
}
for(i = 0; i< 16; i++)
{ // 16
if(InData & 0x8000)
EEPROM_MOSI_1( );
else
EEPROM_MOSI_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
EEPROM_SCLK_0( );
delay_us(100);
InData<<=1;
}
//EEPROM_SCLK_1( );
delay_us(100);
EEPROM_CS_0( );
//EEPROM_SCLK_0( );
delay_us(1); // 1
EEPROM_CS_1( );
delay_us(1);
//while(!EEPROM_MISO_IS_HIGH())
while(!PBin(4))
{ // busy test
EEPROM_SCLK_0( );
delay_us(100);
EEPROM_SCLK_1( );
delay_us(100);
}
EEPROM_SCLK_0( );
delay_us(100);
EEPROM_CS_0( );
delay_us(100);
EEPROM_Ewds(); //2»DèòaD′íê¾íEWDS
}
在主函数中做如下测试:
EEPROM_Ewen();
while (1)
{
EEPROM_Write(0x01,0x01);
data_read_test = EEPROM_Read(0x01);
}
data_read_test始终返回 0xffff。
通过万用表测试 do引脚,发现该引脚始终为高,怎么都不会进入到busy状态。
请熟悉的同志帮忙看看,代码中有什么问题吗?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
datasheet上要求是11位的,100 11 xxxxxx,ORG是置高,按16位访问的。
一周热门 更多>