本帖最后由 橘子阿小 于 2022-1-7 11:01 编辑
void WR_EEPROM(unsigned char addr,unsigned char dat)
{
uint S_addr;
I2C_Start();
if(addr<=256)S_addr=0xa0;
if(256<addr<=512)S_addr=0xa2;
if(512<addr<=768)S_addr=0xa4;
if(768<addr<=1024)S_addr=0xa6;
I2C_SendByte(S_addr, 1);//发送写器件地址
I2C_SendByte(addr, 1);//发送要写入内存地址
I2C_SendByte(dat, 0); //发送数据
I2C_Stop();
delay1ms(500);
}
unsigned char RD_EEPROM(unsigned char addr)
{
unsigned char num;
uint S_addr,s_addr;
if(addr<=256)S_addr=0xa0;s_addr=0xa1;
if(256<addr<=512)S_addr=0xa2;s_addr=0xa3;
if(512<addr<=768)S_addr=0xa4;s_addr=0xa5;
if(768<addr<=1024)S_addr=0xa6;s_addr=0xa7;
I2C_Start();
I2C_SendByte(S_addr, 1); //发送写器件地址
I2C_SendByte(addr, 1); //发送要读取的地址
I2C_Start();
I2C_SendByte(s_addr, 1); //发送读器件地址
num=I2C_ReadByte(); //读取数据
I2C_Stop();
return num;
}
int main(void)
{
init();
while(1)
{
WR_EEPROM(0x01,20);
WR_EEPROM(0x02,30);
WR_EEPROM(0x101,40);
SJDL011=RD_EEPROM(0x01);
SJDL022=RD_EEPROM(0x02);
SJDL033=RD_EEPROM(0x101);
ClrWdt();
}
读到的结果是40 30 40,也就是写0x101地址的时候把0x01覆盖了,该怎么解决? |