An example of microcontroller data communication: analog IIC communication

Publisher:人妙果华Latest update time:2015-04-02 Source: eechinaKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  IIC stands for Inter-Integrated Circuit (IC bus). This type of bus was designed by Philips Semiconductor in the early 1980s. It is mainly used to connect integrated circuits (ICS). IIC is a multi-directional control bus, which means that multiple chips can be connected to the same bus structure, and each chip can be used as a control source for real-time data transmission. This method simplifies the signal transmission bus.

  The I2C serial bus generally has two signal lines, one is the bidirectional data line SDA, and the other is the clock line SCL. All serial data SDA connected to the I2C bus device is connected to the bus SDA, and the clock line SCL of each device is connected to the bus SCL. IO simulation IIC communication is introduced using 51 microcontrollers and AT24C02.

  Start and stop

1.gif 

  Start condition: must be sent before all commands. While the clock line remains at a high level, the data line level jumps from high to low as the start signal of the IIC bus.

  Stop condition: while the clock line remains at a high level, the data line level jumps from low to high as the stop signal of the IIC bus. A stop condition must be sent at the end of the operation.

  void startbit()

  {

  clrSCL();

  setSDA();

  setSCL(); //When the clock is high

  clrSDA(); //SDA falling edge

  clrSCL();

  }

  void stopbit()

  {

  clrSCL();

  clrSDA();

  setSCL(); //When the clock is high

  setSDA(); //SDA rising edge

  clrSCL();

  }

  Response signal

2.gif 

  After each successful data transmission, the slave device sends an acknowledge signal. When the ninth clock signal is generated, the device that generates the acknowledge signal pulls SDA down to low to notify that 8 bits of data have been received.

  void respond()

  {

  unsigned char i = 0;

  setSDA(); //Release bus

  setSCL(); //Clock

  while(SDA != 0)

  {

  i++;

  if(i > 200) break;

  }

  clrSCL();

  }

  Read and write byte operations

3.gif 

  The IIC bus protocol is defined as follows:

  1. Data transmission is allowed only when the bus is not busy. [page]

  2. During data transmission, when the clock line is high, the data line must be in a fixed state and no jumps are allowed. When the clock line is high, any level change of the data line will be regarded as the start or stop condition of the bus

  void writeByte(unsigned char dat)

  {

  unsigned char i = 0;

  for(i = 0;i < 8;i++)

  {

  clrSCL(); //Pull down the clock line and change the level of the SDA line

  if(dat & 0x80) setSDA();

  else clrSDA();

  setSCL(); //After the SDA level is stable, pull up the clock line

  dat <<= 1;

  }

  clrSCL();

  }

  unsigned char readByte()

  {

  unsigned char i = 0,tmp = 0;

  for(i = 0;i < 8;i++)

  {

  clrSCL(); //Pull down the clock line

  tmp <<= 1; //Prepare to read data

  setSCL(); //Pull up the clock line

  if(SDA) tmp |= 0x01;

  }

  clrSCL();

  return tmp;

  }

  AT24C02 read and write operations

4.gif 

  void writeAT24XX(unsigned char addr,unsigned char dat)

  {

  startbit(); //start signal

  writeByte(0xa0); //device address

  respond();

  writeByte(addr); //device internal address

  respond();

  writeByte(dat); //data

  respond();

  stopbit(); //stop

  }

5.gif 

  unsigned char readAT24XX(unsigned char addr)

  {

  unsigned char dat;

  startbit(); //start signal

  writeByte(0xa0); //device address

  respond();

  writeByte(addr); //device internal address

  respond();

  startbit(); //start signal

  writeByte(0xa1); //device address

  respond();

  dat = readByte(); //data

  stopbit(); //stop

  return dat;

  }

  The main function content and program running effect:

  void main()

  {

  unsigned char dat;

  initUart();

  sendString("UART INIT OK!!! "); //Serial port communication initialization

  sendString("write 0x05 --> addr 0x00 ");//write 5 to internal address 0

  writeAT24XX(0x00,0x05); //write data

  sendString("read dat <-- addr 0x00 "); //read and write data

  dat = readAT24XX(0x00); //Read data

  sendString("dat-->"); //Print

  sendByte(dat + '0');

  while(1);

  }

6.jpg 
Keywords:MCU Reference address:An example of microcontroller data communication: analog IIC communication

Previous article:Analysis of single bus data transmission in single chip microcomputer data communication
Next article:An example of microcontroller data communication: simulated SPI data transmission

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号