作者:佚名    来源:不详
#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];
    }
}