MCU simulates I2C bus to read and write EEPROM (24CXX) program 1

Publisher:落霞与孤鹜Latest update time:2016-08-06 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The following is a simple read-write program that can be used to detect the circuit condition. The program and circuit are attached first, followed by instructions.

Circuit:

MCU simulates I2C bus to read and write EEPROM (24CXX) program 1 - Jiangnanyu - Jiangnanyu's blog

Note: I use the LEDs on port P2 to detect which step the circuit has reached. I think it is very clear at a glance.

program:

#include
#define unit unsigned int
#define uchar unsigned char
int ok;
sbit scl=P0^0;          
sbit sda=P0^1;
sbit led0=P2^0;
sbit led1=P2^1;
sbit led2 =P2^2;
sbit led3=P2^3;
sbit led4=P2^4;
sbit led5=P2^5;
sbit led6=P2^6;
sbit led7=P2^7;

delay(void) //delay

  int i;
  led1=1;
  for(i=0;i<1;i++) led1=0;
}

start(void) //start
{
  sda=1;
  scl=1;
  delay();
  sda=0;
  delay();
  scl=0;
  led0=0;
}

stop(void) //stop
{
  sda=0;
  scl=1;
  delay();
  sda=1;
  delay();
  scl=0;
}

checkanswer(void) //check answer
{
  sda=1;
  scl=1;
  if(sda==1)
  {
  F0=1;
  led7=0;
  }
  scl=0; 
  led3=0;
}

sendabyte(int temps) //send a byte
{
  uchar n=8;
  while(n--)
  {
    led2=1;
    if((temps&0x80)==0x80)
 {
   sda=1;
   scl=1;
   delay();
   scl =0;
 }
 else
 {
   sda=0;
   scl=1;
   delay();
   scl=0;
 }
 temps=temps<<1;
 led2=0;
  }
}

reciveabyte() //recive a byte
{
  uchar n=8,tempr;
  while(n--)
  { //uchar idata *abyte
   scl=1;
 tempr=tempr<<1;
 if(sda==1)
   tempr=tempr |0x01;
 else
   tempr=tempr&0xfe;
 scl=0;
  }
  ok=tempr;
  return(ok);
}

main(void) //MAIN
{
start();

sendabyte(0xa0);
checkanswer();
if(F0==1) return;

sendabyte(0x00);
checkanswer();
if(F0==1) return;

sendabyte(0x11);
checkanswer();
if(F0==1) return;
/*-----------------------*/
start();

sendabyte(0xa0);
checkanswer();
if(F0==1) return;

sendabyte(0x00);
checkanswer();
if(F0==1) return;

start();

sendabyte(0xa1);
checkanswer();
if(F0==1) return;

reciveabyte();
if(ok==0x11) led6=0;
stop();
}

illustrate:

1. Before writing the driver, it is recommended to understand the read and write timing of the 24CXX series chips. The Chinese information can be downloaded from my network disk. The free registration download link is: http://wdpvip.qupan.com/6293600.html

2. Program function: first write a data 0x11 at address 0x00, and then read it out. If the written data is the same as the read data, LED6 will be lit.

Keywords:MCU Reference address:MCU simulates I2C bus to read and write EEPROM (24CXX) program 1

Previous article:MCU simulates I2C bus to read and write EEPROM (24CXX) program 2
Next article:Program Status Word Register PSW Introduction

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号