×

AT24C02/AT24C04/AT24C08/AT24C1

消耗积分:10 | 格式:rar | 大小:444 | 2008-08-13

分享资料个

AT24C02/AT24C04/AT24C08/AT24C16/AT24C32/AT24C56序

AT24C01~AT24C256的读写程序,各人可根据自己的需要应用。
在buf1中填入需要写入的内容,buf2的大小可根据需要定义。
addr可根据使用的芯片选择,可从任何位置读写,只要在该芯片的范围内。
enumer=ATxxx,根据使用的芯片赋值。各函数中的形式参数不需改变。
本程序只要在调用的程序中定义实际参数即可,下述各子程序不必改动。*/

#include
#include
#define  ERROR 10     //允许ERROR的最大次数      
sbit     SDA=P3^0;
sbit     SCL=P3^1;
enum  eepromtype {AT2401,AT2402,AT2404,AT2408,AT2416,AT2432,AT2464,AT24128,AT24256};/*器件的型号*/
enum  eepromtype enumer;   //定义一个枚举变量
unsigned char code buf1 []={1,3,5,7,9,10,11,12,13,15}; /* 发送缓冲区 */
unsigned char buf2 [10]; /* 接收缓冲区 */

/* 一个通用的24C01-24C256共9种EEPROM的字节读写操作程序,
此程序有五个入口条件,分别为读写数据缓冲区指针,
进行读写的字节数,EEPROM首址,EEPROM控制字节,
以及EEPROM类型。此程序结构性良好,具有极好的容错性,程序机器码也不多:
DataBuff为读写数据输入/输出缓冲区的首址
Length 为要读写数据的字节数量
Addr 为EEPROM的片内地址 AT24256为0~32767
Control 为EEPROM的控制字节,具体形式为(1)(0)(1)(0)(A2)(A1)(A0)(R/W),其中R/W=1,
表示读操作,R/W=0为写操作,A2,A1,A0为EEPROM的页选或片选地址;
enumer为枚举变量,需为AT2401至AT24256中的一种,分别对应AT24C01至AT24C256;
函数返回值为一个位变量,若返回1表示此次操作失效,0表示操作成功;
ERROR为允许最大次数,若出现ERRORCOUNT次操作失效后,则函数中止操作,并返回1
SDA和SCL由用户自定义,这里暂定义为P3^0和P3^1; */
/*对于1K位,2K位,4K位,8K位,16K位芯片采用一个8位长的字节地址码,对于32K位以上
的采用2个8位长的字节地址码直接寻址,而4K位,8K位,16K位配合页面地址来寻址*/


/* -----  AT24C01~AT24C256 的读写程序 ------ */
bit   RW24xx(unsigned char *DataBuff,unsigned char Length,unsigned int Addr,
                     unsigned char Control,enum eepromtype enumer)
{ void Delay(unsigned char DelayCount);  /*   延时   */
  void Start(void);  /*   启动总线   */
  void Stop(void);   /*   停止IIC总线   */
  bit  RecAck(void); /*   检查应答位   */
  void NoAck(void);  /*   不对IIC总线产生应答   */
  void Ack(void);    /*   对IIC总线产生应答   */
  unsigned char Receive(void); /*   从IIC总线上读数据子程序  */
  void Send(unsigned char sendbyte); /*   向IIC总线写数据   */
  unsigned char data j,i=ERROR;
  bit errorflag=1;  /*   出错标志   */
  while(i--)
  { Start();  /*   启动总线   */
    Send(Control & 0xfe); /*   向IIC总线写数据,器件地址 */
    if(RecAck()) continue; /*   如写不正确结束本次循环   */
    if(enumer > AT2416)
    { Send((unsigned char)(Addr >> 8));//把整型数据转换为字符型数据:弃高取低,只取低8位.如果容量大于32K位,使用16位地址寻址,写入高八位地址
      if(RecAck())  continue;
    }
    Send((unsigned char)Addr); /*   向IIC总线写数据   */
    if(RecAck())  continue; /*   如写正确结束本次循环   */
    if(!(Control & 0x01))   //判断是读器件还是写器件
    { j=Length;
      errorflag=0;         /* 清错误特征位 */
      while(j--)
      { Send(*DataBuff++); /*   向IIC总线写数据   */
        if(!RecAck()) continue; /*   如写正确结束本次循环   */
        errorflag=1;
        break;
      }
      if(errorflag==1) continue;
      break;
    }
    else
    { Start();  /*   启动总线   */
      Send(Control); /*   向IIC总线写数据   */
      if(RecAck()) continue;//器件没应答结束本次本层循环
      while(--Length)  /*   字节长为0结束   */
      { *DataBuff ++= Receive();
        Ack();   /*   对IIC总线产生应答   */
      }
      *DataBuff=Receive(); /* 读最后一个字节 */
      NoAck();  /*   不对IIC总线产生应答   */
      errorflag=0;
      break;
    }
  }
  Stop();  /*   停止IIC总线   */
  if(!(Control & 0x01))
  { Delay(255); Delay(255); Delay(255); Delay(255);
  }
  return(errorflag);
}


