C51
单片机读写93C66A不正常,程序如下,那位大侠用过的指点一下.
//-----------------------------------------------------
//Enable Write 写使能
//-----------------------------------------------------
void EnWr(void)
{
uint8 i;
uint16 comm;
//启动总线
ioEeCs=0;
nop;
nop;
ioClk=0;
nop;
nop;
ioEeCs=1;
nop;
nop;
//发送指令
comm = 0x9800;
for(i=0;i<12;i++)
{
ioClk=0;
nop;
nop;
comm<<=1;
ioSdo=CY;
nop;
nop;
ioClk=1;
nop;
nop;
}
//停止总线
ioEeCs=0;
}
//-----------------------------------------------------
//Disable Write 写保护
//-----------------------------------------------------
void DisWr(void)
{
uint8 i;
uint16 comm;
//启动总线
ioEeCs=0;
nop;
nop;
ioClk=0;
nop;
nop;
ioEeCs=1;
nop;
nop;
//发送指令
comm = 0x8000;
for(i=0; i<12; i++)
{
ioClk=0;
nop;
nop;
comm<<=1;
ioSdo=CY;
nop;
nop;
ioClk=1;
nop;
nop;
}
ioEeCs=0;
}
//-----------------------------------------------------
//写数据
//-----------------------------------------------------
void WriteEe1Bey(uint8 Addr,uint8 InData)
{
uint8 i;
uint16 comm;
uint16 Delays;
//启动总线
ioEeCs=0;
nop;
nop;
ioClk=0;
nop;
nop;
ioEeCs=1;
nop;
nop;
//发送指令
comm = 0x0A00 + (uint16)(Addr);
comm <<= 4;
for(i = 0; i < 12; i++)
{
ioClk = 0;
nop;
nop;
comm <<= 1;
ioSdo = CY;
nop;
nop;
ioClk = 1;
nop;
nop;
}
//发送数据
for(i = 0;i < 8; i++)
{
ioClk=0;
nop;
nop;
InData <<= 1;
ioSdo=CY;
nop;
nop;
ioClk=1;
nop;
nop;
}
ioEeCs=0;
nop;
nop;
ioEeCs=1;
nop;
nop;
Delays = 0;
while(!ioSdi)
{
Delays++;
if(Delays>30000)
{
break;
}
}
ioEeCs=0;
}
void WriteEe(uint8 Addr,uint16 InData)
{
uint8 dataH;
uint8 dataL;
dataL = InData & 0xFF;
InData = InData >> 8;
dataH = InData & 0xFF;
Addr = Addr & 0x3f;
Addr = Addr * 2;
EnWr();
WriteEe1Bey(Addr, dataH);
Addr ++;
WriteEe1Bey(Addr, dataL);
DisWr();
}
//-----------------------------------------------------
//读数据
//-----------------------------------------------------
uint16 ReadEe(uint8 Addr)
{
uint8 i;
uint16 comm;
uint16 EeValue;
Addr = Addr & 0x3f;
Addr = Addr * 2;
//启动总线
ioEeCs=0;
nop;
nop;
ioClk=0;
nop;
nop;
ioEeCs=1;
nop;
nop;
//发送指令
comm = 0x0C00 + (uint16)(Addr);
comm <<= 4;
for(i=0;i<12;i++)
{
ioClk=0;
nop;
nop;
comm<<=1;
ioSdo=CY;
nop;
nop;
ioClk=1;
nop;
nop;
}
EeValue=0;
for(i=0;i<16;i++)
{
ioClk=0;
EeValue<<=1;
ioClk=1;
nop;
nop;
if(ioSdi == 1)
{
EeValue|=1;
}
}
ioEeCs=0;
return EeValue;
}
一周热门 更多>