AT24C02存儲器讀寫數據實驗

基本介紹

  • 中文名:AT24C02存儲器讀寫數據實驗
  • 作者:SG
  • 時間:2004-03-15
  • 版本:V1.0
/***************************************************************
功能:實現存儲器EEPROM AT24C02的數據讀寫操作 採用IIC匯流排讀寫
***************************************************************/
#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,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;
}
}

相關詞條

熱門詞條

聯絡我們