/* * * * * 以下是对IIC总线的操作子程序 * * * * */
/* * * * * * 启动总线 * * * * */
void Start(void)
{ SCL=0; /* SCL处于高电平时,SDA从高电平转向低电平表示 */
  SDA=1; /* 一个"开始"状态,该状态必须在其他命令之前执行 */
  SCL=1;
  _nop_(); _nop_(); _nop_();
  SDA=0;
  _nop_(); _nop_(); _nop_(); _nop_();
  SCL=0;
  SDA=1;    
}

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !

'+ '

'+ '

'+ ''+ '
'+ ''+ ''+ '
'+ ''+ '' ); $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code ==5){ $(pop_this).attr('href',"/login/index.html"); return false } if(data.code == 2){ //跳转到VIP升级页面 window.location.href="//m.jibsdb.com/vip/index?aid=" + webid return false } //是会员 if (data.code > 0) { $('body').append(htmlSetNormalDownload); var getWidth=$("#poplayer").width(); $("#poplayer").css("margin-left","-"+getWidth/2+"px"); $('#tips').html(data.msg) $('.download_confirm').click(function(){ $('#dialog').remove(); }) } else { var down_url = $('#vipdownload').attr('data-url'); isBindAnalysisForm(pop_this, down_url, 1) } }); }); //是否开通VIP $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code == 2 || data.code ==5){ //跳转到VIP升级页面 $('#vipdownload>span').text("开通VIP 免费下载") return false }else{ // 待续费 if(data.code == 3) { vipExpiredInfo.ifVipExpired = true vipExpiredInfo.vipExpiredDate = data.data.endoftime } $('#vipdownload .icon-vip-tips').remove() $('#vipdownload>span').text("VIP免积分下载") } }); }).on("click",".download_cancel",function(){ $('#dialog').remove(); }) var setWeixinShare={};//定义默认的微信分享信息,页面如果要自定义分享,直接更改此变量即可 if(window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'){ var d={ title:'AT24C02/AT24C04/AT24C08/AT24C1',//标题 desc:$('[name=description]').attr("content"), //描述 imgUrl:'https://'+location.host+'/static/images/ele-logo.png',// 分享图标,默认是logo link:'',//链接 type:'',// 分享类型,music、video或link,不填默认为link dataUrl:'',//如果type是music或video,则要提供数据链接,默认为空 success:'', // 用户确认分享后执行的回调函数 cancel:''// 用户取消分享后执行的回调函数 } setWeixinShare=$.extend(d,setWeixinShare); $.ajax({ url:"https://www.elecfans.com/app/wechat/index.php?s=Home/ShareConfig/index", data:"share_url="+encodeURIComponent(location.href)+"&format=jsonp&domain=m", type:'get', dataType:'jsonp', success:function(res){ if(res.status!="successed"){ return false; } $.getScript('https://res.wx.qq.com/open/js/jweixin-1.0.0.js',function(result,status){ if(status!="success"){ return false; } var getWxCfg=res.data; wx.config({ //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId:getWxCfg.appId, // 必填,公众号的唯一标识 timestamp:getWxCfg.timestamp, // 必填,生成签名的时间戳 nonceStr:getWxCfg.nonceStr, // 必填,生成签名的随机串 signature:getWxCfg.signature,// 必填,签名,见附录1 jsApiList:['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); wx.ready(function(){ //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 wx.onMenuShareTimeline({ title: setWeixinShare.title, // 分享标题 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享给朋友”按钮点击状态及自定义分享内容接口 wx.onMenuShareAppMessage({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 type: setWeixinShare.type, // 分享类型,music、video或link,不填默认为link dataUrl: setWeixinShare.dataUrl, // 如果type是music或video,则要提供数据链接,默认为空 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ”按钮点击状态及自定义分享内容接口 wx.onMenuShareQQ({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口 wx.onMenuShareWeibo({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ空间”按钮点击状态及自定义分享内容接口 wx.onMenuShareQZone({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); }); }); } }); } function openX_ad(posterid, htmlid, width, height) { if ($(htmlid).length > 0) { var randomnumber = Math.random(); var now_url = encodeURIComponent(window.location.href); var ga = document.createElement('iframe'); ga.src = 'https://www1.elecfans.com/www/delivery/myafr.php?target=_blank&cb=' + randomnumber + '&zoneid=' + posterid+'&prefer='+now_url; ga.width = width; ga.height = height; ga.frameBorder = 0; ga.scrolling = 'no'; var s = $(htmlid).append(ga); } } openX_ad(828, '#berry-300', 300, 250);