|
这段程序完成了利用IIC写入EEPROM的任务,先是写入IIC slave的地址,然后写入EEPROM的地址,最后写入data。但是,写入EEPROM的地址这段没看懂,照这样不能写入我要求写入的地址,实际运行中这段程序却是可以完成写入EEPROM的功能的,求大家指教下!代码如下:
void EEPROM_WRITE_WORD(Uint16 address, Uint16 data)
{
Uint16 para_temp0, para_temp1, eeprom_waitcount;
int romaddr=0;
romaddr = MDB2ROMADDR(address);
if(romaddr == -1) return;
address = (Uint16)romaddr;
eeprom_waitcount = 0;
while(eeprom_waitcount < 20)
{
ServiceDog(); //KickDog();
eeprom_waitcount++;
STARTE2PROM();
para_temp0 = 0x0a0; //;GET read ctrl byte 0xa0
para_temp1 = EEPROM_WRITE_BYTE(para_temp0);
if(para_temp1 == 0)
{
//;--->>---写地址字节
para_temp0 = address >> 7;
EEPROM_WRITE_BYTE(para_temp0 &0xff); //;Write Address to EEPROM
para_temp0 = ((address << 1) &0xfe);
EEPROM_WRITE_BYTE(para_temp0 &0xff); //;Write Address to EEPROM
WRITEE2PROMAWORD(data); ////;Write DATA to EEPROM
ENDE2PROM();
gSysFaultMessage.bit.InvEEpromFault = 0; //V1.07
sub_ErrCode_delete(234);
return ;
}
}
gSysFaultMessage.bit.InvEEpromFault = 1; //V1.07
error_code = ErrCode_EepromRWErr;
EepromWriteErr++;
//;--->>---写地址字节
para_temp0 = address >> 7;
EEPROM_WRITE_BYTE(para_temp0 &0xff); //;Write Address to EEPROM
para_temp0 = ((address << 1) &0xfe);
EEPROM_WRITE_BYTE(para_temp0 &0xff); //;Write Address to EEPROM
WRITEE2PROMAWORD(data); //;Write DATA to EEPROM
ENDE2PROM();
return ;
}
Uint16 EEPROM_WRITE_BYTE(Uint16 data)
{
Uint16 para_temp0, para_temp1, para_temp2, para_temp3;
para_temp0 = (data << 8) &0xff00;
for(para_temp2 = 0; para_temp2 < 1; para_temp2++)
//;OUTER LOOP COUNTER
{
for(para_temp1 = 0; para_temp1 < 8; para_temp1++)
//;INNER LOOP COUNTER
{
scl_low; //SCL=0
DELAY_US(TLOW);
if(para_temp0 &0x8000)
{
//;--->>---SDA=1 -------->>
sda_high;
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1; // GPIO32 = output
EDIS;
sda_high;
}
else
{
//;--->>---SDA=0 -------->>
sda_low;
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1; // GPIO32 = output
EDIS;
sda_low;
}
DELAY_US(2);
scl_high; //SCL=1
DELAY_US(THIGH);
para_temp0 = para_temp0 << 1;
}
scl_low; //SCL=0
DELAY_US(TFALL);
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 0; // GPIO32 = input
EDIS;
DELAY_US(TLOW);
scl_high; //SCL=1
DELAY_US(THIGH);
para_temp3 = GpioDataRegs.GPBDAT.bit.GPIO32; //;ACK BACKUP
scl_low; //SCL=0
DELAY_US(TFALL);
sda_high;
EALLOW;
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1; // GPIO32 = output
EDIS;
sda_high;
DELAY_US(TLOW);
}
return (para_temp3);
}
|
|