📄 atmel 24cxx驱动程序.txt
字号:
ATMEL 24cXX驱动程序
这是我自己开发的可以访问ATMEL24cXX IIC EEPROM的软件,以下是他的源程序。调试通过了。
/*==============================================
作者:福州 王玮
E-Mail: heraldsoft@netease.com
==============================================*/
// EEPROM atmel 24c00/01/02/03 interface program
// this unit design start at 2000-3-5 13:30
// this unit test pass at 2000/3/7 10:40
// design by wangwei 2000/3/1
/*--------------------------------------------------*/
//本模块对外接口
// SDA SCL 定义I2C的两个管脚
// void i2c_init() 这是每次对i2c器件操作都要的初始化
// void setup_comproent_address(char cadd)设置i2c器件地址,必须和器件的An,An+1,An+2...对应
//
// char i2c_read(int address) //随机读 这两个函数地址空间是16位的,他自己会换算成段和位移
// void i2c_write(int address,rdata,delays)将rdata写到address中,delay=0代表不延时,必须延时10ms后才能继续对eeprom操作.其它值有延时
// void erase_all() //erase all chip to 0xff
/*--------------------------------------------------*/
#define IIC24xx_h
#define eeprom 2402 //这里指定EEPROM的型号
//由于该库同时被其他代码引用,所以使用本单元代码需要 #define ATMEL24xx
#define SCL P1_3
#define SDA P1_4
#ifdef ATMEL24xx
struct buf
{ unsigned char command;
unsigned char address;
unsigned char rdata;
unsigned char respone;
char comproent_address;
char page;
}xdata buf_IIC;
//************************
void setup_comproent_address(char cadd)
{
buf_IIC.comproent_address=cadd;
}
#endif
void send_start()
{SDA=1; SCL=1;SCL=1;SCL=1;
SDA=0; SCL=0;
}
void send_stop()
{SDA=0;SCL=1; SCL=1;
SDA=1; SCL=0;
}
void i2c_init()
{
SCL=0;
send_stop();
}
bit i2c_clock()
{ bit sm;
SCL=1; SCL=1;
sm=SDA;
SCL=0; SCL=0;
return(sm);
}
#ifdef ATMEL24xx
void send_command()
{char i,k;
k=buf_IIC.comproent_address;
#if eeprom==2402
k=k/2;
k=k*2;
buf_IIC.command=buf_IIC.command&0xf1;
buf_IIC.command=buf_IIC.command|k;
#endif
#if eeprom==2404
k=k/2; k=k*4;
k=k&0x0c;
buf_IIC.command=buf_IIC.command&0xf3;
buf_IIC.command=buf_IIC.command|k;
#endif
#if eeprom==2408
k=k/4; k=k*8;
buf_IIC.command=buf_IIC.command&0xf7;
buf_IIC.command=buf_IIC.command|k;
#endif
for (i=0;i<8;i++)
{ SDA=get_bit(buf_IIC.command,7-i);
i2c_clock();
}
}
void send_address()
{char i;
for (i=7;i>=0;i--)
{ SDA=get_bit(buf_IIC.address,i);
i2c_clock();
}
}
void IIC_send_data() //IIC总线发送一个字节
{char i;
for (i=7;i>=0;i--)
{ SDA=get_bit(buf_IIC.rdata,i);
i2c_clock();
}
}
void IIC_get_data() //IIC总线接收一个字节
{char i; bit ss;
buf_IIC.rdata=0;
SDA=1;
for (i=7;i>=0;i--)
{ ss=i2c_clock();
buf_IIC.rdata=buf_IIC.rdata*2;
if (ss) buf_IIC.rdata=buf_IIC.rdata+1;
}
}
#endif
bit get_ack()
{bit bb;
bb=i2c_clock();
return(bb);
}
void no_ack()
{
i2c_clock();
}
//=============================
#ifdef ATMEL24xx
void i2c_write(int address,rdata,delays)//rdata是16位的,实际被写入EEPROM的只有低8位
{char page;
page=address/0x100;
page=page*2;
i2c_init();
buf_IIC.command=0xa0|page;
buf_IIC.address=address % 0x100;
buf_IIC.rdata=rdata;
send_start();
send_command(); get_ack();
send_address(); get_ack();
IIC_send_data(); get_ack();
send_stop();
if (delays!=0) delay(ms10);
}
char i2c_read(int address) //random read
{char page;
page=address/0x100;
page=page*2;
i2c_init();
buf_IIC.command=0xa0|page; //write command and address
buf_IIC.address=address % 0x100;
send_start();
send_command(); get_ack();
send_address(); get_ack();
send_start();
buf_IIC.command=0xa1|page;
send_command(); get_ack();
IIC_get_data(); no_ack();
send_stop();
return(buf_IIC.rdata);
}
void i2c_write_int(int address,rdata) //rdata是16位的,实际被写入EEPROM低16位
{char hc,lc;
hc=rdata/0x100; lc=rdata%0x100;
i2c_write(address,hc,1);
i2c_write(address+1,lc,1);
}
int i2c_read_int(int address) //random read
{char hc,lc;
hc=i2c_read(address);
lc=i2c_read(address+1);
return(hc*0x100+lc);
}
void erase_all() //erase all chip to 0xff
{int i,m;
#if eeprom==2402
m=0xff;
#endif
#if eeprom==2404
m=0x1ff;
#endif
#if eeprom==2408
m=0x3ff;
#endif
#if eeprom==2416
m=0x7ff;
#endif
#if eeprom==2432
m=0xfff;
#endif
#if eeprom==2464
m=0x1fff;
#endif
for (i=0;i<=m;i++)
{i2c_write(i,0xff,1);//将rdata写到address中,delay=0代表不延时,必须延时10ms后才能继续对eeprom操作.其它值有
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -