实现存储器EEPROM AT24C02的数据读写操作 采用IIC总线读写 C程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*************************************************************** 功能: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++)

{

A T24C02_SCL = 0;

input <<= 1;

A T24C02_SDA = CY;

A T24C02_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--)

{

A T24C02_SCL = 0;

_nop_();

rbyte = (rbyte << 1) | AT24C02_SDA;

A T24C02_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)

{

相关文档
最新文档