标签:
/*************************************************************** 功能: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 = 0; _nop_(); AT24C02_SDA = 1; _nop_(); AT24C02_SCL = 1; _nop_(); AT24C02_SDA = 0; _nop_(); } /*------------------------------------------------------------- 功能:发停止信号 ------------------------------------------------------------*/ void Stop_Cond() { AT24C02_SCL = 0; _nop_(); AT24C02_SDA = 0; _nop_(); AT24C02_SCL = 1; _nop_(); AT24C02_SDA = 1; _nop_(); } /*------------------------------------------------------------- 功能:发确认信号 ------------------------------------------------------------*/ void Ack() { AT24C02_SCL = 0; _nop_(); AT24C02_SDA = 0; AT24C02_SCL = 1; _nop_(); AT24C02_SCL = 0; _nop_(); AT24C02_SDA = 1; } /*------------------------------------------------------------- 功能:发无确认信号 ------------------------------------------------------------*/ void NoAck() { AT24C02_SCL = 0; _nop_(); AT24C02_SDA = 1; _nop_(); AT24C02_SCL = 1; _nop_(); AT24C02_SCL = 0; _nop_(); } /*------------------------------------------------------------- 功能:写一个字节数据 ------------------------------------------------------------*/ bit Write8Bit(unsigned char input) { unsigned char i; for (i=0;i<8;i++) { AT24C02_SCL = 0; input <<= 1; AT24C02_SDA = CY; AT24C02_SCL = 1; } AT24C02_SCL = 0; _nop_(); AT24C02_SDA = 1; AT24C02_SCL = 1; _nop_(); CY = AT24C02_SDA; return(CY) ; } /*------------------------------------------------------------- 功能:读一个字节数据 ------------------------------------------------------------*/ unsigned char Read8Bit() { unsigned char temp,rbyte=0; for (temp = 8;temp != 0;temp--) { AT24C02_SCL = 0; _nop_(); rbyte = (rbyte << 1) | AT24C02_SDA; AT24C02_SCL = 1; _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(); //发停止信号 return(0); //返回 } /*------------------------------------------------------------- 功能:初始化EEPROM子程序内容为FF ------------------------------------------------------------*/ bit Init_Flash ( unsigned int nPage ) // 8 Byte/1Page init 0xFF { unsigned char nLen; unsigned char Addr; nLen=8; Addr=8 *nPage; Start_Cond(); Write8Bit(WriteDeviceAddress); Write8Bit(Addr); for(;nLen!=0;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中写的开始地址 for(i = 0;i < nLen;i++) { if(Addr%8==0) //每页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 char dat[16],i; Read_Flash(dat,0,16); //读出AT24C02中从地址0开始依次往后16个字节放入dat数组中 Init_Flash(0); //将AT24C02的第0页初始化为FF Read_Flash(dat,0,16); //读出AT24C02中从地址0开始依次往后16个字节放入dat数组中 for(i = 0;i < 16;i++ ) { dat[i] = i; //对数组dat初始化为0---F } Write_Flash(dat,0,16); //将dat数组中的数据写入AT24C02中地址从0开始 Read_Flash(dat,1,16); //读出AT24C02中从地址1开始依次往后16个字节放入dat数组中 while(1) { i = 9; } }
标签:
原文地址:http://www.cnblogs.com/LittleTiger/p/4384762.html