📄 eeprom.c
字号:
//liyalin@tom.com
//20070120
//24c512 dirve
#define SDA_1 asm("sbi 0x12,5")//SDA=1
#define SDA_0 asm("cbi 0x12,5")//SDA=0
#define SLK_1 asm("sbi 0x12,6")//CLK=1
#define SLK_0 asm("cbi 0x12,6")//CLK=0
void eepyanshi(int aa)
{
long yx11=0;
for(yx11=0;yx11<aa;yx11++)
{
asm("nop");
}
}
void eepkaishi(void)
{
SLK_1;
eepyanshi(20);
SDA_1;
eepyanshi(20);
SDA_0;
eepyanshi(20);
SLK_0;
eepyanshi(20);
}
void eepend(void)
{
SDA_0;
eepyanshi(20);
SLK_1;
eepyanshi(20);
SDA_1;
eepyanshi(20);
SLK_0;
}
void inouteep(uchar eio)
{
if(eio)
{
DDRD|=32;
}
else
{
DDRD&=255-32;
}
}
//CPU接收完数据对EEPROM应答
void ack(void)
{
inouteep(1);//out
SDA_0;
eepyanshi(5);
SLK_1;
eepyanshi(5);
SLK_0;
eepyanshi(5);
//SDA_set;
}
//CPU接收完数据不对EEPROM应答
void noack(void)
{
inouteep(1);//out
SDA_1;
eepyanshi(5);
SLK_1;
eepyanshi(5);
SLK_0;
eepyanshi(5);
}
uchar eepcuowu1=0;
//对EEPROM写完数据,检查EEPROM是否应答
void eepcuowu(void)
{
uchar jsqcw=0;
SLK_0; // 将时钟端口(SCL)设为低
inouteep(0);//in
SDA_1;
eepyanshi(10);
SLK_1; //将时钟端口(SCL)设为高
eepyanshi(10);
while((PIND&32)&&(jsqcw<=250))
{
jsqcw++;
if(jsqcw>250)
eepcuowu1=1;
}
SLK_0; // 将时钟端口(SCL)设为低
eepyanshi(10);
inouteep(1);//out
}
void eepcuowu0(void)
{
uchar jsqcw=0;
SLK_0; // 将时钟端口(SCL)设为低
inouteep(0);//in
SDA_1;
eepyanshi(10);
SLK_1; //将时钟端口(SCL)设为高
eepyanshi(10);
while((PIND&32)&&(jsqcw<=250))
{
jsqcw++;
if(jsqcw>250)
eepcuowu1=1;
}
inouteep(1);
eepyanshi(50);
SDA_0;
eepyanshi(50);
SLK_0;
// 将时钟端口(SCL)设为低
}
void eepxie8(uchar byte) //写一个字节到I2C设备
{
uchar i;
inouteep(1);//out
for (i = 0; i < 8; i++) //传送8位数据
{
SLK_0; //将时钟端口(SCL)设为低
if(byte&128)
SDA_1; // 设置 SDA 位
else
SDA_0; // 清除 SDA 位
eepyanshi(25);
SLK_1; //将时钟端口(SCL)设为高
eepyanshi(25);
byte = byte << 1; //将输出数据左移一位
}
SLK_0; // 校验标志位 (每传送8位,有一校验位)
eepyanshi(20);
eepcuowu();
}
void eepxie81(uchar byte) //写一个字节到I2C设备
{
uchar i;
inouteep(1);//out
for (i = 0; i < 8; i++) //传送8位数据
{
SLK_0; //将时钟端口(SCL)设为低
if(byte & 128)
SDA_1; // 设置 SDA 位
else
SDA_0; // 清除 SDA 位
eepyanshi(25);
SLK_1; //将时钟端口(SCL)设为高
eepyanshi(25);
byte = byte << 1; //将输出数据左移一位
}
SLK_0; // 校验标志位 (每传送8位,有一校验位)
eepyanshi(20);
eepcuowu0();
}
void eepxie82(uchar byte) //写一个字节到I2C设备
{
uchar i;
inouteep(1);//out
for (i = 0; i < 8; i++) //传送8位数据
{
SLK_0; //将时钟端口(SCL)设为低
if(byte & 128)
SDA_1; // 设置 SDA 位
else
SDA_0; // 清除 SDA 位
eepyanshi(25);
SLK_1; //将时钟端口(SCL)设为高
eepyanshi(25);
byte = byte << 1; //将输出数据左移一位
}
SLK_0; // 校验标志位 (每传送8位,有一校验位)
SDA_0;
SLK_1; //将时钟端口(SCL)设为高
eepyanshi(6);
SLK_0;
eepyanshi(6);
}
int eepdu8(void) //读取I2C设备的数据
{
uchar i,buff = 0;
inouteep(0);//in
SDA_1;
eepyanshi(10);
for (i = 0; i < 8; i++)
{
SLK_0; // 将时钟端口(SCL)设为低
eepyanshi(20);
SLK_1; //将时钟端口(SCL)设为高
eepyanshi(20);
// 在 SDA 位上读取数据
buff = (buff << 1);
if (PIND&32)
buff++;
eepyanshi(20);
}
SLK_0;
eepyanshi(10);
return buff; // 返回读取值
}
uchar aee=0;
int eepdu(unsigned int dizhi,uchar *data1,uchar zishu)//(地址)
{
aee+=1;
uchar data=0;
inouteep(1);//out
eepkaishi();// 开动总线
eepyanshi(10);
eepxie8(160);//命令
eepxie8(dizhi>>8);
eepxie81(dizhi);
eepxie8(161);
uchar xd;
for(xd=0;xd<zishu;xd++)
{
*(data1+xd)=eepdu8();
ack();
}
noack();
eepend();//结束总线
return data;
}
void eepxie(unsigned int dizhi,uchar *data,uchar zishu)//(地址,数据,需要写的字节数)
{
inouteep(1);//out
eepkaishi();// 开动总线
eepyanshi(10);
eepxie8(160);//命令
eepxie8(dizhi>>8);
eepxie8(dizhi);
uchar xd;
for(xd=0;xd<zishu;xd++)
{
eepxie82(*data+xd);
}
eepend();//结束总线
}
/*
void eepxie(unsigned int dizhi,uchar data)//(地址,数据)
{
inouteep(1);//out
eepkaishi();// 开动总线
eepyanshi(10);
eepxie8(160);//命令
eepxie8(dizhi>>8);
eepxie8(dizhi);
eepxie82(data);
eepend();//结束总线
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -