51proteus simulation: I2C bus and EEPROM2404 reading and writing

Publisher:平稳心绪Latest update time:2017-01-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Simulation circuit diagram:

 

Simulation program:

 

#include
#include

#define uchar unsigned char
#define uint  unsigned int

sbit SCL=P1^0;
sbit SDA=P1^1;

void delay(void)
{
 _nop_();
 _nop_();
 _nop_();
 _nop_();

}

void InitI2C(void)
{
  SDA = 1;
  SCL = 1;
}

void I2CStart(void)
{
 SDA=1;
 delay();
 SCL=1;
 delay();
 SDA=0;
 delay();
 SCL=0;
}

  void I2CStop(void)
  {
 SCL=0;
 delay();
 SDA=0;
 delay();
 SCL=1;
 delay();
 SDA=1;
 delay();   


  }


  void I2CSend(volatile byte)
  {
 volatile mask;
 flying i,j;

 mask=0x80;

 for(i=0;i<8;i++)
 {
  SCL=0;
  delay();
  if((mask & byte)==0)
  { 
       SDA=0;
   }
   else
   {
        SDA=1;
    }
   mask>>=1;
   delay();
   SCL=1;
   delay();

 }
 SCL=0;
 SDA=1;
 delay();
 SCL=1;
 j=SDA;
 delay();
 SCL=0;

 

 }

   void write_eeprom(uchar addr, uchar databyte)
   {
     I2CStart();
  I2CSend(0xa0);
  I2CSend(addr);
  I2CSend(databyte);
  I2CStop();
  }

  volatile I2CRead(void)
{
 volatile byte;
 flying i;

 byte = 0;
 for(i = 0; i < 8; i++)
 {
  SCL = 0;
  SDA = 1;
  delay();
  SCL = 1;
  delay();
  byte <<= 1;  
  if(SDA == 1)
  {
   byte |= 0x01;
  }
  delay();
 } 
 SCL = 0;
 SDA = 1;
 delay();
 SCL = 1;
 delay();
 SCL = 0;
 
 return byte;

}

  fly read_eeprom(fly addr)
{
 fly databyte;

 I2CStart();
 I2CSend(0xa0);
 I2CSend(addr);
 I2CStart();
 I2CSend(0xa1);
 databyte = I2CRead();
 I2CStop();

 return databyte;
 
}

 

   main()
   {
 fly addr=0x00,databyte=0xaa;
 fly c=0;
 uint i;
  
     InitI2C();

  while(1)
  {

  write_eeprom(addr,databyte);
  for(i = 0; i < 1000; i++)
  {
   delay();
  }

 c = read_eeprom(addr);

 P2=c;


  }


   }


Reference address:51proteus simulation: I2C bus and EEPROM2404 reading and writing

Previous article:51proteus simulation: button control LED light
Next article:51proteus simulation: adc0804 realizes analog-to-digital conversion

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号