📄 eeprom1.c
字号:
#include<reg52.h>
#include"port.h"
#include"main.h"
#include"eeprom.h"
#include<intrins.h>
bit SystemError;
uChar sendbuf[10];
uChar receivebuf[10];
void iic_start(void)
{
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
void iic_stop(void)
{
SCL=1;
SDA=0;
SDA=1;
}
void slave_ACK(void)
{
uChar i=0;
SDA=1;
do
{
SCL = 0;
SCL = 1;
}
while((SDA) && (i++ < 100));//EepromScl=1:if EepromSda=0,exit
SCL=0;
}
void slave_NOACK(void)
{
SDA=1;
SCL=1;
SCL=0;
}
void check_ACK(void)
{
SDA=1;
SCL=1;
F0=0;
if(SDA==1)
F0=1;
SCL=0;
}
void IICSendByte(uChar ch)//发送一个byte 到eeprom
{
uChar n,j;
j=ch;
for(n=0;n<8;n++)
{
if((j&0x80)==0x80)
{
SDA=1;
}
else
{
SDA=0;
}
SCL=1;
SCL=0;
j=j<<1;
}
slave_ACK();//从机应答
}
uChar IICreceiveByte(void)
{
uChar n;
uChar tdata;
for(n=0;n<8;n++)
{
SDA=1;
SCL=1;
tdata=tdata<<1;
if(SDA==1)
tdata=tdata|0x01;
else
tdata=tdata&0xfe;
SCL=0;
}
SDA=0;
return(tdata);
}
void writeNByte(uInt slave_add,uChar n)//Write n Byte to eeprom 2006.8.3
{
union u {uInt word; struct{uChar hi; uChar lo;} bytes;};
union u eepromAdd;
uChar send_da,i=0;
eepromAdd.word=slave_add; //注意该处地址连续,对于24C16地址有效范围为0x0000 to 0x07ff 共2K
eepromAdd.bytes.hi=(eepromAdd.bytes.hi&0x07)<<1;
send_da=0;
iic_start();
IICSendByte(EEPROMWRCMD|eepromAdd.bytes.hi);
IICSendByte(eepromAdd.bytes.lo);//write frist word address
while(n--)
{
i+=3;
send_da=i;
IICSendByte(send_da);
}
iic_stop();
}
void receiveNByte(uChar slave_add,uChar n)
{
uChar receive_da,i=0;
iic_start();
IICSendByte(slave_add);
check_ACK();
if(F0==1)
{
SystemError=1;
return;
}
while(n--)
{
receive_da=IICreceiveByte();
receivebuf[i++]=receive_da;
slave_ACK();
}
slave_NOACK();
iic_stop();
}
uChar EepromRead(uInt rdAddress) //read a byte subprogram (8 bit)
{
union u {uInt word; struct {uChar hi; uChar lo;} bytes;};
union u eepromAdd;
uChar rdData;
eepromAdd.word=rdAddress; //get address
eepromAdd.bytes.hi=(eepromAdd.bytes.hi&0x07)<<1;//get the low three bits and shift move one bit
iic_start();//produce start condition
IICSendByte(EEPROMWRCMD|eepromAdd.bytes.hi);//write eeprom write command
IICSendByte(eepromAdd.bytes.lo);//write frist word address
iic_start();//produce start condition
IICSendByte(EEPROMRDCMD|eepromAdd.bytes.hi);
rdData=IICreceiveByte(); //read a byte from EEPROM
slave_NOACK();
iic_stop();
return rdData; //return
}
void EepromWrite(uInt wrAddress,uChar wrData)
{
union u {uInt word; struct{uChar hi; uChar lo;} bytes;};
union u eepromAdd;
uChar i = 0;
eepromAdd.word=wrAddress; //注意该处地址连续,对于24C16地址有效范围为0x0000 to 0x07ff 共2K
eepromAdd.bytes.hi=(eepromAdd.bytes.hi&0x07)<<1; //将地址拆分,符合24系列的地址格式
if(wrData!=(EepromRead(wrAddress))) //比较写入的内容与读出的内容是否一致
{
do
{
iic_start();
IICSendByte(EEPROMWRCMD|eepromAdd.bytes.hi); //产生报文,包含页地址,操作方式
IICSendByte(eepromAdd.bytes.lo);
IICSendByte(wrData);
iic_stop();
EepromAckPolling(EEPROMWRCMD|eepromAdd.bytes.hi); //轮询
}
while((wrData!=(EepromRead(wrAddress))) && (i++ < 2)); //效验写入的值
}
}
void EepromAckPolling(uChar device)
{
uChar i,j,k = 0; //define variable
do
{
iic_start(); //produce start condition
j=device;
for(i=0;i<8;i++) //write 8 bit
{
if((j&0x80)==0)
{
SDA=0; //get the highest bit
}
else
{
SDA=1;
}
SCL=1;
SCL=0;
j=j<<1; //move left wrTemp one bit
}
SDA=1;
SCL=1;
}
while((SDA) && (k++ < 100)); //EepromSda=0:exit
SCL=0;
SDA=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -