/*************************************************************** 功能:11:32 2008-6-27 作者:SG 时间:2004-03-15 版本:V1.0 ***************************************************************/ #include "INTRINS.H" #include "reg52.h" #define WriteDeviceAddress 0xa0 //写驱动地址指令 #define ReadDeviceAddress 0xa1 //读驱动地址指令 sbit AT24C02_SCL = 0xa4; sbit AT24C02_SDA = 0xa5; /*------------------------------------------------------------- 功能:发起始信号 ------------------------------------------------------------*/ void Start_Cond() { AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; _nop_(); AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; _nop_(); } /*------------------------------------------------------------- 功能:发停止信号 ------------------------------------------------------------*/ void Stop_Cond() { AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; _nop_(); AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; _nop_(); } /*------------------------------------------------------------- 功能:发确认信号 ------------------------------------------------------------*/ void Ack() { AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; AT24C02_SCL = ; _nop_(); AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; } /*------------------------------------------------------------- 功能:发无确认信号 ------------------------------------------------------------*/ void NoAck() { AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; _nop_(); AT24C02_SCL = ; _nop_(); AT24C02_SCL = ; _nop_(); } /*------------------------------------------------------------- 功能:写一个字节数据 ------------------------------------------------------------*/ bit Write8Bit(unsigned char input) { unsigned char i; ;i<;i++) { AT24C02_SCL = ; input <<= ; AT24C02_SDA = CY; AT24C02_SCL = ; } AT24C02_SCL = ; _nop_(); AT24C02_SDA = ; AT24C02_SCL = ; _nop_(); CY = AT24C02_SDA; return(CY) ; } /*------------------------------------------------------------- 功能:读一个字节数据 ------------------------------------------------------------*/ unsigned char Read8Bit() { unsigned ; ;temp != ;temp--) { AT24C02_SCL = ; _nop_(); rbyte = (rbyte << ) | AT24C02_SDA; AT24C02_SCL = ; _nop_(); } return rbyte; } /*------------------------------------------------------------- 功能:从EEPROM中给定一个地址连续读NLEN个字节数据存放在以指针 nContent开头的往下内容。 ------------------------------------------------------------*/ bit Read_Flash ( unsigned char *nContent, unsigned char nAddr, unsigned char nLen ) { unsigned char Addr; Addr = nAddr; Start_Cond(); //写开始信号 Write8Bit(WriteDeviceAddress); //写驱动地址 Write8Bit(Addr); //写从EEPROM中读的开始地址 Start_Cond(); //写开始信号 Write8Bit(ReadDeviceAddress); //写读数据指令 while(--nLen) { *nContent=Read8Bit(); //读出内容 nContent++; //指针加1 Ack(); //发确认信号 } *nContent=Read8Bit(); //读一字节 NoAck(); //没有确认信号 Stop_Cond(); //发停止信号 ); //返回 } /*------------------------------------------------------------- 功能:初始化EEPROM子程序内容为FF ------------------------------------------------------------*/ bit Init_Flash ( unsigned int nPage ) // 8 Byte/1Page init 0xFF { unsigned char nLen; unsigned char Addr; nLen=; Addr= *nPage; Start_Cond(); Write8Bit(WriteDeviceAddress); Write8Bit(Addr); ;nLen--) { if(Write8Bit(0xff)) break; //if Write8Bit() Return 1 Write Flase } Stop_Cond(); return(CY); } /*------------------------------------------------------------- 功能:从EEPROM中给定一个地址连续写NLEN个字节数据存放在以指针 nContent开头的往下内容。 ------------------------------------------------------------*/ bit Write_Flash ( unsigned char *nContent, unsigned char nAddr,unsigned char nLen) { unsigned char i,temp; unsigned char Addr; Addr = nAddr; Start_Cond(); //写开始信号 Write8Bit(WriteDeviceAddress); //写驱动地址 Write8Bit(Addr); //写从EEPROM中写的开始地址 ;i < nLen;i++) { ==) //每页8字节 //换页 { Stop_Cond(); _nop_(); Start_Cond(); Write8Bit(WriteDeviceAddress); Write8Bit(Addr); } temp = *nContent; if(Write8Bit(temp)) break; //CY nContent++; Addr++; } Stop_Cond(); //发停止信号 return(CY); } /*------------------------------------------------------------- 主程序 功能:实现存储器EEPROM AT24C02的数据读写操作 ------------------------------------------------------------*/ void main() { unsigned ],i; Read_Flash(dat,,); //读出AT24C02中从地址0开始依次往后16个字节放入dat数组中 Init_Flash(); //将AT24C02的第0页初始化为FF Read_Flash(dat,,); //读出AT24C02中从地址0开始依次往后16个字节放入dat数组中 ;i < ;i++ ) { dat[i] = i; //对数组dat初始化为0---F } Write_Flash(dat,,); //将dat数组中的数据写入AT24C02中地址从0开始 Read_Flash(dat,,); //读出AT24C02中从地址1开始依次往后16个字节放入dat数组中 ) { i = ; } }