打印

常用IIC串行

[复制链接]
1368|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
24C系列串行EEPROM是目前串行EEPROM中用量最大的一类。正被广泛应用在多种数据卡,电钥匙,智能电话,智能电表等多种智能仪器仪表中。

电路图如下

程序流程图

程序
******************************************************************************
此程序为I2C总线E2PROM的读写实验程序,程序中向AT24C02写入数据,之后从AT24C02写入数据的地址将数据读出,  和写入的数据进行比较,有一个数据不相等,则LED指示灯点亮。
******************************************************************************
#include <51reg.h>
#include <INTRINS.H>
#define  WriteDeviceAddress 0xa0 //写驱动地址指令
#define  ReadDeviceAddress 0xa1  //读驱动地址指令
sbit SCL = P0^0;
sbit SDA = P0^1;
//功能:发起始信号
void Start_Cond()
{
SCL = 0;
    _nop_();
SDA = 1;
    _nop_();
SCL = 1;
    _nop_();
SDA = 0;
_nop_();
}
//功能:发停止信号
void Stop_Cond()
{
SCL = 0;
_nop_();
SDA = 0;
_nop_();
SCL = 1;
_nop_();
SDA = 1;
_nop_();
}
//功能:发确认信号
void Ack()
{
SCL = 0;
_nop_();
SDA = 0;
SCL = 1;
_nop_();
SCL = 0;
_nop_();
SDA = 1;
}
//功能:发无确认信号
void NoAck()
{
SCL = 0;
_nop_();
SDA = 1;
_nop_();
SCL = 1;
_nop_();
SCL = 0;
_nop_();
}
功能:写一个字节数据
bit Write8Bit(unsigned char input)
{
unsigned char i;
for (i=0;i<8;i++)
{
  SCL = 0;
  input <<= 1;
  SDA = CY;
  SCL = 1;
}
SCL = 0;
    _nop_();
SDA = 1;
SCL = 1;
    _nop_();
CY = SDA;
return(CY) ;
}
功能:读一个字节数据
unsigned char Read8Bit()
{
unsigned char temp,rbyte=0;
for (temp = 8;temp != 0;temp--)
{
  SCL = 0;
  _nop_();
  rbyte = (rbyte << 1) | SDA;
  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字节为一页面全写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;
}
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++)
{ temp = *nContent;
  Write8Bit(temp);     //CY
  nContent++;
        Addr++;
  if(Addr%8==0)      //每页8字节 换页
  { Stop_Cond();
            for(temp=0;temp!=255;temp++)
    {_nop_();}
   Start_Cond();
   Write8Bit(WriteDeviceAddress);
   Write8Bit(Addr);
  }
}
Stop_Cond();     //发停止信号
return(CY);
}

相关帖子

沙发
vivisa| | 2012-4-29 17:19 | 只看该作者
多谢楼主的分享。顶起。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:好好学习,天天向上!

519

主题

4195

帖子

31

粉丝