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

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
Yesterday, I implemented the byte read and write operations (Program 1 in the previous article), and today I implemented the page write and continuous read operations.

The circuit remains unchanged. The following is a simulation circuit, except that the digital tube of port P2 is changed from observing which step the program is executed to checking the data in the receiving buffer.MCU simulates I2C bus to read and write EEPROM (24CXX) program 2 - Jiangnanyu - Jiangnanyu's blog

The procedure is as follows:

#include
#define unit unsigned int
#define uchar unsigned char

uchar num=4;
uchar idata sendbuf[4]={0x96,0x84,0xd5,0x63};
uchar idata recivebuf[4];

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;
  for(i=0;i<1;i++);
}

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

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

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

noanswer(void) //no answer
{
  sda=1;
  scl=1;
  delay();
  sda=1;
  scl=0; 
}

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

sendabyte(uchar idata *saddress) //send a byte
{
  uchar n=8,temp=*saddress;
  while(n--)
  {
    if((temp&0x80)==0x80) sda=1;
 else sda=0;
   delay( );
   scl=1;
   delay();
   scl=0;
 temp=temp<<1;
  }
  checkanswer();
  if(F0==1) return;
}

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

sendnbyte(uchar n) //send n byte
{
  uchar idata *ps; 
  ps=&sendbuf[0];

  while(n--)
  {
   sendabyte(ps);
 ps++;
  }
  stop();
}

recivenbyte(uchar n) //recive n byte
{
  uchar idata *pr;
  pr=&recivebuf[0];
  while(n--)
  {
    reciveabyte(pr);
 answer();
 pr++;
  }
  noanswer();
  stop();
}

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

sendabyte(0xa0);

sendabyte(0x00);

sendnbyte(num);

/*-----------------------*/

start();

sendabyte(0xa1);

recivenbyte(num);

P2 = recivebuf[7];
}

Program Description:

The num at the beginning of the program defines the number of data to be transmitted, as well as the send buffer and the receive buffer.

The operation of the main function is clear at a glance and will not be introduced in detail.

Problem: In this program, I defined the data in the receive buffer as 4, and indeed 4 data were received. However, the received data starts from recivebuf[4]. In this example, recivebuf[4] stores 0X96, recivebuf[5] stores 0X84, recivebuf[6] stores 0XD5, and recivebuf[7] stores 0X63.

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

Previous article:Simple keyboard reading and writing program
Next article:MCU simulates I2C bus to read and write EEPROM (24CXX) program 1

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号