實(shí)現(xiàn)AT24C02的數(shù)據(jù)讀寫操作
/***************************************************************
功能:11:32 2008-6-27
作者:SG
時(shí)間:2004-03-15
版本:V1.0
***************************************************************/
#include "INTRINS.H"
#include "reg52.h"
#define WriteDeviceAddress 0xa0 //寫驅(qū)動(dòng)地址指令
#define ReadDeviceAddress 0xa1 //讀驅(qū)動(dòng)地址指令
sbit AT24C02_SCL = 0xa4;
sbit AT24C02_SDA = 0xa5;
/*-------------------------------------------------------------
功能:發(fā)起始信號(hào)
------------------------------------------------------------*/
void Start_Cond()
{
AT24C02_SCL = 0;
_nop_();
AT24C02_SDA = 1;
_nop_();
AT24C02_SCL = 1;
_nop_();
AT24C02_SDA = 0;
_nop_();
}
/*-------------------------------------------------------------
功能:發(fā)停止信號(hào)
------------------------------------------------------------*/
void Stop_Cond()
{
AT24C02_SCL = 0;
_nop_();
AT24C02_SDA = 0;
_nop_();
AT24C02_SCL = 1;
_nop_();
AT24C02_SDA = 1;
_nop_();
}
/*-------------------------------------------------------------
功能:發(fā)確認(rèn)信號(hào)
------------------------------------------------------------*/
void Ack()
{
AT24C02_SCL = 0;
_nop_();
AT24C02_SDA = 0;
AT24C02_SCL = 1;
_nop_();
AT24C02_SCL = 0;
_nop_();
AT24C02_SDA = 1;
}
/*-------------------------------------------------------------
功能:發(fā)無確認(rèn)信號(hào)
------------------------------------------------------------*/
void NoAck()
{
AT24C02_SCL = 0;
_nop_();
AT24C02_SDA = 1;
_nop_();
AT24C02_SCL = 1;
_nop_();
AT24C02_SCL = 0;
_nop_();
}
/*-------------------------------------------------------------
功能:寫一個(gè)字節(jié)數(shù)據(jù)
------------------------------------------------------------*/
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) ;
}
/*-------------------------------------------------------------
功能:讀一個(gè)字節(jié)數(shù)據(jù)
------------------------------------------------------------*/
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中給定一個(gè)地址連續(xù)讀NLEN個(gè)字節(jié)數(shù)據(jù)存放在以指針
nContent開頭的往下內(nèi)容。
------------------------------------------------------------*/
bit Read_Flash ( unsigned char *nContent, unsigned char nAddr, unsigned char nLen )
{
unsigned char Addr;
Addr = nAddr;
Start_Cond(); //寫開始信號(hào)
Write8Bit(WriteDeviceAddress); //寫驅(qū)動(dòng)地址
Write8Bit(Addr); //寫從EEPROM中讀的開始地址
Start_Cond(); //寫開始信號(hào)
Write8Bit(ReadDeviceAddress); //寫讀數(shù)據(jù)指令
while(--nLen)
{
*nContent=Read8Bit(); //讀出內(nèi)容
nContent++; //指針加1
Ack(); //發(fā)確認(rèn)信號(hào)
}
*nContent=Read8Bit(); //讀一字節(jié)
NoAck(); //沒有確認(rèn)信號(hào)
Stop_Cond(); //發(fā)停止信號(hào)
return(0); //返回
}
/*-------------------------------------------------------------
功能:初始化EEPROM子程序內(nèi)容為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中給定一個(gè)地址連續(xù)寫NLEN個(gè)字節(jié)數(shù)據(jù)存放在以指針
nContent開頭的往下內(nèi)容。
------------------------------------------------------------*/
bit Write_Flash ( unsigned char *nContent, unsigned char nAddr,unsigned char nLen)
{
unsigned char i,temp;
unsigned char Addr;
Addr = nAddr;
Start_Cond(); //寫開始信號(hào)
Write8Bit(WriteDeviceAddress); //寫驅(qū)動(dòng)地址
Write8Bit(Addr); //寫從EEPROM中寫的開始地址
for(i = 0;i < nLen;i++)
{
if(Addr%8==0) //每頁8字節(jié) //換頁
{
Stop_Cond();
_nop_();
Start_Cond();
Write8Bit(WriteDeviceAddress);
Write8Bit(Addr);
}
temp = *nContent;
if(Write8Bit(temp)) break; //CY
nContent++;
Addr++;
}
Stop_Cond(); //發(fā)停止信號(hào)
return(CY);
}
/*-------------------------------------------------------------
主程序
功能:實(shí)現(xiàn)存儲(chǔ)器EEPROM AT24C02的數(shù)據(jù)讀寫操作
------------------------------------------------------------*/
void main()
{
unsigned char dat[16],i;
Read_Flash(dat,0,16); //讀出AT24C02中從地址0開始依次往后16個(gè)字節(jié)放入dat數(shù)組中
Init_Flash(0); //將AT24C02的第0頁初始化為FF
Read_Flash(dat,0,16); //讀出AT24C02中從地址0開始依次往后16個(gè)字節(jié)放入dat數(shù)組中
for(i = 0;i < 16;i++ )
{
dat[i] = i; //對(duì)數(shù)組dat初始化為0---F
}
Write_Flash(dat,0,16); //將dat數(shù)組中的數(shù)據(jù)寫入AT24C02中地址從0開始
Read_Flash(dat,1,16); //讀出AT24C02中從地址1開始依次往后16個(gè)字節(jié)放入dat數(shù)組中
while(1)
{
i = 9;
}
}