51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机源码 > 详细内容
24C02 EEPROM存储器的C语言操作(C语言)
发布时间:2009/6/4  阅读次数:745  字体大小: 【】 【】【

作者:佚名    来源:不详

#include <reg51.h>
#ifndef   false
#define   false 0
#endif
#ifndef   true
#define true 1
#endif
#define WriteDeviceAddress 0xa0
#define ReadDviceAddress 0xa1
sbit       SDA             =       P3 ^ 5; //根据实际连接的管脚定义
sbit       SCL             =       P3 ^ 4;
sbit       led   = P1^0;
sbit       led2 = P1^1;
unsigned char code Num[21]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
                                                       0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,
                                                       0x10,0x89};
unsigned char code Disdigit[4] = {0x7F,0xBF,0xDF,0xEF};
unsigned char Disbuf[4];
unsigned char code write_data[5] = { 5,2,5,1,8 };
unsigned char read_data[5];
//--------------------------------------------------------------------------
void Delayus(unsigned int number)
{
  for(;number!=0;number--)
  {
  }
}
//--------------------------------------------------------------------------
void DelayMs(unsigned int number)
{
  unsigned char temp;
  for(;number!=0;number--)
  {
   for(temp=112;temp!=0;temp--)
   {
   }
  }
}
//--------------------------------------------------------------------------
void Start()      
{
  SDA=1;
  Delayus(4);
  SCL=1;
  Delayus(4);
  SDA=0;
  Delayus(4);
  SCL=0;
  Delayus(4);
}
//--------------------------------------------------------------------------
void Stop()      
{
  SCL=0;
  Delayus(4);
  SDA=0;
  Delayus(4);
  SCL=1;
  Delayus(4);
  SDA=1;
  Delayus(4);
}
//--------------------------------------------------------------------------
void Ack()    
{
  SDA=0;
  Delayus(4);
  SCL=1;
  Delayus(4);
  SCL=0;
  Delayus(4);
  SDA=1;
  Delayus(4);
}
//--------------------------------------------------------------------------
void NoAck()  
{
  SDA=1;
  Delayus(4);
  SCL=1;
  Delayus(4);
  SCL=0;
  Delayus(4);
  SDA=0; //
}
//--------------------------------------------------------------------------
bit TestAck()        
{
  bit ErrorBit;
  SDA=1;
  Delayus(4);
  SCL=1;
  Delayus(4);
  ErrorBit=SDA;
  Delayus(4);
  SCL=0;
  return(ErrorBit);
}
//--------------------------------------------------------------------------
void Write8Bit(unsigned char input)
{
  unsigned char temp;
  for(temp=8;temp!=0;temp--)
  {
   SDA=(bit)(input&0x80);
   Delayus(4);
   SCL=1;
   Delayus(4);
   SCL=0;
   Delayus(4);
   input=input<<1;
  }
}
//--------------------------------------------------------------------------
unsigned char Read8Bit()      
{
  unsigned char temp,rbyte=0;
  for(temp=8;temp!=0;temp--)
  {
   SCL=1;
   Delayus(4);
   rbyte=rbyte<<1;
   rbyte=rbyte|((unsigned char)(SDA));
   SCL=0;
  }
  return(rbyte);
}
void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)
{
  Start();
  Write8Bit(WriteDeviceAddress);
  TestAck();
  Write8Bit(RomAddress);
  TestAck();
  for(;number!=0;number--)
  {
   Write8Bit(*Wdata);
   TestAck();
   Wdata++;
  }
  Stop();
  DelayMs(10);
}
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes)
{
  Start();
  Write8Bit(WriteDeviceAddress);
  TestAck();
  Write8Bit(RomAddress);
  TestAck();
  
  Start();
  Write8Bit(ReadDviceAddress);
  TestAck();
  while(bytes!=1)
  {
   *RamAddress=Read8Bit();
   Ack();
   RamAddress++;
   bytes--;
  }
  *RamAddress=Read8Bit();
  NoAck();
  Stop();
}
void Display(void)         //显示
{
unsigned int i = 0;
unsigned int temp,count;
               temp = Disdigit[count];      
    P2 =temp;
    temp = Disbuf[count];
    temp = Num[temp];
               P0 =temp;
               count++;          
       if   (count==4)
               count=0;              
}    
void time1() interrupt 3 using 2
{
          
     Display();      
     TH1 = (65535 - 2000)/256;
     TL1 = (65535 - 2000)%256;
}
void main()
{
  TMOD = 0x15;
  TR1 = 1;
  EA = 1;
  ET1 = 1;  
  
  Write24c02(write_data,0x00,3);
    DelayMs(200);
    Read24c02(read_data,0x00,4);
  while(1)
    {
               Disbuf[0] = read_data[0];
         Disbuf[1] = read_data[1];
         Disbuf[2] = read_data[2];
         Disbuf[3] = read_data[3];
    }
}
  
我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号